View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.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/ecl2.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.rice.krms.util;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.util.RiceKeyConstants;
20  import org.kuali.rice.krad.bo.GlobalBusinessObject;
21  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
22  import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
23  import org.kuali.rice.krad.util.KRADConstants;
24  import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
25  import org.kuali.rice.krms.dto.RuleManagementWrapper;
26  import org.kuali.rice.krms.framework.type.ActionTypeService;
27  import org.kuali.rice.krms.impl.authorization.AgendaAuthorizationService;
28  import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
29  import org.kuali.rice.krms.impl.util.KRMSPropertyConstants;
30  
31  /**
32   * This class contains the rules for the AgendaEditor.
33   */
34  public class RuleEditorBusRule extends MaintenanceDocumentRuleBase {
35  
36      @Override
37      protected boolean primaryKeyCheck(MaintenanceDocument document) {
38          // default to success if no failures
39          boolean success = true;
40          Class<?> dataObjectClass = document.getNewMaintainableObject().getDataObjectClass();
41  
42          // Since the dataObject is a wrapper class we need to return the agendaBo instead.
43          Object oldBo = ((RuleManagementWrapper) document.getOldMaintainableObject().getDataObject());
44          Object newDataObject = ((RuleManagementWrapper) document.getNewMaintainableObject().getDataObject());
45  
46          // We dont do primaryKeyChecks on Global Business Object maintenance documents. This is
47          // because it doesnt really make any sense to do so, given the behavior of Globals. When a
48          // Global Document completes, it will update or create a new record for each BO in the list.
49          // As a result, there's no problem with having existing BO records in the system, they will
50          // simply get updated.
51          if (newDataObject instanceof GlobalBusinessObject) {
52              return success;
53          }
54  
55          // fail and complain if the person has changed the primary keys on
56          // an EDIT maintenance document.
57          if (document.isEdit()) {
58              if (!getDataObjectMetaDataService().equalsByPrimaryKeys(oldBo, newDataObject)) {
59                  // add a complaint to the errors
60                  putDocumentError(KRADConstants.DOCUMENT_ERRORS,
61                          RiceKeyConstants.ERROR_DOCUMENT_MAINTENANCE_PRIMARY_KEYS_CHANGED_ON_EDIT,
62                          getHumanReadablePrimaryKeyFieldNames(dataObjectClass));
63                  success &= false;
64              }
65          }
66  
67          // fail and complain if the person has selected a new object with keys that already exist
68          // in the DB.
69          else if (document.isNew()) {
70  
71              // TODO: check for valid primary keys.
72          }
73  
74          return success;
75      }
76  
77      @Override
78      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
79          return true;
80      }
81  
82      /**
83       * Check that the rule type is valid when specified.
84       * @param ruleTypeId, the type id
85       * @param contextId, the contextId the action needs to belong to.
86       * @return true if valid, false otherwise.
87       */
88      private boolean validRuleType(String ruleTypeId, String contextId) {
89          if (StringUtils.isBlank(ruleTypeId)) {
90              return true;
91          }
92  
93          if (getKrmsTypeRepositoryService().getRuleTypeByRuleTypeIdAndContextId(ruleTypeId, contextId) != null) {
94              return true;
95          } else {
96              this.putFieldError(KRMSPropertyConstants.Rule.TYPE, "error.rule.invalidType");
97              return false;
98          }
99      }
100 
101     /**
102      * Check that the rule action type is valid when specified.
103      * @param typeId, the action type id
104      * @parm contextId, the contextId the action needs to belong to.
105      * @return true if valid, false otherwise.
106      */
107     private boolean validRuleActionType(String typeId, String contextId) {
108         if (StringUtils.isBlank(typeId)) {
109             return true;
110         }
111 
112         if (getKrmsTypeRepositoryService().getActionTypeByActionTypeIdAndContextId(typeId, contextId) != null) {
113             return true;
114         } else {
115             this.putFieldError(KRMSPropertyConstants.Action.TYPE, "error.action.invalidType");
116             return false;
117         }
118     }
119 
120     /**
121      * Check that a action name is specified.
122      */
123     private boolean validRuleActionName(String name) {
124         if (StringUtils.isNotBlank(name)) {
125             return true;
126         } else {
127             this.putFieldError(KRMSPropertyConstants.Action.NAME, "error.action.missingName");
128             return false;
129         }
130     }
131 
132     public KrmsTypeRepositoryService getKrmsTypeRepositoryService() {
133         return KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService();
134     }
135 
136     public ActionTypeService getActionTypeService(String serviceName) {
137         return (ActionTypeService)KrmsRepositoryServiceLocator.getService(serviceName);
138     }
139 
140     public AgendaAuthorizationService getAgendaAuthorizationService() {
141         return KrmsRepositoryServiceLocator.getAgendaAuthorizationService();
142     }
143 
144 }
145