1 17 package org.alfresco.web.ui.repo.renderer; 18 19 import java.io.IOException ; 20 import java.io.Writer ; 21 import java.util.ArrayList ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import javax.faces.component.NamingContainer; 26 import javax.faces.component.UIComponent; 27 import javax.faces.context.FacesContext; 28 import javax.transaction.UserTransaction ; 29 30 import org.alfresco.model.ContentModel; 31 import org.alfresco.service.cmr.dictionary.DictionaryService; 32 import org.alfresco.service.cmr.dictionary.TypeDefinition; 33 import org.alfresco.service.cmr.repository.ChildAssociationRef; 34 import org.alfresco.service.cmr.repository.NodeRef; 35 import org.alfresco.service.cmr.repository.NodeService; 36 import org.alfresco.service.namespace.QName; 37 import org.alfresco.service.namespace.RegexQNamePattern; 38 import org.alfresco.web.bean.repository.Repository; 39 import org.alfresco.web.ui.common.Utils; 40 import org.alfresco.web.ui.common.renderer.BaseRenderer; 41 import org.alfresco.web.ui.repo.component.UINodeDescendants; 42 43 46 public class NodeDescendantsLinkRenderer extends BaseRenderer 47 { 48 51 54 public void decode(FacesContext context, UIComponent component) 55 { 56 Map requestMap = context.getExternalContext().getRequestParameterMap(); 57 String fieldId = getHiddenFieldName(context, component); 58 String value = (String )requestMap.get(fieldId); 59 60 if (value != null && value.startsWith(component.getClientId(context) + NamingContainer.SEPARATOR_CHAR)) 62 { 63 value = value.substring(component.getClientId(context).length() + 1); 64 65 int separatorIndex = value.indexOf(NamingContainer.SEPARATOR_CHAR); 68 String selectedNodeId = value.substring(0, separatorIndex); 69 boolean isParent = Boolean.parseBoolean(value.substring(separatorIndex + 1)); 70 NodeService service = getNodeService(context); 71 NodeRef ref = new NodeRef(Repository.getStoreRef(), selectedNodeId); 72 73 UINodeDescendants.NodeSelectedEvent event = new UINodeDescendants.NodeSelectedEvent(component, ref, isParent); 74 component.queueEvent(event); 75 } 76 } 77 78 81 public void encodeEnd(FacesContext context, UIComponent component) throws IOException 82 { 83 if (component.isRendered() == true) 85 { 86 Writer out = context.getResponseWriter(); 87 88 UINodeDescendants control = (UINodeDescendants)component; 89 90 Object val = control.getValue(); 92 if (val instanceof NodeRef == false) 93 { 94 throw new IllegalArgumentException ("UINodeDescendants component 'value' property must resolve to a NodeRef!"); 95 } 96 NodeRef parentRef = (NodeRef)val; 97 98 NodeService service = getNodeService(context); 100 DictionaryService dd = getDictionaryService(context); 101 UserTransaction tx = null; 102 try 103 { 104 tx = Repository.getUserTransaction(FacesContext.getCurrentInstance(), true); 105 tx.begin(); 106 107 110 String separator = (String )component.getAttributes().get("separator"); 111 if (separator == null) 112 { 113 separator = DEFAULT_SEPARATOR; 114 } 115 116 if (service.exists(parentRef) == true) 118 { 119 List <ChildAssociationRef> childRefs = service.getChildAssocs(parentRef, 120 ContentModel.ASSOC_CONTAINS, RegexQNamePattern.MATCH_ALL); 121 List <ChildAssociationRef> refs = new ArrayList <ChildAssociationRef>(childRefs.size()); 122 for (int index=0; index<childRefs.size(); index++) 123 { 124 ChildAssociationRef ref = childRefs.get(index); 125 QName type = service.getType(ref.getChildRef()); 126 TypeDefinition typeDef = dd.getType(type); 127 if (typeDef != null && dd.isSubClass(type, ContentModel.TYPE_FOLDER) && 128 dd.isSubClass(type, ContentModel.TYPE_SYSTEM_FOLDER) == false) 129 { 130 refs.add(ref); 131 } 132 } 133 134 int total = 0; 136 int maximum = refs.size() > control.getMaxChildren() ? control.getMaxChildren() : refs.size(); 137 for (int index=0; index<maximum; index++) 138 { 139 ChildAssociationRef ref = refs.get(index); 140 QName type = service.getType(ref.getChildRef()); 141 TypeDefinition typeDef = dd.getType(type); 142 if (typeDef != null && dd.isSubClass(type, ContentModel.TYPE_FOLDER) && 143 dd.isSubClass(type, ContentModel.TYPE_SYSTEM_FOLDER) == false) 144 { 145 if (total > 0) 147 { 148 out.write( separator ); 149 } 150 151 out.write(renderDescendant(context, control, ref, false)); 152 total++; 153 } 154 } 155 156 if (control.getShowEllipses() == true && refs.size() > maximum) 158 { 159 out.write( separator ); 160 out.write( renderDescendant(context, control, service.getPrimaryParent(parentRef), true) ); 164 } 165 } 166 167 tx.commit(); 168 } 169 catch (Throwable err) 170 { 171 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 172 throw new RuntimeException (err); 173 } 174 } 175 } 176 177 187 private String renderDescendant(FacesContext context, UINodeDescendants control, ChildAssociationRef childRef, boolean ellipses) 188 { 189 StringBuilder buf = new StringBuilder (256); 190 191 buf.append("<a HREF='#' onclick=\""); 192 String param = control.getClientId(context) + NamingContainer.SEPARATOR_CHAR + 195 childRef.getChildRef().getId() + NamingContainer.SEPARATOR_CHAR + 196 Boolean.toString(ellipses); 197 buf.append(Utils.generateFormSubmit(context, control, getHiddenFieldName(context, control), param)); 198 buf.append('"'); 199 Map attrs = control.getAttributes(); 200 if (attrs.get("style") != null) 201 { 202 buf.append(" style=\"") 203 .append(attrs.get("style")) 204 .append('"'); 205 } 206 if (attrs.get("styleClass") != null) 207 { 208 buf.append(" class=") 209 .append(attrs.get("styleClass")); 210 } 211 buf.append('>'); 212 213 if (ellipses == false) 214 { 215 String name = Repository.getNameForNode(getNodeService(context), childRef.getChildRef()); 217 buf.append(Utils.encode(name)); 218 } 219 else 220 { 221 buf.append("..."); 223 } 224 225 buf.append("</a>"); 226 227 return buf.toString(); 228 } 229 230 231 234 240 private static String getHiddenFieldName(FacesContext context, UIComponent component) 241 { 242 return Utils.getParentForm(context, component).getClientId(context) + NamingContainer.SEPARATOR_CHAR + "ndec"; 243 } 244 245 252 private static NodeService getNodeService(FacesContext context) 253 { 254 NodeService service = Repository.getServiceRegistry(context).getNodeService(); 255 if (service == null) 256 { 257 throw new IllegalStateException ("Unable to obtain NodeService bean reference."); 258 } 259 260 return service; 261 } 262 263 270 private static DictionaryService getDictionaryService(FacesContext context) 271 { 272 DictionaryService service = Repository.getServiceRegistry(context).getDictionaryService(); 273 if (service == null) 274 { 275 throw new IllegalStateException ("Unable to obtain DictionaryService bean reference."); 276 } 277 278 return service; 279 } 280 281 private static final String DEFAULT_SEPARATOR = " | "; 282 } 283 | Popular Tags |