1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.ui.framework.action;
35
36 import info.magnolia.commands.CommandsManager;
37 import info.magnolia.commands.impl.ExportCommand;
38 import info.magnolia.i18nsystem.SimpleTranslator;
39 import info.magnolia.ui.api.action.ActionExecutionException;
40 import info.magnolia.ui.api.context.UiContext;
41 import info.magnolia.ui.framework.util.TempFileStreamResource;
42 import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
43
44 import java.io.IOException;
45 import java.util.Map;
46
47 import javax.inject.Inject;
48 import javax.jcr.Item;
49
50 import com.vaadin.server.Page;
51
52
53
54
55
56
57
58 public class ExportAction extends AbstractCommandAction<ExportActionDefinition> {
59
60 private TempFileStreamResource tempFileStreamResource;
61
62 @Inject
63 public ExportAction(ExportActionDefinition definition, JcrItemAdapter item, CommandsManager commandsManager, UiContext uiContext, SimpleTranslator i18n) throws ActionExecutionException {
64 super(definition, item, commandsManager, uiContext, i18n);
65 }
66
67 @Override
68 protected void onPreExecute() throws Exception {
69 tempFileStreamResource = new TempFileStreamResource();
70 tempFileStreamResource.setTempFileName(getCurrentItem().getItemId().getUuid());
71 tempFileStreamResource.setTempFileExtension("xml");
72
73 super.onPreExecute();
74 }
75
76
77
78
79
80
81
82
83 @Override
84 protected void onPostExecute() throws Exception {
85 final ExportCommand exportCommand = (ExportCommand) getCommand();
86 tempFileStreamResource.setFilename(exportCommand.getFileName());
87 tempFileStreamResource.setMIMEType(exportCommand.getMimeExtension());
88
89 Page.getCurrent().open(tempFileStreamResource, "", true);
90 }
91
92 @Override
93 protected Map<String, Object> buildParams(final Item jcrItem) {
94 Map<String, Object> params = super.buildParams(jcrItem);
95 params.put(ExportCommand.EXPORT_EXTENSION, ".xml");
96 params.put(ExportCommand.EXPORT_FORMAT, Boolean.TRUE);
97 params.put(ExportCommand.EXPORT_KEEP_HISTORY, Boolean.FALSE);
98 try {
99 params.put(ExportCommand.EXPORT_OUTPUT_STREAM, tempFileStreamResource.getTempFileOutputStream());
100 } catch (IOException e) {
101 throw new IllegalStateException("Failed to bind command to temp file output stream: ", e);
102 }
103 return params;
104 }
105 }