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.editor.statusbar.activationstatus;
35
36 import info.magnolia.cms.beans.config.ServerConfiguration;
37 import info.magnolia.context.MgnlContext;
38 import info.magnolia.event.EventBus;
39 import info.magnolia.i18nsystem.SimpleTranslator;
40 import info.magnolia.jcr.util.NodeTypes;
41 import info.magnolia.jcr.util.NodeUtil;
42 import info.magnolia.pages.app.editor.PageEditorPresenter;
43 import info.magnolia.pages.app.editor.extension.AbstractExtension;
44 import info.magnolia.repository.RepositoryConstants;
45 import info.magnolia.ui.api.app.SubAppEventBus;
46 import info.magnolia.ui.api.event.AdmincentralEventBus;
47 import info.magnolia.ui.api.event.ContentChangedEvent;
48 import info.magnolia.ui.api.view.View;
49 import info.magnolia.ui.contentapp.detail.DetailLocation;
50 import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
51 import info.magnolia.ui.vaadin.integration.jcr.JcrNodeItemId;
52
53 import javax.inject.Inject;
54 import javax.inject.Named;
55 import javax.jcr.ItemNotFoundException;
56 import javax.jcr.Node;
57 import javax.jcr.RepositoryException;
58
59
60
61
62
63 public class ActivationStatus extends AbstractExtension {
64
65 private final SimpleTranslator i18n;
66 private final ServerConfiguration serverConfiguration;
67 private final ContentConnector contentConnector;
68 private final EventBus admincentralEventBus;
69 private final EventBus subAppEventBus;
70 private PageEditorPresenter pageEditorPresenter;
71
72 private ActivationStatusView view;
73
74 @Inject
75 public ActivationStatus(ActivationStatusView view, SimpleTranslator i18n, ServerConfiguration serverConfiguration, ContentConnector contentConnector,
76 final @Named(AdmincentralEventBus.NAME) EventBus admincentralEventBus, final @Named(SubAppEventBus.NAME) EventBus subAppEventBus, PageEditorPresenter pageEditorPresenter) {
77 this.view = view;
78 this.i18n = i18n;
79 this.serverConfiguration = serverConfiguration;
80 this.contentConnector = contentConnector;
81 this.admincentralEventBus = admincentralEventBus;
82 this.subAppEventBus = subAppEventBus;
83 this.pageEditorPresenter = pageEditorPresenter;
84 }
85
86 @Override
87 public View start(DetailLocation location) {
88 if (serverConfiguration.isAdmin()) {
89 registerHandlers();
90 updateActivationStatus(location.getNodePath());
91 } else {
92 view.setVisible(false);
93 }
94 return view;
95 }
96
97 private void updateActivationStatus(String nodePath) {
98 int status = NodeTypes.Activatable.ACTIVATION_STATUS_NOT_ACTIVATED;
99 try {
100 Node node = MgnlContext.getJCRSession(RepositoryConstants.WEBSITE).getNode(nodePath);
101 if (!node.isNodeType(NodeTypes.Page.NAME)) {
102 node = NodeUtil.getNearestAncestorOfType(node, NodeTypes.Page.NAME);
103 }
104 status = NodeTypes.Activatable.getActivationStatus(node);
105 } catch (RepositoryException e) {
106
107 }
108 setActivationStatus(status);
109 }
110
111
112 private void updateActivationStatus(JcrNodeItemId itemId) {
113 int status = NodeTypes.Activatable.ACTIVATION_STATUS_NOT_ACTIVATED;
114 try {
115 Node node = MgnlContext.getJCRSession(itemId.getWorkspace()).getNodeByIdentifier(itemId.getUuid());
116 if (!node.isNodeType(NodeTypes.Page.NAME)) {
117 node = NodeUtil.getNearestAncestorOfType(node, NodeTypes.Page.NAME);
118 }
119 status = NodeTypes.Activatable.getActivationStatus(node);
120 } catch (ItemNotFoundException e) {
121
122 updateActivationStatus(pageEditorPresenter.getStatus().getNodePath());
123 return;
124 } catch (RepositoryException e) {
125
126 }
127 setActivationStatus(status);
128
129 }
130
131 protected void setActivationStatus(int status) {
132 if (view.isVisible()) {
133 String icon = "activation-status ";
134 String text = i18n.translate("pages.editPage.statusBar.unpublished");
135 switch (status) {
136 case NodeTypes.Activatable.ACTIVATION_STATUS_MODIFIED:
137 icon += "color-yellow icon-status-orange";
138 text = i18n.translate("pages.editPage.statusBar.modified");
139 break;
140 case NodeTypes.Activatable.ACTIVATION_STATUS_ACTIVATED:
141 icon += "color-green icon-status-green";
142 text = i18n.translate("pages.editPage.statusBar.published");
143 break;
144 default:
145 icon += "color-red icon-status-red";
146 }
147
148 view.setIconStyle(icon);
149 view.setActivationStatus(text);
150 }
151 }
152
153 private void registerHandlers() {
154 subAppEventBus.addHandler(ContentChangedEvent.class, new ContentChangedEvent.Handler() {
155
156 @Override
157 public void onContentChanged(ContentChangedEvent event) {
158 if (event.getItemId() instanceof JcrNodeItemId) {
159 updateActivationStatus((JcrNodeItemId) event.getItemId());
160 }
161 }
162 });
163
164 admincentralEventBus.addHandler(ContentChangedEvent.class, new ContentChangedEvent.Handler() {
165
166 @Override
167 public void onContentChanged(ContentChangedEvent event) {
168 final Object itemId = event.getItemId();
169 final String nodePath = pageEditorPresenter.getStatus().getNodePath();
170 final String itemUrlFragment = contentConnector.getItemUrlFragment(itemId);
171
172 if (contentConnector.canHandleItem(itemId) && itemId instanceof JcrNodeItemId && itemUrlFragment != null && itemUrlFragment.equals(nodePath)) {
173 updateActivationStatus((JcrNodeItemId) itemId);
174 }
175 }
176 });
177 }
178
179 @Override
180 public void onLocationUpdate(DetailLocation location) {
181 if (serverConfiguration.isAdmin()) {
182 if (!view.isVisible()) {
183 view.setVisible(true);
184 }
185 updateActivationStatus(location.getNodePath());
186 }
187 }
188
189 @Override
190 public void deactivate() {
191 view.setVisible(false);
192 }
193 }