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.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.krms.api.repository.context.ContextDefinition;
22  import org.kuali.rice.krms.impl.repository.ContextBo;
23  import org.kuali.rice.krms.impl.repository.ContextBoService;
24  import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
25  import org.kuali.rice.krms.impl.util.KRMSPropertyConstants;
26  
27  public class ContextBusRule extends MaintenanceDocumentRuleBase {
28      @Override
29      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
30          boolean isValid = true;
31  
32          ContextBo context = (ContextBo) document.getNewMaintainableObject().getDataObject();
33          isValid &= validateId(context);
34          isValid &= validateNameNamespace(context);
35  
36          return isValid;
37      }
38  
39      private boolean validateId(ContextBo context) {
40          if (StringUtils.isNotBlank(context.getId())) {
41              ContextDefinition contextInDatabase = getContextBoService().getContextByContextId(context.getId());
42              if ((contextInDatabase  != null) && (!StringUtils.equals(contextInDatabase.getId(), context.getId()))) {
43                  this.putFieldError(KRMSPropertyConstants.Context.CONTEXT_ID, "error.context.duplicateId");
44                  return false;
45              }
46          }
47  
48          return true;
49      }
50  
51      /**
52       * Check if the name-namespace pair already exist.
53       * @param context
54       * @return true if the name-namespace pair is unique, false otherwise
55       */
56      private boolean validateNameNamespace(ContextBo context) {
57          if (StringUtils.isNotBlank(context.getName()) && StringUtils.isNotBlank(context.getNamespace())) {
58              ContextDefinition contextInDatabase = getContextBoService().getContextByNameAndNamespace(context.getName(), context.getNamespace());
59              if((contextInDatabase != null) && (!StringUtils.equals(contextInDatabase.getId(), context.getId()))) {
60                  this.putFieldError(KRMSPropertyConstants.Context.NAME, "error.context.duplicateNameNamespace");
61                  return false;
62              }
63          }
64  
65          return true;
66      }
67  
68      public ContextBoService getContextBoService() {
69          return KrmsRepositoryServiceLocator.getContextBoService();
70      }
71  }