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.bc.report; 017 018 import java.math.BigDecimal; 019 import java.util.ArrayList; 020 import java.util.List; 021 022 import org.kuali.rice.kns.bo.BusinessObject; 023 import org.kuali.rice.kns.util.KualiInteger; 024 import org.kuali.rice.kns.util.ObjectUtils; 025 026 /** 027 * This class contains methods 028 */ 029 public class BudgetConstructionReportHelper { 030 031 public static BigDecimal setDecimalDigit(BigDecimal number, int digit, boolean setNullIndicator) { 032 BigDecimal returnNum = BigDecimal.ZERO; 033 if (number != null) { 034 if ((number.compareTo(BigDecimal.ZERO) == 0) && setNullIndicator){ 035 return null; 036 } 037 returnNum = number.setScale(digit, BigDecimal.ROUND_HALF_UP); 038 } 039 return returnNum; 040 } 041 042 public static BigDecimal calculatePercent(BigDecimal numerator, BigDecimal denominator) { 043 BigDecimal result = BigDecimal.ZERO; 044 if (numerator != null && denominator != null && (denominator.compareTo(BigDecimal.ZERO) != 0) ) { 045 result = numerator.divide(denominator, 3, BigDecimal.ROUND_HALF_UP ).movePointRight(2); 046 } 047 return result; 048 } 049 050 public static BigDecimal calculatePercent(Integer numerator, Integer denominator) { 051 BigDecimal result = BigDecimal.ZERO; 052 if (numerator != null && denominator != null){ 053 return calculatePercent(new BigDecimal(numerator.intValue()), new BigDecimal(denominator.intValue())); 054 } 055 return result; 056 } 057 058 public static BigDecimal calculateDivide(BigDecimal numerator, BigDecimal denominator) { 059 BigDecimal result = BigDecimal.ZERO; 060 if (denominator.compareTo(BigDecimal.ZERO) != 0) { 061 result = numerator.divide(denominator, 3, BigDecimal.ROUND_HALF_UP); 062 } 063 return result; 064 } 065 066 public static Integer convertKualiInteger(KualiInteger num) { 067 Integer returnNum = null; 068 if (num != null) { 069 returnNum = new Integer(num.intValue()); 070 } 071 else { 072 returnNum = new Integer(0); 073 } 074 return returnNum; 075 } 076 077 public static List deleteDuplicated(List<BusinessObject> list, List<String> fields) { 078 List returnList = new ArrayList(); 079 List<String> foundObjects = new ArrayList<String>(); 080 081 for (BusinessObject businessObject : list) { 082 String valueString = ""; 083 for (String fieldName : fields) { 084 Object fieldValue = ObjectUtils.getPropertyValue(businessObject, fieldName); 085 valueString += fieldValue.toString(); 086 } 087 if (!foundObjects.contains(valueString)) { 088 returnList.add(businessObject); 089 foundObjects.add(valueString); 090 } 091 } 092 return returnList; 093 } 094 095 public static boolean isSameEntry(BusinessObject firstObject, BusinessObject secondObject, List<String> fields) { 096 String firstValueString = ""; 097 String secondValueString = ""; 098 for (String fieldName : fields) { 099 Object firstFieldValue = ObjectUtils.getPropertyValue(firstObject, fieldName); 100 Object secondFieldValue = ObjectUtils.getPropertyValue(secondObject, fieldName); 101 firstValueString += firstFieldValue.toString(); 102 secondValueString += secondFieldValue.toString(); 103 } 104 if (firstValueString.equals(secondValueString)) { 105 return true; 106 } 107 else { 108 return false; 109 } 110 111 } 112 }