KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > repo > component > property > UIAssociationEditor


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the GNU Lesser General Public License as
5  * published by the Free Software Foundation; either version
6  * 2.1 of the License, or (at your option) any later version.
7  * You may obtain a copy of the License at
8  *
9  * http://www.gnu.org/licenses/lgpl.txt
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific
15  * language governing permissions and limitations under the
16  * License.
17  */

18 package org.alfresco.web.ui.repo.component.property;
19
20 import java.io.IOException JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
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 /**
39  * Component that allows associations to be edited
40  * i.e. new associations to be added, existing ones to be
41  * removed whilst following the rules in the data dictionary
42  *
43  * @author gavinc
44  */

45 public class UIAssociationEditor extends BaseAssociationEditor
46 {
47    private static final Log logger = LogFactory.getLog(UIAssociationEditor.class);
48
49    // ------------------------------------------------------------------------------
50
// Component implementation
51

52    /**
53     * @see javax.faces.component.UIComponent#getFamily()
54     */

55    public String JavaDoc getFamily()
56    {
57       return "org.alfresco.faces.AssociationEditor";
58    }
59    
60    /**
61     * @see org.alfresco.web.ui.repo.component.property.BaseAssociationEditor#populateAssocationMaps(org.alfresco.web.bean.repository.Node)
62     */

63    protected void populateAssocationMaps(Node node)
64    {
65       // we need to remember the original set of associations (if there are any)
66
// and place them in a map keyed by the id of the child node
67
if (this.originalAssocs == null)
68       {
69          this.originalAssocs = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
70
71          List JavaDoc assocs = (List JavaDoc)node.getAssociations().get(this.associationName);
72          if (assocs != null)
73          {
74             Iterator JavaDoc iter = assocs.iterator();
75             while (iter.hasNext())
76             {
77                AssociationRef assoc = (AssociationRef)iter.next();
78                
79                // add the association to the map
80
this.originalAssocs.put(assoc.getTargetRef().getId(), assoc);
81             }
82          }
83       }
84       
85       // get the map of added associations for this node and association type
86
this.added = (Map JavaDoc)node.getAddedAssociations().get(this.associationName);
87       if (added == null)
88       {
89          // if there aren't any added associations for 'associationName' create a map and add it
90
added = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
91          node.getAddedAssociations().put(this.associationName, (Map JavaDoc)added);
92       }
93       
94       // get the map of removed associations for this node and association type
95
this.removed = (Map JavaDoc)node.getRemovedAssociations().get(this.associationName);
96       if (removed == null)
97       {
98          // if there aren't any added associations for 'associationName' create a map and add it
99
removed = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
100          node.getRemovedAssociations().put(this.associationName, (Map JavaDoc)removed);
101       }
102    }
103
104    /**
105     * @see org.alfresco.web.ui.repo.component.property.BaseAssociationEditor#renderExistingAssociations(javax.faces.context.FacesContext, javax.faces.context.ResponseWriter, org.alfresco.service.cmr.repository.NodeService, boolean)
106     */

107    protected void renderExistingAssociations(FacesContext context, ResponseWriter out,
108          NodeService nodeService, boolean allowManyChildren) throws IOException JavaDoc
109    {
110       boolean itemsRendered = false;
111       
112       // show the associations from the original list if they are not in the removed list
113
Iterator JavaDoc 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       // also show any associations added in this session
125
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       // show the none selected message if no items were rendered
134
if (itemsRendered == false && allowManyChildren == true)
135       {
136          renderNone(context, out);
137       }
138    }
139    
140    /**
141     * @see org.alfresco.web.ui.repo.component.property.BaseAssociationEditor#renderReadOnlyAssociations(javax.faces.context.FacesContext, javax.faces.context.ResponseWriter, org.alfresco.service.cmr.repository.NodeService)
142     */

143    protected void renderReadOnlyAssociations(FacesContext context, ResponseWriter out, NodeService nodeService) throws IOException JavaDoc
144    {
145       if (this.originalAssocs.size() > 0)
146       {
147          out.write("<table cellspacing='0' cellpadding='2' border='0'>");
148          
149          Iterator JavaDoc 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    /**
165     * Updates the component and node state to reflect an association being removed
166     *
167     * @param node The node we are dealing with
168     * @param targetId The id of the child to remove
169     */

170    protected void removeTarget(Node node, String JavaDoc 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          // update the node so it knows to remove the association, but only if the association
178
// was one of the original ones
179
if (this.originalAssocs.containsKey(targetId))
180          {
181             Map JavaDoc<String JavaDoc, 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          // if this association was previously added in this session it will still be
189
// in the added list so remove it if it is
190
Map JavaDoc<String JavaDoc, 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    /**
202     * Updates the component and node state to reflect an association being added
203     *
204     * @param node The node we are dealing with
205     * @param childId The id of the child to add
206     */

207    protected void addTarget(Node node, String JavaDoc[] toAdd)
208    {
209       if (node != null && toAdd != null && toAdd.length > 0)
210       {
211          for (int x = 0; x < toAdd.length; x++)
212          {
213             String JavaDoc targetId = toAdd[x];
214             
215             // update the node so it knows to add the association (if it wasn't there originally)
216
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 JavaDoc<String JavaDoc, 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             // if the association was previously removed and has now been re-added it
230
// will still be in the "to be removed" list so remove it if it is
231
Map JavaDoc<String JavaDoc, 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