]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/argeo-commons/org.argeo.cms.ui.workbench/src/org/argeo/cms/ui/workbench/legacy/rap/OpenFile.java
616dadcf66fe2fb10845f20e0e67501343854c29
[gpl/argeo-slc.git] / legacy / argeo-commons / org.argeo.cms.ui.workbench / src / org / argeo / cms / ui / workbench / legacy / rap / OpenFile.java
1 package org.argeo.cms.ui.workbench.legacy.rap;
2
3 import org.argeo.api.cms.CmsLog;
4 import org.eclipse.core.commands.AbstractHandler;
5 import org.eclipse.core.commands.ExecutionEvent;
6 import org.eclipse.rap.rwt.RWT;
7 import org.eclipse.rap.rwt.client.service.UrlLauncher;
8
9 /**
10 * RWT specific object to open a file retrieved from the server. It forwards the
11 * request to the correct service after encoding file name and path in the
12 * request URI.
13 *
14 * <p>
15 * The parameter "URI" is used to determine the correct file service, the path
16 * and the file name. An optional file name can be added to present the end user
17 * with a different file name as the one used to retrieve it.
18 * </p>
19 *
20 *
21 * <p>
22 * The instance specific service is called by its ID and must have been
23 * externally created
24 * </p>
25 */
26 public class OpenFile extends AbstractHandler {
27 private final static CmsLog log = CmsLog.getLog(OpenFile.class);
28
29 public final static String ID = SingleSourcingConstants.OPEN_FILE_CMD_ID;
30 public final static String PARAM_FILE_NAME = SingleSourcingConstants.PARAM_FILE_NAME;
31 public final static String PARAM_FILE_URI = SingleSourcingConstants.PARAM_FILE_URI;;
32
33 /* DEPENDENCY INJECTION */
34 private String openFileServiceId;
35
36 public Object execute(ExecutionEvent event) {
37 String fileName = event.getParameter(PARAM_FILE_NAME);
38 String fileUri = event.getParameter(PARAM_FILE_URI);
39 // Sanity check
40 if (fileUri == null || "".equals(fileUri.trim()) || openFileServiceId == null
41 || "".equals(openFileServiceId.trim()))
42 return null;
43
44 org.argeo.cms.ui.workbench.legacy.rap.OpenFile openFileClient = new org.argeo.cms.ui.workbench.legacy.rap.OpenFile();
45 openFileClient.execute(openFileServiceId, fileUri, fileName);
46 return null;
47 }
48
49 public Object execute(String openFileServiceId, String fileUri, String fileName) {
50 StringBuilder url = new StringBuilder();
51 url.append(RWT.getServiceManager().getServiceHandlerUrl(openFileServiceId));
52
53 if (notEmpty(fileName))
54 url.append("&").append(SingleSourcingConstants.PARAM_FILE_NAME).append("=").append(fileName);
55 url.append("&").append(SingleSourcingConstants.PARAM_FILE_URI).append("=").append(fileUri);
56
57 String downloadUrl = url.toString();
58 if (log.isTraceEnabled())
59 log.trace("Calling OpenFileService with ID: " + openFileServiceId + " , with download URL: " + downloadUrl);
60
61 UrlLauncher launcher = RWT.getClient().getService(UrlLauncher.class);
62 launcher.openURL(downloadUrl);
63 return null;
64 }
65
66 /* DEPENDENCY INJECTION */
67 public void setOpenFileServiceId(String openFileServiceId) {
68 this.openFileServiceId = openFileServiceId;
69 }
70
71 /** Simply checks if a string is not null nor empty */
72 public static boolean notEmpty(String stringToTest) {
73 return !(stringToTest == null || "".equals(stringToTest.trim()));
74 }
75
76 /** Simply checks if a string is null or empty */
77 public static boolean isEmpty(String stringToTest) {
78 return stringToTest == null || "".equals(stringToTest.trim());
79 }
80
81 }