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.dialog.action;
35  
36  import info.magnolia.cms.beans.runtime.FileProperties;
37  import info.magnolia.commands.CommandsManager;
38  import info.magnolia.event.EventBus;
39  import info.magnolia.importexport.command.JcrImportCommand;
40  import info.magnolia.jcr.util.NodeUtil;
41  import info.magnolia.ui.api.action.AbstractAction;
42  import info.magnolia.ui.api.action.ActionExecutionException;
43  import info.magnolia.ui.api.event.AdmincentralEventBus;
44  import info.magnolia.ui.api.event.ContentChangedEvent;
45  import info.magnolia.ui.form.EditorCallback;
46  import info.magnolia.ui.form.EditorValidator;
47  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
48  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeItemId;
49  
50  import java.util.HashSet;
51  import java.util.List;
52  import java.util.Set;
53  
54  import javax.inject.Inject;
55  import javax.inject.Named;
56  import javax.jcr.Binary;
57  import javax.jcr.Node;
58  import javax.jcr.RepositoryException;
59  
60  import org.apache.jackrabbit.JcrConstants;
61  import org.slf4j.Logger;
62  import org.slf4j.LoggerFactory;
63  
64  import com.vaadin.v7.data.Item;
65  
66  /**
67   * Call Import Command in order to perform the import action.
68   */
69  public class SaveImportDialogAction extends AbstractAction<SaveImportDialogActionDefinition> {
70  
71      private static final Logger log = LoggerFactory.getLogger(SaveImportDialogAction.class);
72  
73      private final Item item;
74      private final CommandsManager commandsManager;
75      private final EditorValidator validator;
76      private final EditorCallback callback;
77      private final EventBus eventBus;
78  
79      @Inject
80      public SaveImportDialogAction(SaveImportDialogActionDefinition definition, Item item, CommandsManager commandsManager, EditorValidator validator, EditorCallback callback, @Named(AdmincentralEventBus.NAME) EventBus eventBus) {
81          super(definition);
82          this.item = item;
83          this.commandsManager = commandsManager;
84          this.validator = validator;
85          this.callback = callback;
86          this.eventBus = eventBus;
87      }
88  
89      /**
90       * @deprecated since 5.4.9 - use {@link #SaveImportDialogAction(SaveImportDialogActionDefinition, Item, CommandsManager, EditorValidator, EditorCallback, EventBus)} instead.
91       */
92      @Deprecated
93      public SaveImportDialogAction(SaveImportDialogActionDefinition definition, Item item, CommandsManager commandsManager, EditorValidator validator, EditorCallback callback) {
94          this(definition, item, commandsManager, validator, callback, null);
95      }
96  
97      @Override
98      public void execute() throws ActionExecutionException {
99          // First Validate
100         validator.showValidation(true);
101         if (validator.isValid()) {
102             try {
103                 JcrNodeAdapter parent = (JcrNodeAdapter) item;
104                 JcrNodeAdapter importXml = (JcrNodeAdapter) parent.getChild("import");
105                 if (importXml == null) {
106                     throw new IllegalArgumentException("Nothing to import, given item does not contain any child named 'import'.");
107                 }
108 
109                 if (eventBus != null) {
110                     List<Node> nodesBeforeImport = NodeUtil.asList(NodeUtil.asIterable(parent.getJcrItem().getNodes()));
111                     executeCommand(parent);
112 
113                     List<Node> nodesAfterImport = NodeUtil.asList(NodeUtil.asIterable(parent.getJcrItem().getNodes()));
114                     Set<JcrNodeItemId> importedNodeItemIds = getImportedNodeItemIds(nodesBeforeImport, nodesAfterImport);
115 
116                     /**
117                      * HACK: Historically the 'import' action is partially mis-configured: instead of starting the import dialog from within a specialised action,
118                      * we typically hijack the OpenCreateDialogAction, which presets a callback that attempts to send ContentChangedEvent for some item
119                      * supposedly created with a dialog. However, the import action acts differently and we need to send the CCE for potentially many items (for all the imported ones).
120                      * To prevent the OpenCreateDialogAction's callback from firing the un-desired CCE, we invoke onCancel() method (effectively just closes a dialog) and send the proper event
121                      * right here in this action.
122                      */
123                     eventBus.fireEvent(new ContentChangedEvent(importedNodeItemIds));
124                     callback.onCancel();
125 
126                 } else {
127                     executeCommand(parent);
128                     callback.onSuccess(getDefinition().getName());
129                 }
130 
131             } catch (RepositoryException e) {
132                 throw new ActionExecutionException(e);
133             }
134         } else {
135             log.info("Validation error(s) occurred. No Import performed.");
136         }
137     }
138 
139     private Set<JcrNodeItemId> getImportedNodeItemIds(List<Node> nodesBeforeImport, List<Node> nodesAfterImport) throws RepositoryException {
140         Set<JcrNodeItemId> importedNodeItemIds = new HashSet<>();
141         for (Node nodeAfterImport : nodesAfterImport) {
142             boolean existedBeforeImport = false;
143             for (Node nodeBeforeImport : nodesBeforeImport) {
144                 if (NodeUtil.isSame(nodeAfterImport, nodeBeforeImport)) {
145                     existedBeforeImport = true;
146                     break;
147                 }
148             }
149             if (!existedBeforeImport) {
150                 importedNodeItemIds.add(new JcrNodeItemId(nodeAfterImport.getIdentifier(), nodeAfterImport.getSession().getWorkspace().getName()));
151             }
152         }
153         return importedNodeItemIds;
154     }
155 
156     public void executeCommand(JcrNodeAdapter itemChanged) throws ActionExecutionException {
157 
158         String commandName = getDefinition().getCommand();
159         String catalog = getDefinition().getCatalog();
160         long start = System.currentTimeMillis();
161         try {
162             JcrImportCommand command = (JcrImportCommand) this.commandsManager.getCommand(catalog, commandName);
163             if (command == null) {
164                 throw new ActionExecutionException(String.format("Could not find command [%s] in any catalog", commandName));
165             }
166             final JcrNodeAdapter node = (JcrNodeAdapter) itemChanged.getChild("import");
167             command.setStream(((Binary)node.getItemProperty(JcrConstants.JCR_DATA).getValue()).getStream());
168             command.setFileName((String) node.getItemProperty(FileProperties.PROPERTY_FILENAME).getValue());
169             command.setRepository(itemChanged.getWorkspace());
170             command.setPath(itemChanged.getJcrItem().getPath());
171             log.debug("Executing command [{}] from catalog [{}] with the following parameters [{}]...", new Object[]{commandName, catalog});
172             commandsManager.executeCommand(command, null);
173             log.debug("Command executed successfully in {} ms ", System.currentTimeMillis() - start);
174         } catch (Exception e) {
175             log.debug("Command execution failed after {} ms ", System.currentTimeMillis() - start);
176             throw new ActionExecutionException(e);
177         }
178     }
179 
180     protected CommandsManager getCommandsManager() {
181         return commandsManager;
182     }
183 }