1   package org.kuali.rice.krad.uif.field;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.rice.core.api.util.ConcreteKeyValue;
5   import org.kuali.rice.krad.datadictionary.AttributeDefinition;
6   import org.kuali.rice.krad.datadictionary.parse.BeanTag;
7   import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
8   import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
9   import org.kuali.rice.krad.uif.UifConstants;
10  import org.kuali.rice.krad.uif.component.Component;
11  import org.kuali.rice.krad.uif.control.*;
12  import org.kuali.rice.krad.uif.element.Message;
13  import org.kuali.rice.krad.uif.util.ComponentFactory;
14  import org.kuali.rice.krad.uif.util.ComponentUtils;
15  import org.kuali.rice.krad.uif.util.KeyMessage;
16  import org.kuali.rice.krad.uif.view.View;
17  import org.kuali.rice.krad.util.KRADConstants;
18  import org.kuali.rice.krad.util.KRADPropertyConstants;
19  
20  import java.util.Map;
21  
22  
23  
24  
25  
26  
27  
28  
29  @BeanTag(name = "lookupCriteriaInputField-bean", parent = "Uif-LookupCriteriaInputField")
30  public class LookupInputField extends InputField {
31      private static final long serialVersionUID = -8294275596836322699L;
32  
33      public static final String CHECKBOX_CONVERTED_RADIO_CONTROL = "Uif-CheckboxConvertedRadioControl";
34  
35      private boolean disableWildcardsAndOperators;
36      private boolean addControlSelectAllOption;
37      private boolean triggerOnChange;
38      private boolean ranged;
39  
40      public LookupInputField() {
41          super();
42  
43          disableWildcardsAndOperators = false;
44          addControlSelectAllOption = false;
45          setTriggerOnChange(false);
46      }
47  
48      
49  
50  
51  
52  
53  
54  
55  
56  
57  
58      @Override
59      public void performFinalize(View view, Object model, Component parent) {
60          super.performFinalize(view, model, parent);
61  
62          
63          if (addControlSelectAllOption && (getControl() != null) && getControl() instanceof MultiValueControl) {
64              String allOptionText = KRADServiceLocatorWeb.getMessageService().getMessageText(
65                      UifConstants.MessageKeys.OPTION_ALL);
66  
67              MultiValueControl multiValueControl = (MultiValueControl) getControl();
68              if (multiValueControl.getOptions() != null) {
69                  multiValueControl.getOptions().add(0, new ConcreteKeyValue("", allOptionText));
70              }
71  
72              if (multiValueControl.getRichOptions() != null) {
73                  Message message = ComponentFactory.getMessage();
74  
75                  view.assignComponentIds(message);
76                  message.setMessageText(allOptionText);
77                  message.setGenerateSpan(false);
78  
79                  multiValueControl.getRichOptions().add(0, new KeyMessage("", allOptionText, message));
80              }
81          }
82      }
83  
84      
85  
86  
87  
88  
89  
90  
91  
92      @Override
93      public void copyFromAttributeDefinition(View view, AttributeDefinition attributeDefinition) {
94          
95          if (StringUtils.isEmpty(getLabel())) {
96              setLabel(attributeDefinition.getLabel());
97          }
98  
99          
100         if (StringUtils.isEmpty(getShortLabel())) {
101             setShortLabel(attributeDefinition.getShortLabel());
102         }
103 
104         
105         if (getDataFieldSecurity().getAttributeSecurity() == null) {
106             getDataFieldSecurity().setAttributeSecurity(attributeDefinition.getAttributeSecurity());
107         }
108 
109         
110         if (getOptionsFinder() == null) {
111             setOptionsFinder(attributeDefinition.getOptionsFinder());
112         }
113 
114         
115 
116         
117         if (getControl() == null) {
118             Control control = convertControlToLookupControl(attributeDefinition);
119             view.assignComponentIds(control);
120 
121             setControl(control);
122         }
123 
124         
125         setMaxLength(100);
126         
127 
128         if ( attributeDefinition.getMaxLength()!=null && (attributeDefinition.getMaxLength() > 100)) {
129             setMaxLength(attributeDefinition.getMaxLength());
130         }
131 
132         
133         if (StringUtils.isEmpty(getDefaultValue())) {
134             if ((StringUtils.equals(getPropertyName(), KRADPropertyConstants.ACTIVE))) {
135                 setDefaultValue(KRADConstants.YES_INDICATOR_VALUE);
136             }
137         }
138 
139         
140 
141 
142 
143 
144     }
145 
146     
147 
148 
149 
150 
151 
152 
153     protected static Control convertControlToLookupControl(AttributeDefinition attributeDefinition) {
154         if (attributeDefinition.getControlField() == null) {
155             return null;
156         }
157 
158         Control newControl = null;
159 
160         
161         if (CheckboxControl.class.isAssignableFrom(attributeDefinition.getControlField().getClass())) {
162             newControl = getCheckboxConvertedRadioControl();
163         }
164         
165         else if (TextAreaControl.class.isAssignableFrom(attributeDefinition.getControlField().getClass())) {
166             newControl = ComponentFactory.getTextControl();
167         } else {
168             newControl = ComponentUtils.copy(attributeDefinition.getControlField(), "");
169         }
170 
171         return newControl;
172     }
173 
174     
175 
176 
177     @BeanTagAttribute(name = "disableWildcardsAndOperators")
178     public boolean isDisableWildcardsAndOperators() {
179         return this.disableWildcardsAndOperators;
180     }
181 
182     
183 
184 
185     public void setDisableWildcardsAndOperators(boolean disableWildcardsAndOperators) {
186         this.disableWildcardsAndOperators = disableWildcardsAndOperators;
187     }
188 
189     
190 
191 
192 
193 
194 
195     @BeanTagAttribute(name = "addControlSelectAllOption")
196     public boolean isAddControlSelectAllOption() {
197         return addControlSelectAllOption;
198     }
199 
200     
201 
202 
203 
204 
205     public void setAddControlSelectAllOption(boolean addControlSelectAllOption) {
206         this.addControlSelectAllOption = addControlSelectAllOption;
207     }
208 
209     
210 
211 
212 
213 
214     public boolean isTriggerOnChange() {
215         return triggerOnChange;
216     }
217 
218     
219 
220 
221 
222 
223     public void setTriggerOnChange(boolean triggerOnChange) {
224         this.triggerOnChange = triggerOnChange;
225     }
226 
227     
228 
229 
230 
231 
232     public boolean isRanged() {
233         return ranged;
234     }
235 
236     
237 
238 
239 
240 
241     public void setRanged(boolean ranged) {
242         this.ranged = ranged;
243     }
244 
245     
246 
247 
248 
249 
250 
251     public Map<String, String> filterSearchCriteria(Map<String, String> searchCriteria) {
252         if (getControl() instanceof FilterableLookupCriteriaControl) {
253             return ((FilterableLookupCriteriaControl) getControl()).filterSearchCriteria(getPropertyName(),
254                     searchCriteria);
255         } else {
256             return searchCriteria;
257         }
258     }
259 
260     
261 
262 
263     @Override
264     protected <T> void copyProperties(T component) {
265         super.copyProperties(component);
266         LookupInputField lookupInputFieldCopy = (LookupInputField) component;
267         lookupInputFieldCopy.setDisableWildcardsAndOperators(this.disableWildcardsAndOperators);
268         lookupInputFieldCopy.setAddControlSelectAllOption(this.addControlSelectAllOption);
269         lookupInputFieldCopy.setTriggerOnChange(this.triggerOnChange);
270         lookupInputFieldCopy.setRanged(this.ranged);
271     }
272 
273     
274 
275 
276 
277 
278 
279     private static RadioGroupControl getCheckboxConvertedRadioControl() {
280         return (RadioGroupControl) ComponentFactory.getNewComponentInstance(CHECKBOX_CONVERTED_RADIO_CONTROL);
281     }
282 }
283