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.Node;
19  import javax.jcr.Property;
20  import javax.jcr.Repository;
21  import javax.jcr.RepositoryException;
22  import javax.jcr.Session;
23  
24  import org.argeo.cms.ArgeoTypes;
25  import org.argeo.cms.ui.workbench.util.CommandUtils;
26  import org.argeo.jcr.JcrUtils;
27  import org.argeo.slc.SlcException;
28  import org.argeo.slc.client.ui.dist.DistPlugin;
29  import org.eclipse.core.commands.AbstractHandler;
30  import org.eclipse.core.commands.ExecutionEvent;
31  import org.eclipse.core.commands.ExecutionException;
32  import org.eclipse.jface.dialogs.MessageDialog;
33  import org.eclipse.jface.resource.ImageDescriptor;
34  
35  /**
36   * Un-register a remote repository by deleting the corresponding RepoNode from
37   * the node Repository. It does not affect the repository instance
38   */
39  public class UnregisterRemoteRepo extends AbstractHandler {
40  	// private static final Log log = LogFactory
41  	// .getLog(UnregisterRemoteRepo.class);
42  	
43  	public final static String ID = DistPlugin.PLUGIN_ID + ".unregisterRemoteRepo";
44  	public final static String DEFAULT_LABEL = "Unregister";
45  	public final static ImageDescriptor DEFAULT_ICON = DistPlugin
46  			.getImageDescriptor("icons/removeItem.gif");
47  
48  	public final static String PARAM_REPO_PATH = DistPlugin.PLUGIN_ID
49  			+ ".repoNodePath";
50  
51  	// DEPENCY INJECTION
52  	private Repository nodeRepository;
53  
54  	public Object execute(ExecutionEvent event) throws ExecutionException {
55  		Session session = null;
56  		String repoPath = event.getParameter(PARAM_REPO_PATH);
57  		if (repoPath == null)
58  			return null;
59  
60  		try {
61  			session = nodeRepository.login();
62  			Node rNode = session.getNode(repoPath);
63  			if (rNode.isNodeType(ArgeoTypes.ARGEO_REMOTE_REPOSITORY)) {
64  
65  				String alias = rNode.getProperty(Property.JCR_TITLE)
66  						.getString();
67  				String msg = "Your are about to unregister remote repository: "
68  						+ alias + "\n" + "Are you sure you want to proceed ?";
69  
70  				boolean result = MessageDialog.openConfirm(DistPlugin
71  						.getDefault().getWorkbench().getDisplay()
72  						.getActiveShell(), "Confirm Delete", msg);
73  
74  				if (result) {
75  					rNode.remove();
76  					session.save();
77  				}
78  				CommandUtils.callCommand(RefreshDistributionsView.ID);
79  			}
80  		} catch (RepositoryException e) {
81  			throw new SlcException(
82  					"Unexpected error while unregistering remote repository.",
83  					e);
84  		} finally {
85  			JcrUtils.logoutQuietly(session);
86  		}
87  		return null;
88  	}
89  
90  	// DEPENCY INJECTION
91  	public void setNodeRepository(Repository nodeRepository) {
92  		this.nodeRepository = nodeRepository;
93  	}
94  }