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