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.Builder krmsContextTypeDefnBuilder = KrmsTypeDefinition.Builder.create("KrmsTestContextType", nameSpace);
69 krmsContextTypeDefnBuilder.setAttributes(contextAttributeBuilders);
70 KrmsTypeDefinition krmsContextTypeDefinition = krmsContextTypeDefnBuilder.build();
71 krmsContextTypeDefinition = krmsTypeRepository.createKrmsType(krmsContextTypeDefinition);
72
73
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
80
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
99 KrmsAttributeDefinition.Builder genericTypeAttributeDefnBuilder = KrmsAttributeDefinition.Builder.create(null,
100 attributeDefinitionName, nameSpace);
101 genericTypeAttributeDefnBuilder.setLabel(attributeDefinitionLabel);
102 KrmsAttributeDefinition genericTypeAttributeDefinition1 = krmsAttributeDefinitionService.createAttributeDefinition(genericTypeAttributeDefnBuilder.build());
103
104
105 KrmsTypeAttribute.Builder genericTypeAttrBuilder = KrmsTypeAttribute.Builder.create(null, genericTypeAttributeDefinition1.getId(), 1);
106
107
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 }