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