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.cms.core.Path;
37 import info.magnolia.event.EventBus;
38 import info.magnolia.ui.api.action.AbstractAction;
39 import info.magnolia.ui.api.action.ActionDefinition;
40 import info.magnolia.ui.api.action.ActionExecutionException;
41 import info.magnolia.ui.api.event.ContentChangedEvent;
42 import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
43 import info.magnolia.ui.vaadin.integration.jcr.JcrItemUtil;
44
45 import javax.jcr.Node;
46 import javax.jcr.RepositoryException;
47 import javax.jcr.Session;
48
49
50
51
52
53
54 public abstract class AbstractRepositoryAction<D extends ActionDefinition> extends AbstractAction<D> {
55
56 public static final String DEFAULT_NEW_ITEM_NAME = "untitled";
57
58 protected final JcrItemAdapter item;
59
60 private final EventBus eventBus;
61
62
63
64
65
66
67
68
69 private String itemIdOfChangedItem;
70
71 private boolean itemContentChanged;
72
73 protected AbstractRepositoryAction(D definition, JcrItemAdapter item, EventBus eventBus) {
74 super(definition);
75 this.item = item;
76 this.eventBus = eventBus;
77 }
78
79 protected void setItemIdOfChangedItem(String itemIdOfChangedItem) {
80 this.itemIdOfChangedItem = itemIdOfChangedItem;
81 }
82
83 protected void setItemContentChanged(boolean itemContentChanged) {
84 this.itemContentChanged = itemContentChanged;
85 }
86
87
88
89
90 @Override
91 public void execute() throws ActionExecutionException {
92 try {
93 Session session = item.getJcrItem().getSession();
94 onExecute(item);
95 session.save();
96
97
98 if (itemIdOfChangedItem != null) {
99 boolean propertyChange = JcrItemUtil.isPropertyItemId(itemIdOfChangedItem);
100 eventBus.fireEvent(new ContentChangedEvent(session.getWorkspace().getName(), itemIdOfChangedItem, itemContentChanged));
101 }
102
103 } catch (RepositoryException e) {
104 throw new ActionExecutionException("Can't execute repository operation.\n" + e.getMessage(), e);
105 }
106 }
107
108 protected abstract void onExecute(JcrItemAdapter item) throws RepositoryException;
109
110 protected String getUniqueNewItemName(Node parent) throws RepositoryException {
111 return getUniqueNewItemName(parent, DEFAULT_NEW_ITEM_NAME);
112 }
113
114 protected String getUniqueNewItemName(Node parent, String name) throws RepositoryException {
115 if (item == null) {
116 throw new IllegalArgumentException("Item cannot be null.");
117 }
118 return Path.getUniqueLabel(parent.getSession(), parent.getPath(), name);
119 }
120 }