]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/AbstractCmsEntryPoint.java
Work on rap migration.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / AbstractCmsEntryPoint.java
1 package org.argeo.cms;
2
3 import java.util.Locale;
4 import java.util.ResourceBundle;
5
6 import javax.jcr.Node;
7 import javax.jcr.Repository;
8 import javax.jcr.RepositoryException;
9 import javax.jcr.Session;
10 import javax.jcr.nodetype.NodeType;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.argeo.jcr.JcrUtils;
15 import org.eclipse.rap.rwt.RWT;
16 import org.eclipse.rap.rwt.application.AbstractEntryPoint;
17 import org.eclipse.rap.rwt.client.service.BrowserNavigation;
18 import org.eclipse.rap.rwt.client.service.BrowserNavigationEvent;
19 import org.eclipse.rap.rwt.client.service.BrowserNavigationListener;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.Shell;
22 import org.springframework.security.core.context.SecurityContextHolder;
23
24 /** Manages history and navigation */
25 public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint
26 implements CmsSession {
27 private final Log log = LogFactory.getLog(AbstractCmsEntryPoint.class);
28
29 private Repository repository;
30 private String workspace;
31 private Session session;
32
33 // current state
34 private Node node;
35 private String state;
36 private String page;
37 private Throwable exception;
38
39 private BrowserNavigation history;
40
41 public AbstractCmsEntryPoint(Repository repository, String workspace) {
42 if (SecurityContextHolder.getContext().getAuthentication() == null)
43 logAsAnonymous();
44
45 this.repository = repository;
46 this.workspace = workspace;
47 authChange();
48
49 history = RWT.getClient().getService(BrowserNavigation.class);
50 if (history != null)
51 history.addBrowserNavigationListener(new CmsNavigationListener());
52
53 // RWT.setLocale(Locale.FRANCE);
54 }
55
56 @Override
57 protected Shell createShell(Display display) {
58 Shell shell = super.createShell(display);
59 shell.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_SHELL);
60 display.disposeExec(new Runnable() {
61
62 @Override
63 public void run() {
64 if (log.isTraceEnabled())
65 log.trace("Logging out " + session);
66 JcrUtils.logoutQuietly(session);
67 }
68 });
69 return shell;
70 }
71
72 /** Recreate header UI */
73 protected abstract void refreshHeader();
74
75 /** Recreate body UI */
76 protected abstract void refreshBody();
77
78 /** Log as anonymous */
79 protected abstract void logAsAnonymous();
80
81 /**
82 * The node to return when no node was found (for authenticated users and
83 * anonymous)
84 */
85 protected abstract Node getDefaultNode(Session session)
86 throws RepositoryException;
87
88 /**
89 * Reasonable default since it is a nt:hierarchyNode and is thus compatible
90 * with the obvious default folder type, nt:folder, conceptual equivalent of
91 * an empty text file in an operating system. To be overridden.
92 */
93 protected String getDefaultNewNodeType() {
94 return CmsTypes.CMS_TEXT;
95 }
96
97 /** Default new folder type (used in mkdirs) is nt:folder. To be overridden. */
98 protected String getDefaultNewFolderType() {
99 return NodeType.NT_FOLDER;
100 }
101
102 public void navigateTo(String state) {
103 exception = null;
104 setState(state);
105 refreshBody();
106 if (history != null)
107 history.pushState(state, state);
108 }
109
110 @Override
111 public void authChange() {
112 try {
113 String currentPath = null;
114 if (node != null)
115 currentPath = node.getPath();
116 JcrUtils.logoutQuietly(session);
117
118 if (SecurityContextHolder.getContext().getAuthentication() == null)
119 logAsAnonymous();
120 session = repository.login(workspace);
121 if (currentPath != null)
122 node = session.getNode(currentPath);
123
124 // refresh UI
125 refreshHeader();
126 refreshBody();
127 } catch (RepositoryException e) {
128 throw new CmsException("Cannot perform auth change", e);
129 }
130
131 }
132
133 @Override
134 public void exception(Throwable e) {
135 this.exception = e;
136 log.error("Unexpected exception in CMS", e);
137 refreshBody();
138 }
139
140 @Override
141 public Object local(Msg msg) {
142 String key = msg.getId();
143 int lastDot = key.lastIndexOf('.');
144 String className = key.substring(0, lastDot);
145 String fieldName = key.substring(lastDot + 1);
146 Locale locale = RWT.getLocale();
147 ResourceBundle rb = ResourceBundle.getBundle(className, locale,
148 msg.getClassLoader());
149 return rb.getString(fieldName);
150 }
151
152 /** Sets the state of the entry point and retrieve the related JCR node. */
153 protected synchronized void setState(String newState) {
154 String previousState = this.state;
155
156 node = null;
157 page = null;
158 this.state = newState;
159
160 try {
161 int firstSlash = state.indexOf('/');
162 if (firstSlash == 0) {
163 if (!session.nodeExists(state))
164 node = addNode(session, state, null);
165 else
166 node = session.getNode(state);
167 page = "";
168 } else if (firstSlash > 0) {
169 String prefix = state.substring(0, firstSlash);
170 String path = state.substring(firstSlash);
171 if (session.getWorkspace().getNodeTypeManager()
172 .hasNodeType(prefix)) {
173 String nodeType = prefix;
174 if (!session.nodeExists(path))
175 node = addNode(session, path, nodeType);
176 else {
177 node = session.getNode(path);
178 if (!node.isNodeType(nodeType))
179 throw new CmsException("Node " + path
180 + " not of type " + nodeType);
181 }
182 } else if ("delete".equals(prefix)) {
183 if (session.itemExists(path)) {
184 Node nodeToDelete = session.getNode(path);
185 // TODO "Are you sure?"
186 nodeToDelete.remove();
187 session.save();
188 log.debug("Deleted " + path);
189 navigateTo(previousState);
190 } else
191 throw new CmsException("Data " + path
192 + " does not exist");
193 } else {
194 if (session.itemExists(path))
195 node = session.getNode(path);
196 else
197 throw new CmsException("Data " + path
198 + " does not exist");
199 }
200 page = prefix;
201 } else {
202 node = getDefaultNode(session);
203 if (state.equals("~"))
204 page = "";
205 else
206 page = state;
207 }
208
209 if (log.isTraceEnabled())
210 log.trace("page=" + page + ", node=" + node + ", state="
211 + state);
212
213 } catch (RepositoryException e) {
214 throw new CmsException("Cannot retrieve node", e);
215 }
216 }
217
218 protected Node addNode(Session session, String path, String nodeType)
219 throws RepositoryException {
220 return JcrUtils.mkdirs(session, path, nodeType != null ? nodeType
221 : getDefaultNewNodeType(), getDefaultNewFolderType(), false);
222 // not saved, so that the UI can discard it later on
223 }
224
225 protected Node getNode() {
226 return node;
227 }
228
229 @Override
230 public String getState() {
231 return state;
232 }
233
234 protected String getPage() {
235 return page;
236 }
237
238 protected Throwable getException() {
239 return exception;
240 }
241
242 protected void resetException() {
243 exception = null;
244 }
245
246 protected Session getSession() {
247 return session;
248 }
249
250 private class CmsNavigationListener implements BrowserNavigationListener {
251 private static final long serialVersionUID = -3591018803430389270L;
252
253 @Override
254 public void navigated(BrowserNavigationEvent event) {
255 setState(event.getState());
256 refreshBody();
257 }
258 }
259
260 }