View Javadoc
1   /*
2    * Copyright 2009 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 1.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl1.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.sec.document.validation.impl;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.kuali.ole.sec.SecKeyConstants;
22  import org.kuali.ole.sec.businessobject.SecurityDefinition;
23  import org.kuali.ole.sys.OLEPropertyConstants;
24  import org.kuali.rice.kns.document.MaintenanceDocument;
25  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
26  import org.kuali.rice.krad.util.GlobalVariables;
27  import org.kuali.rice.krad.util.KRADConstants;
28  
29  
30  /**
31   * Implements business rules checks on the SecurityDefinition maintenance document
32   */
33  public class SecurityDefinitionRule extends MaintenanceDocumentRuleBase {
34      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SecurityDefinitionRule.class);
35  
36      protected SecurityDefinition oldSecurityDefinition;
37      protected SecurityDefinition newSecurityDefinition;
38  
39      /**
40       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomApproveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
41       */
42      @Override
43      protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
44          boolean isValid = super.processCustomApproveDocumentBusinessRules(document);
45  
46          if (!isValid) {
47              return isValid;
48          }
49  
50          boolean isMaintenanceEdit = document.isEdit();
51  
52          isValid &= validateSecurityDefinition(isMaintenanceEdit);
53  
54          return isValid;
55      }
56  
57      /**
58       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
59       */
60      @Override
61      protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
62          boolean isValid = super.processCustomRouteDocumentBusinessRules(document);
63  
64          if (!isValid) {
65              return isValid;
66          }
67  
68          boolean isMaintenanceEdit = document.isEdit();
69  
70          isValid &= validateSecurityDefinition(isMaintenanceEdit);
71  
72          return isValid;
73      }
74  
75      /**
76       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#setupConvenienceObjects()
77       */
78      @Override
79      public void setupConvenienceObjects() {
80          oldSecurityDefinition = (SecurityDefinition) super.getOldBo();
81          newSecurityDefinition = (SecurityDefinition) super.getNewBo();
82      }
83  
84      /**
85       * Validates business rules against a security definition record
86       * 
87       * @param isMaintenanceEdit boolean indicating whether the maintenance action is an edit (true), or a new/copy (false)
88       * @return boolean true if all rules pass, false if at least one fails
89       */
90      protected boolean validateSecurityDefinition(boolean isMaintenanceEdit) {
91          boolean isValid = true;
92  
93          if (!isMaintenanceEdit) {
94              boolean validDefinitionName = verifyDefinitionNameIsUnique(newSecurityDefinition, KRADConstants.MAINTENANCE_NEW_MAINTAINABLE);
95              if (!validDefinitionName) {
96                  isValid = false;
97              }
98          }
99  
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 }