]> 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/DeleteWorkspace.java
Do not clean OSGi runtime
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / commands / DeleteWorkspace.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.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.slc.SlcException;
29 import org.argeo.slc.client.ui.dist.DistPlugin;
30 import org.argeo.slc.client.ui.dist.utils.CommandHelpers;
31 import org.argeo.slc.repo.RepoUtils;
32 import org.argeo.util.security.Keyring;
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
38 /**
39 * Delete chosen workspace in the current repository.
40 *
41 * Due to current version of JackRabbit, it only cleans it for the time being,
42 * removing all nodes of type {@code NodeType.NT_FOLDER} and
43 * {@code NodeType.NT_UNSTRUCTURED}
44 */
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.ID + ".deleteWorkspace";
50 public final static String DEFAULT_LABEL = "Clear";
51 public final static String DEFAULT_ICON_PATH = "icons/removeItem.gif";
52 public final static String PARAM_WORKSPACE_NAME = "workspaceName";
53 public final static String PARAM_TARGET_REPO_PATH = "targetRepoPath";
54
55 // DEPENDENCY INJECTION
56 private RepositoryFactory repositoryFactory;
57 private Keyring keyring;
58 private Repository nodeRepository;
59
60 public Object execute(ExecutionEvent event) throws ExecutionException {
61
62 String targetRepoPath = event.getParameter(PARAM_TARGET_REPO_PATH);
63 String workspaceName = event.getParameter(PARAM_WORKSPACE_NAME);
64
65 Session nodeSession = null;
66 Session session = null;
67
68 try {
69 nodeSession = nodeRepository.login();
70 Node repoNode = nodeSession.getNode(targetRepoPath);
71 Repository repository = RepoUtils.getRepository(repositoryFactory,
72 keyring, repoNode);
73 Credentials credentials = RepoUtils.getRepositoryCredentials(
74 keyring, repoNode);
75
76 String msg = "Your are about to completely delete workspace ["
77 + workspaceName + "].\n Do you really want to proceed ?";
78 boolean result = MessageDialog.openConfirm(DistPlugin.getDefault()
79 .getWorkbench().getDisplay().getActiveShell(),
80 "Confirm workspace deletion", msg);
81
82 if (result) {
83 // msg =
84 // "There is no possible turning back, are your REALLY sure you want to proceed ?";
85 msg = "WARNING: \nCurrent Jackrabbit version used does "
86 + "not support workspace management.\n"
87 + "Thus, the workspace will only be cleaned so "
88 + "that you can launch fetch process again.\n\n"
89 + "Do you still want to proceed ?";
90 result = MessageDialog.openConfirm(DistPlugin.getDefault()
91 .getWorkbench().getDisplay().getActiveShell(),
92 "Confirm workspace deletion", msg);
93 }
94
95 if (result) {
96 session = repository.login(credentials, workspaceName);
97
98 // TODO use this with a newer version of Jackrabbit
99 // Workspace wsp = session.getWorkspace();
100 // wsp.deleteWorkspace(workspaceName);
101
102 NodeIterator nit = session.getRootNode().getNodes();
103 while (nit.hasNext()) {
104 Node node = nit.nextNode();
105 if (node.isNodeType(NodeType.NT_FOLDER)
106 || node.isNodeType(NodeType.NT_UNSTRUCTURED)) {
107 // String path = node.getPath();
108 node.remove();
109 session.save();
110 }
111 }
112 CommandHelpers.callCommand(RefreshDistributionsView.ID);
113 }
114 } catch (RepositoryException re) {
115 throw new SlcException(
116 "Unexpected error while deleting workspace ["
117 + workspaceName + "].", re);
118 } finally {
119 JcrUtils.logoutQuietly(session);
120 JcrUtils.logoutQuietly(nodeSession);
121 }
122 return null;
123 }
124
125 /* DEPENDENCY INJECTION */
126 public void setNodeRepository(Repository nodeRepository) {
127 this.nodeRepository = nodeRepository;
128 }
129
130 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
131 this.repositoryFactory = repositoryFactory;
132 }
133
134 public void setKeyring(Keyring keyring) {
135 this.keyring = keyring;
136 }
137 }