KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > repo > component > UISearchCustomProperties


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.ui.repo.component;
18
19 import java.io.IOException JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.faces.component.NamingContainer;
23 import javax.faces.component.UIComponent;
24 import javax.faces.component.UIInput;
25 import javax.faces.component.UIOutput;
26 import javax.faces.component.UIPanel;
27 import javax.faces.component.UISelectBoolean;
28 import javax.faces.context.FacesContext;
29 import javax.faces.context.ResponseWriter;
30 import javax.faces.el.ValueBinding;
31
32 import org.alfresco.error.AlfrescoRuntimeException;
33 import org.alfresco.service.cmr.dictionary.AspectDefinition;
34 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
35 import org.alfresco.service.cmr.dictionary.DictionaryException;
36 import org.alfresco.service.cmr.dictionary.DictionaryService;
37 import org.alfresco.service.cmr.dictionary.PropertyDefinition;
38 import org.alfresco.service.cmr.dictionary.TypeDefinition;
39 import org.alfresco.service.namespace.QName;
40 import org.alfresco.web.app.Application;
41 import org.alfresco.web.bean.repository.Repository;
42 import org.alfresco.web.config.AdvancedSearchConfigElement;
43 import org.alfresco.web.config.AdvancedSearchConfigElement.CustomProperty;
44 import org.alfresco.web.ui.common.ComponentConstants;
45 import org.alfresco.web.ui.common.Utils;
46 import org.alfresco.web.ui.common.component.SelfRenderingComponent;
47 import org.alfresco.web.ui.repo.RepoConstants;
48 import org.apache.commons.logging.Log;
49 import org.apache.commons.logging.LogFactory;
50
51 /**
52  * @author Kevin Roast
53  */

