]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.client.ui.dist/src/org/argeo/slc/client/ui/dist/commands/CreateWorkspace.java
Adapt to changes in Argeo Commons
[gpl/argeo-slc.git] / legacy / org.argeo.slc.client.ui.dist / src / org / argeo / slc / client / ui / dist / commands / CreateWorkspace.java
1 package org.argeo.slc.client.ui.dist.commands;
2
3 import javax.jcr.Credentials;
4 import javax.jcr.Node;
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;
10
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;
27
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);
31
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");
37
38 // Parameters
39 public final static String PARAM_TARGET_REPO_PATH = "targetRepoPath";
40 public final static String PARAM_WORKSPACE_PREFIX = "workspacePrefix";
41
42 // DEPENDENCY INJECTION
43 private RepositoryFactory repositoryFactory;
44 private Keyring keyring;
45 private Repository nodeRepository;
46
47 public Object execute(ExecutionEvent event) throws ExecutionException {
48
49 String targetRepoPath = event.getParameter(PARAM_TARGET_REPO_PATH);
50 String prefix = event.getParameter(PARAM_WORKSPACE_PREFIX);
51
52 Session nodeSession = null;
53 Session session = null;
54 try {
55 nodeSession = nodeRepository.login();
56 Node repoNode = nodeSession.getNode(targetRepoPath);
57 Repository repository = RepoUtils.getRepository(repositoryFactory,
58 keyring, repoNode);
59 Credentials credentials = RepoUtils.getRepositoryCredentials(
60 keyring, repoNode);
61
62 // TODO : add an input validator
63 InputDialog inputDialog = new InputDialog(HandlerUtil
64 .getActiveWorkbenchWindow(event).getShell(),
65 "Workspace name?",
66 "Choose a name for the workspace to create",
67 prefix == null ? "" : prefix + "-", null);
68 int result = inputDialog.open();
69
70 String enteredName = inputDialog.getValue();
71
72 final String legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXZY0123456789_";
73 char[] arr = enteredName.toUpperCase().toCharArray();
74 int count = 0;
75 for (int i = 0; i < arr.length; i++) {
76 if (legalChars.indexOf(arr[i]) == -1)
77 count = count + 7;
78 else
79 count++;
80 }
81
82 if (log.isTraceEnabled())
83 log.trace("Translated workspace name length: " + count
84 + " (name: " + enteredName + " )");
85
86 if (count > 60) {
87 ErrorFeedback.show("Workspace name '" + enteredName
88 + "' is too long or contains"
89 + " too many special characters such as '.' or '-'.");
90 return null;
91 }
92
93 String workspaceName = enteredName;
94
95 // Canceled by user
96 if (result == Dialog.CANCEL || workspaceName == null
97 || "".equals(workspaceName.trim()))
98 return null;
99
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,
106 Privilege.JCR_ALL);
107 CommandHelpers.callCommand(RefreshDistributionsView.ID);
108 if (log.isTraceEnabled())
109 log.trace("WORKSPACE " + workspaceName + " CREATED");
110
111 } catch (RepositoryException re) {
112 ErrorFeedback.show(
113 "Unexpected error while creating the new workspace.", re);
114 } finally {
115 JcrUtils.logoutQuietly(session);
116 JcrUtils.logoutQuietly(nodeSession);
117 }
118 return null;
119 }
120
121 /* DEPENDENCY INJECTION */
122 public void setNodeRepository(Repository nodeRepository) {
123 this.nodeRepository = nodeRepository;
124 }
125
126 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
127 this.repositoryFactory = repositoryFactory;
128 }
129
130 public void setKeyring(Keyring keyring) {
131 this.keyring = keyring;
132 }
133 }