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 insert elements into a Data Dictionary Bean.
024 * 
025 * @author Kuali Rice Team (rice.collab@kuali.org)
026 *
027 */
028public class FieldOverrideForListElementInsertImpl extends FieldOverrideForListElementBase  implements FieldOverride{
029        
030        private Object insertBefore;
031    private Object insertAfter;
032    
033    public Object getInsertBefore() {
034        return insertBefore;
035    }
036
037    public void setInsertBefore(Object insertBefore) {
038        this.insertBefore = insertBefore;
039    }
040
041    public Object getInsertAfter() {
042        return insertAfter;
043    }
044
045
046    public void setInsertAfter(Object insertAfter) {
047        this.insertAfter = insertAfter;
048    }
049
050
051    protected void varifyConfig()
052    {
053        if ( insertBefore != null && insertAfter != null )
054        {
055            throw new RuntimeException("Configuration Error, insertBefore and insertAfter can not be both NOT-NULL");
056        }
057        if ( insertBefore == null && insertAfter == null )
058        {
059            throw new RuntimeException("Configuration Error, Either insertBefore or insertAfter should be NOT-NULL");            
060        }
061    }
062    
063    private Object getObjectToInsert()
064    {
065        Object objToInsert = null;
066        if ( insertBefore != null )
067        {
068            objToInsert = insertBefore;
069        }
070        if ( insertAfter != null )
071        {
072            if ( objToInsert != null )
073            {
074                throw new RuntimeException("Configuration Error, insertBefore and insertAfter can not be both NOT-NULL");
075            }
076            objToInsert = insertAfter;
077        }
078        if ( objToInsert == null )
079        {
080            throw new RuntimeException("Configuration Error, Either insertBefore or insertAfter must be NOT-NULL");                        
081        }
082        return objToInsert;
083    }
084    
085    public Object performFieldOverride(Object bean, Object property) {
086        Object objToInsert = getObjectToInsert();
087        
088        List oldList = (List)property;
089        
090        int insertPos = getElementPositionInList(getElement(), oldList);
091
092        if ( insertPos == -1 )
093        {
094            insertPos = oldList.size();
095        }
096        else
097        {
098            if ( insertAfter != null )
099            {
100                insertPos = insertPos + 1;
101            }
102        }
103
104        if ( objToInsert instanceof List )
105        {
106            oldList.addAll(insertPos, (List)objToInsert);
107        }
108        else
109        {
110            oldList.add(insertPos, objToInsert);
111        }
112        return oldList;
113    }
114}