1 18 package org.alfresco.web.ui.repo.component.property; 19 20 import java.io.IOException ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import javax.faces.context.FacesContext; 27 import javax.faces.context.ResponseWriter; 28 29 import org.alfresco.service.cmr.repository.AssociationRef; 30 import org.alfresco.service.cmr.repository.NodeRef; 31 import org.alfresco.service.cmr.repository.NodeService; 32 import org.alfresco.service.namespace.QName; 33 import org.alfresco.web.bean.repository.Node; 34 import org.alfresco.web.bean.repository.Repository; 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 45 public class UIAssociationEditor extends BaseAssociationEditor 46 { 47 private static final Log logger = LogFactory.getLog(UIAssociationEditor.class); 48 49 52 55 public String getFamily() 56 { 57 return "org.alfresco.faces.AssociationEditor"; 58 } 59 60 63 protected void populateAssocationMaps(Node node) 64 { 65 if (this.originalAssocs == null) 68 { 69 this.originalAssocs = new HashMap <String , Object >(); 70 71 List assocs = (List )node.getAssociations().get(this.associationName); 72 if (assocs != null) 73 { 74 Iterator iter = assocs.iterator(); 75 while (iter.hasNext()) 76 { 77 AssociationRef assoc = (AssociationRef)iter.next(); 78 79 this.originalAssocs.put(assoc.getTargetRef().getId(), assoc); 81 } 82 } 83 } 84 85 this.added = (Map )node.getAddedAssociations().get(this.associationName); 87 if (added == null) 88 { 89 added = new HashMap <String , Object >(); 91 node.getAddedAssociations().put(this.associationName, (Map )added); 92 } 93 94 this.removed = (Map )node.getRemovedAssociations().get(this.associationName); 96 if (removed == null) 97 { 98 removed = new HashMap <String , Object >(); 100 node.getRemovedAssociations().put(this.associationName, (Map )removed); 101 } 102 } 103 104 107 protected void renderExistingAssociations(FacesContext context, ResponseWriter out, 108 NodeService nodeService, boolean allowManyChildren) throws IOException 109 { 110 boolean itemsRendered = false; 111 112 Iterator iter = this.originalAssocs.values().iterator(); 114 while (iter.hasNext()) 115 { 116 AssociationRef assoc = (AssociationRef)iter.next(); 117 if (removed.containsKey(assoc.getTargetRef().getId()) == false) 118 { 119 renderExistingAssociation(context, out, nodeService, assoc.getTargetRef(), allowManyChildren); 120 itemsRendered = true; 121 } 122 } 123 124 iter = this.added.values().iterator(); 126 while (iter.hasNext()) 127 { 128 AssociationRef assoc = (AssociationRef)iter.next(); 129 renderExistingAssociation(context, out, nodeService, assoc.getTargetRef(), allowManyChildren); 130 itemsRendered = true; 131 } 132 133 if (itemsRendered == false && allowManyChildren == true) 135 { 136 renderNone(context, out); 137 } 138 } 139 140 143 protected void renderReadOnlyAssociations(FacesContext context, ResponseWriter out, NodeService nodeService) throws IOException 144 { 145 if (this.originalAssocs.size() > 0) 146 { 147 out.write("<table cellspacing='0' cellpadding='2' border='0'>"); 148 149 Iterator iter = this.originalAssocs.values().iterator(); 150 while (iter.hasNext()) 151 { 152 out.write("<tr><td>"); 153 AssociationRef assoc = (AssociationRef)iter.next(); 154 out.write(Repository.getDisplayPath(nodeService.getPath(assoc.getTargetRef()))); 155 out.write("/"); 156 out.write(Repository.getNameForNode(nodeService, assoc.getTargetRef())); 157 out.write("</td></tr>"); 158 } 159 160 out.write("</table>"); 161 } 162 } 163 164 170 protected void removeTarget(Node node, String targetId) 171 { 172 if (node != null && targetId != null) 173 { 174 QName assocQName = Repository.resolveToQName(this.associationName); 175 AssociationRef newAssoc = new AssociationRef(node.getNodeRef(), assocQName, new NodeRef(Repository.getStoreRef(), targetId)); 176 177 if (this.originalAssocs.containsKey(targetId)) 180 { 181 Map <String , AssociationRef> removed = node.getRemovedAssociations().get(this.associationName); 182 removed.put(targetId, newAssoc); 183 184 if (logger.isDebugEnabled()) 185 logger.debug("Added association to " + targetId + " to the removed list"); 186 } 187 188 Map <String , AssociationRef> added = node.getAddedAssociations().get(this.associationName); 191 if (added.containsKey(targetId)) 192 { 193 added.remove(targetId); 194 195 if (logger.isDebugEnabled()) 196 logger.debug("Removed association to " + targetId + " from the added list"); 197 } 198 } 199 } 200 201 207 protected void addTarget(Node node, String [] toAdd) 208 { 209 if (node != null && toAdd != null && toAdd.length > 0) 210 { 211 for (int x = 0; x < toAdd.length; x++) 212 { 213 String targetId = toAdd[x]; 214 215 if (this.originalAssocs.containsKey(targetId) == false) 217 { 218 QName assocQName = Repository.resolveToQName(this.associationName); 219 AssociationRef newAssoc = new AssociationRef(node.getNodeRef(), assocQName, 220 new NodeRef(Repository.getStoreRef(), targetId)); 221 222 Map <String , AssociationRef> added = node.getAddedAssociations().get(this.associationName); 223 added.put(targetId, newAssoc); 224 225 if (logger.isDebugEnabled()) 226 logger.debug("Added association to " + targetId + " to the added list"); 227 } 228 229 Map <String , AssociationRef> removed = node.getRemovedAssociations().get(this.associationName); 232 if (removed.containsKey(targetId)) 233 { 234 removed.remove(targetId); 235 236 if (logger.isDebugEnabled()) 237 logger.debug("Removed association to " + targetId + " from the removed list"); 238 } 239 } 240 } 241 } 242 } 243 | Popular Tags |