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.apache.bcel.generic.NEW;
019    import org.kuali.rice.krad.service.BusinessObjectService;
020    import org.kuali.rice.krad.service.KRADServiceLocator;
021    import org.kuali.rice.krms.api.repository.context.ContextDefinition;
022    import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
023    import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
024    import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition.Builder;
025    import org.kuali.rice.krms.api.repository.type.KrmsTypeAttribute;
026    import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
027    import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
028    import org.kuali.rice.krms.impl.repository.ContextAttributeBo;
029    import org.kuali.rice.krms.impl.repository.ContextBoService;
030    import org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService;
031    import org.kuali.rice.krms.impl.repository.TermBoService;
032    import org.kuali.rice.test.BaselineTestCase.BaselineMode;
033    import org.kuali.rice.test.BaselineTestCase.Mode;
034    
035    import java.util.ArrayList;
036    import java.util.Collection;
037    import java.util.Collections;
038    import java.util.HashMap;
039    import java.util.List;
040    import java.util.Map;
041    
042    @BaselineMode(Mode.ROLLBACK)
043    public abstract class KSLumAbstractBoTest extends KRMSTestCase {
044            
045        private static final String ATTR_NUMBER_OF_CREDITS = "Number of Credits";
046            private static final String ATTR_DEPT_ORG_NUMBER = "Dept Org Number";
047            private static final String ATTR_PROPOSED_COURSE = "Proposed Course";
048            private static final String ATTR_APPROVED_COURSE = "Approved Course";
049            protected ContextBoService contextRepository;
050        protected KrmsTypeRepositoryService krmsTypeRepository;
051        protected KrmsAttributeDefinitionService krmsAttributeDefinitionService;
052        protected Map<String, KrmsAttributeDefinition> ksLumAttributeDefinitions = new HashMap<String, KrmsAttributeDefinition>();
053        
054        private TermBoService termBoService;
055    
056        protected BusinessObjectService getBoService() {
057                    return KRADServiceLocator.getBusinessObjectService();
058            }
059    
060        protected ContextDefinition createContextDefinition(String nameSpace, String name,
061                Map<String, String> contextAttributes) {
062            // Attributes for context;
063            List<KrmsTypeAttribute.Builder> contextAttributeBuilders = new ArrayList<KrmsTypeAttribute.Builder>();
064            int contextAttrSequenceIndex = 0;
065    
066            List<KrmsAttributeDefinition> attributeDefintions = new ArrayList<KrmsAttributeDefinition>();
067    
068            createAllLumAttributeDefinitions(nameSpace);
069            
070            KrmsTypeDefinition krmsContextTypeDefinition = createContextType(nameSpace);
071    
072            ContextDefinition contextDefinition = createContext(nameSpace, name,
073                                    krmsContextTypeDefinition);
074    
075            // Context Attribute
076            // TODO: do this fur eel
077            for (KrmsAttributeDefinition contextTypeAttributeDefinition : attributeDefintions) {
078                ContextAttributeBo contextAttribute = new ContextAttributeBo();
079                contextAttribute.setAttributeDefinitionId(contextTypeAttributeDefinition.getId());
080                contextAttribute.setContextId(contextDefinition.getId());
081                contextAttribute.setValue(contextAttributes.get(contextTypeAttributeDefinition.getName()));
082                getBoService().save(contextAttribute);
083            }
084    
085            return contextDefinition;
086        }
087    
088            public ContextDefinition createContext(String nameSpace, String name,
089                            KrmsTypeDefinition krmsContextTypeDefinition) {
090                    // Context
091            ContextDefinition.Builder contextBuilder = ContextDefinition.Builder.create(nameSpace, name);
092    // TODO NINA somthing wrong with Type       contextBuilder.setTypeId(krmsContextTypeDefinition.getId());
093            contextBuilder.setTypeId("T1003");
094            ContextDefinition contextDefinition = contextBuilder.build();
095            contextDefinition = contextRepository.createContext(contextDefinition);
096                    return contextDefinition;
097            }
098    
099            protected KrmsTypeDefinition createContextType(String nameSpace) {
100                    // KrmsType for context
101            KrmsTypeDefinition.Builder krmsContextTypeDefnBuilder = KrmsTypeDefinition.Builder.create("KSLumContextType-Course", nameSpace);   
102            krmsContextTypeDefnBuilder.setServiceName("myKSService");
103            //
104            int contextAttrSequenceIndex = 0;
105            List<KrmsTypeAttribute.Builder> contextAttributeBuilders = new ArrayList<KrmsTypeAttribute.Builder>();
106            for (KrmsAttributeDefinition attrDef : ksLumAttributeDefinitions.values()) {
107                contextAttributeBuilders.add(KrmsTypeAttribute.Builder.create(null, attrDef.getId(),
108                        contextAttrSequenceIndex));
109                contextAttrSequenceIndex += 1;
110                            
111                    }
112            krmsContextTypeDefnBuilder.setAttributes(contextAttributeBuilders);
113            //
114            KrmsTypeDefinition krmsContextTypeDefinition = krmsContextTypeDefnBuilder.build();
115            krmsContextTypeDefinition = krmsTypeRepository.createKrmsType(krmsContextTypeDefinition);
116                    return krmsContextTypeDefinition;
117            }
118            
119    
120    
121            protected void createAllLumAttributeDefinitions(
122                            String nameSpace) {
123                    // Create all the attributes...
124            createNumberOfCreditsAttributeDefinition(nameSpace);
125            createDeptOrgNumberAttributeDefinition(nameSpace);
126            createProposedCourseAttributeDefinition(nameSpace);
127            createApprovedCourseAttributeDefinition(nameSpace);
128            }
129    
130            private void createNumberOfCreditsAttributeDefinition(
131                            String nameSpace) {
132                    KrmsAttributeDefinition.Builder numberOfCreditsBuilder = KrmsAttributeDefinition.Builder.create(null, ATTR_NUMBER_OF_CREDITS, nameSpace);
133            numberOfCreditsBuilder.setLabel(numberOfCreditsBuilder.getName());
134            numberOfCreditsBuilder.setDescription(numberOfCreditsBuilder.getName());
135            //
136                    KrmsAttributeDefinition attributeDefinition = krmsAttributeDefinitionService.createAttributeDefinition(numberOfCreditsBuilder.build());
137                    ksLumAttributeDefinitions.put(attributeDefinition.getName(),attributeDefinition);
138            }
139            
140            private void createDeptOrgNumberAttributeDefinition(
141                            String nameSpace) {
142                    KrmsAttributeDefinition.Builder deptOrgNumberBuilder = KrmsAttributeDefinition.Builder.create(null, ATTR_DEPT_ORG_NUMBER, nameSpace);
143            deptOrgNumberBuilder.setLabel(deptOrgNumberBuilder.getName());
144            deptOrgNumberBuilder.setDescription(deptOrgNumberBuilder.getName());
145            //
146                    KrmsAttributeDefinition attributeDefinition = krmsAttributeDefinitionService.createAttributeDefinition(deptOrgNumberBuilder.build());
147                    ksLumAttributeDefinitions.put(attributeDefinition.getName(),attributeDefinition);
148            }
149            
150            private void createProposedCourseAttributeDefinition(
151                            String nameSpace) {
152                    KrmsAttributeDefinition.Builder proposedCourseBuilder = KrmsAttributeDefinition.Builder.create(null, ATTR_PROPOSED_COURSE, nameSpace);
153            proposedCourseBuilder.setLabel(proposedCourseBuilder.getName());
154            proposedCourseBuilder.setDescription(proposedCourseBuilder.getName());
155            //
156                    KrmsAttributeDefinition attributeDefinition = krmsAttributeDefinitionService.createAttributeDefinition(proposedCourseBuilder.build());
157                    ksLumAttributeDefinitions.put(attributeDefinition.getName(),attributeDefinition);
158            }
159    
160            private void createApprovedCourseAttributeDefinition(
161                            String nameSpace) {
162                    KrmsAttributeDefinition.Builder approvedCourseBuilder = KrmsAttributeDefinition.Builder.create(null, ATTR_APPROVED_COURSE, nameSpace);
163            approvedCourseBuilder.setLabel(approvedCourseBuilder.getName());
164            approvedCourseBuilder.setDescription(approvedCourseBuilder.getName());
165            //
166                    KrmsAttributeDefinition attributeDefinition = krmsAttributeDefinitionService.createAttributeDefinition(approvedCourseBuilder.build());
167                    ksLumAttributeDefinitions.put(attributeDefinition.getName(),attributeDefinition);
168            }
169            
170    
171    
172    //      private KrmsAttributeDefinition createAttributeDefinition(String nameSpace,
173    //                      String attrName) {
174    //              
175    //              KrmsAttributeDefinition.Builder contextTypeAttributeDefnBuilder = KrmsAttributeDefinition.Builder.create(null, attrName, nameSpace);
176    //              contextTypeAttributeDefnBuilder.setLabel(attrName + " attribute label");
177    //              KrmsAttributeDefinition contextTypeAttributeDefinition = krmsAttributeDefinitionService.createAttributeDefinition(contextTypeAttributeDefnBuilder.build());
178    //              return contextTypeAttributeDefinition;
179    //      }
180    
181        protected KrmsTypeDefinition createKrmsGenericTypeDefinition(String nameSpace, String serviceName,
182                String attributeDefinitionLabel, String attributeDefinitionName) {
183            KrmsTypeDefinition krmsGenericTypeDefinition = krmsTypeRepository.getTypeByName(nameSpace, "Event");
184    
185            if (null == krmsGenericTypeDefinition) {
186    
187                // Attribute Defn for generic type;
188                KrmsAttributeDefinition.Builder genericTypeAttributeDefnBuilder = KrmsAttributeDefinition.Builder.create(null,
189                        attributeDefinitionName, nameSpace);
190                genericTypeAttributeDefnBuilder.setLabel(attributeDefinitionLabel);
191                KrmsAttributeDefinition genericTypeAttributeDefinition1 = krmsAttributeDefinitionService.createAttributeDefinition(genericTypeAttributeDefnBuilder.build());
192    
193                // Attr for generic type;
194                KrmsTypeAttribute.Builder genericTypeAttrBuilder = KrmsTypeAttribute.Builder.create(null, genericTypeAttributeDefinition1.getId(), 1);
195    
196                // Can use this generic type for KRMS bits that don't actually rely on services on the bus at this point in time
197                KrmsTypeDefinition.Builder krmsGenericTypeDefnBuilder = KrmsTypeDefinition.Builder.create("KrmsTestGenericType", nameSpace);
198                krmsGenericTypeDefnBuilder.setServiceName(serviceName);
199                krmsGenericTypeDefnBuilder.setAttributes(Collections.singletonList(genericTypeAttrBuilder));
200                krmsGenericTypeDefinition = krmsTypeRepository.createKrmsType(krmsGenericTypeDefnBuilder.build());
201    
202            }
203    
204            return krmsGenericTypeDefinition;
205        }
206            
207    }