]> git.argeo.org Git - gpl/argeo-slc.git/blob - eclipse/plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/commands/CopyWorkspace.java
Add a few functionalities to dist UI
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / commands / CopyWorkspace.java
1 package org.argeo.slc.client.ui.dist.commands;
2
3 import javax.jcr.Node;
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;
10
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;
20
21 /**
22 * Create a copy of the chosen workspace in the current repository.
23 */
24
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
29 + ".workspaceName";
30 public final static String DEFAULT_LABEL = "Copy this workspace";
31 public final static String DEFAULT_ICON_PATH = "icons/addItem.gif";
32
33 /* DEPENDENCY INJECTION */
34 private Repository repository;
35
36 public Object execute(ExecutionEvent event) throws ExecutionException {
37 String srcWorkspaceName = event.getParameter(PARAM_WORKSPACE_NAME);
38
39 if (log.isTraceEnabled())
40 log.debug("WORKSPACE " + srcWorkspaceName + " About to be copied");
41
42 MessageDialog.openWarning(DistPlugin.getDefault()
43 .getWorkbench().getDisplay().getActiveShell(),
44 "WARNING", "Not yet implemented");
45 return null;
46 //
47 //
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;
57 // try {
58 // srcSession = repository.login(srcWorkspaceName);
59 // // FIXME: simple call to Workspace.create(newName, oldName) does not
60 // // work
61 //
62 // srcSession.getWorkspace().createWorkspace(newWorkspaceName,
63 // srcWorkspaceName);
64 //
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();
74 //
75 // CommandHelpers.callCommand(RefreshDistributionsView.ID);
76 // } catch (RepositoryException re) {
77 // throw new ArgeoException(
78 // "Unexpected error while creating the new workspace.", re);
79 // } finally {
80 // if (srcSession != null)
81 // srcSession.logout();
82 // if (newSession != null)
83 // newSession.logout();
84 // }
85 // return null;
86 }
87
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) {
91
92 try {
93 // cannot manipulate security nodes this way:
94 if (fromNode.isNodeType("rep:ACL"))
95 return;
96 // process properties
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()
103 .isProtected())
104 continue properties;
105
106 if (fromProperty.getDefinition().isProtected())
107 continue properties;
108
109 if (propertyName.equals("jcr:created")
110 || propertyName.equals("jcr:createdBy")
111 || propertyName.equals("jcr:lastModified")
112 || propertyName.equals("jcr:lastModifiedBy"))
113 continue properties;
114
115 if (fromProperty.isMultiple()) {
116 toNode.setProperty(propertyName, fromProperty.getValues());
117 } else {
118 toNode.setProperty(propertyName, fromProperty.getValue());
119 }
120 }
121
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);
126
127 // add mixins
128 for (NodeType mixinType : fromNode.getMixinNodeTypes()) {
129 toNode.addMixin(mixinType.getName());
130 }
131
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 + "]";
138 Node toChild;
139 if (toNode.hasNode(nodeRelPath))
140 toChild = toNode.getNode(nodeRelPath);
141 else
142 toChild = toNode.addNode(fromChild.getName(), fromChild
143 .getPrimaryNodeType().getName());
144 copy(fromChild, toChild);
145 }
146
147 toNode.getSession().save();
148 } catch (RepositoryException e) {
149 throw new ArgeoException("Cannot copy " + fromNode + " to "
150 + toNode, e);
151 }
152 }
153
154 /* DEPENDENCY INJECTION */
155 public void setRepository(Repository repository) {
156 this.repository = repository;
157 }
158 }