]> git.argeo.org Git - gpl/argeo-slc.git/blob - cms/org.argeo.slc.client.ui.dist/src/org/argeo/slc/client/ui/dist/commands/DeleteWorkspace.java
Adapt to changes in Argeo Commons.
[gpl/argeo-slc.git] / cms / org.argeo.slc.client.ui.dist / src / org / argeo / slc / client / ui / dist / commands / DeleteWorkspace.java
1 package org.argeo.slc.client.ui.dist.commands;
2
3 import javax.jcr.Credentials;
4 import javax.jcr.Node;
5 import javax.jcr.NodeIterator;
6 import javax.jcr.Repository;
7 import javax.jcr.RepositoryException;
8 import javax.jcr.RepositoryFactory;
9 import javax.jcr.Session;
10 import javax.jcr.nodetype.NodeType;
11
12 import org.argeo.api.security.Keyring;
13 import org.argeo.jcr.JcrUtils;
14 import org.argeo.slc.SlcException;
15 import org.argeo.slc.client.ui.dist.DistPlugin;
16 import org.argeo.slc.client.ui.dist.utils.CommandHelpers;
17 import org.argeo.slc.repo.RepoUtils;
18 import org.eclipse.core.commands.AbstractHandler;
19 import org.eclipse.core.commands.ExecutionEvent;
20 import org.eclipse.core.commands.ExecutionException;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.jface.resource.ImageDescriptor;
23
24 /**
25 * Delete chosen workspace in the current repository.
26 *
27 * Due to current version of JackRabbit, it only cleans it for the time being,
28 * removing all nodes of type {@code NodeType.NT_FOLDER} and
29 * {@code NodeType.NT_UNSTRUCTURED}
30 */
31 public class DeleteWorkspace extends AbstractHandler {
32 // private static final Log log = LogFactory.getLog(DeleteWorkspace.class);
33
34 public final static String ID = DistPlugin.PLUGIN_ID + ".deleteWorkspace";
35 public final static String DEFAULT_LABEL = "Clear";
36 public final static ImageDescriptor DEFAULT_ICON = DistPlugin
37 .getImageDescriptor("icons/removeItem.gif");
38
39 public final static String PARAM_WORKSPACE_NAME = "workspaceName";
40 public final static String PARAM_TARGET_REPO_PATH = "targetRepoPath";
41
42 // DEPENDENCY INJECTION
43 private RepositoryFactory repositoryFactory;
44 private Keyring keyring;
45 private Repository nodeRepository;
46
47 public Object execute(ExecutionEvent event) throws ExecutionException {
48
49 String targetRepoPath = event.getParameter(PARAM_TARGET_REPO_PATH);
50 String workspaceName = event.getParameter(PARAM_WORKSPACE_NAME);
51
52 Session nodeSession = null;
53 Session session = null;
54 try {
55 nodeSession = nodeRepository.login();
56 Node repoNode = nodeSession.getNode(targetRepoPath);
57 Repository repository = RepoUtils.getRepository(repositoryFactory,
58 keyring, repoNode);
59 Credentials credentials = RepoUtils.getRepositoryCredentials(
60 keyring, repoNode);
61
62 String msg = "Your are about to completely delete workspace ["
63 + workspaceName + "].\n Do you really want to proceed?";
64 boolean result = MessageDialog.openConfirm(DistPlugin.getDefault()
65 .getWorkbench().getDisplay().getActiveShell(),
66 "Confirm workspace deletion", msg);
67
68 if (result) {
69 // msg =
70 // "There is no possible turning back, are your REALLY sure you want to proceed ?";
71 msg = "WARNING: \nCurrent Jackrabbit version used does "
72 + "not support workspace deletion.\n"
73 + "Thus, the workspace will only be cleaned so "
74 + "that you can launch fetch process again.\n\n"
75 + "Do you still want to proceed?";
76 result = MessageDialog.openConfirm(DistPlugin.getDefault()
77 .getWorkbench().getDisplay().getActiveShell(),
78 "Confirm workspace deletion", msg);
79 }
80
81 if (result) {
82 session = repository.login(credentials, workspaceName);
83 // TODO use this with a newer version of Jackrabbit
84 // Workspace wsp = session.getWorkspace();
85 // wsp.deleteWorkspace(workspaceName);
86 NodeIterator nit = session.getRootNode().getNodes();
87 while (nit.hasNext()) {
88 Node node = nit.nextNode();
89 if (node.isNodeType(NodeType.NT_FOLDER)
90 || node.isNodeType(NodeType.NT_UNSTRUCTURED)) {
91 // String path = node.getPath();
92 node.remove();
93 session.save();
94 }
95 }
96 CommandHelpers.callCommand(RefreshDistributionsView.ID);
97 }
98 } catch (RepositoryException re) {
99 throw new SlcException(
100 "Unexpected error while deleting workspace ["
101 + workspaceName + "].", re);
102 } finally {
103 JcrUtils.logoutQuietly(session);
104 JcrUtils.logoutQuietly(nodeSession);
105 }
106 return null;
107 }
108
109 /* DEPENDENCY INJECTION */
110 public void setNodeRepository(Repository nodeRepository) {
111 this.nodeRepository = nodeRepository;
112 }
113
114 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
115 this.repositoryFactory = repositoryFactory;
116 }
117
118 public void setKeyring(Keyring keyring) {
119 this.keyring = keyring;
120 }
121 }