View Javadoc
1   /**
2    * Copyright 2005-2015 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.collections.CollectionUtils;
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.core.api.criteria.QueryByCriteria;
21  import org.kuali.rice.core.api.criteria.QueryResults;
22  import org.kuali.rice.krad.data.DataObjectService;
23  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
24  import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
25  import org.kuali.rice.krad.service.KRADServiceLocator;
26  import org.kuali.rice.krms.api.repository.term.TermDefinition;
27  import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
28  import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
29  import org.kuali.rice.krms.impl.repository.TermBo;
30  import org.kuali.rice.krms.impl.repository.TermBoService;
31  import org.kuali.rice.krms.impl.util.KRMSPropertyConstants;
32  
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  public class TermBusRule extends MaintenanceDocumentRuleBase {
37  
38      private DataObjectService dataObjectService;
39  
40      @Override
41      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
42          boolean isValid = true;
43  
44          TermBo term = (TermBo) document.getNewMaintainableObject().getDataObject();
45          isValid &= validateId(term);
46          isValid &= validateDescriptionNamespace(term);
47          isValid &= validateTermSpecId(term);
48  
49          return isValid;
50      }
51  
52      private boolean validateTermSpecId(TermBo term) {
53          if (StringUtils.isBlank(term.getSpecificationId())) {
54              this.putFieldError(KRMSPropertyConstants.Term.TERM_SPECIFICATION_ID, "error.term.invalidTermSpecification");
55  
56              return false;
57          }
58  
59          TermSpecificationDefinition termSpec =
60                  KrmsRepositoryServiceLocator.getTermBoService().getTermSpecificationById(term.getSpecificationId());
61  
62          if (termSpec == null) {
63              this.putFieldError(KRMSPropertyConstants.Term.TERM_SPECIFICATION_ID, "error.term.invalidTermSpecification");
64  
65              return false;
66          }
67  
68          return true;
69      }
70  
71      private boolean validateId(TermBo term) {
72          if (StringUtils.isNotBlank(term.getId())) {
73              TermDefinition termInDatabase = getTermBoService().getTerm(term.getId());
74              if ((termInDatabase  != null) && (!StringUtils.equals(termInDatabase.getId(), term.getId()))) {
75                  this.putFieldError(KRMSPropertyConstants.Term.TERM_ID, "error.term.duplicateId");
76  
77                  return false;
78              }
79          }
80  
81          return true;
82      }
83  
84      /**
85       * Check if the name-namespace pair already exist.
86       * @param term
87       * @return true if the name-namespace pair is unique, false otherwise
88       */
89      private boolean validateDescriptionNamespace(TermBo term) {
90          if (term.getSpecification() != null && StringUtils.isNotBlank(term.getDescription()) && StringUtils.isNotBlank(
91                  term.getSpecification().getNamespace())) {
92  
93              Map<String, String> critMap = new HashMap<String, String>();
94  
95              critMap.put("description", term.getDescription());
96              critMap.put("specification.namespace", term.getSpecification().getNamespace());
97              QueryByCriteria criteria = QueryByCriteria.Builder.andAttributes(critMap).build();
98              QueryResults<TermBo> queryResults = getDataObjectService().findMatching(TermBo.class, criteria);
99  
100             TermBo termInDatabase = null;
101 
102             if (!CollectionUtils.isEmpty(queryResults.getResults()) && queryResults.getResults().size() == 1) {
103                 termInDatabase = queryResults.getResults().get(0);
104             }
105 
106             if((termInDatabase != null) && (!StringUtils.equals(termInDatabase.getId(), term.getId()))) {
107                 this.putFieldError(KRMSPropertyConstants.Term.DESCRIPTION, "error.term.duplicateNameNamespace");
108 
109                 return false;
110             }
111         }
112 
113         return true;
114     }
115 
116     public TermBoService getTermBoService() {
117         return KrmsRepositoryServiceLocator.getTermBoService();
118     }
119 
120     public DataObjectService getDataObjectService() {
121         if(dataObjectService == null){
122             return KRADServiceLocator.getDataObjectService();
123         }
124 
125         return dataObjectService;
126     }
127 
128     public void setBoService(DataObjectService dataObjectService) {
129         this.dataObjectService = dataObjectService;
130     }
131 
132 }