]> 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/CreateWorkspace.java
Fix licence management
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / commands / CreateWorkspace.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.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;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.argeo.eclipse.ui.ErrorFeedback;
29 import org.argeo.jcr.JcrUtils;
30 import org.argeo.slc.SlcConstants;
31 import org.argeo.slc.client.ui.dist.DistPlugin;
32 import org.argeo.slc.client.ui.dist.utils.CommandHelpers;
33 import org.argeo.slc.repo.RepoUtils;
34 import org.argeo.util.security.Keyring;
35 import org.eclipse.core.commands.AbstractHandler;
36 import org.eclipse.core.commands.ExecutionEvent;
37 import org.eclipse.core.commands.ExecutionException;
38 import org.eclipse.jface.dialogs.Dialog;
39 import org.eclipse.jface.dialogs.InputDialog;
40 import org.eclipse.jface.resource.ImageDescriptor;
41 import org.eclipse.ui.handlers.HandlerUtil;
42
43 /**
44 * Create a new empty workspace in a remote repository.
45 */
46 public class CreateWorkspace extends AbstractHandler {
47 private static final Log log = LogFactory.getLog(CreateWorkspace.class);
48
49 // Exposes commands meta-info
50 public final static String ID = DistPlugin.ID + ".createWorkspace";
51 public final static String DEFAULT_LABEL = "Create workspace...";
52 public final static ImageDescriptor DEFAULT_ICON = DistPlugin
53 .getImageDescriptor("icons/addItem.gif");
54
55 // Parameters
56 public final static String PARAM_TARGET_REPO_PATH = "targetRepoPath";
57 public final static String PARAM_WORKSPACE_PREFIX = "workspacePrefix";
58
59 // DEPENDENCY INJECTION
60 private RepositoryFactory repositoryFactory;
61 private Keyring keyring;
62 private Repository nodeRepository;
63
64 public Object execute(ExecutionEvent event) throws ExecutionException {
65
66 String targetRepoPath = event.getParameter(PARAM_TARGET_REPO_PATH);
67 String prefix = event.getParameter(PARAM_WORKSPACE_PREFIX);
68
69 Session nodeSession = null;
70 Session session = null;
71 try {
72 nodeSession = nodeRepository.login();
73 Node repoNode = nodeSession.getNode(targetRepoPath);
74 Repository repository = RepoUtils.getRepository(repositoryFactory,
75 keyring, repoNode);
76 Credentials credentials = RepoUtils.getRepositoryCredentials(
77 keyring, repoNode);
78
79 // TODO : add an input validator
80 InputDialog inputDialog = new InputDialog(HandlerUtil
81 .getActiveWorkbenchWindow(event).getShell(),
82 "Workspace name?",
83 "Choose a name for the workspace to create",
84 prefix == null ? "" : prefix + "-", null);
85 int result = inputDialog.open();
86
87 String enteredName = inputDialog.getValue();
88
89 final String legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXZY0123456789_";
90 char[] arr = enteredName.toUpperCase().toCharArray();
91 int count = 0;
92 for (int i = 0; i < arr.length; i++) {
93 if (legalChars.indexOf(arr[i]) == -1)
94 count = count + 7;
95 else
96 count++;
97 }
98
99 if (log.isTraceEnabled())
100 log.trace("Translated workspace name length: " + count
101 + " (name: " + enteredName + " )");
102
103 if (count > 60) {
104 ErrorFeedback.show("Workspace name '" + enteredName
105 + "' is too long or contains"
106 + " too many special characters such as '.' or '-'.");
107 return null;
108 }
109
110 String workspaceName = enteredName;
111
112 // Canceled by user
113 if (result == Dialog.CANCEL || workspaceName == null
114 || "".equals(workspaceName.trim()))
115 return null;
116
117 session = repository.login(credentials);
118 session.getWorkspace().createWorkspace(workspaceName);
119 JcrUtils.logoutQuietly(session);
120 // init new workspace
121 session = repository.login(credentials, workspaceName);
122 JcrUtils.addPrivilege(session, "/", SlcConstants.ROLE_SLC,
123 Privilege.JCR_ALL);
124 CommandHelpers.callCommand(RefreshDistributionsView.ID);
125 if (log.isTraceEnabled())
126 log.trace("WORKSPACE " + workspaceName + " CREATED");
127
128 } catch (RepositoryException re) {
129 ErrorFeedback.show(
130 "Unexpected error while creating the new workspace.", re);
131 } finally {
132 JcrUtils.logoutQuietly(session);
133 JcrUtils.logoutQuietly(nodeSession);
134 }
135 return null;
136 }
137
138 /* DEPENDENCY INJECTION */
139 public void setNodeRepository(Repository nodeRepository) {
140 this.nodeRepository = nodeRepository;
141 }
142
143 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
144 this.repositoryFactory = repositoryFactory;
145 }
146
147 public void setKeyring(Keyring keyring) {
148 this.keyring = keyring;
149 }
150 }