]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/commands/CopyWorkspace.java
+ Add repo informations
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / commands / CopyWorkspace.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.client.ui.dist.commands;
17
18 import javax.jcr.Node;
19 import javax.jcr.Repository;
20 import javax.jcr.RepositoryException;
21 import javax.jcr.Session;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.argeo.ArgeoException;
26 import org.argeo.slc.client.ui.dist.DistPlugin;
27 import org.argeo.slc.client.ui.dist.utils.CommandHelpers;
28 import org.argeo.slc.repo.RepoUtils;
29 import org.eclipse.core.commands.AbstractHandler;
30 import org.eclipse.core.commands.ExecutionEvent;
31 import org.eclipse.core.commands.ExecutionException;
32 import org.eclipse.jface.dialogs.InputDialog;
33 import org.eclipse.ui.IWorkbenchWindow;
34
35 /**
36 * Create a copy of the chosen workspace in the current repository.
37 */
38
39 public class CopyWorkspace extends AbstractHandler {
40 private static final Log log = LogFactory.getLog(CopyWorkspace.class);
41 public final static String ID = DistPlugin.ID + ".copyWorkspace";
42 public final static String PARAM_WORKSPACE_NAME = DistPlugin.ID
43 + ".workspaceName";
44 public final static String DEFAULT_LABEL = "Duplicate";
45 public final static String DEFAULT_ICON_PATH = "icons/addItem.gif";
46
47 /* DEPENDENCY INJECTION */
48 private Repository repository;
49
50 public Object execute(ExecutionEvent event) throws ExecutionException {
51 String srcWorkspaceName = event.getParameter(PARAM_WORKSPACE_NAME);
52
53 if (log.isTraceEnabled())
54 log.debug("WORKSPACE " + srcWorkspaceName + " About to be copied");
55
56 // MessageDialog.openWarning(DistPlugin.getDefault()
57 // .getWorkbench().getDisplay().getActiveShell(),
58 // "WARNING", "Not yet implemented");
59 // return null;
60
61 IWorkbenchWindow iww = DistPlugin.getDefault().getWorkbench()
62 .getActiveWorkbenchWindow();
63 InputDialog inputDialog = new InputDialog(iww.getShell(),
64 "New copy of the current workspace",
65 "Choose a name for the workspace to create", "", null);
66 inputDialog.open();
67 String newWorkspaceName = inputDialog.getValue();
68 Session srcSession = null;
69 Session newSession = null;
70 try {
71 srcSession = repository.login(srcWorkspaceName);
72
73 // Create the workspace
74 srcSession.getWorkspace().createWorkspace(newWorkspaceName);
75 Node srcRootNode = srcSession.getRootNode();
76 // log in the newly created workspace
77 newSession = repository.login(newWorkspaceName);
78 // newSession.save();
79 Node newRootNode = newSession.getRootNode();
80 RepoUtils.copy(srcRootNode, newRootNode);
81 newSession.save();
82
83 CommandHelpers.callCommand(RefreshDistributionsView.ID);
84 } catch (RepositoryException re) {
85 throw new ArgeoException(
86 "Unexpected error while creating the new workspace.", re);
87 } finally {
88 if (srcSession != null)
89 srcSession.logout();
90 if (newSession != null)
91 newSession.logout();
92 }
93 return null;
94 }
95
96 /* DEPENDENCY INJECTION */
97 public void setRepository(Repository repository) {
98 this.repository = repository;
99 }
100 }