View Javadoc
1   /**
2    * Copyright 2005-2014 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.term.TermSpecificationDefinition;
22  import org.kuali.rice.krms.impl.repository.CategoryBo;
23  import org.kuali.rice.krms.impl.repository.ContextBo;
24  import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
25  import org.kuali.rice.krms.impl.repository.TermBoService;
26  import org.kuali.rice.krms.impl.repository.TermSpecificationBo;
27  import org.kuali.rice.krms.impl.util.KRMSPropertyConstants;
28  
29  import java.util.ArrayList;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  import static org.kuali.rice.krms.impl.repository.BusinessObjectServiceMigrationUtils.findSingleMatching;
35  
36  public class TermSpecBusRule extends MaintenanceDocumentRuleBase {
37  
38      @Override
39      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
40          boolean isValid = true;
41  
42          TermSpecificationBo termSpec = (TermSpecificationBo) document.getNewMaintainableObject().getDataObject();
43          isValid &= validateId(termSpec);
44          isValid &= validateCategory(termSpec);
45          isValid &= validateContext(termSpec);
46          isValid &= validateNameNamespace(termSpec);
47  
48          return isValid;
49      }
50  
51      private boolean validateCategory(TermSpecificationBo termSpecificationBo) {
52          List<CategoryBo> categories = termSpecificationBo.getCategories();
53          List<String> categoryIds = new ArrayList<String>();
54  
55          boolean valid = true;
56          for (CategoryBo category: categories) {
57              if (categoryIds.contains(category.getId())) {
58                  this.putFieldError(KRMSPropertyConstants.TermSpecification.CATEGORY, "error.termSpecification.duplicateCategory");
59                  valid = false;
60              } else {
61                  categoryIds.add(category.getId());
62              }
63          }
64          return valid;
65      }
66      
67      private boolean validateContext(TermSpecificationBo termSpec) {
68          List<ContextBo> termSpecContexts = termSpec.getContexts();
69          List<String> contextIds = new ArrayList<String>();
70          boolean valid = true;
71          for (ContextBo context: termSpecContexts) {
72              if (contextIds.contains(context.getId())) {
73                  this.putFieldError(KRMSPropertyConstants.TermSpecification.CONTEXT, "error.termSpecification.duplicateContext");
74                  valid = false;
75              } else {
76                  contextIds.add(context.getId());
77              }
78          }
79          return valid;
80      }
81  
82      private boolean validateId(TermSpecificationBo termSpec) {
83          if (StringUtils.isNotBlank(termSpec.getId())) {
84              TermSpecificationDefinition termSpecInDatabase = getTermBoService().getTermSpecificationById(termSpec.getId());
85              if ((termSpecInDatabase  != null) && (!StringUtils.equals(termSpecInDatabase.getId(), termSpec.getId()))) {
86                  this.putFieldError(KRMSPropertyConstants.TermSpecification.TERM_SPECIFICATION_ID, "error.termSpecification.duplicateId");
87                  return false;
88              }
89          }
90  
91          return true;
92      }
93  
94      /**
95       * Check if the name-namespace pair already exist.
96       * @param termSpec
97       * @return true if the name-namespace pair is unique, false otherwise
98       */
99      private boolean validateNameNamespace(TermSpecificationBo termSpec) {
100         if (StringUtils.isNotBlank(termSpec.getName()) && StringUtils.isNotBlank(
101                 termSpec.getNamespace())) {
102 
103             Map<String, String> criteria = new HashMap<String, String>();
104 
105             criteria.put("name", termSpec.getName());
106             criteria.put("namespace", termSpec.getNamespace());
107 
108             TermSpecificationBo termSpecInDatabase =
109                     findSingleMatching(getDataObjectService(), TermSpecificationBo.class, criteria);
110 
111             if((termSpecInDatabase != null) && (!StringUtils.equals(termSpecInDatabase.getId(), termSpec.getId()))) {
112                 this.putFieldError(KRMSPropertyConstants.TermSpecification.NAME, "error.term.duplicateNameNamespace");
113                 return false;
114             }
115         }
116 
117         return true;
118     }
119 
120     public TermBoService getTermBoService() {
121         return KrmsRepositoryServiceLocator.getTermBoService();
122     }
123 }