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.endow.document.validation.impl;
017    
018    import java.util.ArrayList;
019    import java.util.HashMap;
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.kuali.kfs.module.endow.EndowKeyConstants;
024    import org.kuali.kfs.module.endow.EndowPropertyConstants;
025    import org.kuali.kfs.module.endow.businessobject.SecurityReportingGroup;
026    import org.kuali.kfs.sys.context.SpringContext;
027    import org.kuali.rice.kns.document.MaintenanceDocument;
028    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
029    import org.kuali.rice.kns.service.BusinessObjectService;
030    import org.kuali.rice.kns.util.GlobalVariables;
031    import org.kuali.rice.kns.util.MessageMap;
032    
033    /**
034     * This class represents the business rules for the maintenance of {@link SecurityReportingGroup} business objects
035     */
036    public class SecurityReportingGroupRule extends MaintenanceDocumentRuleBase {
037    
038        protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SecurityReportingGroupRule.class);
039    
040        private SecurityReportingGroup newReportingGroup;
041    
042        /**
043         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#setupConvenienceObjects()
044         */
045        @Override
046        public void setupConvenienceObjects() {
047            newReportingGroup = (SecurityReportingGroup) super.getNewBo();
048        }
049    
050        /**
051         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
052         */
053        @Override
054        protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
055    
056            boolean isValid = true;
057            isValid &= super.processCustomRouteDocumentBusinessRules(document);
058            MessageMap errorMap = GlobalVariables.getMessageMap();
059            isValid &= errorMap.hasNoErrors();
060    
061            if (isValid) {
062    
063                isValid = validateReportingGroupOrder(newReportingGroup);
064            }
065    
066            return isValid;
067        }
068    
069        /**
070         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
071         */
072        @Override
073        protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
074            return true;
075        }
076    
077        /**
078         * Checks if there is another active security reporting group in the database with the same order number
079         * 
080         * @param reportingGroup the reporting group to validate
081         * @return true if there is no othersecurity reporting group in the database with the same order number, false otherwise
082         */
083        public boolean validateReportingGroupOrder(SecurityReportingGroup reportingGroup) {
084            boolean success = true;
085            BusinessObjectService businessObjectService = SpringContext.getBean(BusinessObjectService.class);
086    
087            // get all the active security reporting groups in the database
088            Map<String, String> fieldValues = new HashMap<String, String>();
089            fieldValues.put(EndowPropertyConstants.SECURITY_REPORTING_GROUP_ACTIVE_INFICATOR, Boolean.TRUE.toString());
090            fieldValues.put(EndowPropertyConstants.SECURITY_REPORTING_GROUP_ORDER, String.valueOf(reportingGroup.getSecurityReportingGrpOrder()));
091    
092            List<SecurityReportingGroup> dataToValidateList = new ArrayList<SecurityReportingGroup>(businessObjectService.findMatching(SecurityReportingGroup.class, fieldValues));
093    
094            // iterate throught the retrieved list of reporting groups and check if there is another reporting group with the same order
095            // number
096            for (SecurityReportingGroup record : dataToValidateList) {
097                if (reportingGroup.getCode() != null && !reportingGroup.getCode().equals(record.getCode())) {
098                    putFieldError(EndowPropertyConstants.SECURITY_REPORTING_GROUP_ORDER, EndowKeyConstants.SecurityReportingGroupConstants.ERROR_SECURITY_REPORTING_GROUP_ORDER_DUPLICATE_VALUE);
099                    success = false;
100                    break;
101                }
102    
103            }
104            return success;
105        }
106    
107    }