1 package org
.argeo
.slc
.client
.ui
.dist
.commands
;
3 import javax
.jcr
.Credentials
;
5 import javax
.jcr
.Repository
;
6 import javax
.jcr
.RepositoryException
;
7 import javax
.jcr
.RepositoryFactory
;
8 import javax
.jcr
.Session
;
9 import javax
.jcr
.security
.Privilege
;
11 import org
.apache
.commons
.logging
.Log
;
12 import org
.apache
.commons
.logging
.LogFactory
;
13 import org
.argeo
.api
.security
.Keyring
;
14 import org
.argeo
.eclipse
.ui
.dialogs
.ErrorFeedback
;
15 import org
.argeo
.jcr
.JcrUtils
;
16 import org
.argeo
.slc
.SlcConstants
;
17 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
18 import org
.argeo
.slc
.client
.ui
.dist
.utils
.CommandHelpers
;
19 import org
.argeo
.slc
.repo
.RepoUtils
;
20 import org
.eclipse
.core
.commands
.AbstractHandler
;
21 import org
.eclipse
.core
.commands
.ExecutionEvent
;
22 import org
.eclipse
.core
.commands
.ExecutionException
;
23 import org
.eclipse
.jface
.dialogs
.Dialog
;
24 import org
.eclipse
.jface
.dialogs
.InputDialog
;
25 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
26 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
28 /** Create a new empty workspace in a remote repository */
29 public class CreateWorkspace
extends AbstractHandler
{
30 private static final Log log
= LogFactory
.getLog(CreateWorkspace
.class);
32 // Exposes commands meta-info
33 public final static String ID
= DistPlugin
.PLUGIN_ID
+ ".createWorkspace";
34 public final static String DEFAULT_LABEL
= "Create workspace...";
35 public final static ImageDescriptor DEFAULT_ICON
= DistPlugin
36 .getImageDescriptor("icons/addItem.gif");
39 public final static String PARAM_TARGET_REPO_PATH
= "targetRepoPath";
40 public final static String PARAM_WORKSPACE_PREFIX
= "workspacePrefix";
42 // DEPENDENCY INJECTION
43 private RepositoryFactory repositoryFactory
;
44 private Keyring keyring
;
45 private Repository nodeRepository
;
47 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
49 String targetRepoPath
= event
.getParameter(PARAM_TARGET_REPO_PATH
);
50 String prefix
= event
.getParameter(PARAM_WORKSPACE_PREFIX
);
52 Session nodeSession
= null;
53 Session session
= null;
55 nodeSession
= nodeRepository
.login();
56 Node repoNode
= nodeSession
.getNode(targetRepoPath
);
57 Repository repository
= RepoUtils
.getRepository(repositoryFactory
,
59 Credentials credentials
= RepoUtils
.getRepositoryCredentials(
62 // TODO : add an input validator
63 InputDialog inputDialog
= new InputDialog(HandlerUtil
64 .getActiveWorkbenchWindow(event
).getShell(),
66 "Choose a name for the workspace to create",
67 prefix
== null ?
"" : prefix
+ "-", null);
68 int result
= inputDialog
.open();
70 String enteredName
= inputDialog
.getValue();
72 final String legalChars
= "ABCDEFGHIJKLMNOPQRSTUVWXZY0123456789_";
73 char[] arr
= enteredName
.toUpperCase().toCharArray();
75 for (int i
= 0; i
< arr
.length
; i
++) {
76 if (legalChars
.indexOf(arr
[i
]) == -1)
82 if (log
.isTraceEnabled())
83 log
.trace("Translated workspace name length: " + count
84 + " (name: " + enteredName
+ " )");
87 ErrorFeedback
.show("Workspace name '" + enteredName
88 + "' is too long or contains"
89 + " too many special characters such as '.' or '-'.");
93 String workspaceName
= enteredName
;
96 if (result
== Dialog
.CANCEL
|| workspaceName
== null
97 || "".equals(workspaceName
.trim()))
100 session
= repository
.login(credentials
);
101 session
.getWorkspace().createWorkspace(workspaceName
);
102 JcrUtils
.logoutQuietly(session
);
103 // init new workspace
104 session
= repository
.login(credentials
, workspaceName
);
105 JcrUtils
.addPrivilege(session
, "/", SlcConstants
.ROLE_SLC
,
107 CommandHelpers
.callCommand(RefreshDistributionsView
.ID
);
108 if (log
.isTraceEnabled())
109 log
.trace("WORKSPACE " + workspaceName
+ " CREATED");
111 } catch (RepositoryException re
) {
113 "Unexpected error while creating the new workspace.", re
);
115 JcrUtils
.logoutQuietly(session
);
116 JcrUtils
.logoutQuietly(nodeSession
);
121 /* DEPENDENCY INJECTION */
122 public void setNodeRepository(Repository nodeRepository
) {
123 this.nodeRepository
= nodeRepository
;
126 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
127 this.repositoryFactory
= repositoryFactory
;
130 public void setKeyring(Keyring keyring
) {
131 this.keyring
= keyring
;