dc8060b3ad6d2bed0f91cf4defcb9eb7422e0f3a
[gpl/argeo-suite.git] / core / org.argeo.suite.ui / src / org / argeo / suite / ui / SuiteApp.java
1 package org.argeo.suite.ui;
2
3 import static org.argeo.cms.ui.CmsView.CMS_VIEW_UID_PROPERTY;
4
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Locale;
10 import java.util.Map;
11 import java.util.Set;
12 import java.util.TreeMap;
13 import java.util.TreeSet;
14
15 import javax.jcr.Node;
16 import javax.jcr.RepositoryException;
17 import javax.jcr.Session;
18 import javax.jcr.nodetype.NodeType;
19 import javax.naming.InvalidNameException;
20 import javax.naming.ldap.LdapName;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.argeo.api.NodeUtils;
25 import org.argeo.cms.CmsUserManager;
26 import org.argeo.cms.LocaleUtils;
27 import org.argeo.cms.auth.CmsSession;
28 import org.argeo.cms.ui.AbstractCmsApp;
29 import org.argeo.cms.ui.CmsTheme;
30 import org.argeo.cms.ui.CmsUiProvider;
31 import org.argeo.cms.ui.CmsView;
32 import org.argeo.cms.ui.dialogs.CmsFeedback;
33 import org.argeo.cms.ui.util.CmsEvent;
34 import org.argeo.cms.ui.util.CmsUiUtils;
35 import org.argeo.eclipse.ui.specific.UiContext;
36 import org.argeo.entity.EntityConstants;
37 import org.argeo.entity.EntityNames;
38 import org.argeo.entity.EntityType;
39 import org.argeo.jcr.Jcr;
40 import org.argeo.suite.RankedObject;
41 import org.argeo.suite.SuiteUtils;
42 import org.argeo.util.LangUtils;
43 import org.eclipse.swt.SWT;
44 import org.eclipse.swt.widgets.Composite;
45 import org.osgi.framework.Constants;
46 import org.osgi.service.event.Event;
47 import org.osgi.service.event.EventHandler;
48 import org.osgi.service.useradmin.User;
49
50 /** The Argeo Suite App. */
51 public class SuiteApp extends AbstractCmsApp implements EventHandler {
52         private final static Log log = LogFactory.getLog(SuiteApp.class);
53
54         public final static String PUBLIC_BASE_PATH_PROPERTY = "publicBasePath";
55         public final static String DEFAULT_UI_NAME_PROPERTY = "defaultUiName";
56         public final static String DEFAULT_THEME_ID_PROPERTY = "defaultThemeId";
57
58         private String publicBasePath = null;
59
60         private String pidPrefix;
61         private String headerPid;
62         private String leadPanePid;
63         private String loginScreenPid;
64 //      private String DASHBOARD_PID = pidPrefix + "dashboard";
65 //      private String RECENT_ITEMS_PID = pidPrefix + "recentItems";
66
67         private String defaultUiName = "app";
68         private String defaultThemeId = "org.argeo.suite.theme.default";
69
70         private Map<String, RankedObject<CmsUiProvider>> uiProvidersByPid = Collections.synchronizedMap(new HashMap<>());
71         private Map<String, RankedObject<CmsUiProvider>> uiProvidersByType = Collections.synchronizedMap(new HashMap<>());
72         private Map<String, RankedObject<SuiteLayer>> layersByPid = Collections.synchronizedSortedMap(new TreeMap<>());
73         private Map<String, RankedObject<SuiteLayer>> layersByType = Collections.synchronizedSortedMap(new TreeMap<>());
74
75         private CmsUserManager cmsUserManager;
76
77         // TODO make more optimal or via CmsSession/CmsView
78         private Map<String, SuiteUi> managedUis = new HashMap<>();
79
80 //      private CmsUiProvider headerPart = null;
81
82         public void init(Map<String, Object> properties) {
83                 if (log.isDebugEnabled())
84                         log.info("Argeo Suite App started");
85
86                 if (properties.containsKey(DEFAULT_UI_NAME_PROPERTY))
87                         defaultUiName = LangUtils.get(properties, DEFAULT_UI_NAME_PROPERTY);
88                 if (properties.containsKey(DEFAULT_THEME_ID_PROPERTY))
89                         defaultThemeId = LangUtils.get(properties, DEFAULT_THEME_ID_PROPERTY);
90                 publicBasePath = LangUtils.get(properties, PUBLIC_BASE_PATH_PROPERTY);
91
92                 if (properties.containsKey(Constants.SERVICE_PID)) {
93                         String servicePid = properties.get(Constants.SERVICE_PID).toString();
94                         if (servicePid.endsWith(".app")) {
95                                 pidPrefix = servicePid.substring(0, servicePid.length() - "app".length());
96                         }
97                 }
98
99                 if (pidPrefix == null)
100                         throw new IllegalArgumentException("PID prefix must be set.");
101
102                 headerPid = pidPrefix + "header";
103                 leadPanePid = pidPrefix + "leadPane";
104                 loginScreenPid = pidPrefix + "loginScreen";
105         }
106
107         public void destroy(Map<String, Object> properties) {
108                 for (SuiteUi ui : managedUis.values())
109                         if (!ui.isDisposed())
110                                 ui.dispose();
111                 if (log.isDebugEnabled())
112                         log.info("Argeo Suite App stopped");
113
114         }
115
116         @Override
117         public Set<String> getUiNames() {
118                 HashSet<String> uiNames = new HashSet<>();
119                 uiNames.add(defaultUiName);
120                 return uiNames;
121         }
122
123         @Override
124         public Composite initUi(Composite parent) {
125                 String uiName = parent.getData(UI_NAME_PROPERTY) != null ? parent.getData(UI_NAME_PROPERTY).toString() : null;
126                 CmsView cmsView = CmsView.getCmsView(parent);
127                 if (cmsView == null)
128                         throw new IllegalStateException("No CMS view is registered.");
129                 CmsTheme theme = getTheme(uiName);
130                 if (theme != null)
131                         CmsTheme.registerCmsTheme(parent.getShell(), theme);
132                 SuiteUi argeoSuiteUi = new SuiteUi(parent, SWT.INHERIT_DEFAULT);
133                 String uid = cmsView.getUid();
134                 managedUis.put(uid, argeoSuiteUi);
135                 argeoSuiteUi.addDisposeListener((e) -> {
136                         managedUis.remove(uid);
137                         if (log.isDebugEnabled())
138                                 log.debug("Suite UI " + uid + " has been disposed.");
139                 });
140                 return argeoSuiteUi;
141         }
142
143         @Override
144         public String getThemeId(String uiName) {
145                 return defaultThemeId;
146         }
147
148         @Override
149         public void refreshUi(Composite parent, String state) {
150                 try {
151                         Node context = null;
152                         SuiteUi ui = (SuiteUi) parent;
153                         CmsView cmsView = CmsView.getCmsView(parent);
154                         if (cmsView.isAnonymous() && publicBasePath == null) {// internal app, must login
155                                 ui.logout();
156                                 refreshPart(findUiProvider(headerPid), ui.getHeader(), context);
157                                 ui.refreshBelowHeader(false);
158                                 refreshPart(findUiProvider(loginScreenPid), ui.getBelowHeader(), context);
159                                 ui.layout(true, true);
160                         } else {
161                                 CmsSession cmsSession = cmsView.getCmsSession();
162                                 if (ui.getUserDir() == null) {
163                                         if (cmsView.isAnonymous()) {
164                                                 assert publicBasePath != null;
165                                                 ui.initSessions(getRepository(), publicBasePath);
166                                         } else {
167                                                 Session adminSession = null;
168                                                 try {
169                                                         adminSession = NodeUtils.openDataAdminSession(getRepository(), null);
170                                                         Node userDir = SuiteUtils.getOrCreateCmsSessionNode(adminSession, cmsSession);
171                                                         ui.initSessions(getRepository(), userDir.getPath());
172                                                 } finally {
173                                                         Jcr.logout(adminSession);
174                                                 }
175                                         }
176                                 }
177                                 initLocale(cmsSession);
178                                 context = stateToNode(ui, state);
179                                 if (context == null)
180                                         context = ui.getUserDir();
181
182                                 refreshPart(findUiProvider(headerPid), ui.getHeader(), context);
183                                 ui.refreshBelowHeader(true);
184                                 for (String key : layersByPid.keySet()) {
185                                         SuiteLayer layer = layersByPid.get(key).get();
186                                         ui.addLayer(key, layer);
187                                 }
188                                 refreshPart(findUiProvider(leadPanePid), ui.getLeadPane(), context);
189                                 ui.layout(true, true);
190                                 setState(parent, state);
191                         }
192                 } catch (Exception e) {
193                         CmsFeedback.show("Unexpected exception", e);
194                 }
195         }
196
197         private void initLocale(CmsSession cmsSession) {
198                 if (cmsSession == null)
199                         return;
200                 Locale locale = cmsSession.getLocale();
201                 UiContext.setLocale(locale);
202                 LocaleUtils.setThreadLocale(locale);
203
204         }
205
206         private void refreshPart(CmsUiProvider uiProvider, Composite part, Node context) {
207                 CmsUiUtils.clear(part);
208                 uiProvider.createUiPart(part, context);
209         }
210
211         private CmsUiProvider findUiProvider(String pid) {
212                 if (!uiProvidersByPid.containsKey(pid))
213                         throw new IllegalArgumentException("No UI provider registered as " + pid);
214                 return uiProvidersByPid.get(pid).get();
215         }
216
217         private <T> T findByType(Map<String, RankedObject<T>> byType, Node context) {
218                 if (context == null)
219                         throw new IllegalArgumentException("A node should be provided");
220                 try {
221                         // mixins
222                         Set<String> types = new TreeSet<>();
223                         for (NodeType nodeType : context.getMixinNodeTypes()) {
224                                 String typeName = nodeType.getName();
225                                 if (byType.containsKey(typeName)) {
226                                         types.add(typeName);
227                                 }
228                         }
229                         // primary node type
230                         {
231                                 NodeType nodeType = context.getPrimaryNodeType();
232                                 String typeName = nodeType.getName();
233                                 if (byType.containsKey(typeName)) {
234                                         types.add(typeName);
235                                 }
236                                 for (NodeType mixin : nodeType.getDeclaredSupertypes()) {
237                                         if (byType.containsKey(mixin.getName())) {
238                                                 types.add(mixin.getName());
239                                         }
240                                 }
241                         }
242                         // entity type
243                         if (context.isNodeType(EntityType.entity.get())) {
244                                 if (context.hasProperty(EntityNames.ENTITY_TYPE)) {
245                                         String typeName = context.getProperty(EntityNames.ENTITY_TYPE).getString();
246                                         if (byType.containsKey(typeName)) {
247                                                 types.add(typeName);
248                                         }
249                                 }
250                         }
251
252 //                      if (context.getPath().equals("/")) {// root node
253 //                              types.add("nt:folder");
254 //                      }
255                         if (NodeUtils.isUserHome(context) && byType.containsKey("nt:folder")) {// home node
256                                 types.add("nt:folder");
257                         }
258
259                         if (types.size() == 0)
260                                 throw new IllegalArgumentException("No type found for " + context);
261                         String type = types.iterator().next();
262                         if (!byType.containsKey(type))
263                                 throw new IllegalArgumentException("No component found for " + context + " with type " + type);
264                         return byType.get(type).get();
265                 } catch (RepositoryException e) {
266                         throw new IllegalStateException(e);
267                 }
268         }
269
270         @Override
271         public void setState(Composite parent, String state) {
272                 if (state == null || state.equals("~"))
273                         return;
274                 if (!state.startsWith("/") && !state.equals("~")) {
275                         if (parent instanceof SuiteUi) {
276                                 SuiteUi ui = (SuiteUi) parent;
277                                 String currentLayerId = ui.getCurrentLayerId();
278                                 if (state.equals(currentLayerId))
279                                         return; // does nothing
280                                 else {
281                                         Map<String, Object> properties = new HashMap<>();
282                                         properties.put(SuiteEvent.LAYER, state);
283                                         ui.getCmsView().sendEvent(SuiteEvent.switchLayer.topic(), properties);
284                                 }
285                         }
286                         return;
287                 }
288                 SuiteUi suiteUi = (SuiteUi) parent;
289                 Node node = stateToNode(suiteUi, state);
290                 if (node == null) {
291                         suiteUi.getCmsView().navigateTo("~");
292                 } else {
293                         suiteUi.getCmsView().sendEvent(SuiteEvent.switchLayer.topic(), SuiteEvent.eventProperties(node));
294                         suiteUi.getCmsView().sendEvent(SuiteEvent.refreshPart.topic(), SuiteEvent.eventProperties(node));
295                 }
296         }
297
298         private String nodeToState(Node node) {
299                 return '/' + Jcr.getWorkspaceName(node) + Jcr.getPath(node);
300         }
301
302         private Node stateToNode(SuiteUi suiteUi, String state) {
303                 if (suiteUi == null)
304                         return null;
305                 if (state == null || !state.startsWith("/"))
306                         return null;
307
308                 String path = state.substring(1);
309                 String workspace;
310                 if (path.equals("")) {
311                         workspace = null;
312                         path = "/";
313                 } else {
314                         int index = path.indexOf('/');
315                         if (index == 0) {
316                                 log.error("Cannot interpret " + state);
317 //                              cmsView.navigateTo("~");
318                                 return null;
319                         } else if (index > 0) {
320                                 workspace = path.substring(0, index);
321                                 path = path.substring(index);
322                         } else {// index<0, assuming root node
323                                 workspace = path;
324                                 path = "/";
325                         }
326                 }
327                 Session session = suiteUi.getSession(workspace);
328                 if (session == null)
329                         return null;
330                 Node node = Jcr.getNode(session, path);
331                 return node;
332         }
333
334         /*
335          * Events management
336          */
337
338         @Override
339         public void handleEvent(Event event) {
340
341                 // Specific UI related events
342                 SuiteUi ui = getRelatedUi(event);
343                 if (ui == null)
344                         return;
345                 try {
346 //                      String currentLayerId = ui.getCurrentLayerId();
347 //                      SuiteLayer currentLayer = currentLayerId != null ? layersByPid.get(currentLayerId).get() : null;
348                         if (isTopic(event, SuiteEvent.refreshPart)) {
349                                 Node node = getNode(ui, event);
350                                 if (node == null)
351                                         return;
352                                 CmsUiProvider uiProvider = findByType(uiProvidersByType, node);
353                                 SuiteLayer layer = findByType(layersByType, node);
354                                 ui.switchToLayer(layer, node);
355                                 ui.getCmsView().runAs(() -> layer.view(uiProvider, ui.getCurrentWorkArea(), node));
356                                 ui.getCmsView().stateChanged(nodeToState(node), Jcr.getTitle(node));
357                         } else if (isTopic(event, SuiteEvent.openNewPart)) {
358                                 Node node = getNode(ui, event);
359                                 if (node == null)
360                                         return;
361                                 CmsUiProvider uiProvider = findByType(uiProvidersByType, node);
362                                 SuiteLayer layer = findByType(layersByType, node);
363                                 ui.switchToLayer(layer, node);
364                                 ui.getCmsView().runAs(() -> layer.open(uiProvider, ui.getCurrentWorkArea(), node));
365                                 ui.getCmsView().stateChanged(nodeToState(node), Jcr.getTitle(node));
366                         } else if (isTopic(event, SuiteEvent.switchLayer)) {
367                                 String layerId = get(event, SuiteEvent.LAYER);
368                                 if (layerId != null) {
369 //                                      ui.switchToLayer(layerId, ui.getUserDir());
370                                         ui.getCmsView().runAs(() -> ui.switchToLayer(layerId, ui.getUserDir()));
371                                         ui.getCmsView().navigateTo(layerId);
372                                 } else {
373                                         Node node = getNode(ui, event);
374                                         if (node != null) {
375                                                 SuiteLayer layer = findByType(layersByType, node);
376                                                 ui.getCmsView().runAs(() -> ui.switchToLayer(layer, node));
377                                         }
378                                 }
379                         }
380                 } catch (Exception e) {
381                         log.error("Cannot handle event " + event, e);
382 //                      CmsView.getCmsView(ui).exception(e);
383                 }
384
385         }
386
387         private Node getNode(SuiteUi ui, Event event) {
388                 String nodePath = get(event, SuiteEvent.NODE_PATH);
389                 String workspaceName = get(event, SuiteEvent.WORKSPACE);
390                 Session session = ui.getSession(workspaceName);
391                 Node node;
392                 if (nodePath == null) {
393                         // look for a user
394                         String username = get(event, SuiteEvent.USERNAME);
395                         if (username == null)
396                                 return null;
397                         User user = cmsUserManager.getUser(username);
398                         if (user == null)
399                                 return null;
400                         LdapName userDn;
401                         try {
402                                 userDn = new LdapName(user.getName());
403                         } catch (InvalidNameException e) {
404                                 throw new IllegalArgumentException("Badly formatted username", e);
405                         }
406                         String userNodePath = SuiteUtils.getUserNodePath(userDn);
407                         if (Jcr.itemExists(session, userNodePath))
408                                 node = Jcr.getNode(session, userNodePath);
409                         else {
410                                 Session adminSession = null;
411                                 try {
412                                         adminSession = NodeUtils.openDataAdminSession(getRepository(), workspaceName);
413                                         SuiteUtils.getOrCreateUserNode(adminSession, userDn);
414                                 } finally {
415                                         Jcr.logout(adminSession);
416                                 }
417                                 node = Jcr.getNode(session, userNodePath);
418                         }
419                 } else {
420                         node = Jcr.getNode(session, nodePath);
421                 }
422                 return node;
423         }
424
425         private SuiteUi getRelatedUi(Event event) {
426                 return managedUis.get(get(event, CMS_VIEW_UID_PROPERTY));
427         }
428
429         private static boolean isTopic(Event event, CmsEvent cmsEvent) {
430                 return event.getTopic().equals(cmsEvent.topic());
431         }
432
433         private static String get(Event event, String key) {
434                 Object value = event.getProperty(key);
435                 if (value == null)
436                         return null;
437 //                      throw new IllegalArgumentException("Property " + key + " must be set");
438                 return value.toString();
439
440         }
441
442         /*
443          * Dependency injection.
444          */
445
446         public void addUiProvider(CmsUiProvider uiProvider, Map<String, Object> properties) {
447                 if (properties.containsKey(Constants.SERVICE_PID)) {
448                         String pid = (String) properties.get(Constants.SERVICE_PID);
449                         RankedObject.putIfHigherRank(uiProvidersByPid, pid, uiProvider, properties);
450                 }
451                 if (properties.containsKey(EntityConstants.TYPE)) {
452                         List<String> types = LangUtils.toStringList(properties.get(EntityConstants.TYPE));
453                         for (String type : types)
454                                 RankedObject.putIfHigherRank(uiProvidersByType, type, uiProvider, properties);
455                 }
456         }
457
458         public void removeUiProvider(CmsUiProvider uiProvider, Map<String, Object> properties) {
459                 if (properties.containsKey(Constants.SERVICE_PID)) {
460                         String pid = (String) properties.get(Constants.SERVICE_PID);
461                         if (uiProvidersByPid.containsKey(pid)) {
462                                 if (uiProvidersByPid.get(pid).equals(new RankedObject<CmsUiProvider>(uiProvider, properties))) {
463                                         uiProvidersByPid.remove(pid);
464                                 }
465                         }
466                 }
467                 if (properties.containsKey(EntityConstants.TYPE)) {
468                         List<String> types = LangUtils.toStringList(properties.get(EntityConstants.TYPE));
469                         for (String type : types) {
470                                 if (uiProvidersByType.containsKey(type)) {
471                                         if (uiProvidersByType.get(type).equals(new RankedObject<CmsUiProvider>(uiProvider, properties))) {
472                                                 uiProvidersByType.remove(type);
473                                         }
474                                 }
475                         }
476                 }
477         }
478
479         public void addLayer(SuiteLayer layer, Map<String, Object> properties) {
480                 if (properties.containsKey(Constants.SERVICE_PID)) {
481                         String pid = (String) properties.get(Constants.SERVICE_PID);
482                         RankedObject.putIfHigherRank(layersByPid, pid, layer, properties);
483                 }
484                 if (properties.containsKey(EntityConstants.TYPE)) {
485                         List<String> types = LangUtils.toStringList(properties.get(EntityConstants.TYPE));
486                         for (String type : types)
487                                 RankedObject.putIfHigherRank(layersByType, type, layer, properties);
488                 }
489         }
490
491         public void removeLayer(SuiteLayer layer, Map<String, Object> properties) {
492                 if (properties.containsKey(Constants.SERVICE_PID)) {
493                         String pid = (String) properties.get(Constants.SERVICE_PID);
494                         if (layersByPid.containsKey(pid)) {
495                                 if (layersByPid.get(pid).equals(new RankedObject<SuiteLayer>(layer, properties))) {
496                                         layersByPid.remove(pid);
497                                 }
498                         }
499                 }
500                 if (properties.containsKey(EntityConstants.TYPE)) {
501                         List<String> types = LangUtils.toStringList(properties.get(EntityConstants.TYPE));
502                         for (String type : types) {
503                                 if (layersByType.containsKey(type)) {
504                                         if (layersByType.get(type).equals(new RankedObject<CmsUiProvider>(layer, properties))) {
505                                                 layersByType.remove(type);
506                                         }
507                                 }
508                         }
509                 }
510         }
511
512         public void setCmsUserManager(CmsUserManager cmsUserManager) {
513                 this.cmsUserManager = cmsUserManager;
514         }
515
516 }