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 org.kuali.rice.krad.inquiry.InquirableImpl;
19  import org.kuali.rice.krad.uif.component.Component;
20  import org.kuali.rice.krad.uif.container.Container;
21  import org.kuali.rice.krad.uif.container.Group;
22  import org.kuali.rice.krad.uif.element.Message;
23  import org.kuali.rice.krad.uif.field.SpaceField;
24  import org.kuali.rice.krad.uif.layout.BoxLayoutManager;
25  import org.kuali.rice.krad.uif.layout.GridLayoutManager;
26  import org.kuali.rice.krad.uif.util.ComponentFactory;
27  import org.kuali.rice.krad.uif.view.View;
28  import org.kuali.rice.krad.web.form.InquiryForm;
29  import org.kuali.rice.krms.api.repository.proposition.PropositionType;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /**
35   * This class is responsible for building the recursive components on the Rule Inquiry View.
36   */
37  public class RuleStudentInquiryViewHelperServiceImpl extends InquirableImpl {
38  
39      private static int ID = 1;
40  
41      private static final String PROPOSITION_GROUP_ID = "propositionGroup";
42  
43      private static final String PROPOSITION_ID = "proposition";
44  
45      private enum Operator {
46          OR("|"), AND("&");
47  
48          private String code;
49  
50          Operator(String code) {
51              this.code = code;
52          }
53  
54          private static Operator fromCode(String code) {
55              if (code == null) {
56                  return null;
57              }
58              for (Operator operator : values()) {
59                  if (operator.code.equals(code)) {
60                      return operator;
61                  }
62              }
63              throw new IllegalArgumentException("Failed to locate the Operator with the given code: " + code);
64          }
65      }
66  
67  
68  
69      @Override
70      protected void addCustomContainerComponents(View view, Object model, Container container) {
71          if (PROPOSITION_GROUP_ID.equals(container.getId())) {
72              RuleBo ruleDataObj = (RuleBo)((InquiryForm)model).getDataObject();
73              PropositionBo proposition = ruleDataObj.getProposition();
74              if (proposition != null) {
75                  if (PropositionType.COMPOUND.getCode().equals(proposition.getPropositionTypeCode())) {
76                      List<Component> groups = new ArrayList<Component>();
77                      handleCompoundPropositions(groups, proposition);
78                      container.setItems(groups);
79                  } else {
80                      Message simplePropName = ComponentFactory.getMessage();
81                      simplePropName.setId(PROPOSITION_ID + "_" + ID++);
82                      simplePropName.setMessageText(proposition.getDescription());
83                      List<Message> simpleProps = new ArrayList<Message>();
84                      simpleProps.add(simplePropName);
85                      container.setItems(simpleProps);
86                  }
87              }
88          }
89      }
90  
91      private void handleCompoundPropositions(List<Component> components, PropositionBo proposition) {
92          Group compoundGroup = getPropositionGroup();
93          compoundGroup.setId(String.valueOf(PROPOSITION_GROUP_ID + "_" + ID++));
94          ((GridLayoutManager)compoundGroup.getLayoutManager()).setNumberOfColumns(2);
95  
96          List<Component> componentItems = new ArrayList<Component>();
97  
98          //Heading
99          Message propositionName = ComponentFactory.getMessage();
100         propositionName.setId(PROPOSITION_ID + "_" + ID++);
101         propositionName.setMessageText(proposition.getDescription());
102 
103         componentItems.add(propositionName);
104 
105         //Space (for layout purposes)
106         SpaceField spaceField1 = ComponentFactory.getSpaceField();
107         spaceField1.setId("space" + "_" + ID++);
108         componentItems.add(spaceField1);
109 
110         //Space (for layout purposes)
111         SpaceField spaceField2 = ComponentFactory.getSpaceField();
112         spaceField2.setId("space" + "_" + ID++);
113         componentItems.add(spaceField2);
114 
115         if (proposition.getCompoundComponents() != null) {
116             int loopCounter = 0;
117             for (PropositionBo nestedProposition : proposition.getCompoundComponents()) {
118                 if (loopCounter != 0) {
119 
120                     //Space (for layout purposes)
121                     SpaceField spaceField3 = ComponentFactory.getSpaceField();
122                     spaceField3.setId("space" + "_" + ID++);
123                     componentItems.add(spaceField3);
124 
125                     Message operator = ComponentFactory.getMessage();
126                     operator.setId(PROPOSITION_ID + "_" + ID++);
127                     operator.setMessageText(Operator.fromCode(proposition.getCompoundOpCode()).toString());
128                     componentItems.add(operator);
129 
130                     //Space (for layout purposes)
131                     SpaceField spaceField4 = ComponentFactory.getSpaceField();
132                     spaceField4.setId("space" + "_" + ID++);
133                     componentItems.add(spaceField4);
134                 }
135                 if (PropositionType.COMPOUND.getCode().equals(nestedProposition.getPropositionTypeCode())) {
136                     handleCompoundPropositions(components, nestedProposition);
137                 } else {
138                     Message simplePropName = ComponentFactory.getMessage();
139                     simplePropName.setId(PROPOSITION_ID + "_" + ID++);
140                     simplePropName.setMessageText(nestedProposition.getDescription());
141 
142                     componentItems.add(simplePropName);
143                 }
144                 loopCounter++;
145             }
146         }
147         compoundGroup.setItems(componentItems);
148 
149         components.add(compoundGroup);
150     }
151 
152     private static Group getPropositionGroup() {
153         Group group = (Group) ComponentFactory.getGroupGridBodyOnly();
154         group.getValidationMessages().setId(String.valueOf(ID++));
155         group.getDisclosure().setId(String.valueOf(ID++));
156         return group;
157     }
158 
159 }