View Javadoc
1   /**
2    * This file Copyright (c) 2013-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.framework.action;
35  
36  import info.magnolia.cms.beans.config.MIMEMapping;
37  import info.magnolia.commands.CommandsManager;
38  import info.magnolia.i18nsystem.SimpleTranslator;
39  import info.magnolia.importexport.DataTransporter;
40  import info.magnolia.importexport.command.JcrExportCommand;
41  import info.magnolia.ui.api.action.ActionExecutionException;
42  import info.magnolia.ui.api.context.UiContext;
43  import info.magnolia.ui.form.EditorCallback;
44  import info.magnolia.ui.framework.util.TempFileStreamResource;
45  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
46  import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
47  
48  import java.util.Map;
49  
50  import javax.jcr.Item;
51  
52  import org.apache.commons.io.FilenameUtils;
53  
54  import com.vaadin.server.Page;
55  
56  /**
57   * Action for exporting a node to XML format. Uses the export command to perform the serialization to XML then sends the
58   * result to the client browser.
59   *
60   * @see ExportActionDefinition
61   */
62  public class ExportAction extends AbstractCommandAction<ExportActionDefinition> {
63  
64      private TempFileStreamResource tempFileStreamResource;
65      private final EditorCallback callback;
66  
67      //this is not marked with @Inject so component provider can detect the correct ctor based on arguments (with or without EditorCallback, depending if the action is used inside of a dialog or not)
68      public ExportAction(ExportActionDefinition definition, JcrItemAdapter item, CommandsManager commandsManager, UiContext uiContext, SimpleTranslator i18n, EditorCallback callback) throws ActionExecutionException {
69          super(definition, item, commandsManager, uiContext, i18n);
70          this.callback = callback;
71      }
72  
73      public ExportAction(ExportActionDefinition definition, JcrItemAdapter item, CommandsManager commandsManager, UiContext uiContext, SimpleTranslator i18n) throws ActionExecutionException {
74          this(definition, item, commandsManager, uiContext, i18n, null);
75      }
76  
77      @Override
78      protected void onPreExecute() throws Exception {
79          tempFileStreamResource = new TempFileStreamResource();
80          tempFileStreamResource.setTempFileName(getCurrentItem().getItemId().getUuid());
81          super.onPreExecute();
82          getCommand().setOutputStream(tempFileStreamResource.getTempFileOutputStream());
83      }
84  
85      @Override
86      protected Map<String, Object> buildParams(Item jcrItem) {
87          final Map<String, Object> params = super.buildParams(jcrItem);
88          getCurrentItem().getItemPropertyIds().forEach(id -> params.put(id.toString(), getCurrentItem().getItemProperty(id).getValue()));
89          return params;
90      }
91  
92      /**
93       * After command execution we push the created file to the client browser.<br>
94       * The created data is put in the temporary file 'fileOutput' linked to 'fileOutputStream' sent to the export command.<br>
95       * This temporary file is the used to create a {@code FileInputStream} that ensure that this temporary file is removed once the <br>
96       * fileInputStream is closed by Vaadin resource component.
97       * Directs the created file to the user.
98       */
99      @Override
100     protected void onPostExecute() throws Exception {
101         if (callback != null) {
102             callback.onSuccess(getDefinition().getName());
103         }
104         // We need to decode UTF8 the file name due to MGNLUI-3787
105         tempFileStreamResource.setFilename(DataTransporter.decodePath(getCommand().getFileName(), DataTransporter.UTF8));
106         tempFileStreamResource.setMIMEType(MIMEMapping.getMIMEType(FilenameUtils.getExtension(tempFileStreamResource.getFilename())));
107         // Opens the resource for download
108         Page.getCurrent().open(tempFileStreamResource, "", true);
109     }
110 
111     @Override
112     protected void onError(Exception e) {
113         final String title = getI18n().translate("ui-framework.abstractcommand.executionfailure");
114         final String body = e.getClass().getName() + ": " + getI18n().translate(e.getMessage());
115         getUiContext().openAlert(MessageStyleTypeEnum.ERROR, title, body, "cancel", () -> {
116             if (callback != null) {
117                 callback.onCancel();
118             }
119         });
120     }
121 
122     protected JcrExportCommand getCommand() {
123         return (JcrExportCommand) super.getCommand();
124     }
125 
126 }