1 package org
.argeo
.slc
.client
.ui
.dist
.commands
;
4 import javax
.jcr
.NodeIterator
;
5 import javax
.jcr
.Property
;
6 import javax
.jcr
.PropertyIterator
;
7 import javax
.jcr
.Repository
;
8 import javax
.jcr
.RepositoryException
;
9 import javax
.jcr
.nodetype
.NodeType
;
11 import org
.apache
.commons
.logging
.Log
;
12 import org
.apache
.commons
.logging
.LogFactory
;
13 import org
.argeo
.ArgeoException
;
14 import org
.argeo
.jcr
.JcrUtils
;
15 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
16 import org
.eclipse
.core
.commands
.AbstractHandler
;
17 import org
.eclipse
.core
.commands
.ExecutionEvent
;
18 import org
.eclipse
.core
.commands
.ExecutionException
;
19 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
22 * Create a copy of the chosen workspace in the current repository.
25 public class CopyWorkspace
extends AbstractHandler
{
26 private static final Log log
= LogFactory
.getLog(CopyWorkspace
.class);
27 public final static String ID
= DistPlugin
.ID
+ ".copyWorkspace";
28 public final static String PARAM_WORKSPACE_NAME
= DistPlugin
.ID
30 public final static String DEFAULT_LABEL
= "Copy this workspace";
31 public final static String DEFAULT_ICON_PATH
= "icons/addItem.gif";
33 /* DEPENDENCY INJECTION */
34 private Repository repository
;
36 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
37 String srcWorkspaceName
= event
.getParameter(PARAM_WORKSPACE_NAME
);
39 if (log
.isTraceEnabled())
40 log
.debug("WORKSPACE " + srcWorkspaceName
+ " About to be copied");
42 MessageDialog
.openWarning(DistPlugin
.getDefault()
43 .getWorkbench().getDisplay().getActiveShell(),
44 "WARNING", "Not yet implemented");
48 // IWorkbenchWindow iww = DistPlugin.getDefault().getWorkbench()
49 // .getActiveWorkbenchWindow();
50 // InputDialog inputDialog = new InputDialog(iww.getShell(),
51 // "New copy of the current workspace",
52 // "Choose a name for the workspace to create", "", null);
53 // inputDialog.open();
54 // String newWorkspaceName = inputDialog.getValue();
55 // Session srcSession = null;
56 // Session newSession = null;
58 // srcSession = repository.login(srcWorkspaceName);
59 // // FIXME: simple call to Workspace.create(newName, oldName) does not
62 // srcSession.getWorkspace().createWorkspace(newWorkspaceName,
65 // // // Create the workspace
66 // // srcSession.getWorkspace().createWorkspace(newWorkspaceName);
67 // // Node srcRootNode = srcSession.getRootNode();
68 // // // log in the newly created workspace
69 // // newSession = repository.login(newWorkspaceName);
70 // // newSession.save();
71 // // Node newRootNode = newSession.getRootNode();
72 // // copy(srcRootNode, newRootNode);
73 // // newSession.save();
75 // CommandHelpers.callCommand(RefreshDistributionsView.ID);
76 // } catch (RepositoryException re) {
77 // throw new ArgeoException(
78 // "Unexpected error while creating the new workspace.", re);
80 // if (srcSession != null)
81 // srcSession.logout();
82 // if (newSession != null)
83 // newSession.logout();
88 // FIXME : commons is frozen, cannot fix the problem directly there.
89 // test and report corresponding patch
90 private void copy(Node fromNode
, Node toNode
) {
93 // cannot manipulate security nodes this way:
94 if (fromNode
.isNodeType("rep:ACL"))
97 PropertyIterator pit
= fromNode
.getProperties();
98 properties
: while (pit
.hasNext()) {
99 Property fromProperty
= pit
.nextProperty();
100 String propertyName
= fromProperty
.getName();
101 if (toNode
.hasProperty(propertyName
)
102 && toNode
.getProperty(propertyName
).getDefinition()
106 if (fromProperty
.getDefinition().isProtected())
109 if (propertyName
.equals("jcr:created")
110 || propertyName
.equals("jcr:createdBy")
111 || propertyName
.equals("jcr:lastModified")
112 || propertyName
.equals("jcr:lastModifiedBy"))
115 if (fromProperty
.isMultiple()) {
116 toNode
.setProperty(propertyName
, fromProperty
.getValues());
118 toNode
.setProperty(propertyName
, fromProperty
.getValue());
122 // update jcr:lastModified and jcr:lastModifiedBy in toNode in case
123 // they existed, before adding the mixins
124 if (!toNode
.getDefinition().isProtected())
125 JcrUtils
.updateLastModified(toNode
);
128 for (NodeType mixinType
: fromNode
.getMixinNodeTypes()) {
129 toNode
.addMixin(mixinType
.getName());
132 // process children nodes
133 NodeIterator nit
= fromNode
.getNodes();
134 while (nit
.hasNext()) {
135 Node fromChild
= nit
.nextNode();
136 Integer index
= fromChild
.getIndex();
137 String nodeRelPath
= fromChild
.getName() + "[" + index
+ "]";
139 if (toNode
.hasNode(nodeRelPath
))
140 toChild
= toNode
.getNode(nodeRelPath
);
142 toChild
= toNode
.addNode(fromChild
.getName(), fromChild
143 .getPrimaryNodeType().getName());
144 copy(fromChild
, toChild
);
147 toNode
.getSession().save();
148 } catch (RepositoryException e
) {
149 throw new ArgeoException("Cannot copy " + fromNode
+ " to "
154 /* DEPENDENCY INJECTION */
155 public void setRepository(Repository repository
) {
156 this.repository
= repository
;