1 17 package org.alfresco.web.ui.repo.renderer; 18 19 import java.io.IOException ; 20 import java.io.Writer ; 21 import java.util.Map ; 22 23 import javax.faces.component.NamingContainer; 24 import javax.faces.component.UIComponent; 25 import javax.faces.context.FacesContext; 26 import javax.transaction.UserTransaction ; 27 28 import org.alfresco.repo.security.permissions.AccessDeniedException; 29 import org.alfresco.service.cmr.repository.ChildAssociationRef; 30 import org.alfresco.service.cmr.repository.InvalidNodeRefException; 31 import org.alfresco.service.cmr.repository.NodeRef; 32 import org.alfresco.service.cmr.repository.NodeService; 33 import org.alfresco.service.cmr.repository.Path; 34 import org.alfresco.web.bean.repository.Repository; 35 import org.alfresco.web.ui.common.Utils; 36 import org.alfresco.web.ui.common.renderer.BaseRenderer; 37 import org.alfresco.web.ui.repo.component.UINodeDescendants; 38 import org.alfresco.web.ui.repo.component.UINodePath; 39 40 43 public class NodePathLinkRenderer extends BaseRenderer 44 { 45 48 51 public void decode(FacesContext context, UIComponent component) 52 { 53 Map requestMap = context.getExternalContext().getRequestParameterMap(); 54 String fieldId = getHiddenFieldName(context, component); 55 String value = (String )requestMap.get(fieldId); 56 57 if (value != null && value.startsWith(component.getClientId(context) + NamingContainer.SEPARATOR_CHAR)) 59 { 60 String selectedNodeId = value.substring(component.getClientId(context).length() + 1); 63 NodeRef ref = new NodeRef(Repository.getStoreRef(), selectedNodeId); 64 65 UINodePath.PathElementEvent event = new UINodePath.PathElementEvent(component, ref); 66 component.queueEvent(event); 67 } 68 } 69 70 73 public void encodeEnd(FacesContext context, UIComponent component) throws IOException 74 { 75 if (component.isRendered() == false) 77 { 78 return; 79 } 80 81 Writer out = context.getResponseWriter(); 82 83 Path path = null; 85 NodeRef nodeRef = null; 86 Object val = ((UINodePath)component).getValue(); 87 if (val instanceof NodeRef == true) 88 { 89 nodeRef = (NodeRef)val; 90 } 91 else if (val instanceof Path == true) 92 { 93 path = (Path)val; 94 } 95 else 96 { 97 throw new IllegalArgumentException ("UINodePath component 'value' property must resolve to a NodeRef or Path!"); 98 } 99 100 boolean isBreadcrumb = false; 101 Boolean breadcrumb = (Boolean )component.getAttributes().get("breadcrumb"); 102 if (breadcrumb != null) 103 { 104 isBreadcrumb = breadcrumb.booleanValue(); 105 } 106 107 boolean isDisabled = false; 108 Boolean disabled = (Boolean )component.getAttributes().get("disabled"); 109 if (disabled != null) 110 { 111 isDisabled = disabled.booleanValue(); 112 } 113 114 boolean showLeaf = false; 115 Boolean showLeafBool = (Boolean )component.getAttributes().get("showLeaf"); 116 if (showLeafBool != null) 117 { 118 showLeaf = showLeafBool.booleanValue(); 119 } 120 121 NodeService service = getNodeService(context); 123 UserTransaction tx = null; 124 try 125 { 126 tx = Repository.getUserTransaction(FacesContext.getCurrentInstance(), true); 127 tx.begin(); 128 129 if (path == null) 130 { 131 path = service.getPath(nodeRef); 132 } 133 134 if (isBreadcrumb == false || isDisabled == true) 135 { 136 out.write(buildPathAsSingular(context, component, path, showLeaf, isDisabled)); 137 } 138 else 139 { 140 out.write(buildPathAsBreadcrumb(context, component, path, showLeaf)); 141 } 142 143 tx.commit(); 144 } 145 catch (InvalidNodeRefException refErr) 146 { 147 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 149 } 150 catch (AccessDeniedException accessErr) 151 { 152 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 154 } 155 catch (Throwable err) 156 { 157 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 158 throw new RuntimeException (err); 159 } 160 } 161 162 171 private String buildPathAsBreadcrumb(FacesContext context, UIComponent component, Path path, boolean showLeaf) 172 { 173 StringBuilder buf = new StringBuilder (1024); 174 175 int size = (showLeaf ? path.size() : path.size() - 1); 176 for (int i=0; i<size; i++) 177 { 178 Path.Element element = path.get(i); 179 String elementString = null; 180 if (element instanceof Path.ChildAssocElement) 181 { 182 ChildAssociationRef elementRef = ((Path.ChildAssocElement)element).getRef(); 183 if (elementRef.getParentRef() != null) 184 { 185 String name = Repository.getNameForNode(getNodeService(context), elementRef.getChildRef()); 186 elementString = renderPathElement(context, component, elementRef.getChildRef(), name); 187 } 188 } 189 else 190 { 191 elementString = element.getElementString(); 192 } 193 194 if (elementString != null) 195 { 196 buf.append("/"); 197 buf.append(elementString); 198 } 199 } 200 201 return buf.toString(); 202 } 203 204 213 private String buildPathAsSingular(FacesContext context, UIComponent component, Path path, boolean showLeaf, boolean disabled) 214 { 215 StringBuilder buf = new StringBuilder (512); 216 217 NodeRef lastElementRef = null; 218 int size = (showLeaf ? path.size() : path.size() - 1); 219 for (int i=0; i<size; i++) 220 { 221 Path.Element element = path.get(i); 222 String elementString = null; 223 if (element instanceof Path.ChildAssocElement) 224 { 225 ChildAssociationRef elementRef = ((Path.ChildAssocElement)element).getRef(); 226 if (elementRef.getParentRef() != null) 227 { 228 elementString = Repository.getNameForNode(getNodeService(context), elementRef.getChildRef()); 229 } 230 if (i == path.size() - 2) 231 { 232 lastElementRef = elementRef.getChildRef(); 233 } 234 } 235 else 236 { 237 elementString = element.getElementString(); 238 } 239 240 if (elementString != null) 241 { 242 buf.append("/"); 243 buf.append(elementString); 244 } 245 } 246 247 if (disabled == false) 248 { 249 return renderPathElement(context, component, lastElementRef, buf.toString()); 250 } 251 else 252 { 253 return buf.toString(); 254 } 255 } 256 257 267 private String renderPathElement(FacesContext context, UIComponent control, NodeRef nodeRef, String label) 268 { 269 StringBuilder buf = new StringBuilder (256); 270 271 buf.append("<a HREF='#' onclick=\""); 272 String param = control.getClientId(context) + NamingContainer.SEPARATOR_CHAR + nodeRef.getId(); 274 buf.append(Utils.generateFormSubmit(context, control, getHiddenFieldName(context, control), param)); 275 buf.append('"'); 276 Map attrs = control.getAttributes(); 277 if (attrs.get("style") != null) 278 { 279 buf.append(" style=\"") 280 .append(attrs.get("style")) 281 .append('"'); 282 } 283 if (attrs.get("styleClass") != null) 284 { 285 buf.append(" class=") 286 .append(attrs.get("styleClass")); 287 } 288 buf.append('>'); 289 290 buf.append(Utils.encode(label)); 291 292 buf.append("</a>"); 293 294 return buf.toString(); 295 } 296 297 298 301 307 private static String getHiddenFieldName(FacesContext context, UIComponent component) 308 { 309 return Utils.getParentForm(context, component).getClientId(context) + NamingContainer.SEPARATOR_CHAR + "npath"; 310 } 311 312 319 private static NodeService getNodeService(FacesContext context) 320 { 321 NodeService service = Repository.getServiceRegistry(context).getNodeService(); 322 if (service == null) 323 { 324 throw new IllegalStateException ("Unable to obtain NodeService bean reference."); 325 } 326 327 return service; 328 } 329 } 330 | Popular Tags |