Adapt to changes in Argeo Commons
[gpl/argeo-suite.git] / org.argeo.app.ui / src / org / argeo / app / ui / SuiteApp.java
index e0a21c6e65b3834a9f3248442e158daba2b90935..204688d81b360bf7c9b34673ced7d55c33ec70b3 100644 (file)
@@ -14,16 +14,12 @@ import java.util.TreeMap;
 import java.util.TreeSet;
 
 import javax.jcr.Node;
-import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
-import javax.jcr.Session;
 import javax.jcr.nodetype.NodeType;
-import javax.naming.InvalidNameException;
-import javax.naming.ldap.LdapName;
+import javax.xml.namespace.QName;
 
 import org.argeo.api.acr.Content;
 import org.argeo.api.acr.ContentRepository;
-import org.argeo.api.acr.ContentSession;
 import org.argeo.api.acr.spi.ProvidedSession;
 import org.argeo.api.cms.CmsConstants;
 import org.argeo.api.cms.CmsLog;
@@ -35,19 +31,18 @@ import org.argeo.app.api.EntityConstants;
 import org.argeo.app.api.EntityNames;
 import org.argeo.app.api.EntityType;
 import org.argeo.app.api.RankedObject;
-import org.argeo.app.core.SuiteUtils;
 import org.argeo.cms.AbstractCmsApp;
 import org.argeo.cms.CmsUserManager;
 import org.argeo.cms.LocaleUtils;
 import org.argeo.cms.Localized;
+import org.argeo.cms.acr.ContentUtils;
 import org.argeo.cms.jcr.CmsJcrUtils;
-import org.argeo.cms.jcr.acr.JcrContentProvider;
+import org.argeo.cms.jcr.acr.JcrContent;
 import org.argeo.cms.swt.CmsSwtUtils;
 import org.argeo.cms.swt.dialogs.CmsFeedback;
 import org.argeo.cms.ui.CmsUiProvider;
 import org.argeo.cms.ux.CmsUxUtils;
 import org.argeo.eclipse.ui.specific.UiContext;
-import org.argeo.jcr.Jcr;
 import org.argeo.jcr.JcrException;
 import org.argeo.util.LangUtils;
 import org.eclipse.swt.SWT;
