001    package org.kuali.ole.catalog.rule;
002    
003    import org.kuali.ole.OLEConstants;
004    import org.kuali.ole.catalog.bo.OleSeperateOrCompositeReport;
005    import org.kuali.rice.krad.maintenance.MaintenanceDocument;
006    import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
007    
008    import java.util.HashMap;
009    import java.util.List;
010    import java.util.Map;
011    
012    /**
013     * OleSeperateOrCompositeReportRule validates maintenance object for Seperate Or Composite Report Maintenance Document
014     */
015    public class OleSeperateOrCompositeReportRule extends MaintenanceDocumentRuleBase {
016    
017        @Override
018        protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
019            boolean isValid = true;
020            OleSeperateOrCompositeReport oleSeperateOrCompositeReport = (OleSeperateOrCompositeReport) document.getNewMaintainableObject().getDataObject();
021    
022            isValid &= validateSeperateOrCompositeReportCode(oleSeperateOrCompositeReport);
023            return isValid;
024        }
025    
026        /**
027         * This method  validates duplicate seperateOrCompositeReport Id and return boolean value.
028         * @param oleSeperateOrCompositeReport
029         * @return  boolean
030         */
031        private boolean validateSeperateOrCompositeReportCode(OleSeperateOrCompositeReport oleSeperateOrCompositeReport) {
032    
033            if (oleSeperateOrCompositeReport.getSeperateOrCompositeReportCode() != null) {
034                Map<String, String> criteria = new HashMap<String, String>();
035                criteria.put(OLEConstants.OleSeperateOrCompositeReport.SEPERATE_OR_COMPOSITE_REPORT_CD, oleSeperateOrCompositeReport.getSeperateOrCompositeReportCode());
036                List<OleSeperateOrCompositeReport> seperateOrCompositeReportCodeInDatabase = (List<OleSeperateOrCompositeReport>) getBoService().findMatching(OleSeperateOrCompositeReport.class, criteria);
037                if ((seperateOrCompositeReportCodeInDatabase.size() > 0)) {
038                    for (OleSeperateOrCompositeReport seperateOrCompositeReportObj : seperateOrCompositeReportCodeInDatabase) {
039                        Integer seperateOrCompositeReportId = seperateOrCompositeReportObj.getSeperateOrCompositeReportId();
040                        if (null == oleSeperateOrCompositeReport.getSeperateOrCompositeReportId() || (seperateOrCompositeReportId.intValue() != oleSeperateOrCompositeReport.getSeperateOrCompositeReportId().intValue())) {
041                            this.putFieldError(OLEConstants.OleSeperateOrCompositeReport.SEPERATE_OR_COMPOSITE_REPORT_CODE, "error.duplicate.code");
042                            return false;
043                        }
044                    }
045                }
046            }
047    
048            return true;
049        }
050    }