/*
 * Copyright 2008 The Kuali Foundation.
 * 
 * Licensed under the Educational Community License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.opensource.org/licenses/ecl1.php
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.module.budget.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.kuali.core.bo.BusinessObject;
import org.kuali.core.util.KualiInteger;
import org.kuali.core.util.ObjectUtils;

/**
 * This class contains methods
 */
public class BudgetConstructionReportHelper {

    public static BigDecimal setDecimalDigit(BigDecimal number, int digit) {
        BigDecimal returnNum = BigDecimal.ZERO;
        if (number != null) {
            number.setScale(digit, BigDecimal.ROUND_HALF_EVEN);
        }
        return returnNum;
    }

    public static BigDecimal calculatePercent(BigDecimal numerator, BigDecimal denominator) {
        BigDecimal result = BigDecimal.ZERO;
        if (!denominator.equals(BigDecimal.ZERO)) {
            result = numerator.divide(denominator, 3).multiply(new BigDecimal(100));
        }
        return result;
    }

    public static Integer convertKualiInteger(KualiInteger num) {
        Integer returnNum = null;
        if (num != null) {
            returnNum = new Integer(num.intValue());
        }
        else {
            returnNum = new Integer(0);
        }
        return returnNum;
    }

    public static List deleteDuplicated(List<BusinessObject> list, List<String> fields) {
        List returnList = new ArrayList();
        List<String> foundObjects = new ArrayList<String>();

        for (BusinessObject businessObject : list) {
            String valueString = "";
            for (String fieldName : fields) {
                Object fieldValue = ObjectUtils.getPropertyValue(businessObject, fieldName);
                valueString += fieldValue.toString();
            }
            if (!foundObjects.contains(valueString)) {
                returnList.add(businessObject);
                foundObjects.add(valueString);
            }
        }
        return returnList;
    }

    public static boolean isSameEntry(BusinessObject firstObject, BusinessObject secondObject, List<String> fields) {
        String firstValueString = "";
        String secondValueString = "";
        for (String fieldName : fields) {
            Object firstFieldValue = ObjectUtils.getPropertyValue(firstObject, fieldName);
            Object secondFieldValue = ObjectUtils.getPropertyValue(secondObject, fieldName);
            firstValueString += firstFieldValue.toString();
            secondValueString += secondFieldValue.toString();
        }
        if (firstValueString.equals(secondValueString)) {
            return true;
        }
        else {
            return false;
        }

    }
}

@@ -681,16 +681,47 @@
 ecimal;%0A
+import java.math.RoundingMode;%0A
 import j
@@ -1393,32 +1393,759 @@
 igDecimal.ZERO;%0A
+        if (numerator != null && denominator != null && !denominator.equals(BigDecimal.ZERO) ) %7B%0A            result = numerator.divide(denominator, 3, BigDecimal.ROUND_HALF_UP ).movePointRight(2);%0A        %7D%0A        return result;%0A    %7D%0A    %0A    public static BigDecimal calculatePercent(Integer numerator, Integer denominator) %7B%0A        BigDecimal result = BigDecimal.ZERO;%0A        if (numerator != null && denominator != null)%7B%0A            return calculatePercent(new BigDecimal(numerator.intValue()), new BigDecimal(denominator.intValue()));%0A        %7D %0A        return result;%0A    %7D%0A    %0A    public static BigDecimal calculateDivide(BigDecimal numerator, BigDecimal denominator) %7B%0A        BigDecimal result = BigDecimal.ZERO;%0A
         if (!den
@@ -2237,38 +2237,8 @@
 , 3)
-.multiply(new BigDecimal(100))
 ;%0A  

update BCReportHelper Class