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