2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.client
.ui
.dist
.commands
;
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
;
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
;
41 * Create a new empty workspace in the current repository.
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";
50 private String slcRole
= "ROLE_SLC";
52 private Repository repository
;
53 private Credentials credentials
;
55 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
56 IWorkbenchWindow iww
= DistPlugin
.getDefault().getWorkbench()
57 .getActiveWorkbenchWindow();
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
;
71 if (repository
!= null) {
72 // TODO : add an input validator
73 InputDialog inputDialog
= new InputDialog(iww
.getShell(),
75 "Choose a name for the workspace to create", prefix
+ "-",
77 int result
= inputDialog
.open();
79 String workspaceName
= inputDialog
.getValue();
82 if (result
== Dialog
.CANCEL
|| workspaceName
== null
83 || "".equals(workspaceName
.trim()))
86 Session session
= null;
88 session
= repository
.login(credentials
);
89 session
.getWorkspace().createWorkspace(workspaceName
);
90 JcrUtils
.logoutQuietly(session
);
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.",
100 JcrUtils
.logoutQuietly(session
);
102 if (log
.isTraceEnabled())
103 log
.trace("WORKSPACE " + workspaceName
+ " CREATED");
108 public void setRepository(Repository repository
) {
109 this.repository
= repository
;
112 public void setCredentials(Credentials credentials
) {
113 this.credentials
= credentials
;
116 public void setSlcRole(String slcRole
) {
117 this.slcRole
= slcRole
;