View Javadoc
1   package org.argeo.connect.e4.handlers;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import javax.inject.Inject;
7   import javax.inject.Named;
8   import javax.jcr.Node;
9   import javax.jcr.Repository;
10  import javax.jcr.RepositoryException;
11  import javax.jcr.Session;
12  
13  import org.argeo.cms.ui.dialogs.CmsWizardDialog;
14  import org.argeo.connect.AppService;
15  import org.argeo.connect.ConnectException;
16  import org.argeo.connect.SystemAppService;
17  import org.argeo.connect.ui.AppWorkbenchService;
18  import org.argeo.connect.ui.ConnectEditor;
19  import org.argeo.connect.ui.SystemWorkbenchService;
20  import org.argeo.jcr.JcrUtils;
21  import org.eclipse.e4.core.commands.ECommandService;
22  import org.eclipse.e4.core.commands.EHandlerService;
23  import org.eclipse.e4.core.di.annotations.Execute;
24  import org.eclipse.jface.wizard.Wizard;
25  import org.eclipse.jface.wizard.WizardDialog;
26  import org.eclipse.swt.widgets.Display;
27  
28  /**
29   * Creates a new entity under draft path of the current repository and opens the
30   * corresponding editor. The Node type of the relevant entity must be passed as
31   * parameter.
32   */
33  public class CreateEntity {
34  	// private final static Log log = LogFactory.getLog(CreateEntity.class);
35  
36  	// public final static String ID = ConnectUiPlugin.PLUGIN_ID + ".createEntity";
37  	public final static String PARAM_TARGET_NODE_TYPE = "targetNodeType";
38  
39  	@Inject
40  	@Named("(cn=home)")
41  	private Repository repository;
42  	@Inject
43  	private SystemAppService systemAppService;
44  	@Inject
45  	private SystemWorkbenchService systemWorkbenchService;
46  	@Inject
47  	private EHandlerService handlerService;
48  	@Inject
49  	private ECommandService commandService;
50  
51  	@Execute
52  	public void execute(@Named(PARAM_TARGET_NODE_TYPE) String nodeType) {
53  
54  		// FIXME : Known bug (or limitation?) in Jackrabbit: When a user does
55  		// not have JCR_READ privileges on the root of the workspace, trying to
56  		// save a session that have transient issues in distinct subtrees of the
57  		// workspace will fail
58  
59  		Session draftSession = null;
60  		Session mainSession = null;
61  		String jcrId = null;
62  
63  		try {
64  			draftSession = repository.login();
65  
66  			Node tmpNode = systemAppService.createDraftEntity(draftSession, nodeType);
67  			Wizard wizard = systemWorkbenchService.getCreationWizard(tmpNode);
68  			if (wizard != null) {
69  				// WizardDialog dialog = new WizardDialog(Display.getCurrent().getActiveShell(),
70  				// wizard);
71  				CmsWizardDialog dialog = new CmsWizardDialog(Display.getCurrent().getActiveShell(), wizard);
72  				dialog.setTitle("New...");
73  				if (dialog.open() != WizardDialog.OK) {
74  					// This will try to remove the newly created temporary Node if
75  					// the process fails before first save
76  					JcrUtils.discardQuietly(draftSession);
77  					return;
78  				}
79  			}
80  			// if(true)
81  			// return null;
82  			mainSession = repository.login();
83  
84  			// By default, all entities are stored at the same place,
85  			// depending on their types.
86  			// We will enhance this in the future, typically to enable
87  			// (partially) private entities
88  			Node parent = mainSession.getNode("/" + systemAppService.getBaseRelPath(nodeType));
89  			Node newNode = systemAppService.publishEntity(parent, nodeType, tmpNode);
90  
91  			newNode = systemAppService.saveEntity(newNode, false);
92  			jcrId = newNode.getIdentifier();
93  
94  		} catch (RepositoryException e) {
95  			throw new ConnectException("Cannot create " + nodeType + " entity", e);
96  		} finally {
97  			JcrUtils.logoutQuietly(draftSession);
98  			JcrUtils.logoutQuietly(mainSession);
99  		}
100 
101 		if (jcrId != null) {
102 			// Open the corresponding editor
103 			Map<String, String> params = new HashMap<>();
104 			params.put(ConnectEditor.PARAM_JCR_ID, jcrId);
105 			params.put(ConnectEditor.PARAM_OPEN_FOR_EDIT, "true");
106 			String openEntityCmd = systemWorkbenchService.getOpenEntityEditorCmdId();
107 			systemWorkbenchService.callCommand(openEntityCmd, params);
108 		}
109 	}
110 
111 	// Expose to children classes
112 	protected Repository getRepository() {
113 		return repository;
114 	}
115 
116 	protected AppService getAppService() {
117 		return systemAppService;
118 	}
119 
120 	protected AppWorkbenchService getAppWorkbenchService() {
121 		return systemWorkbenchService;
122 	}
123 }