View Javadoc
1   package org.kuali.ole.deliver.drools;
2   
3   import org.apache.commons.io.FileUtils;
4   import org.apache.commons.lang.text.StrSubstitutor;
5   import org.apache.commons.lang3.StringUtils;
6   import org.kuali.ole.deliver.bo.drools.DroolsRuleBo;
7   import org.kuali.rice.core.api.config.property.ConfigContext;
8   
9   import java.io.File;
10  import java.io.IOException;
11  import java.net.URISyntaxException;
12  import java.net.URL;
13  import java.util.*;
14  
15  /**
16   * Created by pvsubrah on 7/2/15.
17   */
18  public abstract class DroolFileGenerator {
19  
20      private List<DroolsRuleBo> droolsRuleBos;
21  
22      public List<DroolsRuleBo> getDroolsRuleBos() {
23          return droolsRuleBos;
24      }
25  
26      public void setDroolsRuleBos(List<DroolsRuleBo> droolsRuleBos) {
27          this.droolsRuleBos = droolsRuleBos;
28      }
29  
30      public File generateFile(List<DroolsRuleBo> droolsRuleBos, String fileName) throws Exception {
31  
32          this.droolsRuleBos = droolsRuleBos;
33  
34          StringBuilder droolFileContent = new StringBuilder();
35  
36          List<Map> customRules = getCustomRules();
37  
38          String templateStringForPackageImportTextFile = getTemplateStringForPackageImportTextFile();
39  
40          droolFileContent.append(templateStringForPackageImportTextFile).append("\n");
41  
42          generateRules(droolFileContent, customRules, false);
43  
44          List<Map> customRulesWithParameters = getCustomRulesWithParameters();
45  
46          generateRulesWithParameters(droolFileContent, customRulesWithParameters);
47  
48          String fileContent = droolFileContent.toString();
49  
50          File file = new File(getFilePath(fileName));
51          FileUtils.write(file, fileContent);
52          return file;
53      }
54  
55      private void generateRulesWithParameters(StringBuilder droolFileContent, List<Map> customRulesWithParameters) {
56          generateRules(droolFileContent, customRulesWithParameters, true);
57      }
58  
59      private void generateRules(StringBuilder droolFileContent, List<Map> customRules, boolean parameters) {
60          for (Iterator<Map> iterator = customRules.iterator(); iterator.hasNext(); ) {
61              Map ruleMap = iterator.next();
62              for (Iterator mapIterator = ruleMap.keySet().iterator(); mapIterator.hasNext(); ) {
63                  String rule = (String) mapIterator.next();
64  
65                  String templateStringForRule = getTemplateStringForRule();
66  
67                  String templateStringForErrorMessage = getTemplateStringForErrorMessage();
68  
69                  String templateStringForErrorAndPermissions = getTemplateStringForErrorAndPermissions();
70  
71                  droolFileContent.append(
72                          generateDroolFileContent(templateStringForRule,
73                                  getTemplateStringForAgendaGroup(),
74                                  getTemplateStringForActivationGroup(),
75                                  rule,
76                                  (Map) ruleMap.get(rule),
77                                  templateStringForErrorMessage,
78                                  templateStringForErrorAndPermissions,
79                                  parameters));
80              }
81          }
82      }
83  
84      private String generateDroolFileContent(String templateStringForRule,
85                                              String templateStringForAgendaGroup,
86                                              String templateStringForActivationGroup,
87                                              String customRule,
88                                              Map placeHolderValues,
89                                              String templateStringForErrorMessage, String templateStringForErrorAndPermissions, boolean parameters) {
90          StringBuilder stringBuilder = new StringBuilder();
91  
92          Map map = new HashMap();
93          map.putAll(placeHolderValues);
94  
95          StrSubstitutor sub = new StrSubstitutor(map);
96  
97          stringBuilder
98                  .append(sub.replace(templateStringForRule)).append("\n")
99                  .append(sub.replace(templateStringForAgendaGroup)).append("\n")
100                 .append(processAgendaGroupString(templateStringForActivationGroup, sub,map)).append("\n")
101                 .append("when").append("\n")
102                 .append(getCustomRule(customRule, parameters, sub)).append("\n")
103                 .append(getTemplateStringForError()).append("\n")
104                 .append("then").append("\n")
105                 .append(sub.replace(getThenCustomRules()))
106                 .append(processErrorMessage(sub, map, templateStringForErrorMessage)).append("\n")
107                 .append(getOverridePermissions(templateStringForErrorAndPermissions, map, sub))
108                 .append("end").append("\n").append("\n");
109 
110         return stringBuilder.toString();
111     }
112 
113     private String processErrorMessage(StrSubstitutor sub, Map map, String templateStringForErrorMessage) {
114         if(map.containsKey("errorMessage")){
115             return sub.replace(templateStringForErrorMessage);
116         }
117         return "";
118     }
119 
120     private String processAgendaGroupString(String templateStringForActivationGroup, StrSubstitutor sub, Map map) {
121        return map.containsKey("activation-group")? sub.replace(templateStringForActivationGroup) : "";
122     }
123 
124     private String getCustomRule(String customRule, boolean parameters, StrSubstitutor sub) {
125         if (parameters) {
126             return sub.replace(customRule);
127         }
128         return customRule;
129     }
130 
131     private String getOverridePermissions(String templateStringForErrorAndPermissions, Map map, StrSubstitutor sub) {
132         String replacedString = sub.replace(templateStringForErrorAndPermissions);
133         String overridePermissions = !StringUtils.isEmpty((String) map.get("overridePermissions")) ? replacedString : "";
134         return overridePermissions;
135     }
136 
137     private String getTemplateStringForError() {
138         String templateStringForError = "";
139         URL error = getClass().getResource("error.txt");
140 
141         try {
142             File errorFile = new File(error.toURI());
143             templateStringForError = FileUtils.readFileToString(errorFile);
144         } catch (URISyntaxException e) {
145             e.printStackTrace();
146         } catch (IOException e) {
147             e.printStackTrace();
148         }
149         return templateStringForError;
150     }
151 
152     private String getTemplateStringForErrorAndPermissions() {
153         String templateStringForOverridePermissions = "";
154         URL errorMessage = getClass().getResource("override-permissions.txt");
155 
156         try {
157             File errorMessageFile = new File(errorMessage.toURI());
158             templateStringForOverridePermissions = FileUtils.readFileToString(errorMessageFile);
159         } catch (URISyntaxException e) {
160             e.printStackTrace();
161         } catch (IOException e) {
162             e.printStackTrace();
163         }
164         return templateStringForOverridePermissions;
165     }
166 
167     private String getTemplateStringForErrorMessage() {
168         String templateStringForErrorMessage = "";
169         URL errorMessage = getClass().getResource("error-message.txt");
170 
171         try {
172             File errorMessageFile = new File(errorMessage.toURI());
173             templateStringForErrorMessage = FileUtils.readFileToString(errorMessageFile);
174         } catch (URISyntaxException e) {
175             e.printStackTrace();
176         } catch (IOException e) {
177             e.printStackTrace();
178         }
179         return templateStringForErrorMessage;
180     }
181 
182     private String getTemplateStringForActivationGroup() {
183         String templateStringForActivationGroup = "";
184         URL activationGroup = getClass().getResource("activation-group.txt");
185         try {
186             File activationGroupFile = new File(activationGroup.toURI());
187             templateStringForActivationGroup = FileUtils.readFileToString(activationGroupFile);
188         } catch (URISyntaxException e) {
189             e.printStackTrace();
190         } catch (IOException e) {
191             e.printStackTrace();
192         }
193         return templateStringForActivationGroup;
194     }
195 
196     private String getTemplateStringForAgendaGroup() {
197         String templateStringForAgendaGroup = "";
198         URL agendaGroup = getClass().getResource("agenda-group.txt");
199         try {
200             File agendaGroupFile = new File(agendaGroup.toURI());
201             templateStringForAgendaGroup = FileUtils.readFileToString(agendaGroupFile);
202         } catch (URISyntaxException e) {
203             e.printStackTrace();
204         } catch (IOException e) {
205             e.printStackTrace();
206         }
207         return templateStringForAgendaGroup;
208     }
209 
210     private String getTemplateStringForRule() {
211         String templateStringForRule = "";
212         URL rule = getClass().getResource("rule.txt");
213         try {
214             File ruleFile = new File(rule.toURI());
215             templateStringForRule = FileUtils.readFileToString(ruleFile);
216         } catch (URISyntaxException e) {
217             e.printStackTrace();
218         } catch (IOException e) {
219             e.printStackTrace();
220         }
221         return templateStringForRule;
222     }
223 
224     private String getTemplateStringForPackageImportTextFile() {
225         String templateStringForPackageImportTextFile = "";
226         URL packageImportText = getClass().getResource("package-import.txt");
227         try {
228             File packageImportTextFile = new File(packageImportText.toURI());
229             templateStringForPackageImportTextFile = FileUtils.readFileToString(packageImportTextFile);
230         } catch (URISyntaxException e) {
231             e.printStackTrace();
232         } catch (IOException e) {
233             e.printStackTrace();
234         }
235         return templateStringForPackageImportTextFile;
236     }
237 
238     public abstract String getFilePath(String fileName);
239 
240     protected abstract List<Map> getCustomRules();
241 
242     public abstract List<Map> getCustomRulesWithParameters();
243 
244     public abstract boolean isInterested(String value);
245 
246     protected abstract String getThenCustomRules();
247 
248     protected String getDroolBaseDirectory() {
249         return ConfigContext.getCurrentContextConfig().getProperty("rules.directory");
250     }
251 }