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.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
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
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
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
118
119
120
121
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 }