+ if (CmsJcrUtils.isUserHome(context) && byType.containsKey("nt:folder")) {// home node
+ types.add("nt:folder");
+ }
+
+ if (types.size() == 0)
+ throw new IllegalArgumentException("No type found for " + context + " (" + listTypes(context) + ")");
+ String type = types.iterator().next();
+ if (!byType.containsKey(type))
+ throw new IllegalArgumentException("No component found for " + context + " with type " + type);
+ return byType.get(type).get();
+ } catch (RepositoryException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ private static String listTypes(Node context) {
+ try {
+ StringBuilder sb = new StringBuilder();
+ sb.append(context.getPrimaryNodeType().getName());
+ for (NodeType superType : context.getPrimaryNodeType().getDeclaredSupertypes()) {
+ sb.append(' ');
+ sb.append(superType.getName());
+ }
+
+ for (NodeType nodeType : context.getMixinNodeTypes()) {
+ sb.append(' ');
+ sb.append(nodeType.getName());
+ if (nodeType.getName().equals(EntityType.local.get()))
+ sb.append('/').append(context.getProperty(EntityNames.ENTITY_TYPE).getString());
+ for (NodeType superType : nodeType.getDeclaredSupertypes()) {
+ sb.append(' ');
+ sb.append(superType.getName());
+ }
+ }
+ return sb.toString();
+ } catch (RepositoryException e) {
+ throw new JcrException(e);
+ }
+ }
+
+ @Override
+ public void setState(CmsUi cmsUi, String state) {
+ if (state == null)
+ return;
+ if (!state.startsWith("/")) {
+ if (cmsUi instanceof SuiteUi) {
+ SuiteUi ui = (SuiteUi) cmsUi;
+ if (LOGIN.equals(state)) {
+ String appTitle = "";
+ if (ui.getTitle() != null)
+ appTitle = ui.getTitle().lead();
+ ui.getCmsView().stateChanged(state, appTitle);
+ return;
+ }
+ Map<String, Object> properties = new HashMap<>();
+ String layerId = HOME_STATE.equals(state) ? defaultLayerPid : state;
+ properties.put(SuiteEvent.LAYER, layerId);
+ properties.put(SuiteEvent.NODE_PATH, HOME_STATE);
+ ui.getCmsView().sendEvent(SuiteEvent.switchLayer.topic(), properties);
+ }
+ return;
+ }
+ SuiteUi suiteUi = (SuiteUi) cmsUi;
+ Node node = stateToNode(suiteUi, state);
+ if (node == null) {
+ suiteUi.getCmsView().navigateTo(HOME_STATE);
+ } else {
+ suiteUi.getCmsView().sendEvent(SuiteEvent.switchLayer.topic(), SuiteEvent.eventProperties(node));
+ suiteUi.getCmsView().sendEvent(SuiteEvent.refreshPart.topic(), SuiteEvent.eventProperties(node));
+ }
+ }
+
+ // TODO move it to an internal package?
+ static String nodeToState(Node node) {
+ return '/' + Jcr.getWorkspaceName(node) + Jcr.getPath(node);
+ }
+
+ private Node stateToNode(SuiteUi suiteUi, String state) {
+ if (suiteUi == null)
+ return null;
+ if (state == null || !state.startsWith("/"))
+ return null;
+
+ String path = state.substring(1);
+ String workspace;
+ if (path.equals("")) {
+ workspace = null;
+ path = "/";
+ } else {
+ int index = path.indexOf('/');
+ if (index == 0) {
+ log.error("Cannot interpret " + state);
+// cmsView.navigateTo("~");
+ return null;
+ } else if (index > 0) {
+ workspace = path.substring(0, index);
+ path = path.substring(index);
+ } else {// index<0, assuming root node
+ workspace = path;
+ path = "/";
+ }
+ }
+ Session session = suiteUi.getSession(workspace);
+ if (session == null)
+ return null;
+ Node node = Jcr.getNode(session, path);
+ return node;
+ }
+
+ /*
+ * Events management
+ */