001package org.kuali.ole.select.rule;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.coa.businessobject.*;
005import org.kuali.ole.deliver.bo.OLEPatronDocumentPhone;
006import org.kuali.ole.deliver.bo.OlePatronDocument;
007import org.kuali.ole.select.bo.OlePurchaseOrderPurpose;
008import org.kuali.rice.core.api.criteria.QueryByCriteria;
009import org.kuali.rice.krad.maintenance.MaintenanceDocument;
010import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
011import org.kuali.rice.krad.service.KRADServiceLocator;
012import org.kuali.rice.krad.uif.UifConstants;
013import org.kuali.rice.krad.uif.UifParameters;
014import org.kuali.rice.krad.uif.component.BindingInfo;
015import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
016import org.kuali.rice.krad.util.GlobalVariables;
017import org.kuali.rice.krad.web.controller.MethodAccessible;
018import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
019import org.kuali.rice.krad.web.form.UifFormBase;
020import org.springframework.validation.BindingResult;
021import org.springframework.web.bind.annotation.ModelAttribute;
022import org.springframework.web.bind.annotation.RequestMapping;
023import org.springframework.web.bind.annotation.RequestMethod;
024import org.springframework.web.servlet.ModelAndView;
025
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpServletResponse;
028import java.util.HashMap;
029import java.util.List;
030import java.util.Map;
031
032/**
033 * Created by jating on 24/9/14.
034 */
035public class OleFundCodeRule extends MaintenanceDocumentRuleBase {
036
037
038    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
039        boolean isValid = true;
040        OleFundCode oleFundCode = (OleFundCode) document.getNewMaintainableObject().getDataObject();
041        isValid = validateFundCode(oleFundCode, document);
042        return isValid;
043    }
044
045// validations for the Fund Code
046
047    private boolean validateFundCode(OleFundCode oleFundCode, MaintenanceDocument document) {
048
049        int totalPercentage = 0;
050        if (oleFundCode.getFundCode() != null) {
051            Map<String, String> criteria = new HashMap<String, String>();
052            criteria.put(OLEConstants.OLEEResourceRecord.FUND_CODE, oleFundCode.getFundCode());
053            List<OleFundCode> savedOleFundCode = (List<OleFundCode>) KRADServiceLocator.getDataObjectService().findMatching(OleFundCode.class, QueryByCriteria.Builder.andAttributes(criteria).build()).getResults();
054            if ((savedOleFundCode.size() > 0)) {
055                for (OleFundCode fundCode : savedOleFundCode) {
056                    String fundId = fundCode.getFundCode();
057                    if (null == oleFundCode.getFundCodeId() || (!oleFundCode.getFundCode().equalsIgnoreCase(fundId))) {//
058                        this.putFieldError(OLEConstants.OLEEResourceRecord.FUND_CODE, OLEConstants.OLEEResourceRecord.ERROR_DUPLICATE_FUND_CODE);
059                        return false;
060                    }
061                }
062            }
063            if (oleFundCode.getOleFundCodeAccountingLineList() != null && oleFundCode.getOleFundCodeAccountingLineList().size() > 0) {
064                boolean error = false;
065                for (OleFundCodeAccountingLine oleFundCodeAccountingLine : oleFundCode.getOleFundCodeAccountingLineList()) {
066                    totalPercentage = totalPercentage + Integer.valueOf(oleFundCodeAccountingLine.getPercentage());
067                }
068                if (totalPercentage < 100) {
069                    this.putFieldError(OLEConstants.OLEEResourceRecord.FUND_CODE, OLEConstants.OLEEResourceRecord.ERROR_PERCENTAGE_LESS_THAN_HUNDRED);
070                    return false;
071                } else if (totalPercentage > 100) {
072                    this.putFieldError(OLEConstants.OLEEResourceRecord.FUND_CODE, OLEConstants.OLEEResourceRecord.ERROR_PERCENTAGE_MORE_THAN_HUNDRED);
073                    return false;
074                }
075
076            } else {
077                this.putFieldError(OLEConstants.OLEEResourceRecord.FUND_CODE, OLEConstants.OLEEResourceRecord.ERROR_ATLEAST_ONE_ACCOUNTING_LINE);
078            }
079        }
080        return true;
081    }
082
083}
084
085
086