001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.datadictionary.impl;
017
018import java.util.List;
019
020import org.kuali.rice.krad.datadictionary.FieldOverride;
021
022/**
023 * A Field Override used to replace list elements in a Data Dictionary bean. 
024 * 
025 * @author Kuali Rice Team (rice.collab@kuali.org)
026 * 
027 */
028public class FieldOverrideForListElementReplaceImpl extends FieldOverrideForListElementBase implements FieldOverride {
029        private Object replaceWith;
030
031        public Object getReplaceWith() {
032                return replaceWith;
033        }
034
035        public void setReplaceWith(Object replaceAt) {
036                this.replaceWith = replaceAt;
037        }
038
039        protected void varifyConfig() {
040                if (replaceWith == null) {
041                        throw new RuntimeException(
042                                        "Configuration Error, Missing required replaceWith parameter....");
043                }
044        }
045
046        public Object performFieldOverride(Object bean, Object property) {
047                varifyConfig();
048                List oldList = (List) property;
049
050                int replacePos = getElementPositionInList(getElement(), oldList);
051
052                if (replacePos == -1) {
053                        throw new RuntimeException(
054                                        "Configuration Error, replace element could not be located.");
055                }
056                if (replacePos >= 0 && replacePos < oldList.size()) {
057                        oldList.remove(replacePos);
058                        oldList.add(replacePos, getReplaceWith());
059                }
060
061                return oldList;
062        }
063}