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.NodeIterator;
21  import javax.jcr.Repository;
22  import javax.jcr.RepositoryException;
23  import javax.jcr.RepositoryFactory;
24  import javax.jcr.Session;
25  import javax.jcr.nodetype.NodeType;
26  
27  import org.argeo.jcr.JcrUtils;
28  import org.argeo.node.security.Keyring;
29  import org.argeo.slc.SlcException;
30  import org.argeo.slc.client.ui.dist.DistPlugin;
31  import org.argeo.slc.client.ui.dist.utils.CommandHelpers;
32  import org.argeo.slc.repo.RepoUtils;
33  import org.eclipse.core.commands.AbstractHandler;
34  import org.eclipse.core.commands.ExecutionEvent;
35  import org.eclipse.core.commands.ExecutionException;
36  import org.eclipse.jface.dialogs.MessageDialog;
37  import org.eclipse.jface.resource.ImageDescriptor;
38  
39  /**
40   * Delete chosen workspace in the current repository.
41   * 
42   * Due to current version of JackRabbit, it only cleans it for the time being,
43   * removing all nodes of type {@code NodeType.NT_FOLDER} and
44   * {@code NodeType.NT_UNSTRUCTURED}
45   */
46  public class DeleteWorkspace extends AbstractHandler {
47  	// private static final Log log = LogFactory.getLog(DeleteWorkspace.class);
48  
49  	public final static String ID = DistPlugin.PLUGIN_ID + ".deleteWorkspace";
50  	public final static String DEFAULT_LABEL = "Clear";
51  	public final static ImageDescriptor DEFAULT_ICON = DistPlugin
52  			.getImageDescriptor("icons/removeItem.gif");
53  
54  	public final static String PARAM_WORKSPACE_NAME = "workspaceName";
55  	public final static String PARAM_TARGET_REPO_PATH = "targetRepoPath";
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 workspaceName = event.getParameter(PARAM_WORKSPACE_NAME);
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  			String msg = "Your are about to completely delete workspace ["
78  					+ workspaceName + "].\n Do you really want to proceed?";
79  			boolean result = MessageDialog.openConfirm(DistPlugin.getDefault()
80  					.getWorkbench().getDisplay().getActiveShell(),
81  					"Confirm workspace deletion", msg);
82  
83  			if (result) {
84  				// msg =
85  				// "There is no possible turning back, are your REALLY sure you want to proceed ?";
86  				msg = "WARNING: \nCurrent Jackrabbit version used does "
87  						+ "not support workspace deletion.\n"
88  						+ "Thus, the workspace will only be cleaned so "
89  						+ "that you can launch fetch process again.\n\n"
90  						+ "Do you still want to proceed?";
91  				result = MessageDialog.openConfirm(DistPlugin.getDefault()
92  						.getWorkbench().getDisplay().getActiveShell(),
93  						"Confirm workspace deletion", msg);
94  			}
95  
96  			if (result) {
97  				session = repository.login(credentials, workspaceName);
98  				// TODO use this with a newer version of Jackrabbit
99  				// Workspace wsp = session.getWorkspace();
100 				// wsp.deleteWorkspace(workspaceName);
101 				NodeIterator nit = session.getRootNode().getNodes();
102 				while (nit.hasNext()) {
103 					Node node = nit.nextNode();
104 					if (node.isNodeType(NodeType.NT_FOLDER)
105 							|| node.isNodeType(NodeType.NT_UNSTRUCTURED)) {
106 						// String path = node.getPath();
107 						node.remove();
108 						session.save();
109 					}
110 				}
111 				CommandHelpers.callCommand(RefreshDistributionsView.ID);
112 			}
113 		} catch (RepositoryException re) {
114 			throw new SlcException(
115 					"Unexpected error while deleting workspace ["
116 							+ workspaceName + "].", re);
117 		} finally {
118 			JcrUtils.logoutQuietly(session);
119 			JcrUtils.logoutQuietly(nodeSession);
120 		}
121 		return null;
122 	}
123 
124 	/* DEPENDENCY INJECTION */
125 	public void setNodeRepository(Repository nodeRepository) {
126 		this.nodeRepository = nodeRepository;
127 	}
128 
129 	public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
130 		this.repositoryFactory = repositoryFactory;
131 	}
132 
133 	public void setKeyring(Keyring keyring) {
134 		this.keyring = keyring;
135 	}
136 }