1 17 package org.alfresco.web.ui.repo.component.property; 18 19 import java.io.IOException ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import javax.faces.component.NamingContainer; 25 import javax.faces.component.UIPanel; 26 import javax.faces.context.FacesContext; 27 import javax.faces.el.ValueBinding; 28 29 import org.alfresco.config.Config; 30 import org.alfresco.config.ConfigLookupContext; 31 import org.alfresco.config.ConfigService; 32 import org.alfresco.service.cmr.repository.NodeRef; 33 import org.alfresco.web.app.Application; 34 import org.alfresco.web.bean.repository.Node; 35 import org.alfresco.web.config.PropertySheetConfigElement; 36 import org.alfresco.web.config.PropertySheetConfigElement.AssociationConfig; 37 import org.alfresco.web.config.PropertySheetConfigElement.ChildAssociationConfig; 38 import org.alfresco.web.config.PropertySheetConfigElement.ItemConfig; 39 import org.alfresco.web.config.PropertySheetConfigElement.PropertyConfig; 40 import org.apache.commons.logging.Log; 41 import org.apache.commons.logging.LogFactory; 42 43 48 public class UIPropertySheet extends UIPanel implements NamingContainer 49 { 50 public static final String VIEW_MODE = "view"; 51 public static final String EDIT_MODE = "edit"; 52 53 private static Log logger = LogFactory.getLog(UIPropertySheet.class); 54 private static String DEFAULT_VAR_NAME = "node"; 55 56 private String variable; 57 private NodeRef nodeRef; 58 private Node node; 59 private Boolean readOnly; 60 private String mode; 61 private String configArea; 62 63 66 public UIPropertySheet() 67 { 68 setRendererType("javax.faces.Grid"); 70 } 71 72 75 public String getFamily() 76 { 77 return "javax.faces.Panel"; 78 } 79 80 83 public void encodeBegin(FacesContext context) throws IOException 84 { 85 int howManyKids = getChildren().size(); 86 Boolean externalConfig = (Boolean )getAttributes().get("externalConfig"); 87 88 if (this.variable == null) 90 { 91 this.variable = DEFAULT_VAR_NAME; 92 } 93 94 Node node = getNode(); 96 97 if (howManyKids == 0) 98 { 99 if (externalConfig != null && externalConfig.booleanValue()) 100 { 101 if (logger.isDebugEnabled()) 103 logger.debug("Configuring property sheet using ConfigService"); 104 105 ConfigService configSvc = Application.getConfigService(FacesContext.getCurrentInstance()); 107 Config configProps = null; 108 if (getConfigArea() == null) 109 { 110 configProps = configSvc.getConfig(node); 111 } 112 else 113 { 114 configProps = configSvc.getConfig(node, new ConfigLookupContext(getConfigArea())); 116 } 117 118 PropertySheetConfigElement itemsToDisplay = (PropertySheetConfigElement)configProps. 119 getConfigElement("property-sheet"); 120 121 if (itemsToDisplay != null) 122 { 123 List <ItemConfig> itemsToRender = null; 124 125 if (this.getMode().equalsIgnoreCase(EDIT_MODE)) 126 { 127 itemsToRender = itemsToDisplay.getEditableItemsToShow(); 128 129 if (logger.isDebugEnabled()) 130 logger.debug("Items to render: " + itemsToDisplay.getEditableItemNamesToShow()); 131 } 132 else 133 { 134 itemsToRender = itemsToDisplay.getItemsToShow(); 135 136 if (logger.isDebugEnabled()) 137 logger.debug("Items to render: " + itemsToDisplay.getItemNamesToShow()); 138 } 139 140 createComponentsFromConfig(context, itemsToRender); 141 } 142 else 143 { 144 if (logger.isDebugEnabled()) 145 logger.debug("There are no items to render!"); 146 } 147 } 148 else 149 { 150 if (logger.isDebugEnabled()) 152 logger.debug("Configuring property sheet using node's current state"); 153 154 createComponentsFromNode(context, node); 155 } 156 } 157 158 Map sessionMap = getFacesContext().getExternalContext().getSessionMap(); 160 sessionMap.put(this.variable, node); 161 162 if (logger.isDebugEnabled()) 163 logger.debug("Put node into session with key '" + this.variable + "': " + node); 164 165 super.encodeBegin(context); 166 } 167 168 171 public void restoreState(FacesContext context, Object state) 172 { 173 Object values[] = (Object [])state; 174 super.restoreState(context, values[0]); 176 this.nodeRef = (NodeRef)values[1]; 177 this.node = (Node)values[2]; 178 this.variable = (String )values[3]; 179 this.readOnly = (Boolean )values[4]; 180 this.mode = (String )values[5]; 181 this.configArea = (String )values[6]; 182 } 183 184 187 public Object saveState(FacesContext context) 188 { 189 Object values[] = new Object [7]; 190 values[0] = super.saveState(context); 192 values[1] = this.nodeRef; 193 values[2] = this.node; 194 values[3] = this.variable; 195 values[4] = this.readOnly; 196 values[5] = this.mode; 197 values[6] = this.configArea; 198 return (values); 199 } 200 201 204 public Node getNode() 205 { 206 Node node = null; 207 208 if (this.node == null) 209 { 210 Object value = getAttributes().get("value"); 212 213 if (value == null) 214 { 215 ValueBinding vb = getValueBinding("value"); 216 if (vb != null) 217 { 218 value = vb.getValue(getFacesContext()); 219 } 220 } 221 222 if (value instanceof Node) 224 { 225 node = (Node)value; 226 } 227 } 228 else 229 { 230 node = this.node; 231 } 232 233 return node; 234 } 235 236 239 public void setNode(Node node) 240 { 241 this.node = node; 242 } 243 244 247 public String getVar() 248 { 249 return this.variable; 250 } 251 252 255 public void setVar(String variable) 256 { 257 this.variable = variable; 258 } 259 260 263 public boolean isReadOnly() 264 { 265 if (this.readOnly == null) 266 { 267 ValueBinding vb = getValueBinding("readOnly"); 268 if (vb != null) 269 { 270 this.readOnly = (Boolean )vb.getValue(getFacesContext()); 271 } 272 } 273 274 if (this.readOnly == null) 275 { 276 this.readOnly = Boolean.FALSE; 277 } 278 279 return this.readOnly; 280 } 281 282 285 public void setReadOnly(boolean readOnly) 286 { 287 this.readOnly = Boolean.valueOf(readOnly); 288 } 289 290 293 public String getMode() 294 { 295 if (this.mode == null) 296 { 297 ValueBinding vb = getValueBinding("mode"); 298 if (vb != null) 299 { 300 this.mode = (String )vb.getValue(getFacesContext()); 301 } 302 } 303 304 if (this.mode == null) 305 { 306 mode = EDIT_MODE; 307 } 308 309 return mode; 310 } 311 312 315 public void setMode(String mode) 316 { 317 this.mode = mode; 318 } 319 320 323 public String getConfigArea() 324 { 325 if (this.configArea == null) 326 { 327 ValueBinding vb = getValueBinding("configArea"); 328 if (vb != null) 329 { 330 this.configArea = (String )vb.getValue(getFacesContext()); 331 } 332 } 333 334 return configArea; 335 } 336 337 340 public void setConfigArea(String configArea) 341 { 342 this.configArea = configArea; 343 } 344 345 352 private void createComponentsFromNode(FacesContext context, Node node) 353 throws IOException 354 { 355 Map <String , Object > props = node.getProperties(); 357 for (String propertyName : props.keySet()) 358 { 359 UIProperty propComp = (UIProperty)context.getApplication().createComponent("org.alfresco.faces.Property"); 361 propComp.setId(context.getViewRoot().createUniqueId()); 362 propComp.setName(propertyName); 363 364 if (isReadOnly()) 366 { 367 propComp.setReadOnly(true); 368 } 369 370 372 this.getChildren().add(propComp); 373 374 if (logger.isDebugEnabled()) 375 logger.debug("Created property component " + propComp + "(" + 376 propComp.getClientId(context) + 377 ") for '" + propertyName + 378 "' and added it to property sheet " + this); 379 } 380 381 Map associations = node.getAssociations(); 383 Iterator iter = associations.keySet().iterator(); 384 while (iter.hasNext()) 385 { 386 String assocName = (String )iter.next(); 387 UIAssociation assocComp = (UIAssociation)context.getApplication().createComponent("org.alfresco.faces.Association"); 388 assocComp.setId(context.getViewRoot().createUniqueId()); 389 assocComp.setName(assocName); 390 391 if (isReadOnly()) 393 { 394 assocComp.setReadOnly(true); 395 } 396 397 399 this.getChildren().add(assocComp); 400 401 if (logger.isDebugEnabled()) 402 logger.debug("Created association component " + assocComp + "(" + 403 assocComp.getClientId(context) + 404 ") for '" + assocName + 405 "' and added it to property sheet " + this); 406 } 407 408 Map childAssociations = node.getChildAssociations(); 410 iter = childAssociations.keySet().iterator(); 411 while (iter.hasNext()) 412 { 413 String assocName = (String )iter.next(); 414 UIChildAssociation childAssocComp = (UIChildAssociation)context.getApplication().createComponent( 415 "org.alfresco.faces.ChildAssociation"); 416 childAssocComp.setId(context.getViewRoot().createUniqueId()); 417 childAssocComp.setName(assocName); 418 419 if (isReadOnly()) 421 { 422 childAssocComp.setReadOnly(true); 423 } 424 425 427 this.getChildren().add(childAssocComp); 428 429 if (logger.isDebugEnabled()) 430 logger.debug("Created child association component " + childAssocComp + "(" + 431 childAssocComp.getClientId(context) + 432 ") for '" + assocName + 433 "' and added it to property sheet " + this); 434 } 435 } 436 437 445 private void createComponentsFromConfig(FacesContext context, List <ItemConfig> items) 446 throws IOException 447 { 448 453 for (ItemConfig item : items) 454 { 455 if (item instanceof PropertyConfig) 457 { 458 UIProperty propComp = (UIProperty)context.getApplication().createComponent("org.alfresco.faces.Property"); 459 propComp.setId(context.getViewRoot().createUniqueId()); 460 propComp.setName(item.getName()); 461 propComp.setConverter(item.getConverter()); 462 463 String displayLabel = item.getDisplayLabel(); 464 if (item.getDisplayLabelId() != null) 465 { 466 String label = Application.getMessage(context, item.getDisplayLabelId()); 467 if (label != null) 468 { 469 displayLabel = label; 470 } 471 } 472 propComp.setDisplayLabel(displayLabel); 473 474 if (isReadOnly() || item.isReadOnly()) 477 { 478 propComp.setReadOnly(true); 479 } 480 481 this.getChildren().add(propComp); 482 483 if (logger.isDebugEnabled()) 484 logger.debug("Created property component " + propComp + "(" + 485 propComp.getClientId(context) + 486 ") for '" + item.getName() + 487 "' and added it to property sheet " + this); 488 } 489 else if (item instanceof AssociationConfig) 490 { 491 UIAssociation assocComp = (UIAssociation)context.getApplication().createComponent("org.alfresco.faces.Association"); 492 assocComp.setId(context.getViewRoot().createUniqueId()); 493 assocComp.setName(item.getName()); 494 assocComp.setConverter(item.getConverter()); 495 496 String displayLabel = item.getDisplayLabel(); 497 if (item.getDisplayLabelId() != null) 498 { 499 String label = Application.getMessage(context, item.getDisplayLabelId()); 500 if (label != null) 501 { 502 displayLabel = label; 503 } 504 } 505 assocComp.setDisplayLabel(displayLabel); 506 507 if (isReadOnly() || item.isReadOnly()) 510 { 511 assocComp.setReadOnly(true); 512 } 513 514 this.getChildren().add(assocComp); 515 516 if (logger.isDebugEnabled()) 517 logger.debug("Created association component " + assocComp + "(" + 518 assocComp.getClientId(context) + 519 ") for '" + item.getName() + 520 "' and added it to property sheet " + this); 521 } 522 else if (item instanceof ChildAssociationConfig) 523 { 524 UIChildAssociation assocComp = (UIChildAssociation)context.getApplication().createComponent("org.alfresco.faces.ChildAssociation"); 525 assocComp.setId(context.getViewRoot().createUniqueId()); 526 assocComp.setName(item.getName()); 527 assocComp.setConverter(item.getConverter()); 528 529 String displayLabel = item.getDisplayLabel(); 530 if (item.getDisplayLabelId() != null) 531 { 532 String label = Application.getMessage(context, item.getDisplayLabelId()); 533 if (label != null) 534 { 535 displayLabel = label; 536 } 537 } 538 assocComp.setDisplayLabel(displayLabel); 539 540 if (isReadOnly() || item.isReadOnly()) 543 { 544 assocComp.setReadOnly(true); 545 } 546 547 this.getChildren().add(assocComp); 548 549 if (logger.isDebugEnabled()) 550 logger.debug("Created child association component " + assocComp + "(" + 551 assocComp.getClientId(context) + 552 ") for '" + item.getName() + 553 "' and added it to property sheet " + this); 554 } 555 } 556 } 557 } 558 | Popular Tags |