001 /**
002 * Copyright 2005-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.krms.impl.rule;
017
018 import org.apache.commons.lang.StringUtils;
019 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
020 import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
021 import org.kuali.rice.krms.api.repository.term.TermDefinition;
022 import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
023 import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
024 import org.kuali.rice.krms.impl.repository.TermBo;
025 import org.kuali.rice.krms.impl.repository.TermBoService;
026 import org.kuali.rice.krms.impl.util.KRMSPropertyConstants;
027
028 import java.util.HashMap;
029 import java.util.Map;
030
031 public class TermBusRule extends MaintenanceDocumentRuleBase {
032
033 @Override
034 protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
035 boolean isValid = true;
036
037 TermBo term = (TermBo) document.getNewMaintainableObject().getDataObject();
038 isValid &= validateId(term);
039 isValid &= validateDescriptionNamespace(term);
040 isValid &= validateTermSpecId(term);
041
042 return isValid;
043 }
044
045 private boolean validateTermSpecId(TermBo term) {
046 if (StringUtils.isBlank(term.getSpecificationId())) {
047 this.putFieldError(KRMSPropertyConstants.Term.TERM_SPECIFICATION_ID, "error.term.invalidTermSpecification");
048 return false;
049 }
050
051 TermSpecificationDefinition termSpec =
052 KrmsRepositoryServiceLocator.getTermBoService().getTermSpecificationById(term.getSpecificationId());
053
054 if (termSpec == null) {
055 this.putFieldError(KRMSPropertyConstants.Term.TERM_SPECIFICATION_ID, "error.term.invalidTermSpecification");
056 return false;
057 }
058
059 return true;
060 }
061
062 private boolean validateId(TermBo term) {
063 if (StringUtils.isNotBlank(term.getId())) {
064 TermDefinition termInDatabase = getTermBoService().getTerm(term.getId());
065 if ((termInDatabase != null) && (!StringUtils.equals(termInDatabase.getId(), term.getId()))) {
066 this.putFieldError(KRMSPropertyConstants.Term.TERM_ID, "error.term.duplicateId");
067 return false;
068 }
069 }
070
071 return true;
072 }
073
074 /**
075 * Check if the name-namespace pair already exist.
076 * @param term
077 * @return true if the name-namespace pair is unique, false otherwise
078 */
079 private boolean validateDescriptionNamespace(TermBo term) {
080 if (term.getSpecification() != null && StringUtils.isNotBlank(term.getDescription()) && StringUtils.isNotBlank(
081 term.getSpecification().getNamespace())) {
082
083 Map<String, String> criteria = new HashMap<String, String>();
084
085 criteria.put("description", term.getDescription());
086 criteria.put("specification.namespace", term.getSpecification().getNamespace());
087
088 TermBo termInDatabase = getBoService().findByPrimaryKey(TermBo.class, criteria);
089
090 if((termInDatabase != null) && (!StringUtils.equals(termInDatabase.getId(), term.getId()))) {
091 this.putFieldError(KRMSPropertyConstants.Term.DESCRIPTION, "error.term.duplicateNameNamespace");
092 return false;
093 }
094 }
095
096 return true;
097 }
098
099 public TermBoService getTermBoService() {
100 return KrmsRepositoryServiceLocator.getTermBoService();
101 }
102
103 }