1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
62 contextAttributeBuilders.add(KrmsTypeAttribute.Builder.create(null, contextTypeAttributeDefinition.getId(),
63 contextAttrSequenceIndex));
64 contextAttrSequenceIndex += 1;
65 }
66
67
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
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
84
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
103 KrmsAttributeDefinition.Builder genericTypeAttributeDefnBuilder = KrmsAttributeDefinition.Builder.create(null,
104 attributeDefinitionName, nameSpace);
105 genericTypeAttributeDefnBuilder.setLabel(attributeDefinitionLabel);
106 KrmsAttributeDefinition genericTypeAttributeDefinition1 = krmsAttributeDefinitionService.createAttributeDefinition(genericTypeAttributeDefnBuilder.build());
107
108
109 KrmsTypeAttribute.Builder genericTypeAttrBuilder = KrmsTypeAttribute.Builder.create(null, genericTypeAttributeDefinition1.getId(), 1);
110
111
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 }