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