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 krmsContextTypeDefinition = krmsTypeRepository.getTypeByName(nameSpace, "KrmsTestContextType");
69  
70          if (krmsContextTypeDefinition == null) {
71              KrmsTypeDefinition.Builder krmsContextTypeDefnBuilder = KrmsTypeDefinition.Builder.create("KrmsTestContextType", nameSpace);
72              krmsContextTypeDefnBuilder.setAttributes(contextAttributeBuilders);
73              krmsContextTypeDefinition = krmsContextTypeDefnBuilder.build();
74              krmsContextTypeDefinition = krmsTypeRepository.createKrmsType(krmsContextTypeDefinition);
75          }
76  
77          // Context
78          ContextDefinition.Builder contextBuilder = ContextDefinition.Builder.create(nameSpace, name);
79          contextBuilder.setTypeId(krmsContextTypeDefinition.getId());
80          ContextDefinition contextDefinition = contextBuilder.build();
81          contextDefinition = contextRepository.createContext(contextDefinition);
82  
83          // Context Attribute
84          // TODO: do this fur eel
85          for (KrmsAttributeDefinition contextTypeAttributeDefinition : attributeDefintions) {
86              ContextAttributeBo contextAttribute = new ContextAttributeBo();
87              contextAttribute.setAttributeDefinitionId(contextTypeAttributeDefinition.getId());
88              contextAttribute.setContextId(contextDefinition.getId());
89              contextAttribute.setValue(contextAttributes.get(contextTypeAttributeDefinition.getName()));
90              getBoService().save(contextAttribute);
91          }
92  
93          return contextDefinition;
94      }
95  
96      protected KrmsTypeDefinition createKrmsGenericTypeDefinition(String nameSpace, String serviceName,
97              String attributeDefinitionLabel, String attributeDefinitionName) {
98          KrmsTypeDefinition krmsGenericTypeDefinition = krmsTypeRepository.getTypeByName(nameSpace, "KrmsTestGenericType");
99  
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 }