View Javadoc

1   /**
2    * Copyright 2005-2012 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.field;
17  
18  import org.apache.commons.lang.StringUtils;
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.AttributeDefinition;
22  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
23  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
24  import org.kuali.rice.krad.uif.component.Component;
25  import org.kuali.rice.krad.uif.control.CheckboxControl;
26  import org.kuali.rice.krad.uif.control.Control;
27  import org.kuali.rice.krad.uif.control.MultiValueControl;
28  import org.kuali.rice.krad.uif.control.RadioGroupControl;
29  import org.kuali.rice.krad.uif.control.TextAreaControl;
30  import org.kuali.rice.krad.uif.util.ComponentFactory;
31  import org.kuali.rice.krad.uif.util.ComponentUtils;
32  import org.kuali.rice.krad.uif.view.View;
33  import org.kuali.rice.krad.util.KRADConstants;
34  import org.kuali.rice.krad.util.KRADPropertyConstants;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  /**
40   * Custom <code>InputField</code> for search fields within a lookup view
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  @BeanTag(name="lookupInputField")
45  public class LookupInputField extends InputField {
46      private static final long serialVersionUID = -8294275596836322699L;
47  
48      private boolean disableWildcardsAndOperators;
49      private boolean addControlSelectAllOption;
50  
51      public LookupInputField() {
52          super();
53  
54          disableWildcardsAndOperators = false;
55          addControlSelectAllOption = false;
56      }
57  
58      /**
59       * The following actions are performed:
60       *
61       * <ul>
62       * <li>Add all option if enabled and control is multi-value</li>
63       * </ul>
64       *
65       * @see org.kuali.rice.krad.uif.component.ComponentBase#performFinalize(org.kuali.rice.krad.uif.view.View,
66       *      java.lang.Object, org.kuali.rice.krad.uif.component.Component)
67       */
68      @Override
69      public void performFinalize(View view, Object model, Component parent) {
70          super.performFinalize(view, model, parent);
71  
72          // add all option
73          if (addControlSelectAllOption && (getControl() != null) && getControl() instanceof MultiValueControl) {
74              MultiValueControl multiValueControl = (MultiValueControl) getControl();
75              if (multiValueControl.getOptions() != null) {
76                  List<KeyValue> fieldOptions = multiValueControl.getOptions();
77                  fieldOptions.add(0, new ConcreteKeyValue("", "All"));
78  
79                  multiValueControl.setOptions(fieldOptions);
80              }
81          }
82      }
83  
84      /**
85       * Override of InputField copy to setup properties necessary to make the field usable for inputting
86       * search criteria
87       *
88       * @param attributeDefinition - AttributeDefinition instance the property values should be copied from
89       * @see DataField#copyFromAttributeDefinition(org.kuali.rice.krad.uif.view.View,
90       * org.kuali.rice.krad.datadictionary.AttributeDefinition)
91       */
92      @Override
93      public void copyFromAttributeDefinition(View view, AttributeDefinition attributeDefinition) {
94          // label
95          if (StringUtils.isEmpty(getLabel())) {
96              setLabel(attributeDefinition.getLabel());
97          }
98  
99          // short label
100         if (StringUtils.isEmpty(getShortLabel())) {
101             setShortLabel(attributeDefinition.getShortLabel());
102         }
103 
104         // security
105         if (getDataFieldSecurity().getAttributeSecurity() == null) {
106             getDataFieldSecurity().setAttributeSecurity(attributeDefinition.getAttributeSecurity());
107         }
108 
109         // options
110         if (getOptionsFinder() == null) {
111             setOptionsFinder(attributeDefinition.getOptionsFinder());
112         }
113 
114         // TODO: what about formatter?
115 
116         // use control from dictionary if not specified and convert for searching
117         if (getControl() == null) {
118             Control control = convertControlToLookupControl(attributeDefinition);
119             view.assignComponentIds(control);
120 
121             setControl(control);
122         }
123 
124         // overwrite maxLength to allow for wildcards and ranges
125         setMaxLength(100);
126 
127         // set default value for active field to true
128         if (StringUtils.isEmpty(getDefaultValue())) {
129             if ((StringUtils.equals(getPropertyName(), KRADPropertyConstants.ACTIVE))) {
130                 setDefaultValue(KRADConstants.YES_INDICATOR_VALUE);
131             }
132         }
133 
134         /*
135            * TODO delyea: FieldUtils.createAndPopulateFieldsForLookup used to allow for a set of property names to be passed in via the URL
136            * parameters of the lookup url to set fields as 'read only'
137            */
138 
139     }
140 
141     /**
142      * If control definition is defined on the given attribute definition, converts to an appropriate control for
143      * searching (if necessary) and returns a copy for setting on the field
144      *
145      * @param attributeDefinition - attribute definition instance to retrieve control from
146      * @return Control instance or null if not found
147      */
148     protected static Control convertControlToLookupControl(AttributeDefinition attributeDefinition) {
149         if (attributeDefinition.getControlField() == null) {
150             return null;
151         }
152 
153         Control newControl = null;
154 
155         // convert checkbox to radio with yes/no/both options
156         if (CheckboxControl.class.isAssignableFrom(attributeDefinition.getControlField().getClass())) {
157             newControl = ComponentFactory.getRadioGroupControlHorizontal();
158             List<KeyValue> options = new ArrayList<KeyValue>();
159             options.add(new ConcreteKeyValue("Y", "Yes"));
160             options.add(new ConcreteKeyValue("N", "No"));
161             options.add(new ConcreteKeyValue("", "Both"));
162 
163             ((RadioGroupControl) newControl).setOptions(options);
164         }
165         // text areas get converted to simple text inputs
166         else if (TextAreaControl.class.isAssignableFrom(attributeDefinition.getControlField().getClass())) {
167             newControl = ComponentFactory.getTextControl();
168         } else {
169             newControl = ComponentUtils.copy(attributeDefinition.getControlField(), "");
170         }
171 
172         return newControl;
173     }
174 
175     /**
176      * @return the treatWildcardsAndOperatorsAsLiteral
177      */
178     @BeanTagAttribute(name="disableWildcardsAndOperators")
179     public boolean isDisableWildcardsAndOperators() {
180         return this.disableWildcardsAndOperators;
181     }
182 
183     /**
184      * @param disableWildcardsAndOperators the treatWildcardsAndOperatorsAsLiteral to set
185      */
186     public void setDisableWildcardsAndOperators(boolean disableWildcardsAndOperators) {
187         this.disableWildcardsAndOperators = disableWildcardsAndOperators;
188     }
189 
190     /**
191      * Indicates whether the option for all values (blank key, 'All' label) should be added to the lookup
192      * field, note this is only supported for {@link org.kuali.rice.krad.uif.control.MultiValueControl} instance
193      *
194      * @return boolean true if all option should be added, false if not
195      */
196     @BeanTagAttribute(name="addControlSelectAllOption")
197     public boolean isAddControlSelectAllOption() {
198         return addControlSelectAllOption;
199     }
200 
201     /**
202      * Setter for the add all option indicator
203      *
204      * @param addControlSelectAllOption
205      */
206     public void setAddControlSelectAllOption(boolean addControlSelectAllOption) {
207         this.addControlSelectAllOption = addControlSelectAllOption;
208     }
209 }