001/**
002 * Copyright 2005-2018 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 org.kuali.rice.krad.datadictionary.FieldOverride;
019
020import java.util.List;
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 */
027public class FieldOverrideForListElementReplaceImpl extends FieldOverrideForListElementBase implements FieldOverride {
028    private Object replaceWith;
029
030    public Object getReplaceWith() {
031        return replaceWith;
032    }
033
034    public void setReplaceWith(Object replaceAt) {
035        this.replaceWith = replaceAt;
036    }
037
038    protected void varifyConfig() {
039        if (replaceWith == null) {
040            throw new RuntimeException("Configuration Error, Missing required replaceWith parameter....");
041        }
042    }
043
044    public Object performFieldOverride(Object bean, Object property) {
045        varifyConfig();
046        List oldList = (List) property;
047
048        int replacePos = getElementPositionInList(getElement(), oldList);
049
050        if (replacePos == -1) {
051            throw new RuntimeException("Configuration Error, replace element could not be located.");
052        }
053        if (replacePos >= 0 && replacePos < oldList.size()) {
054            oldList.remove(replacePos);
055            oldList.add(replacePos, getReplaceWith());
056        }
057
058        return oldList;
059    }
060}