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 krmsContextTypeDefinition = krmsTypeRepository.getTypeByName(nameSpace, "KrmsTestContextType");
069    
070            if (krmsContextTypeDefinition == null) {
071                KrmsTypeDefinition.Builder krmsContextTypeDefnBuilder = KrmsTypeDefinition.Builder.create("KrmsTestContextType", nameSpace);
072                krmsContextTypeDefnBuilder.setAttributes(contextAttributeBuilders);
073                krmsContextTypeDefinition = krmsContextTypeDefnBuilder.build();
074                krmsContextTypeDefinition = krmsTypeRepository.createKrmsType(krmsContextTypeDefinition);
075            }
076    
077            // Context
078            ContextDefinition.Builder contextBuilder = ContextDefinition.Builder.create(nameSpace, name);
079            contextBuilder.setTypeId(krmsContextTypeDefinition.getId());
080            ContextDefinition contextDefinition = contextBuilder.build();
081            contextDefinition = contextRepository.createContext(contextDefinition);
082    
083            // Context Attribute
084            // TODO: do this fur eel
085            for (KrmsAttributeDefinition contextTypeAttributeDefinition : attributeDefintions) {
086                ContextAttributeBo contextAttribute = new ContextAttributeBo();
087                contextAttribute.setAttributeDefinitionId(contextTypeAttributeDefinition.getId());
088                contextAttribute.setContextId(contextDefinition.getId());
089                contextAttribute.setValue(contextAttributes.get(contextTypeAttributeDefinition.getName()));
090                getBoService().save(contextAttribute);
091            }
092    
093            return contextDefinition;
094        }
095    
096        protected KrmsTypeDefinition createKrmsGenericTypeDefinition(String nameSpace, String serviceName,
097                String attributeDefinitionLabel, String attributeDefinitionName) {
098            KrmsTypeDefinition krmsGenericTypeDefinition = krmsTypeRepository.getTypeByName(nameSpace, "KrmsTestGenericType");
099    
100            if (null == krmsGenericTypeDefinition) {
101    
102                // Attribute Defn for generic type;
103                KrmsAttributeDefinition.Builder genericTypeAttributeDefnBuilder = KrmsAttributeDefinition.Builder.create(null,
104                        attributeDefinitionName, nameSpace);
105                genericTypeAttributeDefnBuilder.setLabel(attributeDefinitionLabel);
106                KrmsAttributeDefinition genericTypeAttributeDefinition1 = krmsAttributeDefinitionService.createAttributeDefinition(genericTypeAttributeDefnBuilder.build());
107    
108                // Attr for generic type;
109                KrmsTypeAttribute.Builder genericTypeAttrBuilder = KrmsTypeAttribute.Builder.create(null, genericTypeAttributeDefinition1.getId(), 1);
110    
111                // Can use this generic type for KRMS bits that don't actually rely on services on the bus at this point in time
112                KrmsTypeDefinition.Builder krmsGenericTypeDefnBuilder = KrmsTypeDefinition.Builder.create("KrmsTestGenericType", nameSpace);
113                krmsGenericTypeDefnBuilder.setServiceName(serviceName);
114                krmsGenericTypeDefnBuilder.setAttributes(Collections.singletonList(genericTypeAttrBuilder));
115                krmsGenericTypeDefinition = krmsTypeRepository.createKrmsType(krmsGenericTypeDefnBuilder.build());
116    
117            }
118    
119            return krmsGenericTypeDefinition;
120        }
121            
122    }