]> 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
86cf1926b5f267446c36bff05df4b871f5489fc7
[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.Repository;
19 import javax.jcr.RepositoryException;
20 import javax.jcr.Session;
21 import javax.jcr.security.Privilege;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.argeo.ArgeoException;
26 import org.argeo.jcr.JcrUtils;
27 import org.argeo.slc.client.ui.dist.DistPlugin;
28 import org.argeo.slc.client.ui.dist.utils.CommandHelpers;
29 import org.eclipse.core.commands.AbstractHandler;
30 import org.eclipse.core.commands.ExecutionEvent;
31 import org.eclipse.core.commands.ExecutionException;
32 import org.eclipse.jface.dialogs.Dialog;
33 import org.eclipse.jface.dialogs.InputDialog;
34 import org.eclipse.ui.IWorkbenchWindow;
35
36 /**
37 * Create a new empty workspace in the current repository.
38 */
39
40 public class CreateWorkspace extends AbstractHandler {
41 private static final Log log = LogFactory.getLog(CreateWorkspace.class);
42 public final static String ID = DistPlugin.ID + ".createWorkspace";
43 public final static String DEFAULT_LABEL = "Create new workspace";
44 public final static String DEFAULT_ICON_PATH = "icons/addItem.gif";
45
46 private String slcRole = "ROLE_SLC";
47
48 /* DEPENDENCY INJECTION */
49 private Repository repository;
50
51 public Object execute(ExecutionEvent event) throws ExecutionException {
52 IWorkbenchWindow iww = DistPlugin.getDefault().getWorkbench()
53 .getActiveWorkbenchWindow();
54 // TODO : add an input validator
55 InputDialog inputDialog = new InputDialog(iww.getShell(),
56 "New workspace", "Choose a name for the workspace to create",
57 "", null);
58 int result = inputDialog.open();
59
60 // Canceled by user
61 if (result == Dialog.CANCEL)
62 return null;
63
64 String workspaceName = inputDialog.getValue();
65 Session session = null;
66 try {
67 session = repository.login();
68 session.getWorkspace().createWorkspace(workspaceName);
69 JcrUtils.logoutQuietly(session);
70 // init new workspace
71 session = repository.login(workspaceName);
72 JcrUtils.addPrivilege(session, "/", slcRole, Privilege.JCR_ALL);
73 CommandHelpers.callCommand(RefreshDistributionsView.ID);
74 } catch (RepositoryException re) {
75 throw new ArgeoException(
76 "Unexpected error while creating the new workspace.", re);
77 } finally {
78 JcrUtils.logoutQuietly(session);
79 }
80 if (log.isTraceEnabled())
81 log.trace("WORKSPACE " + workspaceName + " CREATED");
82 return null;
83 }
84
85 /* DEPENDENCY INJECTION */
86 public void setRepository(Repository repository) {
87 this.repository = repository;
88 }
89
90 public void setSlcRole(String slcRole) {
91 this.slcRole = slcRole;
92 }
93
94 }