View Javadoc
1   package org.argeo.people.e4.handlers;
2   
3   import javax.inject.Inject;
4   import javax.inject.Named;
5   import javax.jcr.Node;
6   import javax.jcr.Repository;
7   import javax.jcr.RepositoryException;
8   import javax.jcr.Session;
9   
10  import org.apache.commons.logging.Log;
11  import org.apache.commons.logging.LogFactory;
12  import org.argeo.connect.e4.parts.AbstractConnectEditor;
13  import org.argeo.connect.util.ConnectJcrUtils;
14  import org.argeo.jcr.JcrUtils;
15  import org.argeo.people.PeopleException;
16  import org.eclipse.e4.core.di.annotations.Execute;
17  import org.eclipse.e4.ui.model.application.ui.basic.MPart;
18  import org.eclipse.e4.ui.services.IServiceConstants;
19  import org.eclipse.jface.dialogs.MessageDialog;
20  import org.eclipse.swt.widgets.Display;
21  
22  /**
23   * Remove the reference to a given entity. Jcr Identifier of the reference node
24   * to remove and the one of its nearest versionable ancestor must be passed as
25   * parameters.
26   */
27  public class RemoveEntityReference {
28  	private final static Log log = LogFactory.getLog(RemoveEntityReference.class);
29  
30  	public final static String ID = "org.argeo.suite.e4.command.removeEntityReference";
31  	public final static String DEFAULT_LABEL = "Delete";
32  	public final static String PARAM_TOREMOVE_JCR_ID = "toRemoveJcrId";
33  
34  	/* DEPENDENCY INJECTION */
35  	@Inject
36  	private Repository repository;
37  
38  	@Execute
39  	public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart mPart,
40  			@Named(PARAM_TOREMOVE_JCR_ID) String toRemoveJcrId) {
41  
42  		String msg = "Your are about to definitively remove this reference.\n" + "Are you sure you want to proceed ?";
43  
44  		boolean result = MessageDialog.openConfirm(Display.getCurrent().getActiveShell(), "Confirm Deletion", msg);
45  
46  		if (!result)
47  			return;
48  
49  		// String toRemoveJcrId = event.getParameter(PARAM_TOREMOVE_JCR_ID);
50  
51  		Session session = null;
52  		Node versionableParent = null;
53  		Node toRemoveNode = null;
54  		try {
55  			session = repository.login();
56  			toRemoveNode = session.getNodeByIdentifier(toRemoveJcrId);
57  			versionableParent = ConnectJcrUtils.getVersionableAncestor(toRemoveNode);
58  
59  			if (versionableParent == null) {
60  				log.warn("Found no versionnable node in ancestors of " + toRemoveNode + "\n Simply removing.");
61  				toRemoveNode.remove();
62  				session.save();
63  			} else {
64  				// boolean wasCO =
65  				ConnectJcrUtils.checkCOStatusBeforeUpdate(versionableParent);
66  				toRemoveNode.remove();
67  				// FIXME should we save ? commit ? do nothing
68  				ConnectJcrUtils.saveAndPublish(versionableParent, true);
69  				// ConnectJcrUtils.checkCOStatusAfterUpdate(versionableParent,
70  				// wasCO);
71  			}
72  		} catch (RepositoryException e) {
73  			StringBuilder errMsg = new StringBuilder();
74  			errMsg.append("Unable to remove ");
75  			if (toRemoveNode != null)
76  				errMsg.append(toRemoveNode).append(" - ");
77  			errMsg.append("JcrID: " + toRemoveJcrId).append(" on parent versionnable node ");
78  			if (versionableParent != null)
79  				errMsg.append(versionableParent);
80  			throw new PeopleException(errMsg.toString(), e);
81  		} finally {
82  			JcrUtils.logoutQuietly(session);
83  		}
84  		((AbstractConnectEditor) mPart.getObject()).forceRefresh();
85  	}
86  
87  	/* DEPENDENCY INJECTION */
88  	public void setRepository(Repository repository) {
89  		this.repository = repository;
90  	}
91  }