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.impl.rule;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
20  import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
21  import org.kuali.rice.krad.util.KRADConstants;
22  import org.kuali.rice.krms.api.repository.context.ContextDefinition;
23  import org.kuali.rice.krms.impl.repository.ContextBo;
24  import org.kuali.rice.krms.impl.repository.ContextBoService;
25  import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
26  import org.kuali.rice.krms.impl.util.KRMSPropertyConstants;
27  
28  public class ContextBusRule extends MaintenanceDocumentRuleBase {
29      @Override
30      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
31          boolean isValid = true;
32  
33          ContextBo newContext = (ContextBo) document.getNewMaintainableObject().getDataObject();
34          ContextBo oldContext = (ContextBo) document.getOldMaintainableObject().getDataObject();
35          boolean isEditAction = KRADConstants.MAINTENANCE_EDIT_ACTION.equals(document.getNewMaintainableObject().getMaintenanceAction());
36  
37          isValid &= validateId(oldContext, newContext, isEditAction);
38          isValid &= validateNameNamespace(newContext, isEditAction);
39  
40          return isValid;
41      }
42  
43      private boolean validateId(ContextBo oldContext, ContextBo newContext, boolean isEditAction) {
44          if (StringUtils.isBlank(newContext.getId())) {
45              this.putFieldError(KRMSPropertyConstants.Context.CONTEXT_ID, "error.context.blankId");
46              return false;
47          }
48          if (isEditAction) {
49              if (!oldContext.getId().equals(newContext.getId())) {
50                  throw new IllegalStateException("The ID of a Context being edited must not change.");
51              }
52          } else {
53              ContextDefinition contextInDatabase = getContextBoService().getContextByContextId(newContext.getId());
54              if (contextInDatabase  != null) {
55                  this.putFieldError(KRMSPropertyConstants.Context.CONTEXT_ID, "error.context.duplicateId");
56                  return false;
57              }
58          }
59  
60          return true;
61      }
62  
63      /**
64       * Check if the name-namespace pair already exist.
65       * @param newContext
66       * @return true if the name-namespace pair is unique, false otherwise
67       */
68      private boolean validateNameNamespace(ContextBo newContext, boolean isEditAction) {
69          if (isEditAction) {
70              ContextDefinition contextInDatabase = getContextBoService().getContextByNameAndNamespace(newContext.getName(), newContext
71                      .getNamespace());
72              if ((contextInDatabase != null) && !contextInDatabase.getId().equals(newContext.getId())) { // if ID is the same, it's not a duplicate
73                  this.putFieldError(KRMSPropertyConstants.Context.NAME, "error.context.duplicateNameNamespace");
74                  return false;
75              }
76          } else if (StringUtils.isNotBlank(newContext.getName()) && StringUtils.isNotBlank(newContext.getNamespace())) {
77              ContextDefinition contextInDatabase = getContextBoService().getContextByNameAndNamespace(newContext.getName(), newContext
78                      .getNamespace());
79              if(contextInDatabase != null) {
80                  this.putFieldError(KRMSPropertyConstants.Context.NAME, "error.context.duplicateNameNamespace");
81                  return false;
82              }
83          } else {
84              if (StringUtils.isBlank(newContext.getName())) {
85                  this.putFieldError(KRMSPropertyConstants.Context.NAME, "error.context.blankName");
86              }
87              if (StringUtils.isBlank(newContext.getNamespace())) {
88                  this.putFieldError(KRMSPropertyConstants.Context.NAME, "error.context.blankNamespace");
89              }
90              return false;
91          }
92  
93          return true;
94      }
95  
96      public ContextBoService getContextBoService() {
97          return KrmsRepositoryServiceLocator.getContextBoService();
98      }
99  }