001 /* 002 * Copyright 2011 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 */ 016 package org.kuali.kfs.module.ld.util; 017 018 import java.math.BigDecimal; 019 import java.text.SimpleDateFormat; 020 import java.util.Collection; 021 import java.util.Date; 022 023 import org.apache.commons.lang.StringUtils; 024 import org.kuali.kfs.gl.businessobject.CorrectionChangeGroup; 025 import org.kuali.kfs.gl.businessobject.CorrectionCriteria; 026 import org.kuali.kfs.gl.businessobject.OriginEntryFull; 027 import org.kuali.kfs.module.ld.businessobject.LaborOriginEntry; 028 import org.kuali.kfs.module.ld.businessobject.options.LaborOriginEntryFieldFinder; 029 import org.kuali.rice.kns.util.KualiDecimal; 030 031 /** 032 * This class provides utility methods for the Labor correction document 033 */ 034 public class CorrectionDocumentUtils { 035 /** 036 * Returns whether an origin entry matches the passed in criteria. If both the criteria and actual value are both String types 037 * and are empty, null, or whitespace only, then they will match. 038 * 039 * @param cc correction criteria to test against origin entry 040 * @param oe origin entry to test 041 * @return true if origin entry matches the passed in criteria 042 */ 043 public static boolean laborEntryMatchesCriteria(CorrectionCriteria cc, OriginEntryFull oe) { 044 LaborOriginEntryFieldFinder loeff = new LaborOriginEntryFieldFinder(); 045 LaborOriginEntry loe = (LaborOriginEntry) oe; 046 Object fieldActualValue = loe.getFieldValue(cc.getCorrectionFieldName()); 047 String fieldTestValue = StringUtils.isBlank(cc.getCorrectionFieldValue()) ? "" : cc.getCorrectionFieldValue(); 048 String fieldType = loeff.getFieldType(cc.getCorrectionFieldName()); 049 String fieldActualValueString = org.kuali.kfs.gl.document.CorrectionDocumentUtils.convertToString(fieldActualValue, fieldType); 050 051 if ("String".equals(fieldType) || "sw".equals(cc.getCorrectionOperatorCode()) || "ew".equals(cc.getCorrectionOperatorCode()) || "ct".equals(cc.getCorrectionOperatorCode())) { 052 return org.kuali.kfs.gl.document.CorrectionDocumentUtils.compareStringData(cc, fieldTestValue, fieldActualValueString); 053 } 054 int compareTo = 0; 055 try { 056 if (fieldActualValue == null) { 057 return false; 058 } 059 if ("Integer".equals(fieldType)) { 060 compareTo = ((Integer) fieldActualValue).compareTo(Integer.parseInt(fieldTestValue)); 061 } 062 if ("KualiDecimal".equals(fieldType)) { 063 compareTo = ((KualiDecimal) fieldActualValue).compareTo(new KualiDecimal(Double.parseDouble(fieldTestValue))); 064 } 065 if ("BigDecimal".equals(fieldType)) { 066 compareTo = ((BigDecimal) fieldActualValue).compareTo(new BigDecimal(Double.parseDouble(fieldTestValue))); 067 068 } 069 if ("Date".equals(fieldType)) { 070 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 071 compareTo = ((Date) fieldActualValue).compareTo(df.parse(fieldTestValue)); 072 } 073 } 074 catch (Exception e) { 075 // any exception while parsing data return false 076 return false; 077 } 078 return org.kuali.kfs.gl.document.CorrectionDocumentUtils.compareTo(compareTo, cc.getCorrectionOperatorCode()); 079 } 080 081 /** 082 * Returns whether the labor entry matches any of the criteria groups 083 * 084 * @param entry labor origin entry 085 * @param groups collection of correction change group 086 * @return true if labor origin entry matches any of the criteria groups 087 */ 088 public static boolean doesLaborEntryMatchAnyCriteriaGroups(OriginEntryFull entry, Collection<CorrectionChangeGroup> groups) { 089 boolean anyGroupMatch = false; 090 for (CorrectionChangeGroup ccg : groups) { 091 int matches = 0; 092 for (CorrectionCriteria cc : ccg.getCorrectionCriteria()) { 093 if (CorrectionDocumentUtils.laborEntryMatchesCriteria(cc, entry)) { 094 matches++; 095 } 096 } 097 098 // If they all match, change it 099 if (matches == ccg.getCorrectionCriteria().size()) { 100 anyGroupMatch = true; 101 break; 102 } 103 } 104 return anyGroupMatch; 105 } 106 }