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