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.validation; 017 018import org.apache.commons.collections.CollectionUtils; 019import org.apache.commons.lang.StringUtils; 020import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; 021import org.kuali.rice.core.api.uif.RemotableAttributeField; 022import org.kuali.rice.core.api.uif.RemotableRadioButtonGroup; 023import org.kuali.rice.krms.api.repository.rule.RuleDefinition; 024import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition; 025import org.kuali.rice.krms.api.repository.type.KrmsTypeAttribute; 026import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition; 027import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService; 028import org.kuali.rice.krms.framework.engine.Rule; 029import org.kuali.rice.krms.framework.type.ValidationActionType; 030import org.kuali.rice.krms.framework.type.ValidationRuleType; 031import org.kuali.rice.krms.framework.type.ValidationRuleTypeService; 032import org.kuali.rice.krms.impl.provider.repository.RepositoryToEngineTranslator; 033import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator; 034import org.kuali.rice.krms.impl.type.KrmsTypeServiceBase; 035 036import javax.jws.WebParam; 037import java.util.ArrayList; 038import java.util.List; 039import java.util.Map; 040import java.util.TreeMap; 041 042/** 043 * {@link ValidationRuleTypeService} implementation 044 * 045 * @author Kuali Rice Team (rice.collab@kuali.org) 046 */ 047public final class ValidationRuleTypeServiceImpl extends KrmsTypeServiceBase implements ValidationRuleTypeService { 048 049 private RepositoryToEngineTranslator translator; 050 051 /** 052 * private constructor to enforce use of static factory 053 */ 054 private ValidationRuleTypeServiceImpl(){ 055 super(); 056 } 057 058 /** 059 * Factory method for getting a {@link ValidationRuleTypeService} 060 * @return a {@link ValidationRuleTypeService} corresponding to the given {@link ValidationRuleType}. 061 */ 062 public static ValidationRuleTypeService getInstance() { 063 return new ValidationRuleTypeServiceImpl(); 064 } 065 066 @Override 067 public Rule loadRule(RuleDefinition validationRuleDefinition) { 068 if (validationRuleDefinition == null) { throw new RiceIllegalArgumentException("validationRuleDefinition must not be null"); } 069 if (validationRuleDefinition.getAttributes() == null) { throw new RiceIllegalArgumentException("validationRuleDefinition must not be null");} 070 071 if (!validationRuleDefinition.getAttributes().containsKey(VALIDATIONS_RULE_TYPE_CODE_ATTRIBUTE)) { 072 073 throw new RiceIllegalArgumentException("validationRuleDefinition does not contain an " + 074 VALIDATIONS_RULE_TYPE_CODE_ATTRIBUTE + " attribute"); 075 } 076 String validationRuleTypeCode = validationRuleDefinition.getAttributes().get(VALIDATIONS_RULE_TYPE_CODE_ATTRIBUTE); 077 078 if (StringUtils.isBlank(validationRuleTypeCode)) { 079 throw new RiceIllegalArgumentException(VALIDATIONS_RULE_TYPE_CODE_ATTRIBUTE + " attribute must not be null or blank"); 080 } 081 082 if (ValidationRuleType.VALID.getCode().equals(validationRuleTypeCode)) { 083 return new ValidationRule(ValidationRuleType.VALID, validationRuleDefinition.getName(), 084 translator.translatePropositionDefinition(validationRuleDefinition.getProposition()), 085 translator.translateActionDefinitions(validationRuleDefinition.getActions())); 086 } 087 if (ValidationRuleType.INVALID.getCode().equals(validationRuleTypeCode)) { 088 return new ValidationRule(ValidationRuleType.INVALID, validationRuleDefinition.getName(), 089 translator.translatePropositionDefinition(validationRuleDefinition.getProposition()), 090 translator.translateActionDefinitions(validationRuleDefinition.getActions())); 091 } 092 return null; 093 } 094 095 /** 096 * @param translator the RepositoryToEngineTranslator to set 097 */ 098 public void setRepositoryToEngineTranslator(RepositoryToEngineTranslator translator) { 099 this.translator = translator; 100 } 101 102 /** 103 * 104 * @return List<RemotableAttributeField> 105 * @throws RiceIllegalArgumentException 106 */ 107 @Override 108 public List<RemotableAttributeField> getAttributeFields(@WebParam(name = "krmsTypeId") String krmsTypeId) throws RiceIllegalArgumentException { 109 Map<String, String> keyLabels = new TreeMap<String, String>(); 110 keyLabels.put(ValidationRuleType.VALID.getCode(), "Valid - action executed when false"); 111 keyLabels.put(ValidationRuleType.INVALID.getCode(), "Invalid - action executed when true"); 112 return getAttributeFields(krmsTypeId, keyLabels); 113 } 114 115 /** 116 * 117 * @param krmsTypeId 118 * @param keyLabels Map<String, String> where the key is the VallidationRuleType code with the value being the UI Label. 119 * @return List<RemotableAttributeField> for Validation Rules 120 * @throws RiceIllegalArgumentException if krmsType is null (krmsTypeId lookup returns null) 121 */ 122 private List<RemotableAttributeField> getAttributeFields(@WebParam(name = "krmsTypeId") String krmsTypeId, Map<String, String> keyLabels) throws RiceIllegalArgumentException { 123 List<RemotableAttributeField> results = new ArrayList<RemotableAttributeField>(); 124 125 KrmsTypeDefinition krmsType = 126 KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService().getTypeById(krmsTypeId); 127 128 if (krmsType == null) { 129 throw new RiceIllegalArgumentException("krmsTypeId must be a valid id of a KRMS type"); 130 } else { 131 List<KrmsTypeAttribute> typeAttributes = krmsType.getAttributes(); 132 Map<String, Integer> attribDefIdSequenceNumbers = new TreeMap<String, Integer>(); 133 Map<String, String> unsortedIdLables = new TreeMap<String, String>(); 134 if (!CollectionUtils.isEmpty(typeAttributes)) { 135 // translate the attribute and store the sort code in our map 136 KrmsTypeRepositoryService typeRepositoryService = KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService(); 137 KrmsAttributeDefinition attributeDefinition = null; 138 RadioButtonTypeServiceUtil util = new RadioButtonTypeServiceUtil(); 139 140 for (KrmsTypeAttribute typeAttribute : typeAttributes) { 141 142 143 attributeDefinition = typeRepositoryService.getAttributeDefinitionById(typeAttribute.getAttributeDefinitionId()); 144 145 if (VALIDATIONS_RULE_TYPE_CODE_ATTRIBUTE.equals(attributeDefinition.getName())) { 146 RemotableAttributeField attributeField = util.translateTypeAttribute(attributeDefinition, keyLabels); 147 results.add(attributeField); 148 } 149 } 150 } 151 } 152 return results; 153 } 154} 155