View Javadoc

1   package org.kuali.rice.krms.util;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.rice.krms.api.repository.NaturalLanguageTree;
5   import org.kuali.rice.krms.api.repository.RuleManagementService;
6   import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage;
7   import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
8   import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;
9   import org.kuali.rice.krms.api.repository.proposition.PropositionParameterContract;
10  import org.kuali.rice.krms.api.repository.proposition.PropositionParameterType;
11  import org.kuali.rice.krms.api.repository.term.TermDefinition;
12  import org.kuali.rice.krms.api.repository.term.TermParameterDefinition;
13  import org.kuali.rice.krms.dto.PropositionEditor;
14  import org.kuali.rice.krms.dto.TermEditor;
15  import org.kuali.rice.krms.tree.AbstractTreeBuilder;
16  import org.kuali.student.enrollment.class2.courseoffering.service.decorators.PermissionServiceConstants;
17  import org.kuali.student.krms.naturallanguage.util.KsKrmsConstants;
18  
19  import java.io.Serializable;
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.LinkedList;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Queue;
27  
28  /**
29   * Created with IntelliJ IDEA.
30   * User: SW
31   * Date: 2013/04/08
32   * Time: 11:41 AM
33   * To change this template use File | Settings | File Templates.
34   */
35  public class NaturalLanguageHelper {
36  
37      private RuleManagementService ruleManagementService;
38  
39      private Map<String, String> usageMap = new HashMap<String, String>();
40  
41      /**
42       * This method should set the natural language only for the given proposition. It does not
43       * do a recursive call through the tree structure.
44       *
45       * @param proposition
46       * @param usageName
47       */
48      public void setNaturalLanguageForUsage(PropositionEditor proposition, String usageName){
49  
50          //Setup the Proposition
51          List<PropositionParameter.Builder> parameters = new ArrayList<PropositionParameter.Builder>();
52          if(proposition.getParameters()!=null){  //Parameters on Compound Propositions could be null.
53              for(PropositionParameterContract parameter : proposition.getParameters()){
54                  PropositionParameter.Builder parmBuilder = PropositionParameter.Builder.create(parameter);
55  
56                  //Add the edited term.
57                  if(parameter.getParameterType().equals(PropositionParameterType.TERM.getCode())){
58                      TermDefinition.Builder termBuilder = TermDefinition.Builder.create(proposition.getTerm());
59                      for (Map.Entry<String, String> entry : proposition.getNlParameters().entrySet()) {
60                          termBuilder.getParameters().add(TermParameterDefinition.Builder.create(null, null, entry.getKey(), entry.getValue()));
61                      }
62                      parmBuilder.setTermValue(termBuilder.build());
63                  }
64                  parameters.add(parmBuilder);
65              }
66          }
67  
68          //Create the propbuilder but don set compound editors here, we only want to get the nl for current proposition
69          PropositionDefinition.Builder propBuilder = PropositionDefinition.Builder.create(proposition.getId(),
70                  proposition.getPropositionTypeCode(), proposition.getRuleId(), proposition.getTypeId(), parameters);
71  
72          //Build the tree.
73          TreeIterator nlTree = this.buildNaturalLanguageTree(propBuilder, this.getNaturalLanguageUsageId(usageName));
74          this.setTranslatedNaturalLanguage(proposition, usageName, nlTree);
75      }
76  
77      /**
78       * This method sets the natural language descriptions for the given proposition and all its child propositions as
79       * well.
80       *
81       * @param proposition
82       * @param usageName
83       */
84      public void setNaturalLanguageTreeForUsage(PropositionEditor proposition, String usageName){
85          PropositionDefinition.Builder propBuilder = PropositionDefinition.Builder.create(proposition);
86          TreeIterator nlTree = this.buildNaturalLanguageTree(propBuilder, this.getNaturalLanguageUsageId(usageName));
87          this.setTranslatedNaturalLanguage(proposition, usageName, nlTree);
88      }
89  
90      private TreeIterator buildNaturalLanguageTree(PropositionDefinition.Builder propBuilder, String usageId){
91          NaturalLanguageTree nlTree = this.getRuleManagementService().translateNaturalLanguageTreeForProposition(usageId, propBuilder.build(), "en");
92          return new TreeIterator(nlTree);
93      }
94  
95      private void setTranslatedNaturalLanguage(PropositionEditor proposition, String usageName, TreeIterator nlDescriptions){
96          if(!nlDescriptions.hasNext()){
97              return;
98          }
99  
100         proposition.getNaturalLanguage().put(usageName, nlDescriptions.next());
101 
102         if (proposition.getCompoundEditors() != null){
103             for (PropositionEditor child : proposition.getCompoundEditors()){
104                 setTranslatedNaturalLanguage(child, usageName, nlDescriptions);
105             }
106         }
107     }
108 
109     private String getNaturalLanguageUsageId(String usageName){
110         //usageName cannot be null or blank
111         if ((usageName == null) || (StringUtils.isBlank(usageName))){
112             return null;
113         }
114 
115         //Retrieve usageId form map.
116         String usageId = usageMap.get(usageName);
117         if (usageId == null){
118 
119             //Retrieve usage from service if usageId not on map.
120             NaturalLanguageUsage usage = this.getRuleManagementService().getNaturalLanguageUsageByNameAndNamespace(usageName,
121                     PermissionServiceConstants.KS_SYS_NAMESPACE);
122 
123             //Set the usage id to the map.
124             if (usage != null){
125                 usageId = usage.getId();
126                 usageMap.put(usageName, usageId);
127             }
128         }
129         return usageId;
130     }
131 
132     public RuleManagementService getRuleManagementService() {
133         return ruleManagementService;
134     }
135 
136     public void setRuleManagementService(RuleManagementService ruleManagementService) {
137         this.ruleManagementService = ruleManagementService;
138     }
139 
140     /**
141      * Put the natural language descriptions in a queue and retrieve them in
142      * the same order as the propostion tree.
143      */
144     protected class TreeIterator implements Serializable, Iterator<String> {
145 
146         private Queue<String> nl;
147 
148         public TreeIterator(NaturalLanguageTree tree){
149             nl = new LinkedList<String>();
150             this.addToStack(tree);
151         }
152 
153         private void addToStack(NaturalLanguageTree tree){
154             if (tree == null){
155                 return;
156             }
157 
158             nl.offer(tree.getNaturalLanguage());
159             if (tree.getChildren() != null){
160                 for (NaturalLanguageTree child : tree.getChildren()){
161                     addToStack(child);
162                 }
163             }
164         }
165 
166         @Override
167         public boolean hasNext() {
168             return !nl.isEmpty();
169         }
170 
171         @Override
172         public String next() {
173             return nl.poll();
174         }
175 
176         @Override
177         public void remove() {
178             //To change body of implemented methods use File | Settings | File Templates.
179         }
180     }
181 }