View Javadoc

1   /**
2    * Copyright 2005-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.student.krms;
17  
18  import org.junit.Before;
19  import org.junit.Test;
20  import org.kuali.rice.core.api.lifecycle.Lifecycle;
21  import org.kuali.rice.core.framework.resourceloader.SpringResourceLoader;
22  import org.kuali.rice.krad.service.BusinessObjectService;
23  import org.kuali.rice.krad.service.KRADServiceLocator;
24  import org.kuali.rice.krms.api.repository.context.ContextDefinition;
25  import org.kuali.rice.krms.api.repository.term.TermDefinition;
26  import org.kuali.rice.krms.api.repository.term.TermParameterDefinition;
27  import org.kuali.rice.krms.api.repository.term.TermResolverDefinition;
28  import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
29  import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition.Builder;
30  import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
31  import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
32  import org.kuali.rice.krms.impl.repository.*;
33  import org.kuali.rice.test.BaselineTestCase.BaselineMode;
34  import org.kuali.rice.test.BaselineTestCase.Mode;
35  
36  import javax.xml.namespace.QName;
37  import java.util.ArrayList;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  import static org.junit.Assert.assertNotNull;
43  
44  public class TestKRMSCreateTermSpecificationPhase2 extends KSKRMSUploadTestCase {
45  
46  	public TestKRMSCreateTermSpecificationPhase2() {
47  		super();
48  		this.setClearTables(false);
49  	}
50  
51  	static final String KSNAMESPACE = "KR-RULE-TEST";
52  	protected ContextBoService contextRepository;
53  	protected KrmsTypeRepositoryService krmsTypeRepository;
54  	private AgendaBoService agendaBoService;
55  	private RuleBoService ruleBoService;
56  	private FunctionBoServiceImpl functionBoService;
57  
58  	// // Services needed for creation:
59  	private TermBoService termBoService;
60  	private SpringResourceLoader krmsTestResourceLoader;
61  	// Agendas
62  	Map<String, ContextDefinition> phase1ContextDefs = new HashMap<String, ContextDefinition>();
63  	KrmsTypeDefinition krmsTypeDefinition;
64  	ContextDefinition contextStudElig;
65  	static final String AGENDA1 = "Must have successfully completed <course>";
66  	static final String EARTHQUAKE_EVENT = "Earthquake";
67  	public static final String CAMPUS_CODE_TERM_NAME = "campusCodeTermSpec";
68      static final String BOOL1 = "bool1";
69      static final String BOOL2 = "bool2";
70      static final String PREREQ_TERM_NAME = "prereqTermSpec";
71  
72  
73  	@Before
74  	public void setup() {
75  		getLoadApplicationLifecycle();
76  		termBoService = KrmsRepositoryServiceLocator.getTermBoService();
77  		agendaBoService = KrmsRepositoryServiceLocator.getAgendaBoService();
78  		contextRepository = KrmsRepositoryServiceLocator.getContextBoService();
79  		ruleBoService = KrmsRepositoryServiceLocator.getRuleBoService();
80  		krmsTypeRepository = KrmsRepositoryServiceLocator
81  				.getKrmsTypeRepositoryService();
82  		functionBoService = KrmsRepositoryServiceLocator.getBean("functionRepositoryService");
83  
84  	}
85  
86  	@Override
87  	protected void loadSuiteTestData() throws Exception {
88  		// Do nothing
89  	}
90  
91  	@Override
92  	protected List<String> getPerTestTablesNotToClear() {
93  		List<String> tablesNotToClear = super.getPerTestTablesNotToClear();
94  		// tablesNotToClear.add("KRMS_.*");
95  		return tablesNotToClear;
96  	}
97  
98  	@Override
99  	protected Lifecycle getLoadApplicationLifecycle() {
100 		if (krmsTestResourceLoader == null) {
101 			krmsTestResourceLoader = new SpringResourceLoader(new QName(
102 					"KRMSTestHarnessApplicationResourceLoader"),
103 					"classpath:KRMSTestHarnessSpringBeans.xml", null);
104 			krmsTestResourceLoader
105 					.setParentSpringResourceLoader(getTestHarnessSpringResourceLoader());
106 			getTestHarnessSpringResourceLoader().addResourceLoader(
107 					krmsTestResourceLoader);
108 		}
109 		return krmsTestResourceLoader;
110 	}
111 
112 	@Test
113 	public void createAllKRMSTermSpecificationsPhase1() {
114 		String nameSpace = KSNAMESPACE;
115 //		// Create all the terms specifications...
116 //		TermSpecificationDefinition termSpec = null;
117 //		termSpec = createKRMSTermSpecification(nameSpace,
118 //				KSKRMSConstants.TERM_SPEC_ORG_NUMBER,
119 //				KSKRMSConstants.ORG_NUMBER_DESCR, String.class.getCanonicalName());
120 //		// NOTE the term resolver must be defined as a spring bean under the name given here.
121 //		createKRMSTermResolver(KSKRMSConstants.TERM_SPEC_RESOLVER_ORG_NUMBER, termSpec);
122 		
123 		
124 	}
125 
126 	private TermSpecificationDefinition createKRMSTermSpecification(
127 			String nameSpace, String termSpecName, String descr, String termType) {
128 		Map<String, String> queryArgs = new HashMap<String, String>();
129 		queryArgs.put("namespace", nameSpace);
130 		queryArgs.put("name", termSpecName);
131 		TermSpecificationBo termSpecBo = getBoService().findByPrimaryKey(
132 				TermSpecificationBo.class, queryArgs);
133 
134 		// TODO Figure out how to set the Description
135 		TermSpecificationDefinition termSpec = null;
136 		if (termSpecBo == null) {
137 
138 			Builder termSpecDefBuilder = TermSpecificationDefinition.Builder
139 					.create(null, termSpecName, nameSpace, termType);
140 			termSpecDefBuilder.setDescription(descr);
141 
142 			termSpec = termSpecDefBuilder.build();
143 			
144 
145 			termSpec = termBoService.createTermSpecification(termSpec);
146 
147 		} else {
148 			termSpec = termSpecBo.to(termSpecBo);
149 		}
150 		return termSpec;
151 	}
152 
153 	@Test
154 	public void createAllKRMSTermDefinitions() {
155 		String nameSpace = KSNAMESPACE;
156 		// Create all the terms...
157 		// createNumberOfCreditsTermDefinition(nameSpace);
158 		//
159 		List<TermParameterDefinition.Builder> termParameters = new ArrayList<TermParameterDefinition.Builder>();
160 		
161         setupTermParameters(termParameters, KSKRMSConstants.ORG_ID_TERM_PROPERTY, "???termParameterValue");
162 		createKRMSTermDefinition(nameSpace, KSKRMSConstants.TERM_SPEC_ORG_NUMBER,
163 				KSKRMSConstants.TERM_DEPT_NUMBER, null);
164 		
165 		termParameters.clear();
166         setupTermParameters(termParameters, KSKRMSConstants.ORG_KEY_TYPE_TERM_PROPERTY, "???termParameterValue");
167 		createKRMSTermDefinition(nameSpace, KSKRMSConstants.TERM_SPEC_ORG_NUMBER,
168 				KSKRMSConstants.TERM_ADMIN_ORG_NUMBER, null);
169 		
170 		termParameters.clear();
171         setupTermParameters(termParameters, KSKRMSConstants.PERSON_ID_TERM_PROPERTY, "???termParameterValue");
172 		createKRMSTermDefinition(nameSpace, KSKRMSConstants.TERM_SPEC_COURSE,
173 				KSKRMSConstants.TERM_COMPLETED_COURSE_NUMBER, termParameters);
174 		
175 		termParameters.clear();
176         setupTermParameters(termParameters, KSKRMSConstants.PERSON_ID_TERM_PROPERTY, "???termParameterValue");
177 		createKRMSTermDefinition(nameSpace, KSKRMSConstants.TERM_SPEC_COURSE,
178 				KSKRMSConstants.TERM_COMPLETED_COURSE_CODE, termParameters);
179 		
180 		termParameters.clear();
181         setupTermParameters(termParameters, KSKRMSConstants.PERSON_ID_TERM_PROPERTY, "???termParameterValue");
182 		createKRMSTermDefinition(nameSpace, KSKRMSConstants.TERM_SPEC_COURSE,
183 				KSKRMSConstants.TERM_COMPLETED_COURSE_SET, termParameters);
184 		
185 		termParameters.clear();
186         setupTermParameters(termParameters, KSKRMSConstants.PERSON_ID_TERM_PROPERTY, "???termParameterValue");
187 		createKRMSTermDefinition(nameSpace, KSKRMSConstants.TERM_SPEC_COURSE,
188 				KSKRMSConstants.TERM_COMPLETED_EFFECTIVE_DATE_FROM, termParameters);
189 		
190 		termParameters.clear();
191         setupTermParameters(termParameters, KSKRMSConstants.PERSON_ID_TERM_PROPERTY, "???termParameterValue");
192 		createKRMSTermDefinition(nameSpace, KSKRMSConstants.TERM_SPEC_COURSE,
193 				KSKRMSConstants.TERM_COMPLETED_EFFECTIVE_DATE_TO, termParameters);
194 		
195 		termParameters.clear();
196         setupTermParameters(termParameters, KSKRMSConstants.PERSON_ID_TERM_PROPERTY, "???termParameterValue");
197 		createKRMSTermDefinition(nameSpace, KSKRMSConstants.TERM_SPEC_COURSE,
198 				KSKRMSConstants.TERM_COMPLETED_LEARNING_OBJ_DESCR, termParameters);
199 		
200 		termParameters.clear();
201         setupTermParameters(termParameters, KSKRMSConstants.PERSON_ID_TERM_PROPERTY, "???termParameterValue");
202         setupTermParameters(termParameters, KSKRMSConstants.TERM_ID_TERM_PROPERTY, "???termParameterValue");
203 		createKRMSTermDefinition(nameSpace, KSKRMSConstants.TERM_SPEC_COURSE,
204 				KSKRMSConstants.TERM_ENROLLED_COURSE_NUMBER_RANGE, termParameters);
205 		
206 		termParameters.clear();
207         setupTermParameters(termParameters, KSKRMSConstants.PERSON_ID_TERM_PROPERTY, "???termParameterValue");
208         setupTermParameters(termParameters, KSKRMSConstants.TERM_ID_TERM_PROPERTY, "???termParameterValue");
209 		createKRMSTermDefinition(nameSpace, KSKRMSConstants.TERM_SPEC_COURSE,
210 				KSKRMSConstants.TERM_ENROLLED_COURSE_NUMBER_SUBJECT_CODE, termParameters);
211 		
212 		termParameters.clear();
213         setupTermParameters(termParameters, KSKRMSConstants.PERSON_ID_TERM_PROPERTY, "???termParameterValue");
214         setupTermParameters(termParameters, KSKRMSConstants.TERM_ID_TERM_PROPERTY, "???termParameterValue");		
215 		createKRMSTermDefinition(nameSpace, KSKRMSConstants.TERM_SPEC_COURSE,
216 				KSKRMSConstants.TERM_ENROLLED_SET, termParameters);
217 		
218 		termParameters.clear();
219         setupTermParameters(termParameters, KSKRMSConstants.PERSON_ID_TERM_PROPERTY, "???termParameterValue");
220         setupTermParameters(termParameters, KSKRMSConstants.TERM_ID_TERM_PROPERTY, "???termParameterValue");
221 		createKRMSTermDefinition(nameSpace, KSKRMSConstants.TERM_SPEC_COURSE,
222 				KSKRMSConstants.TERM_ENROLLED_COURSE, termParameters);
223 		
224 		termParameters.clear();
225         setupTermParameters(termParameters, KSKRMSConstants.PERSON_ID_TERM_PROPERTY, "???termParameterValue");
226         setupTermParameters(termParameters, KSKRMSConstants.TERM_ID_TERM_PROPERTY, "???termParameterValue");
227 		createKRMSTermDefinition(nameSpace, KSKRMSConstants.TERM_SPEC_COURSE,
228 				KSKRMSConstants.TERM_ENROLLED_COURSES, termParameters);
229 		
230 		termParameters.clear();
231         setupTermParameters(termParameters, KSKRMSConstants.PERSON_ID_TERM_PROPERTY, "???termParameterValue");
232         setupTermParameters(termParameters, KSKRMSConstants.TERM_ID_TERM_PROPERTY, "???termParameterValue");		
233 		createKRMSTermDefinition(nameSpace, KSKRMSConstants.TERM_SPEC_COURSE,
234 				KSKRMSConstants.TERM_ENROLLED_EFFECTIVE_DATE_FROM, termParameters);
235 		
236 		termParameters.clear();
237         setupTermParameters(termParameters, KSKRMSConstants.PERSON_ID_TERM_PROPERTY, "???termParameterValue");
238         setupTermParameters(termParameters, KSKRMSConstants.TERM_ID_TERM_PROPERTY, "???termParameterValue");		
239 		createKRMSTermDefinition(nameSpace, KSKRMSConstants.TERM_SPEC_COURSE,
240 				KSKRMSConstants.TERM_ENROLLED_EFFECTIVE_DATE_TO, termParameters);
241 		
242 		termParameters.clear();
243         setupTermParameters(termParameters, KSKRMSConstants.PERSON_ID_TERM_PROPERTY, "???termParameterValue");
244         setupTermParameters(termParameters, KSKRMSConstants.TERM_ID_TERM_PROPERTY, "???termParameterValue");		
245 		createKRMSTermDefinition(nameSpace, KSKRMSConstants.TERM_SPEC_COURSE,
246 				KSKRMSConstants.TERM_ENROLLED_LEARNING_OBJ_DESCR, termParameters);
247 		
248 		
249         // Term Parameter Example
250 		//List<TermParameterDefinition.Builder> termParameters = new ArrayList<TermParameterDefinition.Builder>();
251         //setupTermParameters(termParameters, KSKRMSConstants.PERSON_ID_TERM_PROPERTY, "???termParameterValue");
252         //setupTermParameters(termParameters, KSKRMSConstants.COURSE_CODE_TERM_PROPERTY, "???termParameterValue");
253         //
254 	}
255 
256 	private void setupTermParameters(
257 			List<TermParameterDefinition.Builder> termParameters, String termParamName, String termParamValue) {
258 		TermParameterDefinition.Builder termParamBuilder2 =
259             TermParameterDefinition.Builder.create(null, null, termParamName, termParamValue);
260         termParameters.add(termParamBuilder2);
261 	}
262 
263 	private void createKRMSTermDefinition(String nameSpace,
264 			String termSpecName, String termName, List<TermParameterDefinition.Builder> termParameters) {
265 
266 		Map<String, String> queryArgs = new HashMap<String, String>();
267 		queryArgs.put("namespace", nameSpace);
268 		queryArgs.put("name", termSpecName);
269 		String a = queryArgs.get("name");
270 		TermSpecificationBo termSpecBo = getBoService().findByPrimaryKey(
271 				TermSpecificationBo.class, queryArgs);
272 		//
273 		TermSpecificationDefinition termSpec = null;
274 
275 		termSpec = termSpecBo.to(termSpecBo);
276 
277 		Builder termSpecDefBuilder = TermSpecificationDefinition.Builder
278 				.create(termSpec);
279 //        // Term Param
280 //		List<TermParameterDefinition.Builder> termParameters = new ArrayList<TermParameterDefinition.Builder>();
281 //        TermParameterDefinition.Builder termParamBuilder2 =
282 //            TermParameterDefinition.Builder.create(null, null, "testParamName", "testParamValue");
283 //        termParameters.add(termParamBuilder2);
284 
285 		TermDefinition.Builder termDefBuilder = TermDefinition.Builder.create(
286 				null, termSpecDefBuilder, termParameters);
287 		
288 		termDefBuilder.setDescription(termName);
289 
290 		TermDefinition termDefinition = termDefBuilder.build();
291 
292 		termDefinition = termBoService.createTerm(termDefinition);
293 
294 	}
295 
296 	private KrmsTypeDefinition getKSKRMSType(String nameSpace, String typeName, String typeServiceName) {
297 		KrmsTypeDefinition krmsAgendaType = krmsTypeRepository
298 				.getTypeByName(nameSpace, typeName);
299 
300 		if (krmsAgendaType == null) {
301 
302 			KrmsTypeDefinition.Builder krmsAgendaTypeDefinition = KrmsTypeDefinition.Builder
303 					.create(typeName, nameSpace);
304 			krmsAgendaTypeDefinition.setServiceName(typeServiceName);
305 
306 			// TODO KSKRMS not sure where the Attributes fit in and how they
307 			// link up
308 			// int contextAttrSequenceIndex = 0;
309 			// List<KrmsTypeAttribute.Builder> contextAttributeBuilders = new
310 			// ArrayList<KrmsTypeAttribute.Builder>();
311 			// for (KrmsAttributeDefinition attrDef :
312 			// ksLumAttributeDefinitions.values()) {
313 			// contextAttributeBuilders.add(KrmsTypeAttribute.Builder.create(null,
314 			// attrDef.getId(),
315 			// contextAttrSequenceIndex));
316 			// contextAttrSequenceIndex += 1;
317 			//
318 			// }
319 			// krmsContextTypeDefnBuilder.setAttributes(contextAttributeBuilders);
320 			
321 			krmsAgendaType = krmsTypeRepository
322 					.createKrmsType(krmsAgendaTypeDefinition.build());
323 			return krmsAgendaType;
324 		}
325 		
326 		return krmsAgendaType;
327 	}
328 	
329 	// @Test
330 	public void TestCreateType() {
331 		getKSKRMSType(KSNAMESPACE, KSKRMSConstants.KS_AGENDA_TYPE, "testAgendaTypeService");
332 	}
333 
334 	protected BusinessObjectService getBoService() {
335 		return KRADServiceLocator.getBusinessObjectService();
336 	}
337 
338 	public TermDefinition krmsTermLookup(String termName) {
339 		// this may be called more than once, we only want to create one though
340 		Map<String, String> queryArgs = new HashMap<String, String>();
341 		queryArgs.put("desc_txt", termName);
342 		TermBo termBo = getBoService()
343 				.findByPrimaryKey(TermBo.class, queryArgs);
344 		if (termBo != null) {
345 			return TermBo.to(termBo);
346 		}
347 		return null;
348 	}
349 	
350 	// @Test
351 	public void testTermLookup() {
352 		
353 		TermDefinition term = krmsTermLookup(KSKRMSConstants.TERM_APPROVED_COURSE);
354 		assertNotNull(term);
355 	}
356 	
357 	public void createKRMSTermResolver(String termResolverName, TermSpecificationDefinition termSpecDefinition) {
358 		// KrmsType for TermResolver
359 		KrmsTypeDefinition krmsTermResolverTypeDefinition = getKSKRMSType(KSKRMSConstants.KSNAMESPACE, KSKRMSConstants.KS_TERM_RESOLVER_TYPE, "ksKRMSTermResolverTypeService");
360 
361         // TermResolver
362 		TermResolverDefinition termResolverDef =
363 			TermResolverDefinition.Builder.create(null, KSKRMSConstants.KSNAMESPACE, termResolverName, krmsTermResolverTypeDefinition.getId(),
364 					TermSpecificationDefinition.Builder.create(termSpecDefinition),
365 					null,
366 					null,
367 					null).build();
368 		termResolverDef = termBoService.createTermResolver(termResolverDef);
369 	}
370 	
371 }