View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krms.impl.repository;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.kuali.rice.krad.inquiry.InquirableImpl;
22  import org.kuali.rice.krad.uif.component.Component;
23  import org.kuali.rice.krad.uif.container.Container;
24  import org.kuali.rice.krad.uif.container.Group;
25  import org.kuali.rice.krad.uif.element.Message;
26  import org.kuali.rice.krad.uif.field.SpaceField;
27  import org.kuali.rice.krad.uif.layout.GridLayoutManager;
28  import org.kuali.rice.krad.uif.util.ComponentFactory;
29  import org.kuali.rice.krad.web.form.InquiryForm;
30  import org.kuali.rice.krms.api.repository.proposition.PropositionType;
31  
32  /**
33   * This class is responsible for building the recursive components on the Rule Inquiry View.
34   */
35  public class RuleStudentInquiryViewHelperServiceImpl extends InquirableImpl {
36  
37      private static int ID = 1;
38  
39      private static final String PROPOSITION_GROUP_ID = "propositionGroup";
40  
41      private static final String PROPOSITION_ID = "proposition";
42  
43      private enum Operator {
44          OR("|"), AND("&");
45  
46          private String code;
47  
48          Operator(String code) {
49              this.code = code;
50          }
51  
52          private static Operator fromCode(String code) {
53              if (code == null) {
54                  return null;
55              }
56              for (Operator operator : values()) {
57                  if (operator.code.equals(code)) {
58                      return operator;
59                  }
60              }
61              throw new IllegalArgumentException("Failed to locate the Operator with the given code: " + code);
62          }
63      }
64  
65  
66  
67      @Override
68      public void addCustomContainerComponents(Object model, Container container) {
69          if (PROPOSITION_GROUP_ID.equals(container.getId())) {
70              RuleBo ruleDataObj = (RuleBo)((InquiryForm)model).getDataObject();
71              PropositionBo proposition = ruleDataObj.getProposition();
72              if (proposition != null) {
73                  if (PropositionType.COMPOUND.getCode().equals(proposition.getPropositionTypeCode())) {
74                      List<Component> groups = new ArrayList<Component>();
75                      handleCompoundPropositions(groups, proposition);
76                      container.setItems(groups);
77                  } else {
78                      Message simplePropName = ComponentFactory.getMessage();
79                      simplePropName.setId(PROPOSITION_ID + "_" + ID++);
80                      simplePropName.setMessageText(proposition.getDescription());
81                      List<Message> simpleProps = new ArrayList<Message>();
82                      simpleProps.add(simplePropName);
83                      container.setItems(simpleProps);
84                  }
85              }
86          }
87      }
88  
89      private void handleCompoundPropositions(List<Component> components, PropositionBo proposition) {
90          Group compoundGroup = getPropositionGroup();
91          compoundGroup.setId(String.valueOf(PROPOSITION_GROUP_ID + "_" + ID++));
92          ((GridLayoutManager)compoundGroup.getLayoutManager()).setNumberOfColumns(2);
93  
94          List<Component> componentItems = new ArrayList<Component>();
95  
96          //Heading
97          Message propositionName = ComponentFactory.getMessage();
98          propositionName.setId(PROPOSITION_ID + "_" + ID++);
99          propositionName.setMessageText(proposition.getDescription());
100 
101         componentItems.add(propositionName);
102 
103         //Space (for layout purposes)
104         SpaceField spaceField1 = ComponentFactory.getSpaceField();
105         spaceField1.setId("space" + "_" + ID++);
106         componentItems.add(spaceField1);
107 
108         //Space (for layout purposes)
109         SpaceField spaceField2 = ComponentFactory.getSpaceField();
110         spaceField2.setId("space" + "_" + ID++);
111         componentItems.add(spaceField2);
112 
113         if (proposition.getCompoundComponents() != null) {
114             int loopCounter = 0;
115             for (PropositionBo nestedProposition : proposition.getCompoundComponents()) {
116                 if (loopCounter != 0) {
117 
118                     //Space (for layout purposes)
119                     SpaceField spaceField3 = ComponentFactory.getSpaceField();
120                     spaceField3.setId("space" + "_" + ID++);
121                     componentItems.add(spaceField3);
122 
123                     Message operator = ComponentFactory.getMessage();
124                     operator.setId(PROPOSITION_ID + "_" + ID++);
125                     operator.setMessageText(Operator.fromCode(proposition.getCompoundOpCode()).toString());
126                     componentItems.add(operator);
127 
128                     //Space (for layout purposes)
129                     SpaceField spaceField4 = ComponentFactory.getSpaceField();
130                     spaceField4.setId("space" + "_" + ID++);
131                     componentItems.add(spaceField4);
132                 }
133                 if (PropositionType.COMPOUND.getCode().equals(nestedProposition.getPropositionTypeCode())) {
134                     handleCompoundPropositions(components, nestedProposition);
135                 } else {
136                     Message simplePropName = ComponentFactory.getMessage();
137                     simplePropName.setId(PROPOSITION_ID + "_" + ID++);
138                     simplePropName.setMessageText(nestedProposition.getDescription());
139 
140                     componentItems.add(simplePropName);
141                 }
142                 loopCounter++;
143             }
144         }
145         compoundGroup.setItems(componentItems);
146 
147         components.add(compoundGroup);
148     }
149 
150     private static Group getPropositionGroup() {
151         Group group = (Group) ComponentFactory.getGroupGridBodyOnly();
152         group.getDisclosure().setId(String.valueOf(ID++));
153         return group;
154     }
155 
156 }