View Javadoc
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.eclipse.ui.dialogs.ErrorFeedback;
29  import org.argeo.jcr.JcrUtils;
30  import org.argeo.node.security.Keyring;
31  import org.argeo.slc.SlcConstants;
32  import org.argeo.slc.client.ui.dist.DistPlugin;
33  import org.argeo.slc.client.ui.dist.utils.CommandHelpers;
34  import org.argeo.slc.repo.RepoUtils;
35  import org.eclipse.core.commands.AbstractHandler;
36  import org.eclipse.core.commands.ExecutionEvent;
37  import org.eclipse.core.commands.ExecutionException;
38  import org.eclipse.jface.dialogs.Dialog;
39  import org.eclipse.jface.dialogs.InputDialog;
40  import org.eclipse.jface.resource.ImageDescriptor;
41  import org.eclipse.ui.handlers.HandlerUtil;
42  
43  /** Create a new empty workspace in a remote repository */
44  public class CreateWorkspace extends AbstractHandler {
45  	private static final Log log = LogFactory.getLog(CreateWorkspace.class);
46  
47  	// Exposes commands meta-info
48  	public final static String ID = DistPlugin.PLUGIN_ID + ".createWorkspace";
49  	public final static String DEFAULT_LABEL = "Create workspace...";
50  	public final static ImageDescriptor DEFAULT_ICON = DistPlugin
51  			.getImageDescriptor("icons/addItem.gif");
52  
53  	// Parameters
54  	public final static String PARAM_TARGET_REPO_PATH = "targetRepoPath";
55  	public final static String PARAM_WORKSPACE_PREFIX = "workspacePrefix";
56  
57  	// DEPENDENCY INJECTION
58  	private RepositoryFactory repositoryFactory;
59  	private Keyring keyring;
60  	private Repository nodeRepository;
61  
62  	public Object execute(ExecutionEvent event) throws ExecutionException {
63  
64  		String targetRepoPath = event.getParameter(PARAM_TARGET_REPO_PATH);
65  		String prefix = event.getParameter(PARAM_WORKSPACE_PREFIX);
66  
67  		Session nodeSession = null;
68  		Session session = null;
69  		try {
70  			nodeSession = nodeRepository.login();
71  			Node repoNode = nodeSession.getNode(targetRepoPath);
72  			Repository repository = RepoUtils.getRepository(repositoryFactory,
73  					keyring, repoNode);
74  			Credentials credentials = RepoUtils.getRepositoryCredentials(
75  					keyring, repoNode);
76  
77  			// TODO : add an input validator
78  			InputDialog inputDialog = new InputDialog(HandlerUtil
79  					.getActiveWorkbenchWindow(event).getShell(),
80  					"Workspace name?",
81  					"Choose a name for the workspace to create",
82  					prefix == null ? "" : prefix + "-", null);
83  			int result = inputDialog.open();
84  
85  			String enteredName = inputDialog.getValue();
86  
87  			final String legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXZY0123456789_";
88  			char[] arr = enteredName.toUpperCase().toCharArray();
89  			int count = 0;
90  			for (int i = 0; i < arr.length; i++) {
91  				if (legalChars.indexOf(arr[i]) == -1)
92  					count = count + 7;
93  				else
94  					count++;
95  			}
96  
97  			if (log.isTraceEnabled())
98  				log.trace("Translated workspace name length: " + count
99  						+ " (name: " + enteredName + " )");
100 
101 			if (count > 60) {
102 				ErrorFeedback.show("Workspace name '" + enteredName
103 						+ "' is too long or contains"
104 						+ " too many special characters such as '.' or '-'.");
105 				return null;
106 			}
107 
108 			String workspaceName = enteredName;
109 
110 			// Canceled by user
111 			if (result == Dialog.CANCEL || workspaceName == null
112 					|| "".equals(workspaceName.trim()))
113 				return null;
114 
115 			session = repository.login(credentials);
116 			session.getWorkspace().createWorkspace(workspaceName);
117 			JcrUtils.logoutQuietly(session);
118 			// init new workspace
119 			session = repository.login(credentials, workspaceName);
120 			JcrUtils.addPrivilege(session, "/", SlcConstants.ROLE_SLC,
121 					Privilege.JCR_ALL);
122 			CommandHelpers.callCommand(RefreshDistributionsView.ID);
123 			if (log.isTraceEnabled())
124 				log.trace("WORKSPACE " + workspaceName + " CREATED");
125 
126 		} catch (RepositoryException re) {
127 			ErrorFeedback.show(
128 					"Unexpected error while creating the new workspace.", re);
129 		} finally {
130 			JcrUtils.logoutQuietly(session);
131 			JcrUtils.logoutQuietly(nodeSession);
132 		}
133 		return null;
134 	}
135 
136 	/* DEPENDENCY INJECTION */
137 	public void setNodeRepository(Repository nodeRepository) {
138 		this.nodeRepository = nodeRepository;
139 	}
140 
141 	public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
142 		this.repositoryFactory = repositoryFactory;
143 	}
144 
145 	public void setKeyring(Keyring keyring) {
146 		this.keyring = keyring;
147 	}
148 }