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.ChildAssociationRef; 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 UIChildAssociationEditor extends BaseAssociationEditor 46 { 47 private static final Log logger = LogFactory.getLog(UIChildAssociationEditor.class); 48 49 52 55 public String getFamily() 56 { 57 return "org.alfresco.faces.ChildAssociationEditor"; 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.getChildAssociations().get(this.associationName); 72 if (assocs != null) 73 { 74 Iterator iter = assocs.iterator(); 75 while (iter.hasNext()) 76 { 77 ChildAssociationRef assoc = (ChildAssociationRef)iter.next(); 78 79 this.originalAssocs.put(assoc.getChildRef().getId(), assoc); 81 } 82 } 83 } 84 85 this.added = (Map )node.getAddedChildAssociations().get(this.associationName); 87 if (added == null) 88 { 89 added = new HashMap <String , Object >(); 91 node.getAddedChildAssociations().put(this.associationName, (Map )added); 92 } 93 94 this.removed = (Map )node.getRemovedChildAssociations().get(this.associationName); 96 if (removed == null) 97 { 98 removed = new HashMap <String , Object >(); 100 node.getRemovedChildAssociations().put(this.associationName, (Map )removed); 101 } 102 } 103 104 107 protected void renderExistingAssociations(FacesContext context, ResponseWriter out, 108 NodeService nodeService, boolean allowMany) throws IOException 109 { 110 boolean itemsRendered = false; 111 112 Iterator iter = this.originalAssocs.values().iterator(); 114 while (iter.hasNext()) 115 { 116 ChildAssociationRef assoc = (ChildAssociationRef)iter.next(); 117 if (removed.containsKey(assoc.getChildRef().getId()) == false) 118 { 119 renderExistingAssociation(context, out, nodeService, assoc.getChildRef(), allowMany); 120 itemsRendered = true; 121 } 122 } 123 124 iter = this.added.values().iterator(); 126 while (iter.hasNext()) 127 { 128 ChildAssociationRef assoc = (ChildAssociationRef)iter.next(); 129 renderExistingAssociation(context, out, nodeService, assoc.getChildRef(), allowMany); 130 itemsRendered = true; 131 } 132 133 if (itemsRendered == false && allowMany == 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 ChildAssociationRef assoc = (ChildAssociationRef)iter.next(); 154 out.write(Repository.getDisplayPath(nodeService.getPath(assoc.getChildRef()))); 155 out.write("/"); 156 out.write(Repository.getNameForNode(nodeService, assoc.getChildRef())); 157 out.write("</tr></td>"); 158 } 159 160 out.write("</table>"); 161 } 162 } 163 164 167 protected void removeTarget(Node node, String childId) 168 { 169 if (node != null && childId != null) 170 { 171 QName assocQName = Repository.resolveToQName(this.associationName); 172 ChildAssociationRef childAssoc = new ChildAssociationRef(assocQName, 173 node.getNodeRef(), assocQName, new NodeRef(Repository.getStoreRef(), childId)); 174 175 if (this.originalAssocs.containsKey(childId)) 178 { 179 Map <String , ChildAssociationRef> removed = node.getRemovedChildAssociations().get(this.associationName); 180 removed.put(childId, childAssoc); 181 182 if (logger.isDebugEnabled()) 183 logger.debug("Added association to " + childId + " to the removed list"); 184 } 185 186 Map <String , ChildAssociationRef> added = node.getAddedChildAssociations().get(this.associationName); 189 if (added.containsKey(childId)) 190 { 191 added.remove(childId); 192 193 if (logger.isDebugEnabled()) 194 logger.debug("Removed association to " + childId + " from the added list"); 195 } 196 } 197 } 198 199 202 protected void addTarget(Node node, String [] toAdd) 203 { 204 if (node != null && toAdd != null && toAdd.length > 0) 205 { 206 for (int x = 0; x < toAdd.length; x++) 207 { 208 String childId = toAdd[x]; 209 210 if (this.originalAssocs.containsKey(childId) == false) 212 { 213 QName assocQName = Repository.resolveToQName(this.associationName); 214 ChildAssociationRef childAssoc = new ChildAssociationRef(assocQName, 215 node.getNodeRef(), assocQName, new NodeRef(Repository.getStoreRef(), childId)); 216 217 Map <String , ChildAssociationRef> added = node.getAddedChildAssociations().get(this.associationName); 218 added.put(childId, childAssoc); 219 220 if (logger.isDebugEnabled()) 221 logger.debug("Added association to " + childId + " to the added list"); 222 } 223 224 Map <String , ChildAssociationRef> removed = node.getRemovedChildAssociations().get(this.associationName); 227 if (removed.containsKey(childId)) 228 { 229 removed.remove(childId); 230 231 if (logger.isDebugEnabled()) 232 logger.debug("Removed association to " + childId + " from the removed list"); 233 } 234 } 235 } 236 } 237 } 238 | Popular Tags |