2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.client
.ui
.dist
.commands
;
18 import javax
.jcr
.Credentials
;
19 import javax
.jcr
.Node
;
20 import javax
.jcr
.Repository
;
21 import javax
.jcr
.RepositoryException
;
22 import javax
.jcr
.RepositoryFactory
;
23 import javax
.jcr
.Session
;
24 import javax
.jcr
.security
.Privilege
;
26 import org
.argeo
.ArgeoException
;
27 import org
.argeo
.jcr
.JcrUtils
;
28 import org
.argeo
.slc
.SlcConstants
;
29 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
30 import org
.argeo
.slc
.client
.ui
.dist
.utils
.CommandHelpers
;
31 import org
.argeo
.slc
.repo
.RepoUtils
;
32 import org
.argeo
.util
.security
.Keyring
;
33 import org
.eclipse
.core
.commands
.AbstractHandler
;
34 import org
.eclipse
.core
.commands
.ExecutionEvent
;
35 import org
.eclipse
.core
.commands
.ExecutionException
;
36 import org
.eclipse
.jface
.dialogs
.InputDialog
;
37 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
38 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
41 * Create a copy of the chosen workspace in the current repository.
44 public class CopyWorkspace
extends AbstractHandler
{
45 // private static final Log log = LogFactory.getLog(CopyWorkspace.class);
46 public final static String ID
= DistPlugin
.ID
+ ".copyWorkspace";
47 public final static String DEFAULT_LABEL
= "Duplicate";
48 public final static String PARAM_SOURCE_WORKSPACE_NAME
= "srcWkspName";
49 public final static String PARAM_TARGET_REPO_PATH
= "targetRepoPath";
50 public final static ImageDescriptor DEFAULT_ICON
= DistPlugin
51 .getImageDescriptor("icons/addItem.gif");
53 // DEPENDENCY INJECTION
54 private RepositoryFactory repositoryFactory
;
55 private Keyring keyring
;
56 private Repository nodeRepository
;
58 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
60 String targetRepoPath
= event
.getParameter(PARAM_TARGET_REPO_PATH
);
61 String wkspName
= event
.getParameter(PARAM_SOURCE_WORKSPACE_NAME
);
63 Session nodeSession
= null;
64 Session srcSession
= null;
65 Session newSession
= null;
67 nodeSession
= nodeRepository
.login();
68 Node repoNode
= nodeSession
.getNode(targetRepoPath
);
69 Repository repository
= RepoUtils
.getRepository(repositoryFactory
,
71 Credentials credentials
= RepoUtils
.getRepositoryCredentials(
74 InputDialog inputDialog
= new InputDialog(HandlerUtil
75 .getActiveWorkbenchWindow(event
).getShell(),
76 "New copy of workspace " + wkspName
,
77 "Choose a name for the workspace to create", "", null);
79 String newWorkspaceName
= inputDialog
.getValue();
80 srcSession
= repository
.login(credentials
, wkspName
);
82 // Create the workspace
83 srcSession
.getWorkspace().createWorkspace(newWorkspaceName
);
84 Node srcRootNode
= srcSession
.getRootNode();
85 // log in the newly created workspace
86 newSession
= repository
.login(credentials
, newWorkspaceName
);
87 Node newRootNode
= newSession
.getRootNode();
88 RepoUtils
.copy(srcRootNode
, newRootNode
);
90 JcrUtils
.addPrivilege(newSession
, "/", SlcConstants
.ROLE_SLC
,
92 CommandHelpers
.callCommand(RefreshDistributionsView
.ID
);
93 } catch (RepositoryException re
) {
94 throw new ArgeoException(
95 "Unexpected error while creating the new workspace.", re
);
97 JcrUtils
.logoutQuietly(newSession
);
98 JcrUtils
.logoutQuietly(srcSession
);
99 JcrUtils
.logoutQuietly(nodeSession
);
104 /* DEPENDENCY INJECTION */
105 public void setNodeRepository(Repository nodeRepository
) {
106 this.nodeRepository
= nodeRepository
;
109 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
110 this.repositoryFactory
= repositoryFactory
;
113 public void setKeyring(Keyring keyring
) {
114 this.keyring
= keyring
;