Coverage Report - org.kuali.rice.krms.impl.type.KrmsTypeResolverImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
KrmsTypeResolverImpl
0%
0/52
0%
0/28
3.545
 
 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.type;
 17  
 
 18  
 import javax.xml.namespace.QName;
 19  
 
 20  
 import org.apache.commons.lang.StringUtils;
 21  
 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
 22  
 import org.kuali.rice.krms.api.engine.EngineResourceUnavailableException;
 23  
 import org.kuali.rice.krms.api.repository.RepositoryDataException;
 24  
 import org.kuali.rice.krms.api.repository.action.ActionDefinition;
 25  
 import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
 26  
 import org.kuali.rice.krms.api.repository.function.FunctionDefinition;
 27  
 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
 28  
 import org.kuali.rice.krms.api.repository.proposition.PropositionType;
 29  
 import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
 30  
 import org.kuali.rice.krms.api.repository.term.TermResolverDefinition;
 31  
 import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
 32  
 import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
 33  
 import org.kuali.rice.krms.framework.type.ActionTypeService;
 34  
 import org.kuali.rice.krms.framework.type.AgendaTypeService;
 35  
 import org.kuali.rice.krms.framework.type.FunctionTypeService;
 36  
 import org.kuali.rice.krms.framework.type.PropositionTypeService;
 37  
 import org.kuali.rice.krms.framework.type.RuleTypeService;
 38  
 import org.kuali.rice.krms.framework.type.TermResolverTypeService;
 39  
 
 40  
 /**
 41  
  * An implementation of {@link KrmsTypeResolver} which knows how to load the
 42  
  * various type services in KRMS.
 43  
  * 
 44  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 45  
  *
 46  
  */
 47  0
 public class KrmsTypeResolverImpl implements KrmsTypeResolver {
 48  
 
 49  
         private KrmsTypeRepositoryService typeRepositoryService;
 50  
         private PropositionTypeService defaultCompoundPropositionTypeService;
 51  
         private PropositionTypeService defaultSimplePropositionTypeService;
 52  
         
 53  
         @Override
 54  
         public PropositionTypeService getPropositionTypeService(PropositionDefinition propositionDefinition) {
 55  0
                 if (propositionDefinition == null) {
 56  0
                         throw new IllegalArgumentException("propositionDefinition was null");
 57  
                 }
 58  0
                 if (propositionDefinition.getTypeId() == null) {
 59  0
                         PropositionType propositionType = PropositionType.fromCode(propositionDefinition.getPropositionTypeCode());
 60  0
                         if (PropositionType.COMPOUND == propositionType) {
 61  0
                                 return defaultCompoundPropositionTypeService;
 62  0
                         } else if (PropositionType.SIMPLE == propositionType) {
 63  0
                                 return defaultSimplePropositionTypeService;
 64  
                         }
 65  0
                         throw new RepositoryDataException("Proposition does not have a typeId defined and does not define a valid proposition type code.  Proposition id is: " + propositionDefinition.getId());
 66  
                 }
 67  0
                 KrmsTypeDefinition typeDefinition = getTypeDefinition(propositionDefinition.getTypeId());
 68  0
                 return resolveTypeService(typeDefinition, PropositionTypeService.class);
 69  
         }
 70  
 
 71  
         @Override
 72  
         public ActionTypeService getActionTypeService(ActionDefinition actionDefinition) {
 73  0
                 if (actionDefinition == null) {
 74  0
                         throw new IllegalArgumentException("actionDefinition was null");
 75  
                 }
 76  0
                 KrmsTypeDefinition typeDefinition = getTypeDefinition(actionDefinition.getTypeId());
 77  0
                 return resolveTypeService(typeDefinition, ActionTypeService.class);
 78  
         }
 79  
 
 80  
     @Override
 81  
     public AgendaTypeService getAgendaTypeService(AgendaDefinition agendaDefinition) {
 82  0
         if (agendaDefinition == null) {
 83  0
             throw new IllegalArgumentException("agendaDefinition was null");
 84  
         }
 85  0
         KrmsTypeDefinition typeDefinition = getTypeDefinition(agendaDefinition.getTypeId());
 86  0
         return resolveTypeService(typeDefinition, AgendaTypeService.class);
 87  
     }
 88  
 
 89  
     @Override
 90  
         public RuleTypeService getRuleTypeService(RuleDefinition ruleDefinition) {
 91  0
         if (ruleDefinition == null) {
 92  0
             throw new IllegalArgumentException("ruleDefinition was null");
 93  
         }
 94  0
         KrmsTypeDefinition typeDefinition = getTypeDefinition(ruleDefinition.getTypeId());
 95  
 
 96  0
         if (typeDefinition == null) { return RuleTypeServiceBase.defaultRuleTypeService; }
 97  
 
 98  0
         return resolveTypeService(typeDefinition, RuleTypeService.class);
 99  
     }
 100  
 
 101  
         @Override
 102  
         public TermResolverTypeService getTermResolverTypeService(TermResolverDefinition termResolverDefintion) {
 103  0
                 if (termResolverDefintion == null) {
 104  0
                         throw new IllegalArgumentException("termResolverDefintion was null");
 105  
                 }
 106  0
                 KrmsTypeDefinition typeDefinition = getTypeDefinition(termResolverDefintion.getTypeId());
 107  0
                 return resolveTypeService(typeDefinition, TermResolverTypeService.class);
 108  
         }
 109  
         
 110  
         @Override
 111  
         public FunctionTypeService getFunctionTypeService(FunctionDefinition functionDefinition) {
 112  0
                 if (functionDefinition == null) {
 113  0
                         throw new IllegalArgumentException("functionDefinition was null");
 114  
                 }
 115  0
                 KrmsTypeDefinition typeDefinition = getTypeDefinition(functionDefinition.getTypeId());
 116  0
                 return resolveTypeService(typeDefinition, FunctionTypeService.class);
 117  
         }
 118  
         
 119  
         protected KrmsTypeDefinition getTypeDefinition(String typeId) {
 120  0
                 if (StringUtils.isBlank(typeId)) {
 121  0
                         return null;
 122  
                 }
 123  0
                 KrmsTypeDefinition typeDefinition = typeRepositoryService.getTypeById(typeId);
 124  0
                 if (typeDefinition == null) {
 125  0
                         throw new RepositoryDataException("Failed to locate a type definition for typeId: " + typeId);
 126  
                 }
 127  0
                 return typeDefinition;
 128  
         }
 129  
         
 130  
         protected <T> T resolveTypeService(KrmsTypeDefinition typeDefinition, Class<T> typeServiceClass) {
 131  0
                 QName serviceName = QName.valueOf(typeDefinition.getServiceName());
 132  0
                 Object service = GlobalResourceLoader.getService(serviceName);
 133  0
                 if (service == null) {
 134  0
                         throw new EngineResourceUnavailableException("Failed to locate the " + typeServiceClass.getSimpleName() + 
 135  
                                         " with name: " + serviceName);
 136  
                 }
 137  0
                 if (!typeServiceClass.isAssignableFrom(service.getClass())) {
 138  0
                         throw new EngineResourceUnavailableException("The service with name '" + serviceName + "' defined on typeId '" + typeDefinition.getId() + 
 139  
                                         "' was not of type " + typeServiceClass.getSimpleName() + ": " + service);
 140  
                 }
 141  0
                 return typeServiceClass.cast(service);
 142  
         }
 143  
         
 144  
         public void setTypeRepositoryService(KrmsTypeRepositoryService typeRepositoryService) {
 145  0
                 this.typeRepositoryService = typeRepositoryService;
 146  0
         }
 147  
 
 148  
         public void setDefaultCompoundPropositionTypeService(PropositionTypeService defaultCompoundPropositionTypeService) {
 149  0
                 this.defaultCompoundPropositionTypeService = defaultCompoundPropositionTypeService;
 150  0
         }
 151  
 
 152  
         public void setDefaultSimplePropositionTypeService(PropositionTypeService defaultSimplePropositionTypeService) {
 153  0
                 this.defaultSimplePropositionTypeService = defaultSimplePropositionTypeService;
 154  0
         }
 155  
         
 156  
 }