1 package org.kuali.ole.deliver.drools.rules;
2
3 import org.apache.commons.io.FileUtils;
4 import org.apache.commons.lang3.StringUtils;
5 import org.kuali.ole.deliver.bo.drools.DroolsRuleBo;
6
7 import java.io.File;
8 import java.io.IOException;
9 import java.net.URISyntaxException;
10 import java.net.URL;
11 import java.util.HashMap;
12 import java.util.Map;
13 import java.util.StringTokenizer;
14
15
16
17
18 public abstract class RuleFormulatorUtil {
19
20 protected Map formulateRule(DroolsRuleBo droolsRuleBo){
21 Map ruleMap = new HashMap();
22
23 Map map = new HashMap();
24
25 processRuleName(droolsRuleBo, map);
26
27 processAgendaGroup(droolsRuleBo, map);
28
29 processActivationGroup(droolsRuleBo, map);
30
31 processExtraRules(droolsRuleBo, map);
32
33 processErrorMessage(droolsRuleBo, map);
34
35 processOverridePermissions(droolsRuleBo, map);
36
37 String templateStringForRule = null == getTemplateFileName() ? getTemplateString(droolsRuleBo) : getTemplateStringForRuleFromFile(getTemplateFileName());
38
39 ruleMap.put(templateStringForRule, map);
40
41 return ruleMap;
42 }
43
44 protected String getTemplateStringForRuleFromFile(String fileName) {
45 String templateStringForActiveIndicator = "";
46 URL fileURL = getClass().getResource(fileName);
47 try {
48 File file = new File(fileURL.toURI());
49 templateStringForActiveIndicator = FileUtils.readFileToString(file);
50 } catch (URISyntaxException e) {
51 e.printStackTrace();
52 } catch (IOException e) {
53 e.printStackTrace();
54 }
55 return templateStringForActiveIndicator;
56 }
57
58 protected void processOverridePermissions(DroolsRuleBo droolsRuleBo, Map map) {
59 StringBuilder stringBuilder;
60 stringBuilder = new StringBuilder();
61 String isPatronActiveOverridePermission = droolsRuleBo.getOverridePermission();
62 if(StringUtils.isNotBlank(isPatronActiveOverridePermission)){
63 stringBuilder.append("\"").append(isPatronActiveOverridePermission).append("\"");
64 map.put("overridePermissions", stringBuilder.toString());
65 }
66 }
67
68 protected void processErrorMessage(DroolsRuleBo droolsRuleBo, Map map) {
69 StringBuilder stringBuilder;
70 stringBuilder = new StringBuilder();
71 String errorMessage = droolsRuleBo.getErrorMessage();
72 if (StringUtils.isNotBlank(errorMessage)) {
73 stringBuilder.append("\"").append(errorMessage).append("\"");
74 map.put("errorMessage", stringBuilder.toString());
75 }
76 }
77
78 protected void processRuleName(DroolsRuleBo droolsRuleBo, Map map) {
79 Object ruleName = droolsRuleBo.getRuleName();
80 StringBuilder stringBuilder = new StringBuilder();
81 stringBuilder.append("\"").append(ruleName).append("\"");
82 map.put("ruleName", stringBuilder.toString());
83 }
84
85 protected void processAgendaGroup(DroolsRuleBo droolsRuleBo, Map map) {
86 StringBuilder stringBuilder = new StringBuilder();
87 stringBuilder.append("\"").append(droolsRuleBo.getAgendaGroup()).append("\"");
88 map.put("agendaGroup", stringBuilder.toString());
89 }
90
91 protected void processActivationGroup(DroolsRuleBo droolsRuleBo, Map map) {
92 StringBuilder stringBuilder = new StringBuilder();
93 stringBuilder.append("\"").append(droolsRuleBo.getActivationGroup()).append("\"");
94 map.put("activationGroup", stringBuilder.toString());
95 }
96
97 protected String processDataByDelimiter(String dataString) {
98 StringBuilder stringBuilder = new StringBuilder();
99 stringBuilder.append("(");
100 StringTokenizer stringTokenizer = new StringTokenizer(dataString, ",");
101 while(stringTokenizer.hasMoreTokens()){
102 String data = stringTokenizer.nextToken();
103 stringBuilder.append("\"").append(data).append("\"");
104 if(stringTokenizer.hasMoreTokens()){
105 stringBuilder.append(",");
106 }
107 }
108
109 stringBuilder.append(")");
110 return stringBuilder.toString();
111 }
112
113
114
115
116 protected abstract String getTemplateFileName();
117
118 protected abstract void processExtraRules(DroolsRuleBo droolsRuleBo, Map map);
119
120 protected String getTemplateString(DroolsRuleBo droolsRuleBo) {
121 return "";
122 }
123 }