View Javadoc

1   /*
2    * Copyright 2006-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.test;
17  
18  import org.kuali.rice.krad.service.BusinessObjectService;
19  import org.kuali.rice.krad.service.KRADServiceLocator;
20  import org.kuali.rice.krms.api.repository.context.ContextDefinition;
21  import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
22  import org.kuali.rice.krms.api.repository.type.KrmsTypeAttribute;
23  import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
24  import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
25  import org.kuali.rice.krms.impl.repository.ContextAttributeBo;
26  import org.kuali.rice.krms.impl.repository.ContextBoService;
27  import org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService;
28  import org.kuali.rice.test.BaselineTestCase.BaselineMode;
29  import org.kuali.rice.test.BaselineTestCase.Mode;
30  
31  import java.util.ArrayList;
32  import java.util.Collections;
33  import java.util.List;
34  import java.util.Map;
35  
36  @BaselineMode(Mode.ROLLBACK)
37  public abstract class AbstractBoTest extends KRMSTestCase {
38  	
39      protected ContextBoService contextRepository;
40      protected KrmsTypeRepositoryService krmsTypeRepository;
41      protected KrmsAttributeDefinitionService krmsAttributeDefinitionService;
42  
43      protected BusinessObjectService getBoService() {
44  		return KRADServiceLocator.getBusinessObjectService();
45  	}
46  
47      protected ContextDefinition createContextDefinition(String nameSpace, String name,
48              Map<String, String> contextAttributes) {
49          // Attributes for context;
50          List<KrmsTypeAttribute.Builder> contextAttributeBuilders = new ArrayList<KrmsTypeAttribute.Builder>();
51          int contextAttrSequenceIndex = 0;
52  
53          List<KrmsAttributeDefinition> attributeDefintions = new ArrayList<KrmsAttributeDefinition>();
54  
55          if (contextAttributes != null) for (Map.Entry<String,String> entry : contextAttributes.entrySet()) {
56              KrmsAttributeDefinition.Builder contextTypeAttributeDefnBuilder = KrmsAttributeDefinition.Builder.create(null, entry.getKey(), nameSpace);
57              contextTypeAttributeDefnBuilder.setLabel(entry.getKey() + " attribute label");
58              KrmsAttributeDefinition contextTypeAttributeDefinition = krmsAttributeDefinitionService.createAttributeDefinition(contextTypeAttributeDefnBuilder.build());
59              attributeDefintions.add(contextTypeAttributeDefinition);
60  
61              // Attr for context;
62              contextAttributeBuilders.add(KrmsTypeAttribute.Builder.create(null, contextTypeAttributeDefinition.getId(),
63                      contextAttrSequenceIndex));
64              contextAttrSequenceIndex += 1;
65          }
66  
67          // KrmsType for context
68          KrmsTypeDefinition.Builder krmsContextTypeDefnBuilder = KrmsTypeDefinition.Builder.create("KrmsTestContextType", nameSpace);
69          krmsContextTypeDefnBuilder.setAttributes(contextAttributeBuilders);
70          KrmsTypeDefinition krmsContextTypeDefinition = krmsContextTypeDefnBuilder.build();
71          krmsContextTypeDefinition = krmsTypeRepository.createKrmsType(krmsContextTypeDefinition);
72  
73          // Context
74          ContextDefinition.Builder contextBuilder = ContextDefinition.Builder.create(nameSpace, name);
75          contextBuilder.setTypeId(krmsContextTypeDefinition.getId());
76          ContextDefinition contextDefinition = contextBuilder.build();
77          contextDefinition = contextRepository.createContext(contextDefinition);
78  
79          // Context Attribute
80          // TODO: do this fur eel
81          for (KrmsAttributeDefinition contextTypeAttributeDefinition : attributeDefintions) {
82              ContextAttributeBo contextAttribute = new ContextAttributeBo();
83              contextAttribute.setAttributeDefinitionId(contextTypeAttributeDefinition.getId());
84              contextAttribute.setContextId(contextDefinition.getId());
85              contextAttribute.setValue(contextAttributes.get(contextTypeAttributeDefinition.getName()));
86              getBoService().save(contextAttribute);
87          }
88  
89          return contextDefinition;
90      }
91  
92      protected KrmsTypeDefinition createKrmsGenericTypeDefinition(String nameSpace, String serviceName,
93              String attributeDefinitionLabel, String attributeDefinitionName) {
94          KrmsTypeDefinition krmsGenericTypeDefinition = krmsTypeRepository.getTypeByName(nameSpace, "KrmsTestGenericType");
95  
96          if (null == krmsGenericTypeDefinition) {
97  
98              // Attribute Defn for generic type;
99              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 }