View Javadoc
1   /**
2    * Copyright 2005-2016 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.TermDefinition;
22  import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
23  import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
24  import org.kuali.rice.krms.impl.repository.TermBo;
25  import org.kuali.rice.krms.impl.repository.TermBoService;
26  import org.kuali.rice.krms.impl.util.KRMSPropertyConstants;
27  
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  public class TermBusRule extends MaintenanceDocumentRuleBase {
32  
33      @Override
34      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
35          boolean isValid = true;
36  
37          TermBo term = (TermBo) document.getNewMaintainableObject().getDataObject();
38          isValid &= validateId(term);
39          isValid &= validateDescriptionNamespace(term);
40          isValid &= validateTermSpecId(term);
41  
42          return isValid;
43      }
44  
45      private boolean validateTermSpecId(TermBo term) {
46          if (StringUtils.isBlank(term.getSpecificationId())) {
47              this.putFieldError(KRMSPropertyConstants.Term.TERM_SPECIFICATION_ID, "error.term.invalidTermSpecification");
48              return false;
49          }
50  
51          TermSpecificationDefinition termSpec =
52                  KrmsRepositoryServiceLocator.getTermBoService().getTermSpecificationById(term.getSpecificationId());
53  
54          if (termSpec == null) {
55              this.putFieldError(KRMSPropertyConstants.Term.TERM_SPECIFICATION_ID, "error.term.invalidTermSpecification");
56              return false;
57          }
58  
59          return true;
60      }
61  
62      private boolean validateId(TermBo term) {
63          if (StringUtils.isNotBlank(term.getId())) {
64              TermDefinition termInDatabase = getTermBoService().getTerm(term.getId());
65              if ((termInDatabase  != null) && (!StringUtils.equals(termInDatabase.getId(), term.getId()))) {
66                  this.putFieldError(KRMSPropertyConstants.Term.TERM_ID, "error.term.duplicateId");
67                  return false;
68              }
69          }
70  
71          return true;
72      }
73  
74      /**
75       * Check if the name-namespace pair already exist.
76       * @param term
77       * @return true if the name-namespace pair is unique, false otherwise
78       */
79      private boolean validateDescriptionNamespace(TermBo term) {
80          if (term.getSpecification() != null && StringUtils.isNotBlank(term.getDescription()) && StringUtils.isNotBlank(
81                  term.getSpecification().getNamespace())) {
82  
83              Map<String, String> criteria = new HashMap<String, String>();
84  
85              criteria.put("description", term.getDescription());
86              criteria.put("specification.namespace", term.getSpecification().getNamespace());
87  
88              TermBo termInDatabase = getBoService().findByPrimaryKey(TermBo.class, criteria);
89  
90              if((termInDatabase != null) && (!StringUtils.equals(termInDatabase.getId(), term.getId()))) {
91                  this.putFieldError(KRMSPropertyConstants.Term.DESCRIPTION, "error.term.duplicateNameNamespace");
92                  return false;
93              }
94          }
95  
96          return true;
97      }
98  
99      public TermBoService getTermBoService() {
100         return KrmsRepositoryServiceLocator.getTermBoService();
101     }
102 
103 }