001    /*
002     * Copyright 2006-2011 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.test;
017    
018    import org.kuali.rice.krad.service.BusinessObjectService;
019    import org.kuali.rice.krad.service.KRADServiceLocator;
020    import org.kuali.rice.krms.api.repository.context.ContextDefinition;
021    import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
022    import org.kuali.rice.krms.api.repository.type.KrmsTypeAttribute;
023    import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
024    import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
025    import org.kuali.rice.krms.impl.repository.ContextAttributeBo;
026    import org.kuali.rice.krms.impl.repository.ContextBoService;
027    import org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService;
028    import org.kuali.rice.test.BaselineTestCase.BaselineMode;
029    import org.kuali.rice.test.BaselineTestCase.Mode;
030    
031    import java.util.ArrayList;
032    import java.util.Collections;
033    import java.util.List;
034    import java.util.Map;
035    
036    @BaselineMode(Mode.ROLLBACK)
037    public abstract class AbstractBoTest extends KRMSTestCase {
038            
039        protected ContextBoService contextRepository;
040        protected KrmsTypeRepositoryService krmsTypeRepository;
041        protected KrmsAttributeDefinitionService krmsAttributeDefinitionService;
042    
043        protected BusinessObjectService getBoService() {
044                    return KRADServiceLocator.getBusinessObjectService();
045            }
046    
047        protected ContextDefinition createContextDefinition(String nameSpace, String name,
048                Map<String, String> contextAttributes) {
049            // Attributes for context;
050            List<KrmsTypeAttribute.Builder> contextAttributeBuilders = new ArrayList<KrmsTypeAttribute.Builder>();
051            int contextAttrSequenceIndex = 0;
052    
053            List<KrmsAttributeDefinition> attributeDefintions = new ArrayList<KrmsAttributeDefinition>();
054    
055            if (contextAttributes != null) for (Map.Entry<String,String> entry : contextAttributes.entrySet()) {
056                KrmsAttributeDefinition.Builder contextTypeAttributeDefnBuilder = KrmsAttributeDefinition.Builder.create(null, entry.getKey(), nameSpace);
057                contextTypeAttributeDefnBuilder.setLabel(entry.getKey() + " attribute label");
058                KrmsAttributeDefinition contextTypeAttributeDefinition = krmsAttributeDefinitionService.createAttributeDefinition(contextTypeAttributeDefnBuilder.build());
059                attributeDefintions.add(contextTypeAttributeDefinition);
060    
061                // Attr for context;
062                contextAttributeBuilders.add(KrmsTypeAttribute.Builder.create(null, contextTypeAttributeDefinition.getId(),
063                        contextAttrSequenceIndex));
064                contextAttrSequenceIndex += 1;
065            }
066    
067            // KrmsType for context
068            KrmsTypeDefinition.Builder krmsContextTypeDefnBuilder = KrmsTypeDefinition.Builder.create("KrmsTestContextType", nameSpace);
069            krmsContextTypeDefnBuilder.setAttributes(contextAttributeBuilders);
070            KrmsTypeDefinition krmsContextTypeDefinition = krmsContextTypeDefnBuilder.build();
071            krmsContextTypeDefinition = krmsTypeRepository.createKrmsType(krmsContextTypeDefinition);
072    
073            // Context
074            ContextDefinition.Builder contextBuilder = ContextDefinition.Builder.create(nameSpace, name);
075            contextBuilder.setTypeId(krmsContextTypeDefinition.getId());
076            ContextDefinition contextDefinition = contextBuilder.build();
077            contextDefinition = contextRepository.createContext(contextDefinition);
078    
079            // Context Attribute
080            // TODO: do this fur eel
081            for (KrmsAttributeDefinition contextTypeAttributeDefinition : attributeDefintions) {
082                ContextAttributeBo contextAttribute = new ContextAttributeBo();
083                contextAttribute.setAttributeDefinitionId(contextTypeAttributeDefinition.getId());
084                contextAttribute.setContextId(contextDefinition.getId());
085                contextAttribute.setValue(contextAttributes.get(contextTypeAttributeDefinition.getName()));
086                getBoService().save(contextAttribute);
087            }
088    
089            return contextDefinition;
090        }
091    
092        protected KrmsTypeDefinition createKrmsGenericTypeDefinition(String nameSpace, String serviceName,
093                String attributeDefinitionLabel, String attributeDefinitionName) {
094            KrmsTypeDefinition krmsGenericTypeDefinition = krmsTypeRepository.getTypeByName(nameSpace, "KrmsTestGenericType");
095    
096            if (null == krmsGenericTypeDefinition) {
097    
098                // Attribute Defn for generic type;
099                KrmsAttributeDefinition.Builder genericTypeAttributeDefnBuilder = KrmsAttributeDefinition.Builder.create(null,
100                        attributeDefinitionName, nameSpace);
101                genericTypeAttributeDefnBuilder.setLabel(attributeDefinitionLabel);
102                KrmsAttributeDefinition genericTypeAttributeDefinition1 = krmsAttributeDefinitionService.createAttributeDefinition(genericTypeAttributeDefnBuilder.build());
103    
104                // Attr for generic type;
105                KrmsTypeAttribute.Builder genericTypeAttrBuilder = KrmsTypeAttribute.Builder.create(null, genericTypeAttributeDefinition1.getId(), 1);
106    
107                // Can use this generic type for KRMS bits that don't actually rely on services on the bus at this point in time
108                KrmsTypeDefinition.Builder krmsGenericTypeDefnBuilder = KrmsTypeDefinition.Builder.create("KrmsTestGenericType", nameSpace);
109                krmsGenericTypeDefnBuilder.setServiceName(serviceName);
110                krmsGenericTypeDefnBuilder.setAttributes(Collections.singletonList(genericTypeAttrBuilder));
111                krmsGenericTypeDefinition = krmsTypeRepository.createKrmsType(krmsGenericTypeDefnBuilder.build());
112    
113            }
114    
115            return krmsGenericTypeDefinition;
116        }
117            
118    }