1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.krms.impl.repository; |
18 | |
|
19 | |
|
20 | |
import java.util.Map.Entry |
21 | |
import org.kuali.rice.core.api.util.tree.Node |
22 | |
import org.kuali.rice.core.api.util.tree.Tree |
23 | |
import org.kuali.rice.krad.bo.PersistableBusinessObjectBase |
24 | |
import org.kuali.rice.krms.api.repository.LogicalOperator |
25 | |
import org.kuali.rice.krms.api.repository.action.ActionDefinition |
26 | |
import org.kuali.rice.krms.api.repository.proposition.PropositionType |
27 | |
import org.kuali.rice.krms.api.repository.rule.RuleDefinition |
28 | |
import org.kuali.rice.krms.api.repository.rule.RuleDefinitionContract |
29 | |
import org.kuali.rice.krms.impl.ui.CompoundOpCodeNode |
30 | |
import org.kuali.rice.krms.impl.ui.RuleTreeNode |
31 | |
import org.kuali.rice.krms.impl.ui.SimplePropositionNode |
32 | |
import org.kuali.rice.krms.impl.ui.SimplePropositionEditNode |
33 | |
import org.kuali.rice.krms.impl.ui.CompoundPropositionEditNode |
34 | |
|
35 | |
public class RuleBo extends PersistableBusinessObjectBase implements RuleDefinitionContract { |
36 | |
|
37 | |
String id; |
38 | |
String namespace; |
39 | |
String description; |
40 | |
String name; |
41 | |
String typeId; |
42 | |
String propId; |
43 | |
|
44 | |
PropositionBo proposition; |
45 | |
List<ActionBo> actions; |
46 | |
List<RuleAttributeBo> attributeBos; |
47 | |
|
48 | |
|
49 | |
|
50 | |
Tree<RuleTreeNode, String> propositionTree; |
51 | |
|
52 | |
|
53 | |
String propositionSummary; |
54 | |
private StringBuffer propositionSummaryBuffer; |
55 | |
String selectedPropositionId; |
56 | |
|
57 | |
public RuleBo() { |
58 | 0 | actions = new ArrayList<ActionBo>(); |
59 | 0 | attributeBos = new ArrayList<RuleAttributeBo>(); |
60 | |
} |
61 | |
|
62 | |
public PropositionBo getProposition(){ |
63 | 0 | return proposition; |
64 | |
} |
65 | |
public void setProposition(PropositionBo proposition){ |
66 | 0 | if (proposition != null) { |
67 | 0 | propId = proposition.getId(); |
68 | |
} else { |
69 | 0 | propId = null; |
70 | |
} |
71 | 0 | this.proposition = proposition; |
72 | |
} |
73 | |
|
74 | |
public Map<String, String> getAttributes() { |
75 | 0 | HashMap<String, String> attributes = new HashMap<String, String>(); |
76 | 0 | for (RuleAttributeBo attr : attributeBos) { |
77 | 0 | attributes.put( attr.getAttributeDefinition().getName(), attr.getValue() ); |
78 | |
} |
79 | 0 | return attributes; |
80 | |
} |
81 | |
|
82 | |
public String getPropositionSummary(){ |
83 | 0 | return propositionSummaryBuffer.toString(); |
84 | |
} |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public Tree getPropositionTree() { |
92 | 0 | if (this.propositionTree == null){ |
93 | 0 | this.propositionTree = refreshPropositionTree(false); |
94 | |
} |
95 | |
|
96 | 0 | return this.propositionTree; |
97 | |
} |
98 | |
public void setPropositionTree(Tree<RuleTreeNode, String> tree) { |
99 | 0 | this.propositionTree == tree; |
100 | |
} |
101 | |
|
102 | |
public Tree refreshPropositionTree(Boolean editMode){ |
103 | 0 | Tree myTree = new Tree<RuleTreeNode, String>(); |
104 | |
|
105 | 0 | Node<RuleTreeNode, String> rootNode = new Node<RuleTreeNode, String>(); |
106 | 0 | myTree.setRootElement(rootNode); |
107 | |
|
108 | 0 | propositionSummaryBuffer = new StringBuffer(); |
109 | 0 | PropositionBo prop = this.getProposition(); |
110 | 0 | buildPropTree( rootNode, prop, editMode ); |
111 | 0 | this.propositionTree = myTree; |
112 | 0 | return myTree; |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
private void buildPropTree( Node sprout, PropositionBo prop, Boolean editMode){ |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | 0 | if (prop != null) { |
130 | 0 | if (PropositionType.SIMPLE.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())){ |
131 | |
|
132 | |
|
133 | 0 | Node<RuleTreeNode, String> child = new Node<RuleTreeNode, String>(); |
134 | 0 | child.setNodeLabel(prop.getDescription()); |
135 | 0 | if (prop.getEditMode()){ |
136 | 0 | child.setNodeLabel(""); |
137 | 0 | child.setNodeType(SimplePropositionEditNode.NODE_TYPE); |
138 | 0 | SimplePropositionEditNode pNode = new SimplePropositionEditNode(prop); |
139 | 0 | child.setData(pNode); |
140 | |
} else { |
141 | 0 | child.setNodeType(SimplePropositionNode.NODE_TYPE); |
142 | 0 | SimplePropositionNode pNode = new SimplePropositionNode(prop); |
143 | 0 | child.setData(pNode); |
144 | |
} |
145 | 0 | sprout.getChildren().add(child); |
146 | 0 | propositionSummaryBuffer.append(prop.getParameterDisplayString()) |
147 | |
} |
148 | 0 | else if (PropositionType.COMPOUND.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())){ |
149 | |
|
150 | 0 | propositionSummaryBuffer.append(" ( "); |
151 | 0 | Node<RuleTreeNode, String> aNode = new Node<RuleTreeNode, String>(); |
152 | 0 | aNode.setNodeLabel(prop.getDescription()); |
153 | |
|
154 | 0 | if (prop.getEditMode()){ |
155 | 0 | aNode.setNodeLabel(""); |
156 | 0 | aNode.setNodeType("ruleTreeNode compoundNode editNode"); |
157 | 0 | CompoundPropositionEditNode pNode = new CompoundPropositionEditNode(prop); |
158 | 0 | aNode.setData(pNode); |
159 | |
} else { |
160 | 0 | aNode.setNodeType("ruleTreeNode compoundNode"); |
161 | 0 | RuleTreeNode pNode = new RuleTreeNode(prop); |
162 | 0 | aNode.setData(pNode); |
163 | |
} |
164 | 0 | sprout.getChildren().add(aNode); |
165 | |
|
166 | 0 | boolean first = true; |
167 | 0 | List <PropositionBo> allMyChildren = prop.getCompoundComponents(); |
168 | 0 | for (PropositionBo child : allMyChildren){ |
169 | |
|
170 | 0 | if (!first){ |
171 | 0 | addOpCodeNode(aNode, prop); |
172 | |
} |
173 | 0 | first = false; |
174 | |
|
175 | 0 | buildPropTree(aNode, child, editMode); |
176 | |
} |
177 | 0 | propositionSummaryBuffer.append(" ) "); |
178 | |
} |
179 | |
} |
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
private void addOpCodeNode(Node currentNode, PropositionBo prop){ |
191 | 0 | String opCodeLabel = ""; |
192 | |
|
193 | 0 | if (LogicalOperator.AND.getCode().equalsIgnoreCase(prop.getCompoundOpCode())){ |
194 | 0 | opCodeLabel = "AND"; |
195 | 0 | } else if (LogicalOperator.OR.getCode().equalsIgnoreCase(prop.getCompoundOpCode())){ |
196 | 0 | opCodeLabel = "OR"; |
197 | |
} |
198 | 0 | propositionSummaryBuffer.append(" "+opCodeLabel+" "); |
199 | 0 | Node<RuleTreeNode, String> aNode = new Node<RuleTreeNode, String>(); |
200 | 0 | aNode.setNodeLabel(""); |
201 | 0 | aNode.setNodeType("ruleTreeNode compoundOpCodeNode"); |
202 | 0 | aNode.setData(new CompoundOpCodeNode(prop)); |
203 | 0 | currentNode.getChildren().add(aNode); |
204 | |
} |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
static RuleDefinition to(RuleBo bo) { |
214 | 0 | if (bo == null) { return null; } |
215 | 0 | return org.kuali.rice.krms.api.repository.rule.RuleDefinition.Builder.create(bo).build(); |
216 | |
} |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
static RuleBo from(RuleDefinition im) { |
224 | 0 | if (im == null) { return null; } |
225 | |
|
226 | 0 | RuleBo bo = new RuleBo(); |
227 | 0 | bo.id = im.getId(); |
228 | 0 | bo.namespace = im.getNamespace(); |
229 | 0 | bo.name = im.getName(); |
230 | 0 | bo.description = im.getDescription(); |
231 | 0 | bo.typeId = im.getTypeId(); |
232 | 0 | bo.propId = im.getPropId(); |
233 | 0 | bo.proposition = PropositionBo.from(im.getProposition()); |
234 | 0 | bo.versionNumber = im.getVersionNumber(); |
235 | |
|
236 | 0 | bo.actions = new ArrayList<ActionBo>(); |
237 | 0 | for (ActionDefinition action : im.getActions()){ |
238 | 0 | bo.actions.add( ActionBo.from(action) ); |
239 | |
} |
240 | |
|
241 | |
|
242 | 0 | List<RuleAttributeBo> attrs = new ArrayList<RuleAttributeBo>(); |
243 | |
|
244 | |
|
245 | 0 | RuleAttributeBo attributeBo; |
246 | 0 | for (Entry<String,String> entry : im.getAttributes().entrySet()){ |
247 | 0 | KrmsAttributeDefinitionBo attrDefBo = KrmsRepositoryServiceLocator |
248 | |
.getKrmsAttributeDefinitionService() |
249 | 0 | .getKrmsAttributeBo(entry.getKey(), im.getNamespace()); |
250 | 0 | attributeBo = new RuleAttributeBo(); |
251 | 0 | attributeBo.setRuleId( im.getId() ); |
252 | 0 | attributeBo.setAttributeDefinitionId( attrDefBo.getId() ); |
253 | 0 | attributeBo.setValue( entry.getValue() ); |
254 | 0 | attributeBo.setAttributeDefinition( attrDefBo ); |
255 | 0 | attrs.add( attributeBo ); |
256 | |
} |
257 | 0 | bo.setAttributeBos(attrs); |
258 | |
|
259 | 0 | return bo; |
260 | |
} |
261 | |
|
262 | |
} |