54 public class UISearchCustomProperties extends SelfRenderingComponent implements NamingContainer
55 {
56    public static final String JavaDoc PREFIX_DATE_TO = "to_";
57    public static final String JavaDoc PREFIX_DATE_FROM = "from_";
58    
59    private static final String JavaDoc MSG_TO = "to";
60    private static final String JavaDoc MSG_FROM = "from";
61    
62    private static Log logger = LogFactory.getLog(UISearchCustomProperties.class);
63    
64    
65    // ------------------------------------------------------------------------------
66
// Component implementation
67

68    /**
69     * @see javax.faces.component.UIComponent#getFamily()
70     */

71    public String JavaDoc getFamily()
72    {
73       return "org.alfresco.faces.AdvancedSearch";
74    }
75
76    /**
77     * @see javax.faces.component.UIComponentBase#encodeBegin(javax.faces.context.FacesContext)
78     */

79    public void encodeBegin(FacesContext context) throws IOException JavaDoc
80    {
81       if (isRendered() == false)
82       {
83          return;
84       }
85       
86       ResponseWriter out = context.getResponseWriter();
87       
88       if (getChildCount() == 0)
89       {
90          createComponentsFromConfig(context);
91       }
92       
93       // encode the components in a 2 column table
94
out.write("<table cellspacing=2 cellpadding=2 border=0");
95       outputAttribute(out, getAttributes().get("styleClass"), "class");
96       outputAttribute(out, getAttributes().get("style"), "style");
97       out.write('>');
98       List JavaDoc<UIComponent> children = getChildren();
99       int colCounter = 0;
100       for (int i=0; i<children.size(); i++)
101       {
102          UIComponent component = children.get(i);
103          if (component instanceof UIPanel)
104          {
105             out.write("<tr><td colspan=2>");
106             Utils.encodeRecursive(context, component);
107             out.write("</td></tr>");
108             colCounter += 2;
109          }
110          else
111          {
112             if ((colCounter & 1) == 0)
113             {
114                out.write("<tr>");
115             }
116             out.write("<td>");
117             Utils.encodeRecursive(context, component);
118             out.write("</td>");
119             if ((colCounter & 1) == 1)
120             {
121                out.write("</tr>");
122             }
123             colCounter++;
124          }
125       }
126       out.write("</table>");
127    }
128    
129    /**
130     * Build the components from the Advanced Search config entries
131     *
132     * @param context FacesContext
133     */

134    private void createComponentsFromConfig(FacesContext context)
135    {
136       DictionaryService dd = Repository.getServiceRegistry(context).getDictionaryService();
137       AdvancedSearchConfigElement config = (AdvancedSearchConfigElement)Application.getConfigService(
138             context).getConfig("Advanced Search").getConfigElement(AdvancedSearchConfigElement.CONFIG_ELEMENT_ID);
139       
140       // create an appropriate component for each custom property
141
// using the DataDictionary to look-up labels and value types
142
String JavaDoc beanBinding = (String JavaDoc)getAttributes().get("bean") + '.' + (String JavaDoc)getAttributes().get("var");
143       List JavaDoc<CustomProperty> props = config.getCustomProperties();
144       if (props != null)
145       {
146          for (CustomProperty property : props)
147          {
148             try
149             {
150                // try to find the Property definition for the specified Type or Aspect
151
PropertyDefinition propDef = null;
152                if (property.Type != null)
153                {
154                   QName type = Repository.resolveToQName(property.Type);
155                   TypeDefinition typeDef = dd.getType(type);
156                   if (typeDef == null)
157                   {
158                      throw new AlfrescoRuntimeException("No Type Definition found for: " + property.Type + " - Was an Aspect expected?");
159                   }
160                   propDef = typeDef.getProperties().get(Repository.resolveToQName(property.Property));
161                }
162                else if (property.Aspect != null)
163                {
164                   QName aspect = Repository.resolveToQName(property.Aspect);
165                   AspectDefinition aspectDef = dd.getAspect(aspect);
166                   if (aspectDef == null)
167                   {
168                      throw new AlfrescoRuntimeException("No Aspect Definition found for: " + property.Aspect + " - Was a Type expected?");
169                   }
170                   propDef = aspectDef.getProperties().get(Repository.resolveToQName(property.Property));
171                }
172                
173                // if we found a def, then we can build components to represent it
174
if (propDef != null)
175                {
176                   // resolve display label I18N message
177
String JavaDoc label;
178                   if (property.LabelId != null && property.LabelId.length() != 0)
179                   {
180                      label = Application.getMessage(context, property.LabelId);
181                   }
182                   else
183                   {
184                      // or use dictionary label or QName as last resort
185
label = propDef.getTitle() != null ? propDef.getTitle() : propDef.getName().getLocalName();
186                   }
187                   
188                   // special handling for Date and DateTime
189
DataTypeDefinition dataTypeDef = propDef.getDataType();
190                   if (DataTypeDefinition.DATE.equals(dataTypeDef.getName()) || DataTypeDefinition.DATETIME.equals(dataTypeDef.getName()))
191                   {
192                      getChildren().add( generateControl(context, propDef, label, beanBinding) );
193                   }
194                   else
195                   {
196                      getChildren().add( generateLabel(context, label) );
197                      getChildren().add( generateControl(context, propDef, null, beanBinding) );
198                   }
199                }
200             }
201             catch (DictionaryException ddErr)
202             {
203                logger.warn("Error building custom properties for Advanced Search: " + ddErr.getMessage());
204             }
205          }
206       }
207    }
208    
209    /**
210     * Generates a JSF OutputText component/renderer
211     *
212     * @param context JSF context
213     * @param displayLabel The display label text
214     * @param parent The parent component for the label
215     *
216     * @return UIComponent
217     */

218    private UIComponent generateLabel(FacesContext context, String JavaDoc displayLabel)
219    {
220       UIOutput label = (UIOutput)context.getApplication().createComponent(ComponentConstants.JAVAX_FACES_OUTPUT);
221       label.setId(context.getViewRoot().createUniqueId());
222       label.setRendererType(ComponentConstants.JAVAX_FACES_TEXT);
223       label.setValue(displayLabel + ": ");
224       return label;
225    }
226    
227    /**
228     * Generates an appropriate control for the given property
229     *
230     * @param context JSF context
231     * @param propDef The definition of the property to create the control for
232     * @param displayLabel Display label for the component
233     * @param beanBinding Combined name of the value bound bean and variable used for value binding expression
234     *
235     * @return UIComponent
236     */

237    private UIComponent generateControl(FacesContext context, PropertyDefinition propDef, String JavaDoc displayLabel, String JavaDoc beanBinding)
238    {
239       UIComponent control = null;
240       
241       DataTypeDefinition dataTypeDef = propDef.getDataType();
242       QName typeName = dataTypeDef.getName();
243       
244       javax.faces.application.Application facesApp = context.getApplication();
245       
246       // create default value binding to a Map of values with a defined name
247
ValueBinding vb = facesApp.createValueBinding(
248             "#{" + beanBinding + "[\"" + propDef.getName().toString() + "\"]}");
249       
250       // generate the appropriate input field
251
if (typeName.equals(DataTypeDefinition.BOOLEAN))
252       {
253          control = (UISelectBoolean)facesApp.createComponent(ComponentConstants.JAVAX_FACES_SELECT_BOOLEAN);
254          control.setRendererType(ComponentConstants.JAVAX_FACES_CHECKBOX);
255          control.setValueBinding("value", vb);
256       }
257       else if (typeName.equals(DataTypeDefinition.CATEGORY))
258       {
259          control = (UICategorySelector)facesApp.createComponent(RepoConstants.ALFRESCO_FACES_CATEGORY_SELECTOR);
260          control.setValueBinding("value", vb);
261       }
262       else if (typeName.equals(DataTypeDefinition.DATETIME) || typeName.equals(DataTypeDefinition.DATE))
263       {
264          Boolean JavaDoc showTime = Boolean.valueOf(typeName.equals(DataTypeDefinition.DATETIME));
265          
266          // Need to output component for From and To date selectors and labels
267
// also neeed checkbox for enable/disable state - requires an outer wrapper component
268
control = (UIPanel)facesApp.createComponent(ComponentConstants.JAVAX_FACES_PANEL);
269          control.setRendererType(ComponentConstants.JAVAX_FACES_GRID);
270          control.getAttributes().put("columns", Integer.valueOf(2));
271          
272          // enabled state checkbox
273
UIInput checkbox = (UIInput)facesApp.createComponent(ComponentConstants.JAVAX_FACES_SELECT_BOOLEAN);
274          checkbox.setRendererType(ComponentConstants.JAVAX_FACES_CHECKBOX);
275          checkbox.setId(context.getViewRoot().createUniqueId());
276          ValueBinding vbCheckbox = facesApp.createValueBinding(
277             "#{" + beanBinding + "[\"" + propDef.getName().toString() + "\"]}");
278          checkbox.setValueBinding("value", vbCheckbox);
279          control.getChildren().add(checkbox);
280          
281          // main display label
282
UIOutput label = (UIOutput)context.getApplication().createComponent(ComponentConstants.JAVAX_FACES_OUTPUT);
283          label.setId(context.getViewRoot().createUniqueId());
284          label.setRendererType(ComponentConstants.JAVAX_FACES_TEXT);
285          label.setValue(displayLabel + ":");
286          control.getChildren().add(label);
287          
288          // from date label
289
UIOutput labelFromDate = (UIOutput)context.getApplication().createComponent(ComponentConstants.JAVAX_FACES_OUTPUT);
290          labelFromDate.setId(context.getViewRoot().createUniqueId());
291          labelFromDate.setRendererType(ComponentConstants.JAVAX_FACES_TEXT);
292          labelFromDate.setValue(Application.getMessage(context, MSG_FROM));
293          control.getChildren().add(labelFromDate);
294          
295          // from date control
296
UIInput inputFromDate = (UIInput)facesApp.createComponent(ComponentConstants.JAVAX_FACES_INPUT);
297          inputFromDate.setId(context.getViewRoot().createUniqueId());
298          inputFromDate.setRendererType(RepoConstants.ALFRESCO_FACES_DATE_PICKER_RENDERER);
299          inputFromDate.getAttributes().put("yearCount", new Integer JavaDoc(30));
300          inputFromDate.getAttributes().put("showTime", showTime);
301          ValueBinding vbFromDate = facesApp.createValueBinding(
302             "#{" + beanBinding + "[\"" + PREFIX_DATE_FROM + propDef.getName().toString() + "\"]}");
303          inputFromDate.setValueBinding("value", vbFromDate);
304          control.getChildren().add(inputFromDate);
305          
306          // to date label
307
UIOutput labelToDate = (UIOutput)context.getApplication().createComponent(ComponentConstants.JAVAX_FACES_OUTPUT);
308          labelToDate.setId(context.getViewRoot().createUniqueId());
309          labelToDate.setRendererType(ComponentConstants.JAVAX_FACES_TEXT);
310          labelToDate.setValue(Application.getMessage(context, MSG_TO));
311          control.getChildren().add(labelToDate);
312          
313          // to date control
314
UIInput inputToDate = (UIInput)facesApp.createComponent(ComponentConstants.JAVAX_FACES_INPUT);
315          inputToDate.setId(context.getViewRoot().createUniqueId());
316          inputToDate.setRendererType(RepoConstants.ALFRESCO_FACES_DATE_PICKER_RENDERER);
317          inputToDate.getAttributes().put("yearCount", new Integer JavaDoc(30));
318          inputToDate.getAttributes().put("showTime", showTime);
319          ValueBinding vbToDate = facesApp.createValueBinding(
320             "#{" + beanBinding + "[\"" + PREFIX_DATE_TO + propDef.getName().toString() + "\"]}");
321          inputToDate.setValueBinding("value", vbToDate);
322          control.getChildren().add(inputToDate);
323       }
324       else if (typeName.equals(DataTypeDefinition.NODE_REF))
325       {
326          control = (UISpaceSelector)facesApp.createComponent(RepoConstants.ALFRESCO_FACES_SPACE_SELECTOR);
327          control.setValueBinding("value", vb);
328       }
329       else
330       {
331          // any other type is represented as an input text field
332
control = (UIInput)facesApp.createComponent(ComponentConstants.JAVAX_FACES_INPUT);
333          control.setRendererType(ComponentConstants.JAVAX_FACES_TEXT);
334          control.getAttributes().put("size", "28");
335          control.getAttributes().put("maxlength", "1024");
336          control.setValueBinding("value", vb);
337       }
338       
339       // set up the common aspects of the control
340
control.setId(context.getViewRoot().createUniqueId());
341       
342       return control;
343    }
344 }
345
Popular Tags