View Javadoc
1   package org.kuali.ole.deliver.drools.rules;
2   
3   import org.apache.commons.collections.CollectionUtils;
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.ole.deliver.drools.DroolsConstants;
8   import org.milyn.util.CollectionsUtil;
9   
10  import java.util.*;
11  
12  /**
13   * Created by sheiksalahudeenm on 7/14/15.
14   */
15  public class CheckoutRuleFormulator extends RuleFormulatorUtil implements RuleFormulator {
16      @Override
17      public Map formulateRuleMap(DroolsRuleBo droolsRuleBo) {
18          return super.formulateRule(droolsRuleBo);
19      }
20  
21      @Override
22      public String formulateRules(DroolsRuleBo droolsRuleBo) {
23          StringBuilder stringBuilder = new StringBuilder();
24          stringBuilder.append(getTemplateStringForRuleFromFile("checkout/circulation-policy.txt")).append("\n");
25          stringBuilder.append(getTemplateStringForRuleFromFile("checkout/loan-period.txt")).append("\n");
26          stringBuilder.append(getTemplateStringForRuleFromFile("checkout/loan-type.txt")).append("\n");
27          return stringBuilder.toString();
28      }
29  
30  
31      /*
32      This method is overridden in this class because its responsible for combining and building the template rule
33      string from various template rule files. In other cases, this should not be required as each class handles
34      one template rule file.
35       */
36      @Override
37      protected String getTemplateString(DroolsRuleBo droolsRuleBo) {
38          StringBuilder ruleTemplateString = new StringBuilder();
39          ruleTemplateString.append(getTemplateStringForRuleFromFile("loan-document.txt")).append("\n");
40          ruleTemplateString.append(getTemplateStringForRuleFromFile("notice-info.txt")).append("\n");
41          ruleTemplateString.append(buildItemCriteria(droolsRuleBo)).append("\n");
42          ruleTemplateString.append(getTemplateStringForRuleFromFile("checkout/patron-document.txt")).append("\n");
43  
44          return ruleTemplateString.toString();
45      }
46  
47      private String buildItemCriteria(DroolsRuleBo droolsRuleBo) {
48  
49          Map map = new HashMap();
50          StrSubstitutor sub = new StrSubstitutor(map);
51          String templateStringForRuleFromFile = getTemplateStringForRuleFromFile("item-record-for-circ.txt");
52          String itemCriteriaRule = getItemCriteriaRule(droolsRuleBo);
53          map.put("itemCriteriaRule", itemCriteriaRule);
54  
55  
56          String substituedString = sub.replace(templateStringForRuleFromFile);
57          return substituedString;
58      }
59  
60      @Override
61      protected String getTemplateFileName() {
62          return null;
63      }
64  
65      @Override
66      protected void processExtraRules(DroolsRuleBo droolsRuleBo, Map map) {
67          if (StringUtils.isNotBlank(droolsRuleBo.getItemTypes())) {
68              String processDataByDelimiter = processDataByDelimiter(droolsRuleBo.getItemTypes());
69              map.put("itemTypesOperator", droolsRuleBo.getItemTypesOperator());
70              map.put("itemTypes", processDataByDelimiter);
71          }
72          if (StringUtils.isNotBlank(droolsRuleBo.getInstitutionLocations())) {
73              map.put("institutionLocationOperator", droolsRuleBo.getInstitutionLocationsOperator());
74              map.put("institutionLocation", processDataByDelimiter(droolsRuleBo.getInstitutionLocations()));
75          }
76          if (StringUtils.isNotBlank(droolsRuleBo.getCampusLocations())) {
77              map.put("campusLocationOperator", droolsRuleBo.getCampusLocationsOperator());
78              map.put("campusLocation", processDataByDelimiter(droolsRuleBo.getCampusLocations()));
79          }
80          if (StringUtils.isNotBlank(droolsRuleBo.getLibraryLocations())) {
81              map.put("libraryLocationOperator", droolsRuleBo.getLibraryLocationsOperator());
82              map.put("libraryLocation", processDataByDelimiter(droolsRuleBo.getLibraryLocations()));
83          }
84          if (StringUtils.isNotBlank(droolsRuleBo.getCollectionLocations())) {
85              map.put("collectionLocationOperator", droolsRuleBo.getCollectionLocationsOperator());
86              map.put("collectionLocation", processDataByDelimiter(droolsRuleBo.getCollectionLocations()));
87          }
88          if (StringUtils.isNotBlank(droolsRuleBo.getShelvingLocations())) {
89              map.put("shelvingLocationOperator", droolsRuleBo.getShelvingLocationsOperator());
90              map.put("shelvingLocation", processDataByDelimiter(droolsRuleBo.getShelvingLocations()));
91          }
92          map.put("circulationPolicy", processQuotesForString(droolsRuleBo.getCirculationPolicyId())) ;
93          map.put("loanPeriod", processQuotesForString(droolsRuleBo.getLoanPeriod()));
94          map.put("recallPeriod", processQuotesForString(droolsRuleBo.getDefaultRecallPeriod()));
95          map.put("loanType", droolsRuleBo.getLoanType());
96          map.put("borrowerType",processDataByDelimiter( droolsRuleBo.getBorrowerTypes()));
97          map.put("borrowerTypeOperator", droolsRuleBo.getBorrowerTypesOperator());
98          map.put("itemTypeCount", droolsRuleBo.getItemTypeCount());
99          map.put("itemTypeCountOperator", droolsRuleBo.getItemTypeCountOperator());
100     }
101 
102     public String processQuotesForString(String data){
103         StringBuilder stringBuilder = new StringBuilder();
104         stringBuilder.append("\"")
105                 .append(data)
106                 .append("\"");
107 
108         return stringBuilder.toString();
109     }
110 
111 
112     @Override
113     public boolean isInterested(String value) {
114         return false;
115     }
116 
117     @Override
118     public boolean isInterestedForParameters(String value) {
119         return value.equals(DroolsConstants.EDITOR_TYPE.CHECKOUT);
120     }
121 
122     @Override
123     public boolean isInterestedForThenCustomRules(String value) {
124         return true;
125     }
126 
127     public String getItemCriteriaRule(DroolsRuleBo droolsRuleBo) {
128         List<String> ruleStrings = new ArrayList<>();
129         StringBuilder stringBuilder = new StringBuilder();
130         if (StringUtils.isNotBlank(droolsRuleBo.getItemTypes())) {
131             ruleStrings.add(getTemplateStringForRuleFromFile("checkout/item-type.txt"));
132         }
133         if (StringUtils.isNotBlank(droolsRuleBo.getInstitutionLocations())) {
134             ruleStrings.add(getTemplateStringForRuleFromFile("checkout/institution-location.txt"));
135         }
136         if (StringUtils.isNotBlank(droolsRuleBo.getCampusLocations())) {
137             ruleStrings.add(getTemplateStringForRuleFromFile("checkout/campus-location.txt"));
138         }
139         if (StringUtils.isNotBlank(droolsRuleBo.getLibraryLocations())) {
140             ruleStrings.add(getTemplateStringForRuleFromFile("checkout/library-location.txt"));
141         }
142         if (StringUtils.isNotBlank(droolsRuleBo.getCollectionLocations())) {
143             ruleStrings.add(getTemplateStringForRuleFromFile("checkout/collection-location.txt"));
144         }
145         if (StringUtils.isNotBlank(droolsRuleBo.getShelvingLocations())) {
146             ruleStrings.add(getTemplateStringForRuleFromFile("checkout/shelving-location.txt"));
147         }
148 
149         for (Iterator<String> iterator = ruleStrings.iterator(); iterator.hasNext(); ) {
150             String next = iterator.next();
151             stringBuilder.append(next);
152             if (iterator.hasNext()) {
153                 stringBuilder.append(",").append("\n");
154             }
155         }
156 
157         return stringBuilder.toString();
158     }
159 }