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.pages.app.action;
35
36 import info.magnolia.context.MgnlContext;
37 import info.magnolia.event.EventBus;
38 import info.magnolia.jcr.util.NodeTypes;
39 import info.magnolia.jcr.util.NodeUtil;
40 import info.magnolia.ui.api.action.AbstractAction;
41 import info.magnolia.ui.api.action.ActionExecutionException;
42 import info.magnolia.ui.api.app.SubAppEventBus;
43 import info.magnolia.ui.api.event.ContentChangedEvent;
44 import info.magnolia.ui.vaadin.gwt.client.shared.AreaElement;
45
46 import javax.inject.Inject;
47 import javax.inject.Named;
48 import javax.jcr.Node;
49 import javax.jcr.RepositoryException;
50 import javax.jcr.Session;
51
52
53
54
55
56
57 public class CreateAreaAction extends AbstractAction<CreateAreaActionDefinition> {
58
59 private AreaElement area;
60 private EventBus eventBus;
61
62 @Inject
63 public CreateAreaAction(CreateAreaActionDefinition definition, AreaElement area, @Named(SubAppEventBus.NAME) EventBus eventBus) {
64 super(definition);
65 this.area = area;
66 this.eventBus = eventBus;
67 }
68
69 @Override
70 public void execute() throws ActionExecutionException {
71 String workspace =area.getWorkspace();
72 String path = area.getPath();
73 int index = path.lastIndexOf("/");
74 String parent = path.substring(0, index);
75 String relPath = path.substring(index + 1);
76
77 try {
78 Session session = MgnlContext.getJCRSession(workspace);
79
80 Node parentNode = session.getNode(parent);
81
82 Node newNode = NodeUtil.createPath(parentNode, relPath, NodeTypes.Area.NAME);
83 session.save();
84
85 eventBus.fireEvent(new ContentChangedEvent(workspace, path));
86 } catch (RepositoryException e) {
87 throw new ActionExecutionException(e);
88 }
89 }
90 }