KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > bean > clipboard > ClipboardBean


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.bean.clipboard;
18
19 import java.text.MessageFormat JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.faces.context.FacesContext;
25 import javax.faces.event.ActionEvent;
26 import javax.transaction.UserTransaction JavaDoc;
27
28 import org.alfresco.model.ContentModel;
29 import org.alfresco.service.cmr.dictionary.DictionaryService;
30 import org.alfresco.service.cmr.repository.ChildAssociationRef;
31 import org.alfresco.service.cmr.repository.CopyService;
32 import org.alfresco.service.cmr.repository.InvalidNodeRefException;
33 import org.alfresco.service.cmr.repository.NodeRef;
34 import org.alfresco.service.cmr.repository.NodeService;
35 import org.alfresco.web.app.Application;
36 import org.alfresco.web.app.context.UIContextService;
37 import org.alfresco.web.bean.NavigationBean;
38 import org.alfresco.web.bean.repository.Node;
39 import org.alfresco.web.bean.repository.Repository;
40 import org.alfresco.web.ui.common.Utils;
41 import org.alfresco.web.ui.common.component.UIActionLink;
42 import org.alfresco.web.ui.repo.component.shelf.UIClipboardShelfItem;
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45
46 /**
47  * @author Kevin Roast
48  */

49 public class ClipboardBean
50 {
51    // ------------------------------------------------------------------------------
52
// Bean property getters and setters
53

54    /**
55     * @return Returns the NodeService.
56     */

57    public NodeService getNodeService()
58    {
59       return this.nodeService;
60    }
61
62    /**
63     * @param nodeService The NodeService to set.
64     */

65    public void setNodeService(NodeService nodeService)
66    {
67       this.nodeService = nodeService;
68    }
69    
70    /**
71     * @return Returns the NodeOperationsService.
72     */

73    public CopyService getNodeOperationsService()
74    {
75       return this.nodeOperationsService;
76    }
77
78    /**
79     * @param nodeOperationsService The NodeOperationsService to set.
80     */

81    public void setNodeOperationsService(CopyService nodeOperationsService)
82    {
83       this.nodeOperationsService = nodeOperationsService;
84    }
85    
86    /**
87     * @return Returns the navigation bean instance.
88     */

89    public NavigationBean getNavigator()
90    {
91       return this.navigator;
92    }
93    
94    /**
95     * @param navigator The NavigationBean to set.
96     */

97    public void setNavigator(NavigationBean navigator)
98    {
99       this.navigator = navigator;
100    }
101    
102    /**
103     * @return Returns the clipboard items.
104     */

105    public List JavaDoc<ClipboardItem> getItems()
106    {
107       return this.items;
108    }
109    
110    /**
111     * @param items The clipboard items to set.
112     */

113    public void setItems(List JavaDoc<ClipboardItem> items)
114    {
115       this.items = items;
116    }
117    
118    
119    // ------------------------------------------------------------------------------
120
// Navigation action event handlers
121

122    /**
123     * Action handler called to add a node to the clipboard for a Copy operation
124     */

125    public void copyNode(ActionEvent event)
126    {
127       UIActionLink link = (UIActionLink)event.getComponent();
128       Map JavaDoc<String JavaDoc, String JavaDoc> params = link.getParameterMap();
129       String JavaDoc id = params.get("id");
130       if (id != null && id.length() != 0)
131       {
132          addClipboardNode(id, ClipboardStatus.COPY);
133       }
134    }
135    
136    /**
137     * Action handler called to add a node to the clipboard for a Cut operation
138     */

139    public void cutNode(ActionEvent event)
140    {
141       UIActionLink link = (UIActionLink)event.getComponent();
142       Map JavaDoc<String JavaDoc, String JavaDoc> params = link.getParameterMap();
143       String JavaDoc id = params.get("id");
144       if (id != null && id.length() != 0)
145       {
146          addClipboardNode(id, ClipboardStatus.CUT);
147       }
148    }
149    
150    /**
151     * Action handler call from the browse screen to Paste All clipboard items into the current Space
152     */

153    public void pasteAll(ActionEvent event)
154    {
155       performPasteItems(-1);
156    }
157    
158    /**
159     * Action handler called to paste one or all items from the clipboard
160     */

161    public void pasteItem(ActionEvent event)
162    {
163       UIClipboardShelfItem.ClipboardEvent clipEvent = (UIClipboardShelfItem.ClipboardEvent)event;
164       
165       int index = clipEvent.Index;
166       if (index >= this.items.size())
167       {
168          throw new IllegalStateException JavaDoc("Clipboard attempting paste a non existent item index: " + index);
169       }
170       
171       performPasteItems(index);
172    }
173    
174    /**
175     * Perform a paste for the specified clipboard item(s)
176     *
177     * @param index of clipboard item to paste or -1 for all
178     */

179    private void performPasteItems(int index)
180    {
181       UserTransaction JavaDoc tx = null;
182       try
183       {
184          tx = Repository.getUserTransaction(FacesContext.getCurrentInstance());
185          tx.begin();
186          
187          if (index == -1)
188          {
189             // paste all
190
for (int i=0; i<this.items.size(); i++)
191             {
192                performClipboardOperation(this.items.get(i));
193             }
194             // remove the cut operation item from the clipboard
195
List JavaDoc<ClipboardItem> newItems = new ArrayList JavaDoc<ClipboardItem>(this.items.size());
196             for (int i=0; i<this.items.size(); i++)
197             {
198                ClipboardItem item = this.items.get(i);
199                if (item.Mode != ClipboardStatus.CUT)
200                {
201                   newItems.add(item);
202                }
203             }
204             setItems(newItems);
205             // TODO: after a paste all - remove items from the clipboard...? or not. ask linton
206
}
207          else
208          {
209             // single paste operation
210
ClipboardItem item = this.items.get(index);
211             performClipboardOperation(item);
212             if (item.Mode == ClipboardStatus.CUT)
213             {
214                this.items.remove(index);
215             }
216          }
217          
218          // commit the transaction
219
tx.commit();
220          
221          // refresh UI on success
222
UIContextService.getInstance(FacesContext.getCurrentInstance()).notifyBeans();
223       }
224       catch (Throwable JavaDoc err)
225       {
226          // rollback the transaction
227
try { if (tx != null) {tx.rollback();} } catch (Exception JavaDoc tex) {}
228          Utils.addErrorMessage(Application.getMessage(
229                FacesContext.getCurrentInstance(), MSG_ERROR_PASTE) + err.getMessage(), err);
230       }
231    }
232
233    /**
234     * Perform the operation for the specified clipboard item
235     *
236     * @param item
237     */

238    private void performClipboardOperation(ClipboardItem item)
239    {
240       NodeRef parentRef = new NodeRef(Repository.getStoreRef(),
241             this.navigator.getCurrentNodeId());
242       
243       // TODO: should we use primary parent here?
244
// The problem is we can't pass round ChildAssocRefs as form params etc. in the UI!
245
// It's tricky if we need to pass childassocref around everywhere...!
246
ChildAssociationRef assocRef = this.nodeService.getPrimaryParent(item.Node.getNodeRef());
247       
248       if (item.Mode == ClipboardStatus.COPY)
249       {
250          if (logger.isDebugEnabled())
251             logger.debug("Trying to copy node ID: " + item.Node.getId() + " into node ID: " + parentRef.getId());
252          
253          // call the node ops service to initiate the copy
254
// TODO: should the assoc qname be derived from the type...?
255
DictionaryService dd = Repository.getServiceRegistry(FacesContext.getCurrentInstance()).getDictionaryService();
256          boolean copyChildren = dd.isSubClass(item.Node.getType(), ContentModel.TYPE_FOLDER);
257          NodeRef copyRef = this.nodeOperationsService.copy(
258                item.Node.getNodeRef(),
259                parentRef,
260                ContentModel.ASSOC_CONTAINS,
261                assocRef.getQName(),
262                copyChildren);
263       }
264       else
265       {
266          if (logger.isDebugEnabled())
267             logger.debug("Trying to move node ID: " + item.Node.getId() + " into node ID: " + parentRef.getId());
268          
269          // move the node
270
this.nodeService.moveNode(
271                item.Node.getNodeRef(),
272                parentRef,
273                ContentModel.ASSOC_CONTAINS,
274                assocRef.getQName());
275       }
276    }
277    
278    /**
279     * Add a clipboard node for an operation to the clipboard
280     *
281     * @param id ID of the node for the operation
282     * @param mode ClipboardStatus for the operation
283     */

284    private void addClipboardNode(String JavaDoc id, ClipboardStatus mode)
285    {
286       try
287       {
288          NodeRef ref = new NodeRef(Repository.getStoreRef(), id);
289          
290          // check for duplicates first
291
ClipboardItem item = new ClipboardItem(new Node(ref), mode);
292          boolean foundDuplicate = false;
293          for (int i=0; i<items.size(); i++)
294          {
295             if (items.get(i).equals(item))
296             {
297                // found a duplicate replace with new instance as copy mode may have changed
298
items.set(i, item);
299                foundDuplicate = true;
300                break;
301             }
302          }
303          // if duplicate not found, then append to list
304
if (foundDuplicate == false)
305          {
306             items.add(item);
307          }
308       }
309       catch (InvalidNodeRefException refErr)
310       {
311          Utils.addErrorMessage(MessageFormat.format(Application.getMessage(
312                FacesContext.getCurrentInstance(), Repository.ERROR_NODEREF), new Object JavaDoc[] {id}) );
313       }
314    }
315    
316    
317    // ------------------------------------------------------------------------------
318
// Private data
319

320    private static Log logger = LogFactory.getLog(ClipboardBean.class);
321    
322    /** I18N messages */
323    private static final String JavaDoc MSG_ERROR_PASTE = "error_paste";
324    
325    /** The NodeService to be used by the bean */
326    protected NodeService nodeService;
327    
328    /** The NodeOperationsService to be used by the bean */
329    protected CopyService nodeOperationsService;
330    
331    /** The NavigationBean reference */
332    protected NavigationBean navigator;
333    
334    /** Current state of the clipboard items */
335    private List JavaDoc<ClipboardItem> items = new ArrayList JavaDoc<ClipboardItem>(4);
336 }
337
Popular Tags