1 17 package org.alfresco.web.ui.repo.component; 18 19 import java.io.IOException ; 20 import java.util.List ; 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 54 public class UISearchCustomProperties extends SelfRenderingComponent implements NamingContainer 55 { 56 public static final String PREFIX_DATE_TO = "to_"; 57 public static final String PREFIX_DATE_FROM = "from_"; 58 59 private static final String MSG_TO = "to"; 60 private static final String MSG_FROM = "from"; 61 62 private static Log logger = LogFactory.getLog(UISearchCustomProperties.class); 63 64 65 68 71 public String getFamily() 72 { 73 return "org.alfresco.faces.AdvancedSearch"; 74 } 75 76 79 public void encodeBegin(FacesContext context) throws IOException 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 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 <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 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 String beanBinding = (String )getAttributes().get("bean") + '.' + (String )getAttributes().get("var"); 143 List <CustomProperty> props = config.getCustomProperties(); 144 if (props != null) 145 { 146 for (CustomProperty property : props) 147 { 148 try 149 { 150 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 (propDef != null) 175 { 176 String label; 178 if (property.LabelId != null && property.LabelId.length() != 0) 179 { 180 label = Application.getMessage(context, property.LabelId); 181 } 182 else 183 { 184 label = propDef.getTitle() != null ? propDef.getTitle() : propDef.getName().getLocalName(); 186 } 187 188 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 218 private UIComponent generateLabel(FacesContext context, String 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 237 private UIComponent generateControl(FacesContext context, PropertyDefinition propDef, String displayLabel, String 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 ValueBinding vb = facesApp.createValueBinding( 248 "#{" + beanBinding + "[\"" + propDef.getName().toString() + "\"]}"); 249 250 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 showTime = Boolean.valueOf(typeName.equals(DataTypeDefinition.DATETIME)); 265 266 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 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 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 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 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 (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 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 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 (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 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 control.setId(context.getViewRoot().createUniqueId()); 341 342 return control; 343 } 344 } 345 | Popular Tags |