001package org.kuali.ole.deliver.drools;
002
003import org.apache.commons.io.FileUtils;
004import org.apache.commons.lang.text.StrSubstitutor;
005import org.apache.commons.lang3.StringUtils;
006import org.kuali.ole.deliver.bo.drools.DroolsRuleBo;
007import org.kuali.rice.core.api.config.property.ConfigContext;
008
009import java.io.File;
010import java.io.IOException;
011import java.net.URISyntaxException;
012import java.net.URL;
013import java.util.*;
014
015/**
016 * Created by pvsubrah on 7/2/15.
017 */
018public abstract class DroolFileGenerator {
019
020    private List<DroolsRuleBo> droolsRuleBos;
021
022    public List<DroolsRuleBo> getDroolsRuleBos() {
023        return droolsRuleBos;
024    }
025
026    public void setDroolsRuleBos(List<DroolsRuleBo> droolsRuleBos) {
027        this.droolsRuleBos = droolsRuleBos;
028    }
029
030    public File generateFile(List<DroolsRuleBo> droolsRuleBos) throws Exception {
031
032        this.droolsRuleBos = droolsRuleBos;
033
034        StringBuilder droolFileContent = new StringBuilder();
035
036        List<Map> customRules = getCustomRules();
037
038        String templateStringForPackageImportTextFile = getTemplateStringForPackageImportTextFile();
039
040        droolFileContent.append(templateStringForPackageImportTextFile).append("\n");
041
042        generateRules(droolFileContent, customRules, false);
043
044        List<Map> customRulesWithParameters = getCustomRulesWithParameters();
045
046        generateRulesWithParameters(droolFileContent, customRulesWithParameters);
047
048        String fileContent = droolFileContent.toString();
049
050        File file = new File(getFileName());
051        FileUtils.write(file, fileContent);
052        return file;
053    }
054
055    private void generateRulesWithParameters(StringBuilder droolFileContent, List<Map> customRulesWithParameters) {
056        generateRules(droolFileContent, customRulesWithParameters, true);
057    }
058
059    private void generateRules(StringBuilder droolFileContent, List<Map> customRules, boolean parameters) {
060        for (Iterator<Map> iterator = customRules.iterator(); iterator.hasNext(); ) {
061            Map ruleMap = iterator.next();
062            for (Iterator mapIterator = ruleMap.keySet().iterator(); mapIterator.hasNext(); ) {
063                String rule = (String) mapIterator.next();
064
065                String templateStringForRule = getTemplateStringForRule();
066
067                String templateStringForAgendaGroup = getTemplateStringForAgendaGroup();
068
069                String templateStringForActivationGroup = getTemplateStringForActivationGroup();
070
071                String templateStringForErrorMessage = getTemplateStringForErrorMessage();
072
073                String templateStringForErrorAndPermissions = getTemplateStringForErrorAndPermissions();
074
075                droolFileContent.append(
076                        generateDroolFileContent(
077                                templateStringForRule,
078                                templateStringForAgendaGroup,
079                                templateStringForActivationGroup,
080                                rule,
081                                (Map) ruleMap.get(rule),
082                                templateStringForErrorMessage,
083                                templateStringForErrorAndPermissions,
084                                parameters));
085            }
086        }
087    }
088
089    private String generateDroolFileContent(String templateStringForRule,
090                                            String templateStringForAgendaGroup,
091                                            String templateStringForActivationGroup,
092                                            String customRule,
093                                            Map placeHolderValues,
094                                            String templateStringForErrorMessage, String templateStringForErrorAndPermissions, boolean parameters) {
095        StringBuilder stringBuilder = new StringBuilder();
096        String agendaGroup = getAgendaGroup();
097        agendaGroup = "\"" + agendaGroup + "\"";
098
099        Map map = new HashMap();
100        map.putAll(placeHolderValues);
101        map.put("agendaGroup", agendaGroup);
102
103        StrSubstitutor sub = new StrSubstitutor(map);
104
105        stringBuilder
106                .append(sub.replace(templateStringForRule)).append("\n")
107                .append(sub.replace(templateStringForAgendaGroup)).append("\n")
108                .append(getActivationGroupString(templateStringForActivationGroup, map, sub))
109                .append("when").append("\n")
110                .append(getCustomRule(customRule, parameters, sub)).append("\n")
111                .append(getTemplateStringForError()).append("\n")
112                .append("then").append("\n")
113                .append(sub.replace(templateStringForErrorMessage)).append("\n")
114                .append(getOverridePermissions(templateStringForErrorAndPermissions, map, sub))
115                .append("end").append("\n").append("\n");
116
117        return stringBuilder.toString();
118    }
119
120    protected String getActivationGroupString(String templateStringForActivationGroup, Map map, StrSubstitutor sub) {
121        if (null != map.get("activationGroup")) {
122            return sub.replace(templateStringForActivationGroup);
123        }
124        return "";
125    }
126
127    private String getCustomRule(String customRule, boolean parameters, StrSubstitutor sub) {
128        if (parameters) {
129            return sub.replace(customRule);
130        }
131        return customRule;
132    }
133
134    private String getOverridePermissions(String templateStringForErrorAndPermissions, Map map, StrSubstitutor sub) {
135        String replacedString = sub.replace(templateStringForErrorAndPermissions);
136        String overridePermissions = !StringUtils.isEmpty((String) map.get("overridePermissions")) ? replacedString : "";
137        return overridePermissions;
138    }
139
140    private String getTemplateStringForError() {
141        String templateStringForError = "";
142        URL error = getClass().getResource("error.txt");
143
144        try {
145            File errorFile = new File(error.toURI());
146            templateStringForError = FileUtils.readFileToString(errorFile);
147        } catch (URISyntaxException e) {
148            e.printStackTrace();
149        } catch (IOException e) {
150            e.printStackTrace();
151        }
152        return templateStringForError;
153    }
154
155    private String getTemplateStringForErrorAndPermissions() {
156        String templateStringForOverridePermissions = "";
157        URL errorMessage = getClass().getResource("override-permissions.txt");
158
159        try {
160            File errorMessageFile = new File(errorMessage.toURI());
161            templateStringForOverridePermissions = FileUtils.readFileToString(errorMessageFile);
162        } catch (URISyntaxException e) {
163            e.printStackTrace();
164        } catch (IOException e) {
165            e.printStackTrace();
166        }
167        return templateStringForOverridePermissions;
168    }
169
170    private String getTemplateStringForErrorMessage() {
171        String templateStringForErrorMessage = "";
172        URL errorMessage = getClass().getResource("error-message.txt");
173
174        try {
175            File errorMessageFile = new File(errorMessage.toURI());
176            templateStringForErrorMessage = FileUtils.readFileToString(errorMessageFile);
177        } catch (URISyntaxException e) {
178            e.printStackTrace();
179        } catch (IOException e) {
180            e.printStackTrace();
181        }
182        return templateStringForErrorMessage;
183    }
184
185    private String getTemplateStringForActivationGroup() {
186        String templateStringForActivationGroup = "";
187        URL activationGroup = getClass().getResource("activation-group.txt");
188        try {
189            File activationGroupFile = new File(activationGroup.toURI());
190            templateStringForActivationGroup = FileUtils.readFileToString(activationGroupFile);
191        } catch (URISyntaxException e) {
192            e.printStackTrace();
193        } catch (IOException e) {
194            e.printStackTrace();
195        }
196        return templateStringForActivationGroup;
197    }
198
199    private String getTemplateStringForAgendaGroup() {
200        String templateStringForAgendaGroup = "";
201        URL agendaGroup = getClass().getResource("agenda-group.txt");
202        try {
203            File agendaGroupFile = new File(agendaGroup.toURI());
204            templateStringForAgendaGroup = FileUtils.readFileToString(agendaGroupFile);
205        } catch (URISyntaxException e) {
206            e.printStackTrace();
207        } catch (IOException e) {
208            e.printStackTrace();
209        }
210        return templateStringForAgendaGroup;
211    }
212
213    private String getTemplateStringForRule() {
214        String templateStringForRule = "";
215        URL rule = getClass().getResource("rule.txt");
216        try {
217            File ruleFile = new File(rule.toURI());
218            templateStringForRule = FileUtils.readFileToString(ruleFile);
219        } catch (URISyntaxException e) {
220            e.printStackTrace();
221        } catch (IOException e) {
222            e.printStackTrace();
223        }
224        return templateStringForRule;
225    }
226
227    private String getTemplateStringForPackageImportTextFile() {
228        String templateStringForPackageImportTextFile = "";
229        URL packageImportText = getClass().getResource("package-import.txt");
230        try {
231            File packageImportTextFile = new File(packageImportText.toURI());
232            templateStringForPackageImportTextFile = FileUtils.readFileToString(packageImportTextFile);
233        } catch (URISyntaxException e) {
234            e.printStackTrace();
235        } catch (IOException e) {
236            e.printStackTrace();
237        }
238        return templateStringForPackageImportTextFile;
239    }
240
241    public abstract String getFileName();
242
243    protected abstract List<Map> getCustomRules();
244
245    public abstract String getAgendaGroup();
246
247    public abstract String getActivationGroup();
248
249    public abstract List<Map> getCustomRulesWithParameters();
250
251    public abstract boolean isInterested(String value);
252
253    protected String getDroolBaseDirectory() {
254        return ConfigContext.getCurrentContextConfig().getProperty("rules.directory");
255    }
256}