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