Coverage Report - org.kuali.rice.krms.impl.rule.TermBusRule
 
Classes in this File Line Coverage Branch Coverage Complexity
TermBusRule
0%
0/22
0%
0/14
3.25
 
 1  
 /**
 2  
  * Copyright 2005-2011 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.document.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.impl.repository.KrmsRepositoryServiceLocator;
 23  
 import org.kuali.rice.krms.impl.repository.TermBo;
 24  
 import org.kuali.rice.krms.impl.repository.TermBoService;
 25  
 import org.kuali.rice.krms.impl.util.KRMSPropertyConstants;
 26  
 
 27  
 import java.util.HashMap;
 28  
 import java.util.Map;
 29  
 
 30  0
 public class TermBusRule extends MaintenanceDocumentRuleBase {
 31  
 
 32  
     @Override
 33  
     protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
 34  0
         boolean isValid = true;
 35  
 
 36  0
         TermBo term = (TermBo) document.getNewMaintainableObject().getDataObject();
 37  0
         isValid &= validateId(term);
 38  0
         isValid &= validateDescriptionNamespace(term);
 39  
 
 40  0
         return isValid;
 41  
     }
 42  
 
 43  
     private boolean validateId(TermBo term) {
 44  0
         if (StringUtils.isNotBlank(term.getId())) {
 45  0
             TermDefinition termInDatabase = getTermBoService().getTermById(term.getId());
 46  0
             if ((termInDatabase  != null) && (!StringUtils.equals(termInDatabase.getId(), term.getId()))) {
 47  0
                 this.putFieldError(KRMSPropertyConstants.Term.TERM_ID, "error.term.duplicateId");
 48  0
                 return false;
 49  
             }
 50  
         }
 51  
 
 52  0
         return true;
 53  
     }
 54  
 
 55  
     /**
 56  
      * Check if the name-namespace pair already exist.
 57  
      * @param term
 58  
      * @return true if the name-namespace pair is unique, false otherwise
 59  
      */
 60  
     private boolean validateDescriptionNamespace(TermBo term) {
 61  0
         if (StringUtils.isNotBlank(term.getDescription()) && StringUtils.isNotBlank(
 62  
                 term.getSpecification().getNamespace())) {
 63  
 
 64  0
             Map<String, String> criteria = new HashMap<String, String>();
 65  
 
 66  0
             criteria.put("description", term.getDescription());
 67  0
             criteria.put("specification.namespace", term.getSpecification().getNamespace());
 68  
 
 69  0
             TermBo termInDatabase = getBoService().findByPrimaryKey(TermBo.class, criteria);
 70  
 
 71  0
             if((termInDatabase != null) && (!StringUtils.equals(termInDatabase.getId(), term.getId()))) {
 72  0
                 this.putFieldError(KRMSPropertyConstants.Term.DESCRIPTION, "error.term.duplicateNameNamespace");
 73  0
                 return false;
 74  
             }
 75  
         }
 76  
 
 77  0
         return true;
 78  
     }
 79  
 
 80  
     public TermBoService getTermBoService() {
 81  0
         return KrmsRepositoryServiceLocator.getTermBoService();
 82  
     }
 83  
 
 84  
 }