KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 /**
39  * Component that allows child 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 UIChildAssociationEditor extends BaseAssociationEditor
46 {
47    private static final Log logger = LogFactory.getLog(UIChildAssociationEditor.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.ChildAssociationEditor";
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.getChildAssociations().get(this.associationName);
72          if (assocs != null)
73          {
74             Iterator JavaDoc iter = assocs.iterator();
75             while (iter.hasNext())
76             {
77                ChildAssociationRef assoc = (ChildAssociationRef)iter.next();
78                
79                // add the association to the map
80
this.originalAssocs.put(assoc.getChildRef().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.getAddedChildAssociations().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.getAddedChildAssociations().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.getRemovedChildAssociations().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.getRemovedChildAssociations().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 allowMany) 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          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       // also show any associations added in this session
125
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       // show the none selected message if no items were rendered
134
if (itemsRendered == false && allowMany == 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             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    /**
165     * @see org.alfresco.web.ui.repo.component.property.BaseAssociationEditor#removeTarget(org.alfresco.web.bean.repository.Node, java.lang.String)
166     */

167    protected void removeTarget(Node node, String JavaDoc 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          // update the node so it knows to remove the association, but only if the association
176
// was one of the original ones
177
if (this.originalAssocs.containsKey(childId))
178          {
179             Map JavaDoc<String JavaDoc, 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          // if this association was previously added in this session it will still be
187
// in the added list so remove it if it is
188
Map JavaDoc<String JavaDoc, 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    /**
200     * @see org.alfresco.web.ui.repo.component.property.BaseAssociationEditor#addTarget(org.alfresco.web.bean.repository.Node, java.lang.String[])
201     */

202    protected void addTarget(Node node, String JavaDoc[] toAdd)
203    {
204       if (node != null && toAdd != null && toAdd.length > 0)
205       {
206          for (int x = 0; x < toAdd.length; x++)
207          {
208             String JavaDoc childId = toAdd[x];
209             
210             // update the node so it knows to add the association
211
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 JavaDoc<String JavaDoc, 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             // if the association was previously removed and has now been re-added it
225
// will still be in the "to be removed" list so remove it if it is
226
Map JavaDoc<String JavaDoc, 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