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.sec.document.validation.impl;
017    
018    import java.util.HashMap;
019    import java.util.Map;
020    
021    import org.kuali.kfs.sec.SecConstants;
022    import org.kuali.kfs.sec.SecKeyConstants;
023    import org.kuali.kfs.sec.businessobject.SecurityDefinition;
024    import org.kuali.kfs.sec.businessobject.SecurityModel;
025    import org.kuali.kfs.sys.KFSPropertyConstants;
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.KNSConstants;
032    
033    
034    /**
035     * Implements business rules checks on the SecurityDefinition maintenance document
036     */
037    public class SecurityDefinitionRule extends MaintenanceDocumentRuleBase {
038        protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SecurityDefinitionRule.class);
039    
040        private SecurityDefinition oldSecurityDefinition;
041        private SecurityDefinition newSecurityDefinition;
042    
043        public SecurityDefinitionRule() {
044            super();
045        }
046    
047        /**
048         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomApproveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
049         */
050        @Override
051        protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
052            boolean isValid = super.processCustomApproveDocumentBusinessRules(document);
053    
054            if (!isValid) {
055                return isValid;
056            }
057    
058            boolean isMaintenanceEdit = document.isEdit();
059    
060            isValid &= validateSecurityDefinition(isMaintenanceEdit);
061    
062            return isValid;
063        }
064    
065        /**
066         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
067         */
068        @Override
069        protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
070            boolean isValid = super.processCustomRouteDocumentBusinessRules(document);
071    
072            if (!isValid) {
073                return isValid;
074            }
075    
076            boolean isMaintenanceEdit = document.isEdit();
077    
078            isValid &= validateSecurityDefinition(isMaintenanceEdit);
079    
080            return isValid;
081        }
082    
083        /**
084         * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#setupConvenienceObjects()
085         */
086        @Override
087        public void setupConvenienceObjects() {
088            oldSecurityDefinition = (SecurityDefinition) super.getOldBo();
089            newSecurityDefinition = (SecurityDefinition) super.getNewBo();
090        }
091    
092        /**
093         * Validates business rules against a security definition record
094         * 
095         * @param isMaintenanceEdit boolean indicating whether the maintenance action is an edit (true), or a new/copy (false)
096         * @return boolean true if all rules pass, false if at least one fails
097         */
098        protected boolean validateSecurityDefinition(boolean isMaintenanceEdit) {
099            boolean isValid = true;
100    
101            if (!isMaintenanceEdit) {
102                boolean validDefinitionName = verifyDefinitionNameIsUnique(newSecurityDefinition, KNSConstants.MAINTENANCE_NEW_MAINTAINABLE);
103                if (!validDefinitionName) {
104                    isValid = false;
105                }
106            }
107    
108            return isValid;
109        }
110    
111        /**
112         * For new or copy action verifies the name given for the definition is not being used by another definition
113         * 
114         * @param securityDefinition SecurityDefinition with name to check
115         * @param errorKeyPrefix String errorPrefix to use if any errors are found
116         * @return boolean true if name exists, false if not
117         */
118        protected boolean verifyDefinitionNameIsUnique(SecurityDefinition securityDefinition, String errorKeyPrefix) {
119            boolean isValid = true;
120    
121            Map<String, String> searchValues = new HashMap<String, String>();
122            searchValues.put(KFSPropertyConstants.NAME, securityDefinition.getName());
123    
124            int matchCount = SpringContext.getBean(BusinessObjectService.class).countMatching(SecurityDefinition.class, searchValues);
125            if (matchCount > 0) {
126                GlobalVariables.getMessageMap().putError(errorKeyPrefix + KFSPropertyConstants.NAME, SecKeyConstants.ERROR_DEFINITION_NAME_NON_UNIQUE, securityDefinition.getName());
127                isValid = false;
128            }
129    
130            return isValid;
131        }
132    
133    
134    }