View Javadoc

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