5ca46393daeed6dc5423ec42f7204027e06cc6da
[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.Localized;
28 import org.argeo.cms.auth.CmsSession;
29 import org.argeo.cms.ui.AbstractCmsApp;
30 import org.argeo.cms.ui.CmsTheme;
31 import org.argeo.cms.ui.CmsUiProvider;
32 import org.argeo.cms.ui.CmsView;
33 import org.argeo.cms.ui.dialogs.CmsFeedback;
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.jcr.JcrException;
41 import org.argeo.suite.RankedObject;
42 import org.argeo.suite.SuiteUtils;
43 import org.argeo.util.LangUtils;
44 import org.eclipse.swt.SWT;
45 import org.eclipse.swt.widgets.Composite;
46 import org.osgi.framework.Constants;
47 import org.osgi.service.event.Event;
48 import org.osgi.service.event.EventHandler;
49 import org.osgi.service.useradmin.User;
50
51 /** The Argeo Suite App. */
52 public class SuiteApp extends AbstractCmsApp implements EventHandler {
53         private final static Log log = LogFactory.getLog(SuiteApp.class);
54
55         public final static String PUBLIC_BASE_PATH_PROPERTY = "publicBasePath";
56         public final static String DEFAULT_UI_NAME_PROPERTY = "defaultUiName";
57         public final static String DEFAULT_THEME_ID_PROPERTY = "defaultThemeId";
58         public final static String DEFAULT_LAYER_PROPERTY = "defaultLayer";
59         private final static String LOGIN = "login";
60         private final static String HOME_STATE = "~";
61
62         private String publicBasePath = null;
63
64         private String pidPrefix;
65         private String headerPid;
66         private String footerPid;
67         private String leadPanePid;
68         private String loginScreenPid;
69
70         private String defaultLayerPid = "argeo.suite.ui.dashboardLayer";
71
72         private String defaultUiName = "app";
73         private String defaultThemeId = "org.argeo.suite.theme.default";
74
75         private Map<String, RankedObject<CmsUiProvider>> uiProvidersByPid = Collections.synchronizedMap(new HashMap<>());
76         private Map<String, RankedObject<CmsUiProvider>> uiProvidersByType = Collections.synchronizedMap(new HashMap<>());
77         private Map<String, RankedObject<SuiteLayer>> layersByPid = Collections.synchronizedSortedMap(new TreeMap<>());
78         private Map<String, RankedObject<SuiteLayer>> layersByType = Collections.synchronizedSortedMap(new TreeMap<>());
79
80         private CmsUserManager cmsUserManager;
81
82         // TODO make more optimal or via CmsSession/CmsView
83         private Map<String, SuiteUi> managedUis = new HashMap<>();
84
85         public void init(Map<String, Object> properties) {
86                 if (log.isDebugEnabled())
87                         log.info("Argeo Suite App started");
88
89                 if (properties.containsKey(DEFAULT_UI_NAME_PROPERTY))
90                         defaultUiName = LangUtils.get(properties, DEFAULT_UI_NAME_PROPERTY);
91                 if (properties.containsKey(DEFAULT_THEME_ID_PROPERTY))
92                         defaultThemeId = LangUtils.get(properties, DEFAULT_THEME_ID_PROPERTY);
93                 if (properties.containsKey(DEFAULT_LAYER_PROPERTY))
94                         defaultLayerPid = LangUtils.get(properties, DEFAULT_LAYER_PROPERTY);
95                 publicBasePath = LangUtils.get(properties, PUBLIC_BASE_PATH_PROPERTY);
96
97                 if (properties.containsKey(Constants.SERVICE_PID)) {
98                         String servicePid = properties.get(Constants.SERVICE_PID).toString();
99                         if (servicePid.endsWith(".app")) {
100                                 pidPrefix = servicePid.substring(0, servicePid.length() - "app".length());
101                         }
102                 }
103
104                 if (pidPrefix == null)
105                         throw new IllegalArgumentException("PID prefix must be set.");
106
107                 headerPid = pidPrefix + "header";
108                 footerPid = pidPrefix + "footer";
109                 leadPanePid = pidPrefix + "leadPane";
110                 loginScreenPid = pidPrefix + "loginScreen";
111         }
112
113         public void destroy(Map<String, Object> properties) {
114                 for (SuiteUi ui : managedUis.values())
115                         if (!ui.isDisposed())
116                                 ui.dispose();
117                 if (log.isDebugEnabled())
118                         log.info("Argeo Suite App stopped");
119
120         }
121
122         @Override
123         public Set<String> getUiNames() {
124                 HashSet<String> uiNames = new HashSet<>();
125                 uiNames.add(defaultUiName);
126                 return uiNames;
127         }
128
129         @Override
130         public Composite initUi(Composite parent) {
131                 String uiName = parent.getData(UI_NAME_PROPERTY) != null ? parent.getData(UI_NAME_PROPERTY).toString() : null;
132                 CmsView cmsView = CmsView.getCmsView(parent);
133                 if (cmsView == null)
134                         throw new IllegalStateException("No CMS view is registered.");
135                 CmsTheme theme = getTheme(uiName);
136                 if (theme != null)
137                         CmsTheme.registerCmsTheme(parent.getShell(), theme);
138                 SuiteUi argeoSuiteUi = new SuiteUi(parent, SWT.INHERIT_DEFAULT);
139                 String uid = cmsView.getUid();
140                 managedUis.put(uid, argeoSuiteUi);
141                 argeoSuiteUi.addDisposeListener((e) -> {
142                         managedUis.remove(uid);
143                         if (log.isDebugEnabled())
144                                 log.debug("Suite UI " + uid + " has been disposed.");
145                 });
146                 return argeoSuiteUi;
147         }
148
149         @Override
150         public String getThemeId(String uiName) {
151                 return defaultThemeId;
152         }
153
154         @Override
155         public void refreshUi(Composite parent, String state) {
156                 try {
157                         Node context = null;
158                         SuiteUi ui = (SuiteUi) parent;
159                         CmsView cmsView = CmsView.getCmsView(parent);
160                         CmsUiProvider headerUiProvider = findUiProvider(headerPid);
161                         CmsUiProvider footerUiProvider = findUiProvider(footerPid);
162                         CmsUiProvider leadPaneUiProvider = findUiProvider(leadPanePid);
163
164                         Localized appTitle = null;
165                         if (headerUiProvider instanceof DefaultHeader) {
166                                 appTitle = ((DefaultHeader) headerUiProvider).getTitle();
167                         }
168                         ui.setTitle(appTitle);
169
170                         if (cmsView.isAnonymous() && publicBasePath == null) {// internal app, must login
171                                 ui.logout();
172                                 if (headerUiProvider != null)
173                                         refreshPart(headerUiProvider, ui.getHeader(), context);
174                                 ui.refreshBelowHeader(false);
175                                 refreshPart(findUiProvider(loginScreenPid), ui.getBelowHeader(), context);
176                                 if (footerUiProvider != null)
177                                         refreshPart(footerUiProvider, ui.getFooter(), context);
178                                 ui.layout(true, true);
179                                 setState(ui, LOGIN);
180                         } else {
181                                 if (LOGIN.equals(state))
182                                         state = null;
183                                 CmsSession cmsSession = cmsView.getCmsSession();
184                                 if (ui.getUserDir() == null) {
185                                         // FIXME NPE on CMSSession when logging in from anonymous
186                                         if (cmsSession == null || cmsView.isAnonymous()) {
187                                                 assert publicBasePath != null;
188                                                 ui.initSessions(getRepository(), publicBasePath);
189                                         } else {
190                                                 Session adminSession = null;
191                                                 try {
192                                                         adminSession = NodeUtils.openDataAdminSession(getRepository(), null);
193                                                         Node userDir = SuiteUtils.getOrCreateCmsSessionNode(adminSession, cmsSession);
194                                                         ui.initSessions(getRepository(), userDir.getPath());
195                                                 } finally {
196                                                         Jcr.logout(adminSession);
197                                                 }
198                                         }
199                                 }
200                                 initLocale(cmsSession);
201                                 context = stateToNode(ui, state);
202                                 if (context == null)
203                                         context = ui.getUserDir();
204
205                                 if (headerUiProvider != null)
206                                         refreshPart(headerUiProvider, ui.getHeader(), context);
207                                 ui.refreshBelowHeader(true);
208                                 for (String key : layersByPid.keySet()) {
209                                         SuiteLayer layer = layersByPid.get(key).get();
210                                         ui.addLayer(key, layer);
211                                 }
212
213                                 if (leadPaneUiProvider != null)
214                                         refreshPart(leadPaneUiProvider, ui.getLeadPane(), context);
215                                 if (footerUiProvider != null)
216                                         refreshPart(footerUiProvider, ui.getFooter(), context);
217                                 ui.layout(true, true);
218                                 setState(parent, state != null ? state : defaultLayerPid);
219                         }
220                 } catch (Exception e) {
221                         CmsFeedback.show("Unexpected exception", e);
222                 }
223         }
224
225         private void initLocale(CmsSession cmsSession) {
226                 if (cmsSession == null)
227                         return;
228                 Locale locale = cmsSession.getLocale();
229                 UiContext.setLocale(locale);
230                 LocaleUtils.setThreadLocale(locale);
231
232         }
233
234         private void refreshPart(CmsUiProvider uiProvider, Composite part, Node context) {
235                 CmsUiUtils.clear(part);
236                 uiProvider.createUiPart(part, context);
237         }
238
239         private CmsUiProvider findUiProvider(String pid) {
240                 if (!uiProvidersByPid.containsKey(pid))
241                         return null;
242                 return uiProvidersByPid.get(pid).get();
243         }
244
245         private SuiteLayer findLayer(String pid) {
246                 if (!layersByPid.containsKey(pid))
247                         return null;
248                 return layersByPid.get(pid).get();
249         }
250
251         private <T> T findByType(Map<String, RankedObject<T>> byType, Node context) {
252                 if (context == null)
253                         throw new IllegalArgumentException("A node should be provided");
254                 try {
255                         // mixins
256                         Set<String> types = new TreeSet<>();
257                         for (NodeType mixinType : context.getMixinNodeTypes()) {
258                                 String mixinTypeName = mixinType.getName();
259                                 if (byType.containsKey(mixinTypeName)) {
260                                         types.add(mixinTypeName);
261                                 }
262                                 for (NodeType superType : mixinType.getDeclaredSupertypes()) {
263                                         if (byType.containsKey(superType.getName())) {
264                                                 types.add(superType.getName());
265                                         }
266                                 }
267                         }
268                         // primary node type
269                         NodeType primaryType = context.getPrimaryNodeType();
270                         String primaryTypeName = primaryType.getName();
271                         if (byType.containsKey(primaryTypeName)) {
272                                 types.add(primaryTypeName);
273                         }
274                         for (NodeType superType : primaryType.getDeclaredSupertypes()) {
275                                 if (byType.containsKey(superType.getName())) {
276                                         types.add(superType.getName());
277                                 }
278                         }
279                         // entity type
280                         if (context.isNodeType(EntityType.entity.get())) {
281                                 if (context.hasProperty(EntityNames.ENTITY_TYPE)) {
282                                         String entityTypeName = context.getProperty(EntityNames.ENTITY_TYPE).getString();
283                                         if (byType.containsKey(entityTypeName)) {
284                                                 types.add(entityTypeName);
285                                         }
286                                 }
287                         }
288
289 //                      if (context.getPath().equals("/")) {// root node
290 //                              types.add("nt:folder");
291 //                      }
292                         if (NodeUtils.isUserHome(context) && byType.containsKey("nt:folder")) {// home node
293                                 types.add("nt:folder");
294                         }
295
296                         if (types.size() == 0)
297                                 throw new IllegalArgumentException("No type found for " + context + " (" + listTypes(context) + ")");
298                         String type = types.iterator().next();
299                         if (!byType.containsKey(type))
300                                 throw new IllegalArgumentException("No component found for " + context + " with type " + type);
301                         return byType.get(type).get();
302                 } catch (RepositoryException e) {
303                         throw new IllegalStateException(e);
304                 }
305         }
306
307         private static String listTypes(Node context) {
308                 try {
309                         StringBuilder sb = new StringBuilder();
310                         sb.append(context.getPrimaryNodeType().getName());
311                         for (NodeType superType : context.getPrimaryNodeType().getDeclaredSupertypes()) {
312                                 sb.append(' ');
313                                 sb.append(superType.getName());
314                         }
315
316                         for (NodeType nodeType : context.getMixinNodeTypes()) {
317                                 sb.append(' ');
318                                 sb.append(nodeType.getName());
319                                 if (nodeType.getName().equals(EntityType.local.get()))
320                                         sb.append('/').append(context.getProperty(EntityNames.ENTITY_TYPE).getString());
321                                 for (NodeType superType : nodeType.getDeclaredSupertypes()) {
322                                         sb.append(' ');
323                                         sb.append(superType.getName());
324                                 }
325                         }
326                         return sb.toString();
327                 } catch (RepositoryException e) {
328                         throw new JcrException(e);
329                 }
330         }
331
332         @Override
333         public void setState(Composite parent, String state) {
334                 if (state == null)
335                         return;
336                 if (!state.startsWith("/")) {
337                         if (parent instanceof SuiteUi) {
338                                 SuiteUi ui = (SuiteUi) parent;
339                                 if (LOGIN.equals(state)) {
340                                         String appTitle = "";
341                                         if (ui.getTitle() != null)
342                                                 appTitle = ui.getTitle().lead();
343                                         ui.getCmsView().stateChanged(state, appTitle);
344                                         return;
345                                 }
346                                 String currentLayerId = ui.getCurrentLayerId();
347 //                              if (state.equals(currentLayerId))
348 //                                      return; // does nothing
349 //                              else {
350                                 Map<String, Object> properties = new HashMap<>();
351                                 String layerId = HOME_STATE.equals(state) ? defaultLayerPid : state;
352                                 properties.put(SuiteEvent.LAYER, layerId);
353                                 properties.put(SuiteEvent.NODE_PATH, HOME_STATE);
354                                 ui.getCmsView().sendEvent(SuiteEvent.switchLayer.topic(), properties);
355 //                              }
356                         }
357                         return;
358                 }
359                 SuiteUi suiteUi = (SuiteUi) parent;
360                 Node node = stateToNode(suiteUi, state);
361                 if (node == null) {
362                         suiteUi.getCmsView().navigateTo(HOME_STATE);
363                 } else {
364                         suiteUi.getCmsView().sendEvent(SuiteEvent.switchLayer.topic(), SuiteEvent.eventProperties(node));
365                         suiteUi.getCmsView().sendEvent(SuiteEvent.refreshPart.topic(), SuiteEvent.eventProperties(node));
366                 }
367         }
368
369         // TODO move it to an internal package?
370         static String nodeToState(Node node) {
371                 return '/' + Jcr.getWorkspaceName(node) + Jcr.getPath(node);
372         }
373
374         private Node stateToNode(SuiteUi suiteUi, String state) {
375                 if (suiteUi == null)
376                         return null;
377                 if (state == null || !state.startsWith("/"))
378                         return null;
379
380                 String path = state.substring(1);
381                 String workspace;
382                 if (path.equals("")) {
383                         workspace = null;
384                         path = "/";
385                 } else {
386                         int index = path.indexOf('/');
387                         if (index == 0) {
388                                 log.error("Cannot interpret " + state);
389 //                              cmsView.navigateTo("~");
390                                 return null;
391                         } else if (index > 0) {
392                                 workspace = path.substring(0, index);
393                                 path = path.substring(index);
394                         } else {// index<0, assuming root node
395                                 workspace = path;
396                                 path = "/";
397                         }
398                 }
399                 Session session = suiteUi.getSession(workspace);
400                 if (session == null)
401                         return null;
402                 Node node = Jcr.getNode(session, path);
403                 return node;
404         }
405
406         /*
407          * Events management
408          */
409
410         @Override
411         public void handleEvent(Event event) {
412
413                 // Specific UI related events
414                 SuiteUi ui = getRelatedUi(event);
415                 if (ui == null)
416                         return;
417                 try {
418                         String appTitle = "";
419                         if (ui.getTitle() != null)
420                                 appTitle = ui.getTitle().lead() + " - ";
421
422 //                      String currentLayerId = ui.getCurrentLayerId();
423 //                      SuiteLayer currentLayer = currentLayerId != null ? layersByPid.get(currentLayerId).get() : null;
424                         if (SuiteUiUtils.isTopic(event, SuiteEvent.refreshPart)) {
425                                 Node node = getNode(ui, event);
426                                 if (node == null)
427                                         return;
428                                 CmsUiProvider uiProvider = findByType(uiProvidersByType, node);
429                                 SuiteLayer layer = findByType(layersByType, node);
430                                 ui.switchToLayer(layer, node);
431                                 ui.getCmsView().runAs(() -> layer.view(uiProvider, ui.getCurrentWorkArea(), node));
432                                 ui.getCmsView().stateChanged(nodeToState(node), appTitle + Jcr.getTitle(node));
433                         } else if (SuiteUiUtils.isTopic(event, SuiteEvent.openNewPart)) {
434                                 Node node = getNode(ui, event);
435                                 if (node == null)
436                                         return;
437                                 CmsUiProvider uiProvider = findByType(uiProvidersByType, node);
438                                 SuiteLayer layer = findByType(layersByType, node);
439                                 ui.switchToLayer(layer, node);
440                                 ui.getCmsView().runAs(() -> layer.open(uiProvider, ui.getCurrentWorkArea(), node));
441                                 ui.getCmsView().stateChanged(nodeToState(node), appTitle + Jcr.getTitle(node));
442                         } else if (SuiteUiUtils.isTopic(event, SuiteEvent.switchLayer)) {
443                                 String layerId = get(event, SuiteEvent.LAYER);
444                                 if (layerId != null) {
445 //                                      ui.switchToLayer(layerId, ui.getUserDir());
446                                         SuiteLayer suiteLayer = findLayer(layerId);
447                                         if (suiteLayer == null)
448                                                 throw new IllegalArgumentException("No layer '" + layerId + "' available.");
449                                         Localized layerTitle = suiteLayer.getTitle();
450                                         // FIXME make sure we don't rebuild the work area twice
451                                         Composite workArea = ui.getCmsView().doAs(() -> ui.switchToLayer(layerId, ui.getUserDir()));
452                                         String title = null;
453                                         if (layerTitle != null)
454                                                 title = layerTitle.lead();
455                                         Node nodeFromState = getNode(ui, event);
456                                         if (nodeFromState != null && nodeFromState.getPath().equals(ui.getUserDir().getPath())) {
457                                                 // default layer view is forced
458                                                 String state = defaultLayerPid.equals(layerId) ? "~" : layerId;
459                                                 ui.getCmsView().stateChanged(state, appTitle + title);
460                                                 suiteLayer.view(null, workArea, nodeFromState);
461                                         } else {
462                                                 Node layerCurrentContext = suiteLayer.getCurrentContext(workArea);
463                                                 if (layerCurrentContext != null) {
464                                                         // layer was already showing a context so we set the state to it
465                                                         ui.getCmsView().stateChanged(nodeToState(layerCurrentContext),
466                                                                         appTitle + Jcr.getTitle(layerCurrentContext));
467                                                 } else {
468                                                         // no context was shown
469                                                         ui.getCmsView().stateChanged(layerId, appTitle + title);
470                                                 }
471                                         }
472                                 } else {
473                                         Node node = getNode(ui, event);
474                                         if (node != null) {
475                                                 SuiteLayer layer = findByType(layersByType, node);
476                                                 ui.getCmsView().runAs(() -> ui.switchToLayer(layer, node));
477                                         }
478                                 }
479                         }
480                 } catch (Exception e) {
481                         log.error("Cannot handle event " + event, e);
482 //                      CmsView.getCmsView(ui).exception(e);
483                 }
484
485         }
486
487         private Node getNode(SuiteUi ui, Event event) {
488                 String nodePath = get(event, SuiteEvent.NODE_PATH);
489                 if (nodePath != null && nodePath.equals(HOME_STATE))
490                         return ui.getUserDir();
491                 String workspaceName = get(event, SuiteEvent.WORKSPACE);
492                 Session session = ui.getSession(workspaceName);
493                 Node node;
494                 if (nodePath == null) {
495                         // look for a user
496                         String username = get(event, SuiteEvent.USERNAME);
497                         if (username == null)
498                                 return null;
499                         User user = cmsUserManager.getUser(username);
500                         if (user == null)
501                                 return null;
502                         LdapName userDn;
503                         try {
504                                 userDn = new LdapName(user.getName());
505                         } catch (InvalidNameException e) {
506                                 throw new IllegalArgumentException("Badly formatted username", e);
507                         }
508                         String userNodePath = SuiteUtils.getUserNodePath(userDn);
509                         if (Jcr.itemExists(session, userNodePath))
510                                 node = Jcr.getNode(session, userNodePath);
511                         else {
512                                 Session adminSession = null;
513                                 try {
514                                         adminSession = NodeUtils.openDataAdminSession(getRepository(), workspaceName);
515                                         SuiteUtils.getOrCreateUserNode(adminSession, userDn);
516                                 } finally {
517                                         Jcr.logout(adminSession);
518                                 }
519                                 node = Jcr.getNode(session, userNodePath);
520                         }
521                 } else {
522                         node = Jcr.getNode(session, nodePath);
523                 }
524                 return node;
525         }
526
527         private SuiteUi getRelatedUi(Event event) {
528                 return managedUis.get(get(event, CMS_VIEW_UID_PROPERTY));
529         }
530
531         public static String get(Event event, String key) {
532                 Object value = event.getProperty(key);
533                 if (value == null)
534                         return null;
535 //                      throw new IllegalArgumentException("Property " + key + " must be set");
536                 return value.toString();
537
538         }
539
540         /*
541          * Dependency injection.
542          */
543
544         public void addUiProvider(CmsUiProvider uiProvider, Map<String, Object> properties) {
545                 if (properties.containsKey(Constants.SERVICE_PID)) {
546                         String pid = (String) properties.get(Constants.SERVICE_PID);
547                         RankedObject.putIfHigherRank(uiProvidersByPid, pid, uiProvider, properties);
548                 }
549                 if (properties.containsKey(EntityConstants.TYPE)) {
550                         List<String> types = LangUtils.toStringList(properties.get(EntityConstants.TYPE));
551                         for (String type : types)
552                                 RankedObject.putIfHigherRank(uiProvidersByType, type, uiProvider, properties);
553                 }
554         }
555
556         public void removeUiProvider(CmsUiProvider uiProvider, Map<String, Object> properties) {
557                 if (properties.containsKey(Constants.SERVICE_PID)) {
558                         String pid = (String) properties.get(Constants.SERVICE_PID);
559                         if (uiProvidersByPid.containsKey(pid)) {
560                                 if (uiProvidersByPid.get(pid).equals(new RankedObject<CmsUiProvider>(uiProvider, properties))) {
561                                         uiProvidersByPid.remove(pid);
562                                 }
563                         }
564                 }
565                 if (properties.containsKey(EntityConstants.TYPE)) {
566                         List<String> types = LangUtils.toStringList(properties.get(EntityConstants.TYPE));
567                         for (String type : types) {
568                                 if (uiProvidersByType.containsKey(type)) {
569                                         if (uiProvidersByType.get(type).equals(new RankedObject<CmsUiProvider>(uiProvider, properties))) {
570                                                 uiProvidersByType.remove(type);
571                                         }
572                                 }
573                         }
574                 }
575         }
576
577         public void addLayer(SuiteLayer layer, Map<String, Object> properties) {
578                 if (properties.containsKey(Constants.SERVICE_PID)) {
579                         String pid = (String) properties.get(Constants.SERVICE_PID);
580                         RankedObject.putIfHigherRank(layersByPid, pid, layer, properties);
581                 }
582                 if (properties.containsKey(EntityConstants.TYPE)) {
583                         List<String> types = LangUtils.toStringList(properties.get(EntityConstants.TYPE));
584                         for (String type : types)
585                                 RankedObject.putIfHigherRank(layersByType, type, layer, properties);
586                 }
587         }
588
589         public void removeLayer(SuiteLayer layer, Map<String, Object> properties) {
590                 if (properties.containsKey(Constants.SERVICE_PID)) {
591                         String pid = (String) properties.get(Constants.SERVICE_PID);
592                         if (layersByPid.containsKey(pid)) {
593                                 if (layersByPid.get(pid).equals(new RankedObject<SuiteLayer>(layer, properties))) {
594                                         layersByPid.remove(pid);
595                                 }
596                         }
597                 }
598                 if (properties.containsKey(EntityConstants.TYPE)) {
599                         List<String> types = LangUtils.toStringList(properties.get(EntityConstants.TYPE));
600                         for (String type : types) {
601                                 if (layersByType.containsKey(type)) {
602                                         if (layersByType.get(type).equals(new RankedObject<CmsUiProvider>(layer, properties))) {
603                                                 layersByType.remove(type);
604                                         }
605                                 }
606                         }
607                 }
608         }
609
610         public void setCmsUserManager(CmsUserManager cmsUserManager) {
611                 this.cmsUserManager = cmsUserManager;
612         }
613
614 }