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.krad.uif.control;
17  
18  import com.google.common.collect.Lists;
19  import org.kuali.rice.core.api.util.ConcreteKeyValue;
20  import org.kuali.rice.core.api.util.KeyValue;
21  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
22  import org.kuali.rice.krad.uif.UifConstants;
23  import org.kuali.rice.krad.uif.component.Component;
24  import org.kuali.rice.krad.uif.container.Container;
25  import org.kuali.rice.krad.uif.element.Message;
26  import org.kuali.rice.krad.uif.field.InputField;
27  import org.kuali.rice.krad.uif.view.ExpressionEvaluator;
28  import org.kuali.rice.krad.uif.util.ComponentFactory;
29  import org.kuali.rice.krad.uif.util.ExpressionUtils;
30  import org.kuali.rice.krad.uif.util.KeyMessage;
31  import org.kuali.rice.krad.uif.util.UrlInfo;
32  import org.kuali.rice.krad.uif.util.UifKeyValueLocation;
33  import org.kuali.rice.krad.uif.view.View;
34  
35  import java.util.ArrayList;
36  import java.util.List;
37  
38  /**
39   * Base class for controls that accept/display multiple values
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  public abstract class MultiValueControlBase extends ControlBase implements MultiValueControl {
44      private static final long serialVersionUID = -8691367056245775455L;
45  
46      private List<KeyValue> options;
47      private List<KeyMessage> richOptions;
48      private List<Component> inlineComponents;
49  
50      private boolean locationSelect = false;
51  
52      public MultiValueControlBase() {
53          super();
54      }
55  
56      /**
57       * Process rich message content that may be in the options, by creating and initializing the richOptions
58       *
59       * @see org.kuali.rice.krad.uif.component.ComponentBase#performApplyModel(org.kuali.rice.krad.uif.view.View,
60       *      Object, org.kuali.rice.krad.uif.component.Component)
61       */
62      @Override
63      public void performApplyModel(View view, Object model, Component parent) {
64          super.performApplyModel(view, model, parent);
65  
66          if (options != null && richOptions == null) {
67              richOptions = new ArrayList<KeyMessage>();
68  
69              for (KeyValue option : options) {
70                  Message message = ComponentFactory.getMessage();
71                  view.assignComponentIds(message);
72                  message.setMessageText(option.getValue());
73                  message.setInlineComponents(inlineComponents);
74                  message.setGenerateSpan(false);
75  
76                  view.getViewHelperService().performComponentInitialization(view, model, message);
77                  richOptions.add(new KeyMessage(option.getKey(), option.getValue(), message));
78              }
79          }
80      }
81  
82      /**
83       * Adds appropriate parent data to inputs internal to the controls that may be in rich content of options
84       *
85       * @see org.kuali.rice.krad.uif.component.Component#performFinalize(org.kuali.rice.krad.uif.view.View, Object,
86       *      org.kuali.rice.krad.uif.component.Component)
87       */
88      @Override
89      public void performFinalize(View view, Object model, Component parent) {
90          super.performFinalize(view, model, parent);
91  
92          ExpressionEvaluator expressionEvaluator =
93                  view.getViewHelperService().getExpressionEvaluator();
94  
95          if (options != null && !options.isEmpty()) {
96              for (KeyValue option : options) {
97                  if (option instanceof UifKeyValueLocation) {
98                      locationSelect = true;
99  
100                     UrlInfo url = ((UifKeyValueLocation) option).getLocation();
101 
102                     ExpressionUtils.populatePropertyExpressionsFromGraph(url, false);
103                     expressionEvaluator.evaluateExpressionsOnConfigurable(view, url, view.getContext());
104                 }
105             }
106         }
107 
108         if (richOptions == null || richOptions.isEmpty()) {
109             return;
110         }
111 
112         //Messages included in options which have have rich message content need to be aware of their parent for
113         //validation purposes
114         for (KeyMessage richOption : richOptions) {
115             List<Component> components = richOption.getMessage().getMessageComponentStructure();
116 
117             if (components != null && !components.isEmpty()) {
118                 for (Component c : components) {
119                     if (c instanceof Container || c instanceof InputField) {
120                         c.addDataAttribute(UifConstants.DataAttributes.PARENT, parent.getId());
121                     }
122                 }
123             }
124         }
125 
126     }
127 
128     /**
129      * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle()
130      */
131     @Override
132     public List<Component> getComponentsForLifecycle() {
133         List<Component> components = super.getComponentsForLifecycle();
134 
135         if (richOptions != null) {
136             for (KeyMessage richOption : richOptions) {
137                 components.add(richOption.getMessage());
138             }
139         }
140 
141         return components;
142     }
143 
144     /**
145      * @see MultiValueControl#getOptions()
146      */
147     @BeanTagAttribute(name = "options", type = BeanTagAttribute.AttributeType.LISTBEAN)
148     public List<KeyValue> getOptions() {
149         return this.options;
150     }
151 
152     /**
153      * @see MultiValueControl#setOptions(java.util.List<org.kuali.rice.core.api.util.KeyValue>)
154      */
155     public void setOptions(List<KeyValue> options) {
156         this.options = options;
157     }
158 
159     /**
160      * Gets the inlineComponents which represent components that can be referenced in an option's value
161      * by index
162      *
163      * @return the components that can be used in rich values of options
164      */
165     @BeanTagAttribute(name = "inlineComponents", type = BeanTagAttribute.AttributeType.LISTBEAN)
166     public List<Component> getInlineComponents() {
167         return inlineComponents;
168     }
169 
170     /**
171      * Sets the inlineComponents which represent components that can be referenced in an option's value
172      * by index
173      *
174      * @param inlineComponents
175      */
176     public void setInlineComponents(List<Component> inlineComponents) {
177         this.inlineComponents = inlineComponents;
178     }
179 
180     /**
181      * @see MultiValueControl#getRichOptions()
182      */
183     public List<KeyMessage> getRichOptions() {
184         return richOptions;
185     }
186 
187     /**
188      * Sets the richOptions.  This will always override/ignore options if set.
189      *
190      * <p><b>Messages MUST be defined</b> when using this setter, do not use this setter for most cases
191      * as setting options through setOptions, with a richMessage value, is appropriate in MOST cases.  This
192      * setter is only available for full control.</p>
193      *
194      * @param richOptions with their messages predefined
195      */
196     public void setRichOptions(List<KeyMessage> richOptions) {
197         this.richOptions = richOptions;
198     }
199 
200     /**
201      * If true, this select represents a location select (navigate on select of option)
202      *
203      * @return true if this is a location select
204      */
205     public boolean isLocationSelect() {
206         return locationSelect;
207     }
208 
209     public void setLocationSelect(boolean locationSelect) {
210         this.locationSelect = locationSelect;
211     }
212 
213     /**
214      * @see org.kuali.rice.krad.uif.component.ComponentBase#copy()
215      */
216     @Override
217     protected <T> void copyProperties(T component) {
218         super.copyProperties(component);
219         MultiValueControlBase multiValueControlBaseCopy = (MultiValueControlBase) component;
220 
221         if(options != null) {
222             List<KeyValue> optionsCopy = Lists.newArrayListWithExpectedSize(options.size());
223             for(KeyValue option : options)   {
224                 KeyValue keyValue = null;
225                 keyValue = new ConcreteKeyValue(option.getKey(), option.getValue());
226                 optionsCopy.add(keyValue);
227             }
228             multiValueControlBaseCopy.setOptions(optionsCopy);
229         }
230 
231         if(richOptions != null) {
232             List<KeyMessage> richOptionsCopy = Lists.newArrayListWithExpectedSize(richOptions.size());
233             for(KeyMessage richOption : richOptions)   {
234                 KeyMessage keyMessage = new KeyMessage(richOption.getKey(),richOption.getValue(),richOption.getMessage());
235                 richOptionsCopy.add(keyMessage);
236             }
237             multiValueControlBaseCopy.setRichOptions(richOptionsCopy);
238         }
239 
240         if(inlineComponents != null) {
241             List<Component> inlineComponentsCopy = Lists.newArrayListWithExpectedSize(inlineComponents.size());
242             for(Component inlineComponent : inlineComponents)   {
243                 inlineComponentsCopy.add((Component)inlineComponent.copy());
244             }
245             multiValueControlBaseCopy.setInlineComponents(inlineComponentsCopy);
246         }
247 
248         multiValueControlBaseCopy.setLocationSelect(this.isLocationSelect());
249     }
250 }