1 17 package org.alfresco.web.bean.clipboard; 18 19 import java.text.MessageFormat ; 20 import java.util.ArrayList ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import javax.faces.context.FacesContext; 25 import javax.faces.event.ActionEvent; 26 import javax.transaction.UserTransaction ; 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 49 public class ClipboardBean 50 { 51 54 57 public NodeService getNodeService() 58 { 59 return this.nodeService; 60 } 61 62 65 public void setNodeService(NodeService nodeService) 66 { 67 this.nodeService = nodeService; 68 } 69 70 73 public CopyService getNodeOperationsService() 74 { 75 return this.nodeOperationsService; 76 } 77 78 81 public void setNodeOperationsService(CopyService nodeOperationsService) 82 { 83 this.nodeOperationsService = nodeOperationsService; 84 } 85 86 89 public NavigationBean getNavigator() 90 { 91 return this.navigator; 92 } 93 94 97 public void setNavigator(NavigationBean navigator) 98 { 99 this.navigator = navigator; 100 } 101 102 105 public List <ClipboardItem> getItems() 106 { 107 return this.items; 108 } 109 110 113 public void setItems(List <ClipboardItem> items) 114 { 115 this.items = items; 116 } 117 118 119 122 125 public void copyNode(ActionEvent event) 126 { 127 UIActionLink link = (UIActionLink)event.getComponent(); 128 Map <String , String > params = link.getParameterMap(); 129 String id = params.get("id"); 130 if (id != null && id.length() != 0) 131 { 132 addClipboardNode(id, ClipboardStatus.COPY); 133 } 134 } 135 136 139 public void cutNode(ActionEvent event) 140 { 141 UIActionLink link = (UIActionLink)event.getComponent(); 142 Map <String , String > params = link.getParameterMap(); 143 String id = params.get("id"); 144 if (id != null && id.length() != 0) 145 { 146 addClipboardNode(id, ClipboardStatus.CUT); 147 } 148 } 149 150 153 public void pasteAll(ActionEvent event) 154 { 155 performPasteItems(-1); 156 } 157 158 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 ("Clipboard attempting paste a non existent item index: " + index); 169 } 170 171 performPasteItems(index); 172 } 173 174 179 private void performPasteItems(int index) 180 { 181 UserTransaction tx = null; 182 try 183 { 184 tx = Repository.getUserTransaction(FacesContext.getCurrentInstance()); 185 tx.begin(); 186 187 if (index == -1) 188 { 189 for (int i=0; i<this.items.size(); i++) 191 { 192 performClipboardOperation(this.items.get(i)); 193 } 194 List <ClipboardItem> newItems = new ArrayList <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 } 207 else 208 { 209 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 tx.commit(); 220 221 UIContextService.getInstance(FacesContext.getCurrentInstance()).notifyBeans(); 223 } 224 catch (Throwable err) 225 { 226 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 228 Utils.addErrorMessage(Application.getMessage( 229 FacesContext.getCurrentInstance(), MSG_ERROR_PASTE) + err.getMessage(), err); 230 } 231 } 232 233 238 private void performClipboardOperation(ClipboardItem item) 239 { 240 NodeRef parentRef = new NodeRef(Repository.getStoreRef(), 241 this.navigator.getCurrentNodeId()); 242 243 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 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 this.nodeService.moveNode( 271 item.Node.getNodeRef(), 272 parentRef, 273 ContentModel.ASSOC_CONTAINS, 274 assocRef.getQName()); 275 } 276 } 277 278 284 private void addClipboardNode(String id, ClipboardStatus mode) 285 { 286 try 287 { 288 NodeRef ref = new NodeRef(Repository.getStoreRef(), id); 289 290 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 items.set(i, item); 299 foundDuplicate = true; 300 break; 301 } 302 } 303 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 [] {id}) ); 313 } 314 } 315 316 317 320 private static Log logger = LogFactory.getLog(ClipboardBean.class); 321 322 323 private static final String MSG_ERROR_PASTE = "error_paste"; 324 325 326 protected NodeService nodeService; 327 328 329 protected CopyService nodeOperationsService; 330 331 332 protected NavigationBean navigator; 333 334 335 private List <ClipboardItem> items = new ArrayList <ClipboardItem>(4); 336 } 337 | Popular Tags |