]> 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
Do not clean OSGi runtime
[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.ArgeoException;
29 import org.argeo.jcr.JcrUtils;
30 import org.argeo.slc.client.ui.dist.DistPlugin;
31 import org.argeo.slc.client.ui.dist.utils.CommandHelpers;
32 import org.argeo.slc.repo.RepoUtils;
33 import org.argeo.util.security.Keyring;
34 import org.eclipse.core.commands.AbstractHandler;
35 import org.eclipse.core.commands.ExecutionEvent;
36 import org.eclipse.core.commands.ExecutionException;
37 import org.eclipse.jface.dialogs.Dialog;
38 import org.eclipse.jface.dialogs.InputDialog;
39 import org.eclipse.ui.handlers.HandlerUtil;
40
41 /**
42 * Create a new empty workspace in the current repository.
43 */
44
45 public class CreateWorkspace extends AbstractHandler {
46 private static final Log log = LogFactory.getLog(CreateWorkspace.class);
47 public final static String ID = DistPlugin.ID + ".createWorkspace";
48 public final static String PARAM_TARGET_REPO_PATH = "targetRepoPath";
49 public final static String PARAM_WORKSPACE_PREFIX = "workspacePrefix";
50 public final static String DEFAULT_LABEL = "Create workspace...";
51 public final static String DEFAULT_ICON_PATH = "icons/addItem.gif";
52
53 private String slcRole = "ROLE_SLC";
54
55 // DEPENDENCY INJECTION
56 private RepositoryFactory repositoryFactory;
57 private Keyring keyring;
58 private Repository nodeRepository;
59
60 public Object execute(ExecutionEvent event) throws ExecutionException {
61
62 String targetRepoPath = event.getParameter(PARAM_TARGET_REPO_PATH);
63 String prefix = event.getParameter(PARAM_WORKSPACE_PREFIX);
64
65 Session nodeSession = null;
66 Session session = null;
67 try {
68 nodeSession = nodeRepository.login();
69 Node repoNode = nodeSession.getNode(targetRepoPath);
70 Repository repository = RepoUtils.getRepository(repositoryFactory,
71 keyring, repoNode);
72 Credentials credentials = RepoUtils.getRepositoryCredentials(
73 keyring, repoNode);
74
75 // TODO : add an input validator
76 InputDialog inputDialog = new InputDialog(HandlerUtil
77 .getActiveWorkbenchWindow(event).getShell(),
78 "Workspace name?",
79 "Choose a name for the workspace to create",
80 prefix == null ? "" : prefix + "-", null);
81 int result = inputDialog.open();
82
83 String workspaceName = inputDialog.getValue();
84
85 // Canceled by user
86 if (result == Dialog.CANCEL || workspaceName == null
87 || "".equals(workspaceName.trim()))
88 return null;
89
90 session = repository.login(credentials);
91 session.getWorkspace().createWorkspace(workspaceName);
92 JcrUtils.logoutQuietly(session);
93 // init new workspace
94 session = repository.login(credentials, workspaceName);
95 JcrUtils.addPrivilege(session, "/", slcRole, Privilege.JCR_ALL);
96 CommandHelpers.callCommand(RefreshDistributionsView.ID);
97 if (log.isTraceEnabled())
98 log.trace("WORKSPACE " + workspaceName + " CREATED");
99
100 } catch (RepositoryException re) {
101 throw new ArgeoException(
102 "Unexpected error while creating the new workspace.", re);
103 } finally {
104 JcrUtils.logoutQuietly(session);
105 JcrUtils.logoutQuietly(nodeSession);
106 }
107 return null;
108 }
109
110 /* DEPENDENCY INJECTION */
111 public void setNodeRepository(Repository nodeRepository) {
112 this.nodeRepository = nodeRepository;
113 }
114
115 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
116 this.repositoryFactory = repositoryFactory;
117 }
118
119 public void setKeyring(Keyring keyring) {
120 this.keyring = keyring;
121 }
122 }