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.Comparator;
019import java.util.List;
020
021import org.apache.commons.beanutils.BeanComparator;
022import org.apache.commons.lang.StringUtils;
023
024/**
025 * The super class which implementations of the FieldOverride interface will extend. 
026 * 
027 * @author Kuali Rice Team (rice.collab@kuali.org)
028 *
029 */
030public class FieldOverrideForListElementBase {
031        
032    private String propertyName;
033    private Object element;
034    protected String propertyNameForElementCompare;
035    
036
037    public String getPropertyNameForElementCompare() {
038        return propertyNameForElementCompare;
039    }
040
041    public void setPropertyNameForElementCompare(String propertyNameForElementCompare) {
042        this.propertyNameForElementCompare = propertyNameForElementCompare;
043    }
044
045    protected int getElementPositionInList(Object object, List theList) {
046        Comparator comparator = this.getComparator();
047        int pos = -1;
048        
049        if ( object != null && theList != null )
050        {
051            for ( int i = 0; i < theList.size(); ++i )
052            {
053                Object item = theList.get(i);
054                boolean equalFlag = false;
055                if ( comparator != null )
056                {
057                    equalFlag = comparator.compare(object, item) == 0;
058                }
059                else
060                {
061                    equalFlag = item.equals(object);   
062                }
063                if ( equalFlag )
064                {
065                    pos = i;
066                    break;
067                }
068            }
069        }
070        return pos;
071    }
072
073    public String getPropertyName() {
074        return propertyName;
075    }
076
077    public void setPropertyName(String propertyName) {
078        this.propertyName = propertyName;
079    }
080
081    public Object getElement() {
082        return element;
083    }
084
085    public void setElement(Object value) {
086        this.element = value;
087    }
088
089
090    public FieldOverrideForListElementBase() {
091        super();
092    }
093
094    protected Comparator getComparator() {
095        Comparator comparator = null;
096        if ( StringUtils.isNotBlank(propertyNameForElementCompare))
097        {
098            comparator = new BeanComparator(propertyNameForElementCompare);
099        }
100        else
101        {
102            throw new RuntimeException("Missing required comparator definitions.");
103        }
104        return comparator;
105    }
106
107}