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.util;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
20  import org.kuali.rice.krad.uif.UifConstants;
21  import org.kuali.rice.krad.uif.component.BindingInfo;
22  import org.kuali.rice.krad.uif.component.Component;
23  import org.kuali.rice.krad.uif.container.CollectionGroup;
24  import org.kuali.rice.krad.uif.container.Group;
25  import org.kuali.rice.krad.uif.container.GroupBase;
26  import org.kuali.rice.krad.uif.field.DataField;
27  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
28  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleRestriction;
29  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleUtils;
30  import org.kuali.rice.krad.uif.util.LifecycleElement;
31  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
32  import org.kuali.rice.krad.uif.view.View;
33  import org.kuali.rice.krms.dto.AgendaEditor;
34  import org.kuali.rice.krms.service.RuleViewHelperService;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  /**
40   * @author Kuali Student Team
41   */
42  public class AgendaSection extends GroupBase {
43  
44      private String propertyName;
45      private BindingInfo bindingInfo;
46  
47      private Group agendaPrototype;
48      private Group rulePrototype;
49  
50      private AgendaBuilder agendaBuilder;
51  
52      public AgendaSection(){
53          super();
54      }
55  
56      @Override
57      public void performInitialization(Object model) {
58          setFieldBindingObjectPath(getBindingInfo().getBindingObjectPath());
59  
60          super.performInitialization(model);
61  
62          View view = ViewLifecycle.getView();
63  
64          if (bindingInfo != null) {
65              bindingInfo.setDefaults(view, getPropertyName());
66          }
67  
68          setCollectionPath();
69  
70      }
71  
72      protected void setCollectionPath() {
73          // set static collection path on items
74          String collectionPath = "";
75          if (StringUtils.isNotBlank(getBindingInfo().getCollectionPath())) {
76              collectionPath += getBindingInfo().getCollectionPath() + ".";
77          }
78          if (StringUtils.isNotBlank(getBindingInfo().getBindByNamePrefix())) {
79              collectionPath += getBindingInfo().getBindByNamePrefix() + ".";
80          }
81          collectionPath += getBindingInfo().getBindingName();
82  
83          List<DataField> agendafields = ViewLifecycleUtils.getElementsOfTypeDeep(agendaPrototype, DataField.class);
84          for (DataField collectionField : agendafields) {
85              collectionField.getBindingInfo().setCollectionPath(collectionPath);
86          }
87  
88          List<DataField> rulefields = ViewLifecycleUtils.getElementsOfTypeDeep(rulePrototype, DataField.class);
89          for (DataField collectionField : rulefields) {
90              collectionField.getBindingInfo().setCollectionPath(collectionPath);
91          }
92      }
93  
94      @Override
95      public void performApplyModel(Object model, LifecycleElement parent) {
96          // get the collection for this group from the model
97          List<Object> modelCollection = ObjectPropertyUtils.getPropertyValue(model,
98                  this.getBindingInfo().getBindingPath());
99          if (null == modelCollection)
100         {
101             modelCollection = new ArrayList<Object>();
102         }
103         View view = ViewLifecycle.getView();
104 
105         //Set the ruleviewhelperservice on the agendabuilder.
106         this.getAgendaBuilder().setViewHelperService((RuleViewHelperService) view.getViewHelperService());
107 
108         // create agenda sections for each agenda
109         List<Component> items = new ArrayList<Component>();
110         for (int index = 0; index < modelCollection.size(); index++) {
111             AgendaEditor agenda = (AgendaEditor) modelCollection.get(index);
112             items.add(this.getAgendaBuilder().buildAgenda(agenda, index, this));
113         }
114         this.setItems(items);
115 
116         super.performApplyModel(model, parent);
117     }
118 
119 
120     public AgendaBuilder getAgendaBuilder() {
121         if (this.agendaBuilder == null) {
122             this.agendaBuilder = new AgendaBuilder();
123         }
124         return this.agendaBuilder;
125     }
126 
127     public String getPropertyName() {
128         return propertyName;
129     }
130 
131     public void setPropertyName(String propertyName) {
132         this.propertyName = propertyName;
133     }
134 
135     public BindingInfo getBindingInfo() {
136         return bindingInfo;
137     }
138 
139     public void setBindingInfo(BindingInfo bindingInfo) {
140         this.bindingInfo = bindingInfo;
141     }
142 
143     @ViewLifecycleRestriction(UifConstants.ViewPhases.INITIALIZE)
144     @BeanTagAttribute(name = "agendaPrototype", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
145     public Group getAgendaPrototype() {
146         return agendaPrototype;
147     }
148 
149     public void setAgendaPrototype(Group agendaPrototype) {
150         this.agendaPrototype = agendaPrototype;
151     }
152 
153     @ViewLifecycleRestriction(UifConstants.ViewPhases.INITIALIZE)
154     @BeanTagAttribute(name = "rulePrototype", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
155     public Group getRulePrototype() {
156         return rulePrototype;
157     }
158 
159     public void setRulePrototype(Group rulePrototype) {
160         this.rulePrototype = rulePrototype;
161     }
162 }