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