@@ -98,10 +93,10 @@ public class SuiteApp extends AbstractCmsApp implements EventHandler {
 
        // ACR
        private ContentRepository contentRepository;
-       private JcrContentProvider jcrContentProvider;
+//     private JcrContentProvider jcrContentProvider;
 
        // JCR
-       private Repository repository;
+//     private Repository repository;
 
        public void init(Map<String, Object> properties) {
                if (log.isDebugEnabled())
@@ -183,7 +178,7 @@ public class SuiteApp extends AbstractCmsApp implements EventHandler {
        @Override
        public void refreshUi(CmsUi cmsUi, String state) {
                try {
-                       Node context = null;
+                       Content context = null;
                        SuiteUi ui = (SuiteUi) cmsUi;
 
                        String uiName = Objects.toString(ui.getParent().getData(UI_NAME_PROPERTY), null);
@@ -191,7 +186,7 @@ public class SuiteApp extends AbstractCmsApp implements EventHandler {
                                throw new IllegalStateException("UI name should not be null");
                        CmsView cmsView = CmsSwtUtils.getCmsView(ui);
 
-                       ContentSession contentSession = CmsUxUtils.getContentSession(contentRepository, cmsView);
+                       ProvidedSession contentSession = (ProvidedSession) CmsUxUtils.getContentSession(contentRepository, cmsView);
 
                        CmsUiProvider headerUiProvider = findUiProvider(headerPid);
                        CmsUiProvider footerUiProvider = findUiProvider(footerPid);
@@ -230,16 +225,18 @@ public class SuiteApp extends AbstractCmsApp implements EventHandler {
                                                ui.setUserDir(userDir);
 //                                             ui.initSessions(getRepository(), publicBasePath);
                                        } else {
-                                               Session adminSession = null;
-                                               try {
-                                                       adminSession = CmsJcrUtils.openDataAdminSession(getRepository(), null);
-                                                       Node userDirNode = SuiteUtils.getOrCreateCmsSessionNode(adminSession, cmsSession);
-                                                       Content userDir = contentSession.get(CmsConstants.SYS_WORKSPACE + userDirNode.getPath());
-                                                       ui.setUserDir(userDir);
-//                                                     ui.initSessions(getRepository(), userDirNode.getPath());
-                                               } finally {
-                                                       Jcr.logout(adminSession);
-                                               }
+//                                             Session adminSession = null;
+//                                             try {
+//                                                     adminSession = CmsJcrUtils.openDataAdminSession(getRepository(), null);
+//                                                     Node userDirNode = SuiteUtils.getOrCreateCmsSessionNode(adminSession, cmsSession);
+//                                                     Content userDir = contentSession.get(CmsConstants.SYS_WORKSPACE + userDirNode.getPath());
+//                                                     ui.setUserDir(userDir);
+////                                                   ui.initSessions(getRepository(), userDirNode.getPath());
+//                                             } finally {
+//                                                     Jcr.logout(adminSession);
+//                                             }
+                                               Content userDir = contentSession.getSessionRunDir();
+                                               ui.setUserDir(userDir);
                                        }
                                }
                                initLocale(cmsSession);
@@ -276,7 +273,7 @@ public class SuiteApp extends AbstractCmsApp implements EventHandler {
 
        }
 
-       private void refreshPart(CmsUiProvider uiProvider, Composite part, Node context) {
+       private void refreshPart(CmsUiProvider uiProvider, Composite part, Content context) {
                CmsSwtUtils.clear(part);
                uiProvider.createUiPart(part, context);
        }
@@ -293,59 +290,82 @@ public class SuiteApp extends AbstractCmsApp implements EventHandler {
                return layersByPid.get(pid).get();
        }
 
-       private <T> T findByType(Map<String, RankedObject<T>> byType, Node context) {
-               if (context == null)
+       private <T> T findByType(Map<String, RankedObject<T>> byType, Content content) {
+               if (content == null)
                        throw new IllegalArgumentException("A node should be provided");
-               try {
-                       // mixins
-                       Set<String> types = new TreeSet<>();
-                       for (NodeType mixinType : context.getMixinNodeTypes()) {
-                               String mixinTypeName = mixinType.getName();
-                               if (byType.containsKey(mixinTypeName)) {
-                                       types.add(mixinTypeName);
+
+               if (content instanceof JcrContent) {
+                       Node context = ((JcrContent) content).getJcrNode();
+                       try {
+                               // mixins
+                               Set<String> types = new TreeSet<>();
+                               for (NodeType mixinType : context.getMixinNodeTypes()) {
+                                       String mixinTypeName = mixinType.getName();
+                                       if (byType.containsKey(mixinTypeName)) {
+                                               types.add(mixinTypeName);
+                                       }
+                                       for (NodeType superType : mixinType.getDeclaredSupertypes()) {
+                                               if (byType.containsKey(superType.getName())) {
+                                                       types.add(superType.getName());
+                                               }
+                                       }
                                }
-                               for (NodeType superType : mixinType.getDeclaredSupertypes()) {
+                               // primary node type
+                               NodeType primaryType = context.getPrimaryNodeType();
+                               String primaryTypeName = primaryType.getName();
+                               if (byType.containsKey(primaryTypeName)) {
+                                       types.add(primaryTypeName);
+                               }
+                               for (NodeType superType : primaryType.getDeclaredSupertypes()) {
                                        if (byType.containsKey(superType.getName())) {
                                                types.add(superType.getName());
                                        }
                                }
-                       }
-                       // primary node type
-                       NodeType primaryType = context.getPrimaryNodeType();
-                       String primaryTypeName = primaryType.getName();
-                       if (byType.containsKey(primaryTypeName)) {
-                               types.add(primaryTypeName);
-                       }
-                       for (NodeType superType : primaryType.getDeclaredSupertypes()) {
-                               if (byType.containsKey(superType.getName())) {
-                                       types.add(superType.getName());
-                               }
-                       }
-                       // entity type
-                       if (context.isNodeType(EntityType.entity.get())) {
-                               if (context.hasProperty(EntityNames.ENTITY_TYPE)) {
-                                       String entityTypeName = context.getProperty(EntityNames.ENTITY_TYPE).getString();
-                                       if (byType.containsKey(entityTypeName)) {
-                                               types.add(entityTypeName);
+                               // entity type
+                               if (context.isNodeType(EntityType.entity.get())) {
+                                       if (context.hasProperty(EntityNames.ENTITY_TYPE)) {
+                                               String entityTypeName = context.getProperty(EntityNames.ENTITY_TYPE).getString();
+                                               if (byType.containsKey(entityTypeName)) {
+                                                       types.add(entityTypeName);
+                                               }
                                        }
                                }
-                       }
 
 //                     if (context.getPath().equals("/")) {// root node
 //                             types.add("nt:folder");
 //                     }
-                       if (CmsJcrUtils.isUserHome(context) && byType.containsKey("nt:folder")) {// home node
-                               types.add("nt:folder");
+                               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);
                        }
 
+               } else {
+
+                       List<QName> objectClasses = content.getTypes();
+                       Set<String> types = new TreeSet<>();
+                       for (QName cc : objectClasses) {
+                               String type = cc.getPrefix() + ":" + cc.getLocalPart();
+                               if (byType.containsKey(type))
+                                       types.add(type);
+                       }
                        if (types.size() == 0)
-                               throw new IllegalArgumentException("No type found for " + context + " (" + listTypes(context) + ")");
+                               throw new IllegalArgumentException("No type found for " + content + " (" + objectClasses + ")");
                        String type = types.iterator().next();
                        if (!byType.containsKey(type))
-                               throw new IllegalArgumentException("No component found for " + context + " with type " + type);
+                               throw new IllegalArgumentException("No component found for " + content + " with type " + type);
                        return byType.get(type).get();
-               } catch (RepositoryException e) {
-                       throw new IllegalStateException(e);
+                       // throw new UnsupportedOperationException("Content " +
+                       // content.getClass().getName() + " is not supported.");
                }
        }
 
@@ -397,7 +417,7 @@ public class SuiteApp extends AbstractCmsApp implements EventHandler {
                        return;
                }
                SuiteUi suiteUi = (SuiteUi) cmsUi;
-               Node node = stateToNode(suiteUi, state);
+               Content node = stateToNode(suiteUi, state);
                if (node == null) {
                        suiteUi.getCmsView().navigateTo(HOME_STATE);
                } else {
@@ -407,44 +427,46 @@ public class SuiteApp extends AbstractCmsApp implements EventHandler {
        }
 
        // TODO move it to an internal package?
-       static String nodeToState(Node node) {
-               return '/' + Jcr.getWorkspaceName(node) + Jcr.getPath(node);
+       static String nodeToState(Content node) {
+               return node.getPath();
        }
 
-       private Node stateToNode(SuiteUi suiteUi, String state) {
+       private Content 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 = "/";
-                       }
-               }
+               String path = state;
+//             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 = "/";
+//                     }
+//             }
 
                ProvidedSession contentSession = (ProvidedSession) CmsUxUtils.getContentSession(contentRepository,
                                suiteUi.getCmsView());
-               Session session = jcrContentProvider.getJcrSession(contentSession, workspace);
-//             Session session = suiteUi.getSession(workspace);
-               if (session == null)
-                       return null;
-               Node node = Jcr.getNode(session, path);
-               return node;
+               return contentSession.get(path);
+//             Session session = jcrContentProvider.getJcrSession(contentSession, workspace);
+////           Session session = suiteUi.getSession(workspace);
+//             if (session == null)
+//                     return null;
+//             Node node = Jcr.getNode(session, path);
+//             return node;
        }
 
        /*
@@ -466,23 +488,23 @@ public class SuiteApp extends AbstractCmsApp implements EventHandler {
 //                     String currentLayerId = ui.getCurrentLayerId();
 //                     SuiteLayer currentLayer = currentLayerId != null ? layersByPid.get(currentLayerId).get() : null;
                        if (SuiteUiUtils.isTopic(event, SuiteEvent.refreshPart)) {
-                               Node node = getNode(ui, event);
+                               Content node = getNode(ui, event);
                                if (node == null)
                                        return;
                                CmsUiProvider uiProvider = findByType(uiProvidersByType, node);
                                SuiteLayer layer = findByType(layersByType, node);
                                ui.switchToLayer(layer, node);
                                ui.getCmsView().runAs(() -> layer.view(uiProvider, ui.getCurrentWorkArea(), node));
-                               ui.getCmsView().stateChanged(nodeToState(node), appTitle + Jcr.getTitle(node));
+                               ui.getCmsView().stateChanged(nodeToState(node), appTitle + CmsUxUtils.getTitle(node));
                        } else if (SuiteUiUtils.isTopic(event, SuiteEvent.openNewPart)) {
-                               Node node = getNode(ui, event);
+                               Content node = getNode(ui, event);
                                if (node == null)
                                        return;
                                CmsUiProvider uiProvider = findByType(uiProvidersByType, node);
                                SuiteLayer layer = findByType(layersByType, node);
                                ui.switchToLayer(layer, node);
                                ui.getCmsView().runAs(() -> layer.open(uiProvider, ui.getCurrentWorkArea(), node));
-                               ui.getCmsView().stateChanged(nodeToState(node), appTitle + Jcr.getTitle(node));
+                               ui.getCmsView().stateChanged(nodeToState(node), appTitle + CmsUxUtils.getTitle(node));
                        } else if (SuiteUiUtils.isTopic(event, SuiteEvent.switchLayer)) {
                                String layerId = get(event, SuiteEvent.LAYER);
                                if (layerId != null) {
@@ -496,25 +518,25 @@ public class SuiteApp extends AbstractCmsApp implements EventHandler {
                                        String title = null;
                                        if (layerTitle != null)
                                                title = layerTitle.lead();
-                                       Node nodeFromState = getNode(ui, event);
+                                       Content nodeFromState = getNode(ui, event);
                                        if (nodeFromState != null && nodeFromState.getPath().equals(ui.getUserDirNode().getPath())) {
                                                // default layer view is forced
                                                String state = defaultLayerPid.equals(layerId) ? "~" : layerId;
                                                ui.getCmsView().stateChanged(state, appTitle + title);
                                                suiteLayer.view(null, workArea, nodeFromState);
                                        } else {
-                                               Node layerCurrentContext = suiteLayer.getCurrentContext(workArea);
+                                               Content layerCurrentContext = suiteLayer.getCurrentContext(workArea);
                                                if (layerCurrentContext != null) {
                                                        // layer was already showing a context so we set the state to it
                                                        ui.getCmsView().stateChanged(nodeToState(layerCurrentContext),
-                                                                       appTitle + Jcr.getTitle(layerCurrentContext));
+                                                                       appTitle + CmsUxUtils.getTitle(layerCurrentContext));
                                                } else {
                                                        // no context was shown
                                                        ui.getCmsView().stateChanged(layerId, appTitle + title);
                                                }
                                        }
                                } else {
-                                       Node node = getNode(ui, event);
+                                       Content node = getNode(ui, event);
                                        if (node != null) {
                                                SuiteLayer layer = findByType(layersByType, node);
                                                ui.getCmsView().runAs(() -> ui.switchToLayer(layer, node));
@@ -528,19 +550,21 @@ public class SuiteApp extends AbstractCmsApp implements EventHandler {
 
        }
 
-       private Node getNode(SuiteUi ui, Event event) {
+       private Content getNode(SuiteUi ui, Event event) {
                ProvidedSession contentSession = (ProvidedSession) CmsUxUtils.getContentSession(contentRepository,
                                ui.getCmsView());
 
-               String nodePath = get(event, SuiteEvent.NODE_PATH);
-               if (nodePath != null && nodePath.equals(HOME_STATE))
-                       return ui.getUserDirNode();
-               String workspace = get(event, SuiteEvent.WORKSPACE);
+               String path = get(event, SuiteEvent.CONTENT_PATH);
+
+//             String nodePath = get(event, SuiteEvent.NODE_PATH);
+               if (path != null && path.equals(HOME_STATE))
+                       return ui.getUserDir();
+//             String workspace = get(event, SuiteEvent.WORKSPACE);
 
-               Session session = jcrContentProvider.getJcrSession(contentSession, workspace);
-//             Session session = ui.getSession(workspace);
-               Node node;
-               if (nodePath == null) {
+//             Session session = jcrContentProvider.getJcrSession(contentSession, workspace);
+////           Session session = ui.getSession(workspace);
+               Content node;
+               if (path == null) {
                        // look for a user
                        String username = get(event, SuiteEvent.USERNAME);
                        if (username == null)
@@ -548,27 +572,30 @@ public class SuiteApp extends AbstractCmsApp implements EventHandler {
                        User user = cmsUserManager.getUser(username);
                        if (user == null)
                                return null;
-                       LdapName userDn;
-                       try {
-                               userDn = new LdapName(user.getName());
-                       } catch (InvalidNameException e) {
-                               throw new IllegalArgumentException("Badly formatted username", e);
-                       }
-                       String userNodePath = SuiteUtils.getUserNodePath(userDn);
-                       if (Jcr.itemExists(session, userNodePath))
-                               node = Jcr.getNode(session, userNodePath);
-                       else {
-                               Session adminSession = null;
-                               try {
-                                       adminSession = CmsJcrUtils.openDataAdminSession(getRepository(), workspace);
-                                       SuiteUtils.getOrCreateUserNode(adminSession, userDn);
-                               } finally {
-                                       Jcr.logout(adminSession);
-                               }
-                               node = Jcr.getNode(session, userNodePath);
-                       }
+                       node = ContentUtils.roleToContent(cmsUserManager, contentSession, user);
+//                     LdapName userDn;
+//                     try {
+//                             userDn = new LdapName(user.getName());
+//                     } catch (InvalidNameException e) {
+//                             throw new IllegalArgumentException("Badly formatted username", e);
+//                     }
+//                     String userNodePath = SuiteUtils.getUserNodePath(userDn);
+                       // FIXME deal with home path
+//                     return null;
+//                     if (Jcr.itemExists(session, userNodePath))
+//                             node = Jcr.getNode(session, userNodePath);
+//                     else {
+//                             Session adminSession = null;
+//                             try {
+//                                     adminSession = CmsJcrUtils.openDataAdminSession(getRepository(), workspace);
+//                                     SuiteUtils.getOrCreateUserNode(adminSession, userDn);
+//                             } finally {
+//                                     Jcr.logout(adminSession);
+//                             }
+//                             node = Jcr.getNode(session, userNodePath);
+//                     }
                } else {
-                       node = Jcr.getNode(session, nodePath);
+                       node = contentSession.get(path);
                }
                return node;
        }
@@ -648,7 +675,7 @@ public class SuiteApp extends AbstractCmsApp implements EventHandler {
                        List<String> types = LangUtils.toStringList(properties.get(EntityConstants.TYPE));
                        for (String type : types) {
                                if (layersByType.containsKey(type)) {
-                                       if (layersByType.get(type).equals(new RankedObject<CmsUiProvider>(layer, properties))) {
+                                       if (layersByType.get(type).equals(new RankedObject<SuiteLayer>(layer, properties))) {
                                                layersByType.remove(type);
                                        }
                                }
@@ -660,13 +687,13 @@ public class SuiteApp extends AbstractCmsApp implements EventHandler {
                this.cmsUserManager = cmsUserManager;
        }
 
-       protected Repository getRepository() {
-               return repository;
-       }
-
-       public void setRepository(Repository repository) {
-               this.repository = repository;
-       }
+//     protected Repository getRepository() {
+//             return repository;
+//     }
+//
+//     public void setRepository(Repository repository) {
+//             this.repository = repository;
+//     }
 
        protected ContentRepository getContentRepository() {
                return contentRepository;
@@ -676,8 +703,8 @@ public class SuiteApp extends AbstractCmsApp implements EventHandler {
                this.contentRepository = contentRepository;
        }
 
-       public void setJcrContentProvider(JcrContentProvider jcrContentProvider) {
-               this.jcrContentProvider = jcrContentProvider;
-       }
+//     public void setJcrContentProvider(JcrContentProvider jcrContentProvider) {
+//             this.jcrContentProvider = jcrContentProvider;
+//     }
 
 }