001/** 002 * Copyright 2005-2016 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 */ 016package org.kuali.rice.krms.impl.rule; 017 018import org.apache.commons.lang.StringUtils; 019import org.kuali.rice.krad.maintenance.MaintenanceDocument; 020import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase; 021import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition; 022import org.kuali.rice.krms.impl.repository.CategoryBo; 023import org.kuali.rice.krms.impl.repository.ContextBo; 024import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator; 025import org.kuali.rice.krms.impl.repository.TermBoService; 026import org.kuali.rice.krms.impl.repository.TermSpecificationBo; 027import org.kuali.rice.krms.impl.util.KRMSPropertyConstants; 028 029import java.util.ArrayList; 030import java.util.HashMap; 031import java.util.LinkedList; 032import java.util.List; 033import java.util.Map; 034 035public class TermSpecBusRule extends MaintenanceDocumentRuleBase { 036 037 @Override 038 protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) { 039 boolean isValid = true; 040 041 TermSpecificationBo termSpec = (TermSpecificationBo) document.getNewMaintainableObject().getDataObject(); 042 isValid &= validateId(termSpec); 043 isValid &= validateCategory(termSpec); 044 isValid &= validateContext(termSpec); 045 isValid &= validateNameNamespace(termSpec); 046 047 return isValid; 048 } 049 050 private boolean validateCategory(TermSpecificationBo termSpecificationBo) { 051 List<CategoryBo> categories = termSpecificationBo.getCategories(); 052 List<String> categoryIds = new ArrayList<String>(); 053 054 boolean valid = true; 055 for (CategoryBo category: categories) { 056 if (categoryIds.contains(category.getId())) { 057 this.putFieldError(KRMSPropertyConstants.TermSpecification.CATEGORY, "error.termSpecification.duplicateCategory"); 058 valid = false; 059 } else { 060 categoryIds.add(category.getId()); 061 } 062 } 063 return valid; 064 } 065 066 private boolean validateContext(TermSpecificationBo termSpec) { 067 List<ContextBo> termSpecContexts = termSpec.getContexts(); 068 List<String> contextIds = new ArrayList<String>(); 069 boolean valid = true; 070 for (ContextBo context: termSpecContexts) { 071 if (contextIds.contains(context.getId())) { 072 this.putFieldError(KRMSPropertyConstants.TermSpecification.CONTEXT, "error.termSpecification.duplicateContext"); 073 valid = false; 074 } else { 075 contextIds.add(context.getId()); 076 } 077 } 078 return valid; 079 } 080 081 private boolean validateId(TermSpecificationBo termSpec) { 082 if (StringUtils.isNotBlank(termSpec.getId())) { 083 TermSpecificationDefinition termSpecInDatabase = getTermBoService().getTermSpecificationById(termSpec.getId()); 084 if ((termSpecInDatabase != null) && (!StringUtils.equals(termSpecInDatabase.getId(), termSpec.getId()))) { 085 this.putFieldError(KRMSPropertyConstants.TermSpecification.TERM_SPECIFICATION_ID, "error.termSpecification.duplicateId"); 086 return false; 087 } 088 } 089 090 return true; 091 } 092 093 /** 094 * Check if the name-namespace pair already exist. 095 * @param termSpec 096 * @return true if the name-namespace pair is unique, false otherwise 097 */ 098 private boolean validateNameNamespace(TermSpecificationBo termSpec) { 099 if (StringUtils.isNotBlank(termSpec.getName()) && StringUtils.isNotBlank( 100 termSpec.getNamespace())) { 101 102 Map<String, String> criteria = new HashMap<String, String>(); 103 104 criteria.put("name", termSpec.getName()); 105 criteria.put("namespace", termSpec.getNamespace()); 106 107 TermSpecificationBo termSpecInDatabase = getBoService().findByPrimaryKey(TermSpecificationBo.class, criteria); 108 109 if((termSpecInDatabase != null) && (!StringUtils.equals(termSpecInDatabase.getId(), termSpec.getId()))) { 110 this.putFieldError(KRMSPropertyConstants.TermSpecification.NAME, "error.term.duplicateNameNamespace"); 111 return false; 112 } 113 } 114 115 return true; 116 } 117 118 public TermBoService getTermBoService() { 119 return KrmsRepositoryServiceLocator.getTermBoService(); 120 } 121 122}