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.util;
17  
18  import org.kuali.rice.core.framework.resourceloader.SpringResourceLoader;
19  import org.kuali.rice.krad.service.KRADServiceLocator;
20  import org.kuali.rice.krms.api.repository.proposition.PropositionParameterType;
21  import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
22  import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
23  import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
24  import org.kuali.rice.krms.impl.repository.AgendaBoService;
25  import org.kuali.rice.krms.impl.repository.ContextBoService;
26  import org.kuali.rice.krms.impl.repository.RuleBoService;
27  import org.kuali.rice.krms.impl.repository.TermBoService;
28  import org.kuali.rice.krad.service.BusinessObjectService;
29  import org.kuali.rice.krms.api.repository.context.ContextDefinition;
30  import org.kuali.student.common.util.PropertiesFilterFactoryBean;
31  import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
32  import org.kuali.rice.krms.api.repository.agenda.AgendaItemDefinition;
33  import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
34  import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;
35  import org.kuali.rice.krms.api.repository.proposition.PropositionType;
36  import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
37  import org.kuali.rice.krms.api.repository.term.TermDefinition;
38  import org.kuali.rice.krms.api.repository.LogicalOperator;
39  import org.kuali.rice.krms.impl.repository.TermBo;
40  
41  import java.util.Arrays;
42  import java.util.Enumeration;
43  import java.util.HashMap;
44  import java.util.Map;
45  import java.util.Properties;
46  import java.util.ArrayList;
47  import java.util.Collections;
48  import java.util.HashMap;
49  import java.util.List;
50  import java.util.Map.Entry;
51  import java.util.Set;
52  
53  import javax.xml.namespace.QName;
54  
55  public class KSKRMSAgendaDataSetupUtility {
56  
57  
58      public static final String PROPERTY_namespace = "Namespace";
59      public static final String PROPERTY_PROPOSITION1 = "Proposition1";
60      public static final String PROPERTY_PROPOSITION2 = "Proposition2";
61      public static final String PROPERTY_PROPOSITION3 = "Proposition3";
62      public static final String PROPERTY_PROPOSITION4 = "Proposition4";
63      public static final String PROPERTY_PROPOSITION5 = "Proposition5";
64      public static final String PROPERTY_PROPOSITION6 = "Proposition6";
65      public static final String PROPERTY_PROPOSITION7 = "Proposition7";
66      public static final String PROPERTY_PROPOSITION8 = "Proposition8";
67      public static final String PROPERTY_PROPOSITION9 = "Proposition9";
68  
69      KrmsTypeDefinition krmsTypeForContext;
70  
71      private ContextBoService contextRepository;
72      private KrmsTypeRepositoryService krmsTypeRepository;
73      private AgendaBoService agendaBoService;
74      private RuleBoService ruleBoService;
75      private TermBoService termBoService;
76      private SpringResourceLoader krmsTestResourceLoader;
77      private BusinessObjectService boService;
78  
79      PropertiesFilterFactoryBean propertyUtil;
80      private String namespace;
81      // Needed for agendas
82      KrmsTypeDefinition krmsTypeDefinition;
83  
84  
85      public static void main(String [ ] args)
86      {
87          KSKRMSAgendaDataSetupUtility utility = new KSKRMSAgendaDataSetupUtility();
88          utility.setupRequiredServices();
89          utility.createKSKRMSData();
90      }
91  
92      public void createKSKRMSData() {
93          setupPropertyFile();
94          namespace = getPropertyValue(PROPERTY_namespace);
95          createKRMSProposition1FromPropertyFile();
96          createKRMSProposition2FromPropertyFile();
97          createKRMSProposition3FromPropertyFile();
98          createKRMSProposition4FromPropertyFile();
99          createKRMSProposition5FromPropertyFile();
100         createKRMSProposition6FromPropertyFile();
101         createKRMSProposition7FromPropertyFile();
102         createKRMSProposition8FromPropertyFile();
103         createKRMSProposition9FromPropertyFile();
104 
105     }
106     public void createKRMSProposition1FromPropertyFile() {
107         Properties properties = getProperties(PROPERTY_PROPOSITION1);
108         //
109         Enumeration elements = properties.elements();
110 
111         ArrayList<PropositionParametersBuilder> propositionList = new ArrayList<PropositionParametersBuilder>();
112 
113         PropositionParametersBuilder proposition = null;
114         int i = 0;
115         //
116         while (elements.hasMoreElements()) {
117             propositionList = new ArrayList<PropositionParametersBuilder>();
118             String termSpecValues =  (String) elements.nextElement();
119             System.out.println(i + " - " + termSpecValues);
120 
121             String delims = ",+"; // use + to treat consecutive delims as one;
122             // omit to treat consecutive delims separately
123             String[] tokens = termSpecValues.split(delims);
124             proposition = createProposition(
125                     tokens[0],tokens[1],tokens[2]);
126             propositionList.add(proposition);
127             i++;
128 
129 
130         }
131         createAgendaAndRuleAndPropositions(KSKRMSReplaceWithPropertyFile.AGENDA1,
132                 contextRepository.getContextByNameAndNamespace(
133                         KSKRMSReplaceWithPropertyFile.CONTEXT_STUD_ELIGIBILITY, namespace),
134                 null, namespace, propositionList.toArray(new PropositionParametersBuilder[propositionList.size()]));
135     }
136 
137     public void createKRMSProposition2FromPropertyFile() {
138         Properties properties = getProperties(PROPERTY_PROPOSITION2);
139         //
140         Enumeration elements = properties.elements();
141 
142         ArrayList<PropositionParametersBuilder> propositionList = new ArrayList<PropositionParametersBuilder>();
143 
144         PropositionParametersBuilder proposition = null;
145         int i = 0;
146         //
147         while (elements.hasMoreElements()) {
148             propositionList = new ArrayList<PropositionParametersBuilder>();
149             String termSpecValues =  (String) elements.nextElement();
150             System.out.println(i + " - " + termSpecValues);
151 
152             String delims = ",+"; // use + to treat consecutive delims as one;
153             // omit to treat consecutive delims separately
154             String[] tokens = termSpecValues.split(delims);
155             proposition = createProposition(
156                     tokens[0],tokens[1],tokens[2]);
157             propositionList.add(proposition);
158             i++;
159 
160 
161         }
162         createAgendaAndRuleAndPropositions(KSKRMSReplaceWithPropertyFile.AGENDA2,
163                 contextRepository.getContextByNameAndNamespace(
164                         KSKRMSReplaceWithPropertyFile.CONTEXT_STUD_ELIGIBILITY, namespace),
165                 null, namespace, propositionList.toArray(new PropositionParametersBuilder[propositionList.size()]));
166     }
167 
168     public void createKRMSProposition3FromPropertyFile() {
169         Properties properties = getProperties(PROPERTY_PROPOSITION3);
170         //
171         Enumeration elements = properties.elements();
172 
173         ArrayList<PropositionParametersBuilder> propositionList = new ArrayList<PropositionParametersBuilder>();
174 
175         PropositionParametersBuilder proposition = null;
176         int i = 0;
177         //
178         while (elements.hasMoreElements()) {
179             propositionList = new ArrayList<PropositionParametersBuilder>();
180             String termSpecValues =  (String) elements.nextElement();
181             System.out.println(i + " - " + termSpecValues);
182 
183             String delims = ",+"; // use + to treat consecutive delims as one;
184             // omit to treat consecutive delims separately
185             String[] tokens = termSpecValues.split(delims);
186             proposition = createProposition(
187                     tokens[0],tokens[1],tokens[2]);
188             propositionList.add(proposition);
189             i++;
190         }
191         createAgendaAndRuleAndPropositions(KSKRMSReplaceWithPropertyFile.AGENDA3,
192                 contextRepository.getContextByNameAndNamespace(
193                         KSKRMSReplaceWithPropertyFile.CONTEXT_STUD_ELIGIBILITY, namespace),
194                 null, namespace, propositionList.toArray(new PropositionParametersBuilder[propositionList.size()]));
195     }
196 
197     public void createKRMSProposition4FromPropertyFile() {
198         Properties properties = getProperties(PROPERTY_PROPOSITION4);
199         //
200         Enumeration elements = properties.elements();
201 
202         ArrayList<PropositionParametersBuilder> propositionList = new ArrayList<PropositionParametersBuilder>();
203 
204         PropositionParametersBuilder proposition = null;
205         int i = 0;
206         //
207         while (elements.hasMoreElements()) {
208             propositionList = new ArrayList<PropositionParametersBuilder>();
209             String termSpecValues =  (String) elements.nextElement();
210             System.out.println(i + " - " + termSpecValues);
211 
212             String delims = ",+"; // use + to treat consecutive delims as one;
213             // omit to treat consecutive delims separately
214             String[] tokens = termSpecValues.split(delims);
215             proposition = createProposition(
216                     tokens[0],tokens[1],tokens[2]);
217             propositionList.add(proposition);
218             i++;
219         }
220         createAgendaAndRuleAndPropositions(KSKRMSReplaceWithPropertyFile.AGENDA4,
221                 contextRepository.getContextByNameAndNamespace(
222                         KSKRMSReplaceWithPropertyFile.CONTEXT_STUD_ELIGIBILITY, namespace),
223                 null, namespace, propositionList.toArray(new PropositionParametersBuilder[propositionList.size()]));
224     }
225 
226     public void createKRMSProposition5FromPropertyFile() {
227         Properties properties = getProperties(PROPERTY_PROPOSITION5);
228         //
229         Enumeration elements = properties.elements();
230 
231         ArrayList<PropositionParametersBuilder> propositionList = new ArrayList<PropositionParametersBuilder>();
232 
233         PropositionParametersBuilder proposition = null;
234         int i = 0;
235         //
236         while (elements.hasMoreElements()) {
237             propositionList = new ArrayList<PropositionParametersBuilder>();
238             String termSpecValues =  (String) elements.nextElement();
239             System.out.println(i + " - " + termSpecValues);
240 
241             String delims = ",+"; // use + to treat consecutive delims as one;
242             // omit to treat consecutive delims separately
243             String[] tokens = termSpecValues.split(delims);
244             proposition = createProposition(
245                     tokens[0],tokens[1],tokens[2]);
246             propositionList.add(proposition);
247             i++;
248         }
249         createAgendaAndRuleAndPropositions(KSKRMSReplaceWithPropertyFile.AGENDA5,
250                 contextRepository.getContextByNameAndNamespace(
251                         KSKRMSReplaceWithPropertyFile.CONTEXT_STUD_ELIGIBILITY, namespace),
252                 null, namespace, propositionList.toArray(new PropositionParametersBuilder[propositionList.size()]));
253     }
254 
255     public void createKRMSProposition6FromPropertyFile() {
256         Properties properties = getProperties(PROPERTY_PROPOSITION6);
257         //
258         Enumeration elements = properties.elements();
259 
260         ArrayList<PropositionParametersBuilder> propositionList = new ArrayList<PropositionParametersBuilder>();
261 
262         PropositionParametersBuilder proposition = null;
263         int i = 0;
264         //
265         while (elements.hasMoreElements()) {
266             propositionList = new ArrayList<PropositionParametersBuilder>();
267             String termSpecValues =  (String) elements.nextElement();
268             System.out.println(i + " - " + termSpecValues);
269 
270             String delims = ",+"; // use + to treat consecutive delims as one;
271             // omit to treat consecutive delims separately
272             String[] tokens = termSpecValues.split(delims);
273             proposition = createProposition(
274                     tokens[0],tokens[1],tokens[2]);
275             propositionList.add(proposition);
276             i++;
277         }
278         createAgendaAndRuleAndPropositions(KSKRMSReplaceWithPropertyFile.AGENDA6,
279                 contextRepository.getContextByNameAndNamespace(
280                         KSKRMSReplaceWithPropertyFile.CONTEXT_STUD_ELIGIBILITY, namespace),
281                 null, namespace, propositionList.toArray(new PropositionParametersBuilder[propositionList.size()]));
282     }
283 
284 
285     public void createKRMSProposition7FromPropertyFile() {
286         Properties properties = getProperties(PROPERTY_PROPOSITION7);
287         //
288         Enumeration elements = properties.elements();
289 
290         ArrayList<PropositionParametersBuilder> propositionList = new ArrayList<PropositionParametersBuilder>();
291 
292         PropositionParametersBuilder proposition = null;
293         int i = 0;
294         //
295         while (elements.hasMoreElements()) {
296             propositionList = new ArrayList<PropositionParametersBuilder>();
297             String termSpecValues =  (String) elements.nextElement();
298             System.out.println(i + " - " + termSpecValues);
299 
300             String delims = ",+"; // use + to treat consecutive delims as one;
301             // omit to treat consecutive delims separately
302             String[] tokens = termSpecValues.split(delims);
303             proposition = createProposition(
304                     tokens[0],tokens[1],tokens[2]);
305             propositionList.add(proposition);
306             i++;
307         }
308         createAgendaAndRuleAndPropositions(KSKRMSReplaceWithPropertyFile.AGENDA7,
309                 contextRepository.getContextByNameAndNamespace(
310                         KSKRMSReplaceWithPropertyFile.CONTEXT_STUD_ELIGIBILITY, namespace),
311                 null, namespace, propositionList.toArray(new PropositionParametersBuilder[propositionList.size()]));
312     }
313 
314     public void createKRMSProposition8FromPropertyFile() {
315         Properties properties = getProperties(PROPERTY_PROPOSITION8);
316         //
317         Enumeration elements = properties.elements();
318 
319         ArrayList<PropositionParametersBuilder> propositionList = new ArrayList<PropositionParametersBuilder>();
320 
321         PropositionParametersBuilder proposition = null;
322         int i = 0;
323         //
324         while (elements.hasMoreElements()) {
325             propositionList = new ArrayList<PropositionParametersBuilder>();
326             String termSpecValues =  (String) elements.nextElement();
327             System.out.println(i + " - " + termSpecValues);
328 
329             String delims = ",+"; // use + to treat consecutive delims as one;
330             // omit to treat consecutive delims separately
331             String[] tokens = termSpecValues.split(delims);
332             proposition = createProposition(
333                     tokens[0],tokens[1],tokens[2]);
334             propositionList.add(proposition);
335             i++;
336         }
337 
338         createAgendaAndRuleAndPropositions(KSKRMSReplaceWithPropertyFile.AGENDA8,
339                 contextRepository.getContextByNameAndNamespace(
340                         KSKRMSReplaceWithPropertyFile.CONTEXT_STUD_ELIGIBILITY, namespace),
341                 null, namespace,  propositionList.toArray(new PropositionParametersBuilder[propositionList.size()]));
342 
343         // Creating agenda 9 based on Made Up Data
344         createAgendaAndRuleAndPropositions(KSKRMSReplaceWithPropertyFile.AGENDA9,
345                 contextRepository.getContextByNameAndNamespace(
346                         KSKRMSReplaceWithPropertyFile.CONTEXT_STUD_ELIGIBILITY, namespace),
347                 null, namespace);
348 
349         // Creating agenda 10 based on Made Up Data
350         createAgendaAndRuleAndPropositions(KSKRMSReplaceWithPropertyFile.AGENDA10,
351                 contextRepository.getContextByNameAndNamespace(
352                         KSKRMSReplaceWithPropertyFile.CONTEXT_STUD_ELIGIBILITY, namespace),
353                 null, namespace);
354     }
355     public void createKRMSProposition9FromPropertyFile() {
356         Properties properties = getProperties(PROPERTY_PROPOSITION9);
357         //
358         Enumeration elements = properties.elements();
359 
360         ArrayList<PropositionParametersBuilder> propositionList = new ArrayList<PropositionParametersBuilder>();
361 
362         PropositionParametersBuilder proposition = null;
363         int i = 0;
364         //
365         while (elements.hasMoreElements()) {
366             propositionList = new ArrayList<PropositionParametersBuilder>();
367             String termSpecValues =  (String) elements.nextElement();
368             System.out.println(i + " - " + termSpecValues);
369 
370             String delims = ",+"; // use + to treat consecutive delims as one;
371             // omit to treat consecutive delims separately
372             String[] tokens = termSpecValues.split(delims);
373             proposition = createProposition(
374                     tokens[0],tokens[1],tokens[2]);
375             propositionList.add(proposition);
376             i++;
377         }
378         createAgendaAndRuleAndPropositions(KSKRMSReplaceWithPropertyFile.AGENDA11,
379                 contextRepository.getContextByNameAndNamespace(
380                         KSKRMSReplaceWithPropertyFile.CONTEXT_STUD_ELIGIBILITY, namespace),
381                 null, namespace, propositionList.toArray(new PropositionParametersBuilder[propositionList.size()]));
382     }
383 
384     private void setupPropertyFile() {
385         propertyUtil = new PropertiesFilterFactoryBean();
386         propertyUtil.setPropertyFile("classpath:KSKRMSDataToLoad.properties");
387 
388     }
389 
390     private void setupRequiredServices() {
391         // Setup all the services...
392 //        termBoService = KrmsRepositoryServiceLocator.getTermBoService();
393 //        agendaBoService = KrmsRepositoryServiceLocator.getAgendaBoService();
394 //        contextRepository = KrmsRepositoryServiceLocator.getContextBoService();
395 //        ruleBoService = KrmsRepositoryServiceLocator.getRuleBoService();
396 //        krmsTypeRepository = KrmsRepositoryServiceLocator
397 //                .getKrmsTypeRepositoryService();
398 //        functionBoService = KrmsRepositoryServiceLocator.getBean("functionRepositoryService");
399     }
400 
401    /**
402     * Service methods setup
403     */
404     public void setKrmsTypeDefinition(KrmsTypeDefinition krmsTypeDefinition) {
405         this.krmsTypeForContext = krmsTypeDefinition;
406     }
407 
408     public void setContextRepository(ContextBoService contextRepository) {
409         this.contextRepository = contextRepository;
410     }
411 
412     public void setKrmsTypeRepository(KrmsTypeRepositoryService krmsTypeRepository) {
413         this.krmsTypeRepository = krmsTypeRepository;
414     }
415 
416     public void setAgendaBoService(AgendaBoService agendaBoService) {
417         this.agendaBoService = agendaBoService;
418     }
419 
420     public void setRuleBoService(RuleBoService ruleBoService) {
421         this.ruleBoService = ruleBoService;
422     }
423 
424     public void setTermBoService(TermBoService termBoService) {
425         this.termBoService = termBoService;
426     }
427 
428     public void setBoService(BusinessObjectService boService) {
429         this.boService = boService;
430     }
431 
432     private void writeError(Exception e) {
433         System.out.println("Error loading " + propertyUtil.getPrefix() + " from Propertyfile : " + propertyUtil.getPropertyFile() );
434         System.out.println(e.getMessage());
435     }
436     private String getPropertyValue(String propertyPrefix) {
437 
438         Properties properties = getProperties(propertyPrefix);
439         Enumeration elements = properties.elements();
440         while (elements.hasMoreElements()) {
441             return (String) elements.nextElement();
442         }
443         return null;
444     }
445 
446     private Properties getProperties(String propertyPrefix) {
447         propertyUtil.setPrefix(propertyPrefix);
448         Properties properties = null;
449         try {
450             properties = (Properties) propertyUtil.getObject();
451         } catch (Exception e) {
452             writeError(e);
453         }
454         return properties;
455     }
456 
457     private PropositionParametersBuilder createProposition(String termUsed,
458                                                            String PropositionConstant, String Operator) {
459         Map<String, PropositionParameterType> propositionsMap;
460         propositionsMap = new HashMap<String, PropositionParameterType>();
461         propositionsMap.put(krmsTermLookup(termUsed).getId(),
462                 PropositionParameterType.TERM);
463         propositionsMap.put(PropositionConstant,
464                 PropositionParameterType.CONSTANT);
465         propositionsMap.put(Operator, PropositionParameterType.OPERATOR);
466         PropositionParametersBuilder proposition = buildKRMSProposition(propositionsMap);
467         return proposition;
468     }
469 
470     private void createAgendaAndRuleAndPropositions(String agendaName,
471                                                     ContextDefinition contextDefinition, String eventName,
472                                                     String nameSpace, PropositionParametersBuilder...  proposition) {
473         // Create Agenda...
474         AgendaDefinition agendaDef = createKRMSAgendaDefinition(agendaName,
475                 contextDefinition);
476 
477         // Create AgendaItems
478         AgendaItemDefinition.Builder agendaItemBuilder1 = AgendaItemDefinition.Builder
479                 .create(null, agendaDef.getId());
480 
481         // Create Rules
482         String ruleDefinitionID = createKRMSRuleDefinition(nameSpace,
483                 agendaName + "::Rule1", contextDefinition, LogicalOperator.OR,
484                 proposition).getId();
485 
486         agendaItemBuilder1.setRuleId(ruleDefinitionID);
487 
488         // Create Agenda Items on DB
489         AgendaItemDefinition agendaItem1 = agendaBoService
490                 .createAgendaItem(agendaItemBuilder1.build());
491         AgendaDefinition.Builder agendaDefBuilder1 = AgendaDefinition.Builder
492                 .create(agendaDef);
493         // agendaDefBuilder1.setAttributes(Collections.singletonMap("Event",
494         // eventName));
495         agendaDefBuilder1.setFirstItemId(agendaItem1.getId());
496         agendaDef = agendaDefBuilder1.build();
497         // Update Agenda with the AgendaItems id...
498         agendaBoService.updateAgenda(agendaDef);
499 
500     }
501 
502     private AgendaDefinition createKRMSAgendaDefinition(String agendaName,
503                                                         ContextDefinition contextDefinition) {
504         AgendaDefinition agendaDef = agendaBoService
505                 .getAgendaByNameAndContextId(agendaName,
506                         contextDefinition.getId());
507         if (krmsTypeDefinition == null) {
508             krmsTypeDefinition = getKSKRMSType(namespace);
509         }
510         if (agendaDef == null) {
511             agendaDef = AgendaDefinition.Builder.create(null, agendaName,
512                     krmsTypeDefinition.getId(), contextDefinition.getId())
513                     .build();
514             agendaDef = agendaBoService.createAgenda(agendaDef);
515 
516         }
517         return agendaDef;
518     }
519 
520     private PropositionParametersBuilder buildKRMSProposition(
521             Map<String, PropositionParameterType> propositionsMap) {
522         int orderOfEntries = 0;
523         PropositionParametersBuilder params1 = new PropositionParametersBuilder();
524         Set<Entry<String, PropositionParameterType>> allValues = propositionsMap
525                 .entrySet();
526 
527         for (Entry<String, PropositionParameterType> entry : allValues) {
528             System.out.println(entry.getKey() + " " + entry.getValue());
529             if (entry.getValue() == PropositionParameterType.TERM
530                     && orderOfEntries == 0) {
531                 params1.add(entry.getKey(), entry.getValue());
532                 orderOfEntries = 1;
533             } else if (entry.getValue() == PropositionParameterType.CONSTANT
534                     && orderOfEntries == 1) {
535                 params1.add(entry.getKey(), entry.getValue());
536                 orderOfEntries = 2;
537             } else if (entry.getValue() == PropositionParameterType.OPERATOR
538                     && orderOfEntries == 2) {
539                 params1.add(entry.getKey(), entry.getValue());
540                 orderOfEntries = 3;
541             }
542         }
543         if (orderOfEntries == 1) {
544             for (Entry<String, PropositionParameterType> entry : allValues) {
545                 System.out.println(entry.getKey() + " " + entry.getValue());
546                 if (entry.getValue() == PropositionParameterType.CONSTANT
547                         && orderOfEntries == 1) {
548                     params1.add(entry.getKey(), entry.getValue());
549                     orderOfEntries = 2;
550                 } else if (entry.getValue() == PropositionParameterType.OPERATOR
551                         && orderOfEntries == 2) {
552                     params1.add(entry.getKey(), entry.getValue());
553                     orderOfEntries = 3;
554                 }
555             }
556         }
557         if (orderOfEntries == 2) {
558             for (Entry<String, PropositionParameterType> entry : allValues) {
559                 System.out.println(entry.getKey() + " " + entry.getValue());
560                 if (entry.getValue() == PropositionParameterType.OPERATOR
561                         && orderOfEntries == 2) {
562                     params1.add(entry.getKey(), entry.getValue());
563                     orderOfEntries = 3;
564                 }
565             }
566         }
567         return params1;
568     }
569 
570     private PropositionDefinition.Builder createKRMSPropositionDefinition(
571             String propDescription, PropositionParametersBuilder params,
572             RuleDefinition parentRule) {
573         // Proposition for rule 2
574         PropositionDefinition.Builder propositionDefBuilder1 = PropositionDefinition.Builder
575                 .create(null, PropositionType.SIMPLE.getCode(),
576                         parentRule.getId(), null /*
577 												 * type code is only for custom
578 												 * props
579 												 */,
580                         Collections.<PropositionParameter.Builder> emptyList());
581         propositionDefBuilder1.setDescription(propDescription);
582 
583         // PropositionParams for rule 2
584         List<PropositionParameter.Builder> propositionParams1 = params.build();
585 
586         // set the parent proposition so the builder will not puke
587         for (PropositionParameter.Builder propositionParamBuilder : propositionParams1) {
588             propositionParamBuilder.setProposition(propositionDefBuilder1);
589         }
590 
591         propositionDefBuilder1.setParameters(propositionParams1);
592 
593         return propositionDefBuilder1;
594     }
595 
596     private RuleDefinition createKRMSRuleDefinition(String nameSpace,
597                                                     String ruleName, ContextDefinition contextDefinition,
598                                                     LogicalOperator operator, PropositionParametersBuilder... pbs) {
599         // Create the Rule
600         RuleDefinition.Builder ruleDefBuilder = RuleDefinition.Builder.create(
601                 null, ruleName, nameSpace, null, null);
602         ruleDefBuilder.setDescription(ruleName + "::Description");
603 
604         //
605         RuleDefinition ruleDef = ruleBoService.getRuleByNameAndNamespace(
606                 ruleName, namespace);
607         //
608         if (ruleDef == null) {
609             ruleDef = ruleBoService.createRule(ruleDefBuilder.build());
610         }
611 
612         // Create the proposition for the rule
613         PropositionDefinition.Builder parentProposition = PropositionDefinition.Builder
614                 .create(null, PropositionType.COMPOUND.getCode(),
615                         ruleDef.getId(), null, null);
616         parentProposition
617                 .setCompoundComponents(new ArrayList<PropositionDefinition.Builder>());
618 
619         if (operator != null) {
620             parentProposition.setCompoundOpCode(operator.getCode());
621         }
622 
623         ruleDefBuilder = RuleDefinition.Builder.create(ruleDef);
624 
625         //
626         ArrayList<PropositionDefinition.Builder> newComponentList = new ArrayList<PropositionDefinition.Builder>();
627         for (PropositionParametersBuilder params : pbs) {
628 
629             StringBuilder propositionNameBuilder = new StringBuilder(
630                     "Proposition");
631 
632             propositionNameBuilder.append("::");
633             for (Object[] param : params.params) {
634                 propositionNameBuilder.append(param[0].toString());
635                 propositionNameBuilder.append("--");
636             }
637 
638             PropositionDefinition.Builder propositionBuilder = createKRMSPropositionDefinition(
639                     propositionNameBuilder.toString(), params, ruleDef);
640 
641             if (pbs.length > 1) {
642                 // add it to the compound prop
643                 // List<org.kuali.rice.krms.api.repository.proposition.PropositionDefinition.Builder>
644                 // compoundComponents =
645                 // parentProposition.getCompoundComponents();
646                 newComponentList.add(propositionBuilder);
647             } else {
648                 // if there is only one proposition to build, make it the parent
649                 parentProposition = propositionBuilder;
650             }
651 
652         }
653         if (newComponentList.size() > 0) {
654             parentProposition.setCompoundComponents(newComponentList);
655 
656         }
657         ruleDefBuilder.setProposition(parentProposition);
658         ruleDef = ruleDefBuilder.build();
659         ruleBoService.updateRule(ruleDef); // Creating the krms_prop_parm_t &
660         // krms_prop_t
661 
662         // Action
663         // ActionDefinition.Builder actionDefBuilder1 =
664         // ActionDefinition.Builder.create(null, ruleName + "::TestAction",
665         // nameSpace, createKrmsActionTypeDefinition(nameSpace).getId(),
666         // ruleDef1.getId(), 1);
667         // ActionDefinition actionDef1 =
668         // actionBoService.createAction(actionDefBuilder1.build());
669 
670         return ruleDef;
671     }
672 
673     private TermDefinition krmsTermLookup(String termName) {
674         // this may be called more than once, we only want to create one though
675         Map<String, String> queryArgs = new HashMap<String, String>();
676         queryArgs.put("desc_txt", termName);
677         TermBo termBo = getBoService()
678                 .findByPrimaryKey(TermBo.class, queryArgs);
679         if (termBo != null) {
680             return TermBo.to(termBo);
681         }
682         return null;
683     }
684 
685     private static class PropositionParametersBuilder {
686 
687         // poor OOD but this is quick and dirty :-P
688         private List<Object[]> params = new ArrayList<Object[]>();
689 
690         public PropositionParametersBuilder add(String value,
691                                                 PropositionParameterType type) {
692             if (type == null)
693                 throw new IllegalArgumentException("type must not be null");
694             params.add(new Object[] { value, type });
695             return this;
696         }
697 
698         public List<PropositionParameter.Builder> build() {
699             int seqCounter = 0;
700 
701             List<PropositionParameter.Builder> results = new ArrayList<PropositionParameter.Builder>();
702 
703             for (Object[] param : params) {
704                 results.add(PropositionParameter.Builder.create(null, null,
705                         (String) param[0],
706                         ((PropositionParameterType) param[1]).getCode(),
707                         seqCounter++));
708             }
709 
710             return results;
711         }
712     }
713 
714     // methods used
715     private KrmsTypeDefinition getKSKRMSType(String nameSpace) {
716         KrmsTypeDefinition krmsContextTypeDefinition = krmsTypeRepository
717                 .getTypeByName(nameSpace, KSKRMSReplaceWithPropertyFile.KS_AGENDA_TYPE);
718 
719         if (krmsContextTypeDefinition == null) {
720 
721             KrmsTypeDefinition.Builder krmsContextTypeDefnBuilder = KrmsTypeDefinition.Builder
722                     .create(KSKRMSReplaceWithPropertyFile.KS_AGENDA_TYPE, nameSpace);
723             krmsContextTypeDefnBuilder.setServiceName("myKSService");
724 
725             // TODO KSKRMS not sure where the Attributes fit in and how they
726             // link up
727             // int contextAttrSequenceIndex = 0;
728             // List<KrmsTypeAttribute.Builder> contextAttributeBuilders = new
729             // ArrayList<KrmsTypeAttribute.Builder>();
730             // for (KrmsAttributeDefinition attrDef :
731             // ksLumAttributeDefinitions.values()) {
732             // contextAttributeBuilders.add(KrmsTypeAttribute.Builder.create(null,
733             // attrDef.getId(),
734             // contextAttrSequenceIndex));
735             // contextAttrSequenceIndex += 1;
736             //
737             // }
738             // krmsContextTypeDefnBuilder.setAttributes(contextAttributeBuilders);
739 
740             krmsContextTypeDefinition = krmsTypeRepository
741                     .createKrmsType(krmsContextTypeDefnBuilder.build());
742             return krmsContextTypeDefinition;
743         }
744 
745         return krmsContextTypeDefinition;
746     }
747 
748     protected BusinessObjectService getBoService() {
749         return KRADServiceLocator.getBusinessObjectService();
750     }
751 
752 }