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