Coverage Report - org.kuali.rice.krms.impl.type.KrmsTypeResolverImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
KrmsTypeResolverImpl
0%
0/43
0%
0/22
3.444
 
 1  
 /*
 2  
  * Copyright 2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 1.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/ecl1.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.function.FunctionDefinition;
 26  
 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
 27  
 import org.kuali.rice.krms.api.repository.proposition.PropositionType;
 28  
 import org.kuali.rice.krms.api.repository.term.TermResolverDefinition;
 29  
 import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
 30  
 import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
 31  
 import org.kuali.rice.krms.framework.type.ActionTypeService;
 32  
 import org.kuali.rice.krms.framework.type.FunctionTypeService;
 33  
 import org.kuali.rice.krms.framework.type.PropositionTypeService;
 34  
 import org.kuali.rice.krms.framework.type.TermResolverTypeService;
 35  
 
 36  
 /**
 37  
  * An implementation of {@link KrmsTypeResolver} which knows how to load the
 38  
  * various type services in KRMS.
 39  
  * 
 40  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 41  
  *
 42  
  */
 43  0
 public class KrmsTypeResolverImpl implements KrmsTypeResolver {
 44  
 
 45  
         private KrmsTypeRepositoryService typeRepositoryService;
 46  
         private PropositionTypeService defaultCompoundPropositionTypeService;
 47  
         private PropositionTypeService defaultSimplePropositionTypeService;
 48  
         
 49  
         @Override
 50  
         public PropositionTypeService getPropositionTypeService(PropositionDefinition propositionDefinition) {
 51  0
                 if (propositionDefinition == null) {
 52  0
                         throw new IllegalArgumentException("propositionDefinition was null");
 53  
                 }
 54  0
                 if (propositionDefinition.getTypeId() == null) {
 55  0
                         PropositionType propositionType = PropositionType.fromCode(propositionDefinition.getPropositionTypeCode());
 56  0
                         if (PropositionType.COMPOUND == propositionType) {
 57  0
                                 return defaultCompoundPropositionTypeService;
 58  0
                         } else if (PropositionType.SIMPLE == propositionType) {
 59  0
                                 return defaultSimplePropositionTypeService;
 60  
                         }
 61  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());
 62  
                 }
 63  0
                 KrmsTypeDefinition typeDefinition = getTypeDefinition(propositionDefinition.getTypeId());
 64  0
                 return resolveTypeService(typeDefinition, PropositionTypeService.class);
 65  
         }
 66  
 
 67  
         @Override
 68  
         public ActionTypeService getActionTypeService(ActionDefinition actionDefinition) {
 69  0
                 if (actionDefinition == null) {
 70  0
                         throw new IllegalArgumentException("actionDefinition was null");
 71  
                 }
 72  0
                 KrmsTypeDefinition typeDefinition = getTypeDefinition(actionDefinition.getTypeId());
 73  0
                 return resolveTypeService(typeDefinition, ActionTypeService.class);
 74  
         }
 75  
         
 76  
         @Override
 77  
         public TermResolverTypeService getTermResolverTypeService(TermResolverDefinition termResolverDefintion) {
 78  0
                 if (termResolverDefintion == null) {
 79  0
                         throw new IllegalArgumentException("termResolverDefintion was null");
 80  
                 }
 81  0
                 KrmsTypeDefinition typeDefinition = getTypeDefinition(termResolverDefintion.getTypeId());
 82  0
                 return resolveTypeService(typeDefinition, TermResolverTypeService.class);
 83  
         }
 84  
         
 85  
         @Override
 86  
         public FunctionTypeService getFunctionTypeService(FunctionDefinition functionDefinition) {
 87  0
                 if (functionDefinition == null) {
 88  0
                         throw new IllegalArgumentException("functionDefinition was null");
 89  
                 }
 90  0
                 KrmsTypeDefinition typeDefinition = getTypeDefinition(functionDefinition.getTypeId());
 91  0
                 return resolveTypeService(typeDefinition, FunctionTypeService.class);
 92  
         }
 93  
         
 94  
         protected KrmsTypeDefinition getTypeDefinition(String typeId) {
 95  0
                 if (StringUtils.isBlank(typeId)) {
 96  0
                         throw new IllegalArgumentException("The given typeId was null.");
 97  
                 }
 98  0
                 KrmsTypeDefinition typeDefinition = typeRepositoryService.getTypeById(typeId);
 99  0
                 if (typeDefinition == null) {
 100  0
                         throw new RepositoryDataException("Failed to locate a type definition for typeId: " + typeId);
 101  
                 }
 102  0
                 return typeDefinition;
 103  
         }
 104  
         
 105  
         protected <T> T resolveTypeService(KrmsTypeDefinition typeDefinition, Class<T> typeServiceClass) {
 106  0
                 QName serviceName = QName.valueOf(typeDefinition.getServiceName());
 107  0
                 Object service = GlobalResourceLoader.getService(serviceName);
 108  0
                 if (service == null) {
 109  0
                         throw new EngineResourceUnavailableException("Failed to locate the " + typeServiceClass.getSimpleName() + 
 110  
                                         " with name: " + serviceName);
 111  
                 }
 112  0
                 if (!typeServiceClass.isAssignableFrom(service.getClass())) {
 113  0
                         throw new EngineResourceUnavailableException("The service with name '" + serviceName + "' defined on typeId '" + typeDefinition.getId() + 
 114  
                                         "' was not of type " + typeServiceClass.getSimpleName() + ": " + service);
 115  
                 }
 116  0
                 return typeServiceClass.cast(service);
 117  
         }
 118  
         
 119  
         public void setTypeRepositoryService(KrmsTypeRepositoryService typeRepositoryService) {
 120  0
                 this.typeRepositoryService = typeRepositoryService;
 121  0
         }
 122  
 
 123  
         public void setDefaultCompoundPropositionTypeService(PropositionTypeService defaultCompoundPropositionTypeService) {
 124  0
                 this.defaultCompoundPropositionTypeService = defaultCompoundPropositionTypeService;
 125  0
         }
 126  
 
 127  
         public void setDefaultSimplePropositionTypeService(PropositionTypeService defaultSimplePropositionTypeService) {
 128  0
                 this.defaultSimplePropositionTypeService = defaultSimplePropositionTypeService;
 129  0
         }
 130  
         
 131  
 }