]> 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
Add anonymous perspective
[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.Repository;
20 import javax.jcr.RepositoryException;
21 import javax.jcr.Session;
22 import javax.jcr.security.Privilege;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.argeo.ArgeoException;
27 import org.argeo.jcr.JcrUtils;
28 import org.argeo.slc.client.ui.dist.DistPlugin;
29 import org.argeo.slc.client.ui.dist.utils.CommandHelpers;
30 import org.argeo.slc.client.ui.dist.views.DistributionsView;
31 import org.argeo.slc.client.ui.dist.views.DistributionsView.DistributionViewSelectedElement;
32 import org.eclipse.core.commands.AbstractHandler;
33 import org.eclipse.core.commands.ExecutionEvent;
34 import org.eclipse.core.commands.ExecutionException;
35 import org.eclipse.jface.dialogs.Dialog;
36 import org.eclipse.jface.dialogs.InputDialog;
37 import org.eclipse.ui.IWorkbenchPart;
38 import org.eclipse.ui.IWorkbenchWindow;
39
40 /**
41 * Create a new empty workspace in the current repository.
42 */
43
44 public class CreateWorkspace extends AbstractHandler {
45 private static final Log log = LogFactory.getLog(CreateWorkspace.class);
46 public final static String ID = DistPlugin.ID + ".createWorkspace";
47 public final static String DEFAULT_LABEL = "Create new workspace...";
48 public final static String DEFAULT_ICON_PATH = "icons/addItem.gif";
49
50 private String slcRole = "ROLE_SLC";
51
52 private Repository repository;
53 private Credentials credentials;
54
55 public Object execute(ExecutionEvent event) throws ExecutionException {
56 IWorkbenchWindow iww = DistPlugin.getDefault().getWorkbench()
57 .getActiveWorkbenchWindow();
58 String prefix = "";
59
60 IWorkbenchPart view = iww.getActivePage().getActivePart();
61 if (view instanceof DistributionsView) {
62 DistributionViewSelectedElement dvse = ((DistributionsView) view)
63 .getSelectedElement();
64 if (dvse != null && (dvse.isRepository || dvse.isWorkspaceGroup)) {
65 repository = dvse.repository;
66 credentials = dvse.credentials;
67 prefix = dvse.wkspPrefix;
68 }
69 }
70
71 if (repository != null) {
72 // TODO : add an input validator
73 InputDialog inputDialog = new InputDialog(iww.getShell(),
74 "Workspace name?",
75 "Choose a name for the workspace to create", prefix + "-",
76 null);
77 int result = inputDialog.open();
78
79 String workspaceName = inputDialog.getValue();
80
81 // Canceled by user
82 if (result == Dialog.CANCEL || workspaceName == null
83 || "".equals(workspaceName.trim()))
84 return null;
85
86 Session session = null;
87 try {
88 session = repository.login(credentials);
89 session.getWorkspace().createWorkspace(workspaceName);
90 JcrUtils.logoutQuietly(session);
91 // init new workspace
92 session = repository.login(workspaceName);
93 JcrUtils.addPrivilege(session, "/", slcRole, Privilege.JCR_ALL);
94 CommandHelpers.callCommand(RefreshDistributionsView.ID);
95 } catch (RepositoryException re) {
96 throw new ArgeoException(
97 "Unexpected error while creating the new workspace.",
98 re);
99 } finally {
100 JcrUtils.logoutQuietly(session);
101 }
102 if (log.isTraceEnabled())
103 log.trace("WORKSPACE " + workspaceName + " CREATED");
104 }
105 return null;
106 }
107
108 public void setRepository(Repository repository) {
109 this.repository = repository;
110 }
111
112 public void setCredentials(Credentials credentials) {
113 this.credentials = credentials;
114 }
115
116 public void setSlcRole(String slcRole) {
117 this.slcRole = slcRole;
118 }
119 }