work on a first draft of the AKB UI
authorBruno Sinou <bsinou@argeo.org>
Fri, 8 Nov 2013 18:51:28 +0000 (18:51 +0000)
committerBruno Sinou <bsinou@argeo.org>
Fri, 8 Nov 2013 18:51:28 +0000 (18:51 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@6606 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

14 files changed:
modules/org.argeo.slc.akb.services/META-INF/spring/services-osgi.xml
plugins/org.argeo.slc.akb.ui/META-INF/spring/commands.xml
plugins/org.argeo.slc.akb.ui/META-INF/spring/osgi.xml
plugins/org.argeo.slc.akb.ui/pom.xml
plugins/org.argeo.slc.akb.ui/src/main/java/org/argeo/slc/akb/ui/AkbEnvPerspective.java
plugins/org.argeo.slc.akb.ui/src/main/java/org/argeo/slc/akb/ui/AkbTemplatesPerspective.java
plugins/org.argeo.slc.akb.ui/src/main/java/org/argeo/slc/akb/ui/commands/OpenAkbNodeEditor.java
plugins/org.argeo.slc.akb.ui/src/main/java/org/argeo/slc/akb/ui/editors/AbstractAkbNodeEditor.java
plugins/org.argeo.slc.akb.ui/src/main/java/org/argeo/slc/akb/ui/providers/TemplatesTreeContentProvider.java
plugins/org.argeo.slc.akb.ui/src/main/java/org/argeo/slc/akb/ui/views/AkbTemplatesTreeView.java
runtime/org.argeo.slc.akb/src/main/java/org/argeo/slc/akb/AkbService.java [new file with mode: 0644]
runtime/org.argeo.slc.akb/src/main/java/org/argeo/slc/akb/core/AkbServiceImpl.java
runtime/org.argeo.slc.akb/src/main/java/org/argeo/slc/akb/utils/AkbJcrUtils.java [new file with mode: 0644]
runtime/org.argeo.slc.akb/src/main/resources/org/argeo/slc/akb/akb.cnd

index d852bfe0d72e87acafd018c812ed814ec826ac18..9b792f372433fcd182a59a879c392e3b754adf91 100644 (file)
@@ -22,4 +22,5 @@
                        <beans:entry key="argeo.jcr.repository.alias" value="akb" />\r
                </service-properties>\r
        </service>\r
+       <service ref="akbService" interface="org.argeo.slc.akb.AkbService" />\r
 </beans:beans>
index 43e7e4b397d75aa655867c31ae37c48095a128cb..36470a90c878072828ae058ecb02bba910879306 100644 (file)
@@ -8,5 +8,6 @@
        <bean id="openAkbNodeEditor" class="org.argeo.slc.akb.ui.commands.OpenAkbNodeEditor"
                scope="prototype">
                <property name="repository" ref="repository" />
+               <property name="akbService" ref="akbService" />
        </bean>
 </beans>
index 62479ef7ba49dd0ba363360419f9962b514ec443..103227e879fcd9bc339e878b30aa6e1e1752d8f8 100644 (file)
@@ -12,4 +12,5 @@
        <!-- REFERENCES -->\r
        <reference id="repository" interface="javax.jcr.Repository"\r
                filter="(argeo.jcr.repository.alias=akb)" cardinality="0..1" />\r
+       <reference id="akbService" interface="org.argeo.slc.akb.AkbService" />\r
 </beans:beans>
\ No newline at end of file
index eb6096a97e295acb49af10394999f413430c9328..e5f2c2d1755ceb44a1b190335b51cbce7a9b2f17 100644 (file)
@@ -28,6 +28,7 @@
                                                <Import-Package>
                                                        *,
                                                        org.argeo.eclipse.spring,
+                                                       javax.jcr.nodetype
                                                </Import-Package>
                                        </instructions>
                                </configuration>
index cfd6d357153a2919a490a6cf2bbba9c6c5a1eb2f..7a899d2e7c3459cdd320f39b5ac5af0f1b337fba 100644 (file)
@@ -17,7 +17,6 @@ public class AkbEnvPerspective implements IPerspectiveFactory {
 
                IFolderLayout left = layout.createFolder("left", IPageLayout.LEFT,
                                0.3f, editorArea);
-               left.addView(AkbTemplatesTreeView.ID);
                left.addView(AkbDefaultView.ID);
        }
 }
index 137ccd28c04f9a262242729a033847171ba09891..0d2aeff6f4e0c39cdc1cc0a1111f0fb6dc8b5fbc 100644 (file)
@@ -1,6 +1,7 @@
 package org.argeo.slc.akb.ui;
 
 import org.argeo.slc.akb.ui.views.AkbDefaultView;
+import org.argeo.slc.akb.ui.views.AkbTemplatesTreeView;
 import org.eclipse.ui.IFolderLayout;
 import org.eclipse.ui.IPageLayout;
 import org.eclipse.ui.IPerspectiveFactory;
@@ -16,6 +17,7 @@ public class AkbTemplatesPerspective implements IPerspectiveFactory {
 
                IFolderLayout left = layout.createFolder("left", IPageLayout.LEFT,
                                0.3f, editorArea);
+               left.addView(AkbTemplatesTreeView.ID);
                left.addView(AkbDefaultView.ID);
        }
 }
index c02f118b69d444444de9ef6a5bd3564ed9670b70..46de0c3d16377fc2fab8478e16f9581e12b18128 100644 (file)
@@ -5,8 +5,10 @@ import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
+import org.argeo.eclipse.ui.dialogs.SingleValue;
 import org.argeo.jcr.JcrUtils;
 import org.argeo.slc.akb.AkbException;
+import org.argeo.slc.akb.AkbService;
 import org.argeo.slc.akb.AkbTypes;
 import org.argeo.slc.akb.ui.AkbUiPlugin;
 import org.argeo.slc.akb.ui.editors.AkbConnectorAliasEditor;
@@ -15,6 +17,7 @@ import org.argeo.slc.akb.ui.editors.AkbNodeEditorInput;
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.handlers.HandlerUtil;
 
@@ -35,6 +38,7 @@ public class OpenAkbNodeEditor extends AbstractHandler {
 
        /* DEPENDENCY INJECTION */
        private Repository repository;
+       private AkbService akbService;
 
        public final static String PARAM_NODE_JCR_ID = "param.nodeJcrId";
        public final static String PARAM_NODE_TYPE = "param.nodeType";
@@ -48,6 +52,10 @@ public class OpenAkbNodeEditor extends AbstractHandler {
 
                Session session = null;
                try {
+                       // caches current Page
+                       IWorkbenchPage currentPage = HandlerUtil.getActiveWorkbenchWindow(
+                                       event).getActivePage();
+
                        session = repository.login();
                        Node node = null;
 
@@ -60,13 +68,20 @@ public class OpenAkbNodeEditor extends AbstractHandler {
                        else
                                node = session.getNodeByIdentifier(nodeJcrId);
 
+                       // no node has been found or created, return
+                       if (node == null)
+                               return null;
+
                        String editorId = getEditorForNode(node);
 
+                       // no editor has been found, return
+                       if (editorId == null)
+                               return null;
+
                        AkbNodeEditorInput eei = new AkbNodeEditorInput(
                                        node.getIdentifier());
 
-                       HandlerUtil.getActiveWorkbenchWindow(event).getActivePage()
-                                       .openEditor(eei, editorId);
+                       currentPage.openEditor(eei, editorId);
                } catch (PartInitException pie) {
                        throw new AkbException(
                                        "Unexpected PartInitException while opening akb node editor",
@@ -82,8 +97,19 @@ public class OpenAkbNodeEditor extends AbstractHandler {
 
        private Node createNewNode(Session session, String nodeType,
                        String parentNodeJcrId) throws RepositoryException {
-               Node parentNode = session.getNodeByIdentifier(parentNodeJcrId);
-               Node node = parentNode.addNode("new", nodeType);
+               Node node = null;
+               if (AkbTypes.AKB_ENV_TEMPLATE.equals(nodeType)) {
+                       String name = SingleValue.ask("Template name",
+                                       "Please give a name to the template to create");
+                       if (name != null)
+                               node = akbService.createAkbTemplate(
+                                               session.getNodeByIdentifier(parentNodeJcrId), name);
+                       else
+                               return null;
+               } else {
+                       Node parentNode = session.getNodeByIdentifier(parentNodeJcrId);
+                       node = parentNode.addNode("new", nodeType);
+               }
                // corresponding node is saved but not checked in, in order to ease
                // cancel actions.
                session.save();
@@ -92,7 +118,6 @@ public class OpenAkbNodeEditor extends AbstractHandler {
 
        private String getEditorForNode(Node node) throws RepositoryException {
                String editorId = null;
-
                if (node.isNodeType(AkbTypes.AKB_CONNECTOR_ALIAS))
                        editorId = AkbConnectorAliasEditor.ID;
                else if (node.isNodeType(AkbTypes.AKB_ENV_TEMPLATE))
@@ -106,4 +131,8 @@ public class OpenAkbNodeEditor extends AbstractHandler {
        public void setRepository(Repository repository) {
                this.repository = repository;
        }
+
+       public void setAkbService(AkbService akbService) {
+               this.akbService = akbService;
+       }
 }
\ No newline at end of file
index ba2963e638f98ce9ec406008a7f4dcfe3b61499f..cc7c8ddea52eeca8110a661344ffa72042b34908 100644 (file)
@@ -1,23 +1,23 @@
 package org.argeo.slc.akb.ui.editors;
 
 import javax.jcr.Node;
+import javax.jcr.Property;
 import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
 import org.argeo.jcr.JcrUtils;
 import org.argeo.slc.akb.AkbException;
-import org.argeo.slc.akb.ui.AkbUiPlugin;
+import org.argeo.slc.akb.utils.AkbJcrUtils;
 import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.swt.widgets.Composite;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IEditorSite;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.part.EditorPart;
 
 /**
- * Parent Abstract Node editor for AKB. Manage life cycle of the JCR session that
- * is bound to it.
+ * Parent Abstract Node editor for AKB. Manage life cycle of the JCR session
+ * that is bound to it.
  */
 public abstract class AbstractAkbNodeEditor extends EditorPart {
        // private final static Log log = LogFactory
@@ -31,6 +31,9 @@ public abstract class AbstractAkbNodeEditor extends EditorPart {
        // Business Objects
        private Node akbNode;
 
+       // Some constants
+       private final static int SHORT_NAME_LENGHT = 10;
+
        // LIFE CYCLE
        public void init(IEditorSite site, IEditorInput input)
                        throws PartInitException {
@@ -40,33 +43,29 @@ public abstract class AbstractAkbNodeEditor extends EditorPart {
                        session = repository.login();
                        AkbNodeEditorInput anei = (AkbNodeEditorInput) getEditorInput();
                        akbNode = session.getNodeByIdentifier(anei.getIdentifier());
-
-                       // try to set a default part name
-                       updatePartName();
-
-                       // update tooltip
-                       // String displayName = CommonsJcrUtils.get(getEntity(),
-                       // Property.JCR_TITLE);
-
-                       // if (CommonsJcrUtils.isEmptyString(displayName))
-                       // displayName = "current item";
-                       // setTitleToolTip("Display and edit information for " +
-                       // displayName);
+                       updatePartNameAndToolTip();
                } catch (RepositoryException e) {
                        throw new AkbException("Unable open editor for akb node", e);
                }
        }
 
        /**
-        * Overwrite to provide a specific part Name
+        * Overwrite to provide a specific part Name and / or tooltip
         */
-       protected void updatePartName() {
-               // String name = CommonsJcrUtils.get(entity, Property.JCR_TITLE);
-               // if (CommonsJcrUtils.checkNotEmptyString(name)) {
-               // if (name.length() > SHORT_NAME_LENGHT)
-               // name = name.substring(0, SHORT_NAME_LENGHT - 1) + "...";
-               // setPartName(name);
-               // }
+       protected void updatePartNameAndToolTip() {
+               String name = JcrUtils.get(akbNode, Property.JCR_TITLE);
+
+               // Name
+               if (AkbJcrUtils.checkNotEmptyString(name)) {
+                       if (name.length() > SHORT_NAME_LENGHT)
+                               name = name.substring(0, SHORT_NAME_LENGHT - 1) + "...";
+                       setPartName(name);
+               }
+
+               // Tooltip
+               if (AkbJcrUtils.isEmptyString(name))
+                       name = "current item";
+               setTitleToolTip("Display and edit " + name);
        }
 
        /* EXPOSES TO CHILDREN CLASSES */
index 6d57dce63db8463cdc007b22c11b89375d4c153f..33571446fe98114077edb06771382b490f6d6ef5 100644 (file)
  */
 package org.argeo.slc.akb.ui.providers;
 
-import org.argeo.eclipse.ui.TreeParent;
+import java.util.List;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+
+import org.argeo.jcr.JcrUtils;
+import org.argeo.slc.akb.AkbException;
 import org.eclipse.jface.viewers.ITreeContentProvider;
 import org.eclipse.jface.viewers.Viewer;
 
@@ -35,15 +41,29 @@ public class TemplatesTreeContentProvider implements ITreeContentProvider {
        }
 
        public Object getParent(Object child) {
-               return ((TreeParent) child).getParent();
+               try {
+                       return ((Node) child).getParent();
+               } catch (RepositoryException e) {
+                       throw new AkbException("Error while getting parent node", e);
+               }
        }
 
        public Object[] getChildren(Object parent) {
-               return ((TreeParent) parent).getChildren();
+               try {
+                       List<Node> nodes = JcrUtils.nodeIteratorToList(((Node) parent)
+                                       .getNodes());
+                       return nodes.toArray();
+               } catch (RepositoryException e) {
+                       throw new AkbException("Error while getting children nodes", e);
+               }
        }
 
        public boolean hasChildren(Object parent) {
-               return ((TreeParent) parent).hasChildren();
+               try {
+                       return ((Node) parent).hasNodes();
+               } catch (RepositoryException e) {
+                       throw new AkbException("Error while checking children nodes", e);
+               }
        }
 
        public void dispose() {
index 9e82b4bebcfbeed546fbcdc79eadaa870dad1c57..9ab230520c0bc4a4cbf6311e75f16168d9e82fa9 100644 (file)
  */\r
 package org.argeo.slc.akb.ui.views;\r
 \r
+import java.util.ArrayList;\r
 import java.util.HashMap;\r
+import java.util.List;\r
 import java.util.Map;\r
 \r
 import javax.jcr.Node;\r
+import javax.jcr.NodeIterator;\r
 import javax.jcr.Repository;\r
 import javax.jcr.RepositoryException;\r
 import javax.jcr.Session;\r
 \r
 import org.argeo.eclipse.ui.utils.CommandUtils;\r
+import org.argeo.jcr.JcrUtils;\r
 import org.argeo.slc.akb.AkbException;\r
 import org.argeo.slc.akb.AkbNames;\r
 import org.argeo.slc.akb.AkbTypes;\r
@@ -95,7 +99,8 @@ public class AkbTemplatesTreeView extends ViewPart {
        public void createPartControl(Composite parent) {\r
                initialize();\r
 \r
-               createResultsTreeViewer(parent);\r
+               resultTreeViewer = createResultsTreeViewer(parent);\r
+               resultTreeViewer.setInput(initializeResultTree());\r
 \r
                // parent.setLayout(new FillLayout());\r
                // // Main layout\r
@@ -173,12 +178,6 @@ public class AkbTemplatesTreeView extends ViewPart {
                // .getDecoratorManager().getLabelDecorator();\r
                // viewer.setLabelProvider(new DecoratingLabelProvider(rtLblProvider,\r
                // decorator));\r
-               // viewer.addDoubleClickListener(new ViewDoubleClickListener());\r
-\r
-               // Override default behaviour to insure that 2 distincts results that\r
-               // have the same name will be correctly and distincly returned by\r
-               // corresponding TreeViewer.getSelection() method.\r
-               // viewer.setComparer(new ResultItemsComparer());\r
 \r
                getSite().setSelectionProvider(viewer);\r
 \r
@@ -215,65 +214,26 @@ public class AkbTemplatesTreeView extends ViewPart {
        public void setFocus() {\r
        }\r
 \r
-       // private ResultParent[] initializeResultTree() {\r
-       // try {\r
-       // // Force initialization of the tree structure if needed\r
-       // SlcJcrResultUtils.getSlcResultsParentNode(session);\r
-       // SlcJcrResultUtils.getMyResultParentNode(session);\r
-       // // Remove yesterday and last 7 days virtual folders\r
-       // // ResultParent[] roots = new ResultParent[5];\r
-       // ResultParent[] roots = new ResultParent[3];\r
-       //\r
-       // // My results\r
-       // roots[0] = new ParentNodeFolder(null,\r
-       // SlcJcrResultUtils.getMyResultParentNode(session),\r
-       // SlcUiConstants.DEFAULT_MY_RESULTS_FOLDER_LABEL);\r
-       //\r
-       // // today\r
-       // Calendar cal = Calendar.getInstance();\r
-       // String relPath = JcrUtils.dateAsPath(cal);\r
-       // List<String> datePathes = new ArrayList<String>();\r
-       // datePathes.add(relPath);\r
-       // roots[1] = new VirtualFolder(null,\r
-       // ResultParentUtils.getResultsForDates(session, datePathes),\r
-       // "Today");\r
-       //\r
-       // // // Yesterday\r
-       // // cal = Calendar.getInstance();\r
-       // // cal.add(Calendar.DAY_OF_YEAR, -1);\r
-       // // relPath = JcrUtils.dateAsPath(cal);\r
-       // // datePathes = new ArrayList<String>();\r
-       // // datePathes.add(relPath);\r
-       // // roots[2] = new VirtualFolder(null,\r
-       // // ResultParentUtils.getResultsForDates(session, datePathes),\r
-       // // "Yesterday");\r
-       // // // Last 7 days\r
-       // //\r
-       // // cal = Calendar.getInstance();\r
-       // // datePathes = new ArrayList<String>();\r
-       // //\r
-       // // for (int i = 0; i < 7; i++) {\r
-       // // cal.add(Calendar.DAY_OF_YEAR, -i);\r
-       // // relPath = JcrUtils.dateAsPath(cal);\r
-       // // datePathes.add(relPath);\r
-       // // }\r
-       // // roots[3] = new VirtualFolder(null,\r
-       // // ResultParentUtils.getResultsForDates(session, datePathes),\r
-       // // "Last 7 days");\r
-       //\r
-       // // All results\r
-       // Node otherResultsPar = session.getNode(SlcJcrResultUtils\r
-       // .getSlcResultsBasePath(session));\r
-       // // roots[4] = new ParentNodeFolder(null, otherResultsPar,\r
-       // // "All results");\r
-       // roots[2] = new ParentNodeFolder(null, otherResultsPar,\r
-       // "All results");\r
-       // return roots;\r
-       // } catch (RepositoryException re) {\r
-       // throw new ArgeoException(\r
-       // "Unexpected error while initializing ResultTree.", re);\r
-       // }\r
-       // }\r
+       private Node[] initializeResultTree() {\r
+               try {\r
+\r
+                       NodeIterator ni = templatesParentNode.getNodes();\r
+                       List<Node> templates = new ArrayList<Node>();\r
+\r
+                       while (ni.hasNext()) {\r
+                               Node currNode = ni.nextNode();\r
+                               if (currNode.isNodeType(AkbTypes.AKB_ENV_TEMPLATE))\r
+                                       templates.add(currNode);\r
+                       }\r
+\r
+                       Node[] templateArr = templates.toArray(new Node[templates.size()]);\r
+\r
+                       return templateArr;\r
+               } catch (RepositoryException re) {\r
+                       throw new AkbException("Error while initializing templates Tree.",\r
+                                       re);\r
+               }\r
+       }\r
 \r
        // Manage context menu\r
        /**\r
@@ -283,54 +243,6 @@ public class AkbTemplatesTreeView extends ViewPart {
                IWorkbenchWindow window = AkbUiPlugin.getDefault().getWorkbench()\r
                                .getActiveWorkbenchWindow();\r
                try {\r
-                       // IStructuredSelection selection = (IStructuredSelection)\r
-                       // resultTreeViewer\r
-                       // .getSelection();\r
-                       // boolean canAddSubfolder = false;\r
-                       // boolean canRenamefolder = false;\r
-                       // boolean isSingleResultNode = false;\r
-                       // boolean isUnderMyResult = false;\r
-                       // boolean validMultipleDelete = false;\r
-                       //\r
-                       // // Building conditions\r
-                       // if (selection.size() == 1) {\r
-                       // Object obj = selection.getFirstElement();\r
-                       // if (obj instanceof SingleResultNode)\r
-                       // isSingleResultNode = true;\r
-                       // else if (obj instanceof ParentNodeFolder) {\r
-                       // Node cNode = ((ParentNodeFolder) obj).getNode();\r
-                       // if (cNode.isNodeType(SlcTypes.SLC_RESULT_FOLDER)) {\r
-                       // canAddSubfolder = true;\r
-                       // canRenamefolder = true;\r
-                       // isUnderMyResult = true;\r
-                       // } else if (cNode\r
-                       // .isNodeType(SlcTypes.SLC_MY_RESULT_ROOT_FOLDER)) {\r
-                       // canAddSubfolder = true;\r
-                       // }\r
-                       // }\r
-                       // } else {\r
-                       // @SuppressWarnings("rawtypes")\r
-                       // Iterator it = selection.iterator();\r
-                       // multicheck: while (it.hasNext()) {\r
-                       // validMultipleDelete = true;\r
-                       // Object obj = it.next();\r
-                       // if (obj instanceof SingleResultNode)\r
-                       // continue multicheck;\r
-                       // else if (obj instanceof ParentNodeFolder) {\r
-                       // Node cNode = ((ParentNodeFolder) obj).getNode();\r
-                       // if (cNode.isNodeType(SlcTypes.SLC_RESULT_FOLDER))\r
-                       // continue multicheck;\r
-                       // else {\r
-                       // validMultipleDelete = false;\r
-                       // break multicheck;\r
-                       // }\r
-                       // } else {\r
-                       // validMultipleDelete = false;\r
-                       // break multicheck;\r
-                       // }\r
-                       // }\r
-                       // }\r
-\r
                        Map<String, String> params = new HashMap<String, String>();\r
                        params.put(OpenAkbNodeEditor.PARAM_PARENT_NODE_JCR_ID,\r
                                        templatesParentNode.getIdentifier());\r
@@ -342,39 +254,6 @@ public class AkbTemplatesTreeView extends ViewPart {
                                        OpenAkbNodeEditor.ID, "Create new template...", null, true,\r
                                        params);\r
 \r
-                       //\r
-                       // CommandUtils.refreshCommand(menuManager, window, DeleteItems.ID,\r
-                       // DeleteItems.DEFAULT_LABEL, DeleteItems.DEFAULT_IMG_DESCRIPTOR,\r
-                       // isUnderMyResult || isSingleResultNode || validMultipleDelete);\r
-                       //\r
-                       // CommandUtils.refreshCommand(menuManager, window,\r
-                       // AddResultFolder.ID,\r
-                       // AddResultFolder.DEFAULT_LABEL,\r
-                       // ClientUiPlugin.getDefault().getWorkbench().getSharedImages()\r
-                       // .getImageDescriptor(ISharedImages.IMG_OBJ_ADD),\r
-                       // canAddSubfolder);\r
-                       //\r
-                       // CommandUtils.refreshCommand(menuManager, window,\r
-                       // RenameResultFolder.ID,\r
-                       // RenameResultFolder.DEFAULT_LABEL,\r
-                       // RenameResultFolder.DEFAULT_IMG_DESCRIPTOR, canRenamefolder);\r
-                       //\r
-                       // // Command removed for the time being.\r
-                       // CommandUtils.refreshCommand(menuManager, window,\r
-                       // RenameResultNode.ID,\r
-                       // RenameResultNode.DEFAULT_LABEL,\r
-                       // RenameResultNode.DEFAULT_IMG_DESCRIPTOR, false);\r
-                       //\r
-                       // // Test to be removed\r
-                       // // If you use this pattern, do not forget to call\r
-                       // // menuManager.setRemoveAllWhenShown(true);\r
-                       // // when creating the menuManager\r
-                       //\r
-                       // // menuManager.add(new Action("Test") {\r
-                       // // public void run() {\r
-                       // // log.debug("do something");\r
-                       // // }\r
-                       // // });\r
                } catch (RepositoryException re) {\r
                        throw new AkbException("Error while refreshing context menu", re);\r
                }\r
@@ -398,60 +277,11 @@ public class AkbTemplatesTreeView extends ViewPart {
                }\r
        }\r
 \r
-       // class MyResultsObserver extends AsyncUiEventListener {\r
-       //\r
-       // public MyResultsObserver(Display display) {\r
-       // super(display);\r
-       // }\r
-       //\r
-       // @Override\r
-       // protected Boolean willProcessInUiThread(List<Event> events)\r
-       // throws RepositoryException {\r
-       // // unfiltered for the time being\r
-       // return true;\r
-       // }\r
-       //\r
-       // protected void onEventInUiThread(List<Event> events)\r
-       // throws RepositoryException {\r
-       // List<Node> nodesToRefresh = new ArrayList<Node>();\r
-       //\r
-       // for (Event event : events) {\r
-       // String parPath = JcrUtils.parentPath(event.getPath());\r
-       // if (session.nodeExists(parPath)) {\r
-       // Node node = session.getNode(parPath);\r
-       // if (!nodesToRefresh.contains(node)) {\r
-       // nodesToRefresh.add(node);\r
-       // }\r
-       // }\r
-       // }\r
-       //\r
-       // // Update check nodes\r
-       // for (Node node : nodesToRefresh)\r
-       // jcrRefresh(node);\r
-       // refresh(null);\r
-       // }\r
-       // }\r
-\r
-       // class AllResultsObserver extends AsyncUiEventListener {\r
-       //\r
-       // public AllResultsObserver(Display display) {\r
-       // super(display);\r
-       // }\r
-       //\r
-       // @Override\r
-       // protected Boolean willProcessInUiThread(List<Event> events)\r
-       // throws RepositoryException {\r
-       // // unfiltered for the time being\r
-       // return true;\r
-       // }\r
-       //\r
-       // protected void onEventInUiThread(List<Event> events)\r
-       // throws RepositoryException {\r
-       // refresh(null);\r
-       // // if (lastSelectedSourceElementParent != null)\r
-       // // refresh(lastSelectedSourceElementParent);\r
-       // }\r
-       // }\r
+       @Override\r
+       public void dispose() {\r
+               JcrUtils.logoutQuietly(session);\r
+               super.dispose();\r
+       }\r
 \r
        /* DEPENDENCY INJECTION */\r
        public void setRepository(Repository repository) {\r
diff --git a/runtime/org.argeo.slc.akb/src/main/java/org/argeo/slc/akb/AkbService.java b/runtime/org.argeo.slc.akb/src/main/java/org/argeo/slc/akb/AkbService.java
new file mode 100644 (file)
index 0000000..7f5348a
--- /dev/null
@@ -0,0 +1,11 @@
+package org.argeo.slc.akb;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+
+/** Provides method interfaces to manage an AKB repository */
+public interface AkbService {
+
+       /** Creates a preconfigured AKB Template */
+       public Node createAkbTemplate(Node parent, String name) throws RepositoryException;
+}
index 02ccdc6f6f508a4203f3a3428c8fac0b4b518890..b228b0a75a073f6db754c2dd93fe221d66054603 100644 (file)
@@ -3,20 +3,26 @@ package org.argeo.slc.akb.core;
 import java.util.Map;
 
 import javax.annotation.Resource;
+import javax.jcr.Node;
+import javax.jcr.Property;
 import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
 import javax.jcr.Session;
+import javax.jcr.nodetype.NodeType;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.jcr.JcrUtils;
 import org.argeo.slc.akb.AkbException;
 import org.argeo.slc.akb.AkbNames;
+import org.argeo.slc.akb.AkbService;
+import org.argeo.slc.akb.AkbTypes;
 
 /**
  * Concrete access to akb services. It provides among other an initialized
  * environment
  */
-public class AkbServiceImpl implements AkbNames {
+public class AkbServiceImpl implements AkbService, AkbNames {
        private final static Log log = LogFactory.getLog(AkbServiceImpl.class);
 
        /* DEPENDENCY INJECTION */
@@ -55,19 +61,39 @@ public class AkbServiceImpl implements AkbNames {
                } finally {
                        JcrUtils.logoutQuietly(adminSession);
                }
-               // log.info("AKB service has been initialized.");
        }
 
        /** Clean shutdown of the backend. */
        public void destroy() {
-               // Do nothing
        }
 
-       /** Expose injected repository */
-       public Repository getRepository() {
-               return repository;
+       @Override
+       public Node createAkbTemplate(Node parentNode, String name)
+                       throws RepositoryException {
+               String connectorParentName = "Connectors";
+               String itemsParentName = "Items";
+
+               Node newTemplate = parentNode.addNode(name, AkbTypes.AKB_ENV_TEMPLATE);
+               newTemplate.setProperty(Property.JCR_TITLE, name);
+
+               Node connectorParent = newTemplate.addNode(connectorParentName,
+                               NodeType.NT_UNSTRUCTURED);
+               connectorParent.addMixin(NodeType.MIX_TITLE);
+               connectorParent.setProperty(Property.JCR_TITLE, connectorParentName);
+
+               Node itemsParent = newTemplate.addNode(itemsParentName,
+                               NodeType.NT_UNSTRUCTURED);
+               itemsParent.addMixin(NodeType.MIX_TITLE);
+               itemsParent.setProperty(Property.JCR_TITLE, itemsParentName);
+
+               return newTemplate;
        }
 
+       // /** Expose injected repository */
+       // public Repository getRepository() {
+       // return repository;
+       // }
+
        /* DEPENDENCY INJECTION */
        public void setRepository(Repository repository) {
                this.repository = repository;
diff --git a/runtime/org.argeo.slc.akb/src/main/java/org/argeo/slc/akb/utils/AkbJcrUtils.java b/runtime/org.argeo.slc.akb/src/main/java/org/argeo/slc/akb/utils/AkbJcrUtils.java
new file mode 100644 (file)
index 0000000..b0c8353
--- /dev/null
@@ -0,0 +1,310 @@
+package org.argeo.slc.akb.utils;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.PropertyIterator;
+import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.Value;
+import javax.jcr.query.Row;
+import javax.jcr.query.RowIterator;
+
+import org.argeo.jcr.JcrUtils;
+import org.argeo.jcr.PropertyDiff;
+import org.argeo.slc.akb.AkbException;
+
+/** Some static utils methods that might be factorized in a near future */
+public class AkbJcrUtils {
+
+       /**
+        * Call {@link Repository#login()} without exceptions (useful in super
+        * constructors and dependency injection).
+        */
+       public static Session login(Repository repository) {
+               try {
+                       return repository.login();
+               } catch (RepositoryException re) {
+                       throw new AkbException("Unable to login", re);
+               }
+       }
+
+       /**
+        * Convert a {@link rowIterator} to a list of {@link Node} given a selector
+        * name. It relies on the <code>Row.getNode(String selectorName)</code>
+        * method.
+        */
+       public static List<Node> rowIteratorToList(RowIterator rowIterator,
+                       String selectorName) throws RepositoryException {
+               List<Node> nodes = new ArrayList<Node>();
+               while (rowIterator.hasNext()) {
+                       Row row = rowIterator.nextRow();
+                       if (row.getNode(selectorName) != null)
+                               nodes.add(row.getNode(selectorName));
+               }
+               return nodes;
+       }
+
+       /**
+        * Check if a string is null or an empty string (a string with only spaces
+        * is considered as empty
+        */
+       public static boolean isEmptyString(String stringToTest) {
+               return stringToTest == null || "".equals(stringToTest.trim());
+       }
+
+       /**
+        * Check if a string is null or an empty string (a string with only spaces
+        * is considered as empty
+        */
+       public static boolean checkNotEmptyString(String string) {
+               return string != null && !"".equals(string.trim());
+       }
+
+       /**
+        * Wraps the versionMananger.isCheckedOut(path) method to adapt it to the
+        * current check in / check out policy.
+        * 
+        * TODO : add management of check out by others.
+        */
+       public static boolean isNodeCheckedOut(Node node) {
+               try {
+                       return node.getSession().getWorkspace().getVersionManager()
+                                       .isCheckedOut(node.getPath());
+               } catch (RepositoryException re) {
+                       throw new AkbException("Unable to get check out status for node",
+                                       re);
+               }
+       }
+
+       /**
+        * For the time being, same as isNodeCheckedOut(Node node).
+        * 
+        * TODO : add management of check out by others.
+        */
+       public static boolean isNodeCheckedOutByMe(Node node) {
+               return isNodeCheckedOut(node);
+       }
+
+       /**
+        * Wraps the versionMananger.checkedOut(path) method to adapt it to the
+        * current check in / check out policy.
+        * 
+        * TODO : add management of check out by others.
+        */
+       public static void checkout(Node node) {
+               try {
+                       node.getSession().getWorkspace().getVersionManager()
+                                       .checkout(node.getPath());
+               } catch (RepositoryException re) {
+                       throw new AkbException("Unable to check out Node", re);
+               }
+       }
+
+       /**
+        * Wraps the versionMananger.checkedIn(path) method to adapt it to the
+        * current check in / check out policy.
+        * 
+        * It also checked if the current entity has to be moved or not. TODO : add
+        * management of check out by others.
+        */
+       public static void saveAndCheckin(Node node) {
+               try {
+                       JcrUtils.updateLastModified(node);
+                       node.getSession().save();
+                       node.getSession().getWorkspace().getVersionManager()
+                                       .checkin(node.getPath());
+               } catch (RepositoryException re) {
+                       throw new AkbException("Unable to save and chek in node", re);
+               }
+       }
+
+       /**
+        * Wraps the versionMananger.checkedIn(path) method to adapt it to the
+        * current check in / check out policy.
+        * 
+        * TODO : add management of check out by others. TODO : manage usecase where
+        * a node that has never been checked in (draft node) is canceled and thus
+        * must be deleted
+        */
+       public static void cancelAndCheckin(Node node) {
+               try {
+                       String path = node.getPath();
+                       Session session = node.getSession();
+                       JcrUtils.discardUnderlyingSessionQuietly(node);
+                       // if the node has never been saved, it does not exist anymore.
+                       if (session.nodeExists(path))
+                               session.getWorkspace().getVersionManager().checkin(path);
+               } catch (RepositoryException re) {
+                       throw new AkbException("Unable to save and chek in node", re);
+               }
+       }
+
+       /**
+        * Concisely get the string value of a property. It returns an empty String
+        * rather than null if this node doesn't have this property or if the
+        * corresponding property is an empty string.
+        */
+       public static String get(Node node, String propertyName) {
+               try {
+                       if (!node.hasProperty(propertyName))
+                               return "";
+                       else
+                               return node.getProperty(propertyName).getString();
+               } catch (RepositoryException e) {
+                       throw new AkbException("Cannot get property " + propertyName
+                                       + " of " + node, e);
+               }
+       }
+
+       /**
+        * Concisely get the value of a property or null if this node doesn't have
+        * this property
+        */
+       public static Boolean getBooleanValue(Node node, String propertyName) {
+               try {
+                       if (!node.hasProperty(propertyName))
+                               return null;
+                       else
+                               return node.getProperty(propertyName).getBoolean();
+               } catch (RepositoryException e) {
+                       throw new AkbException("Cannot get boolean property "
+                                       + propertyName + " of " + node, e);
+               }
+       }
+
+       /**
+        * Concisely get the identifier of a node in Ui listener for instance
+        * */
+       public static String getIdentifierQuietly(Node node) {
+               try {
+                       return node.getIdentifier();
+               } catch (RepositoryException e) {
+                       throw new AkbException("Cannot get identifier for node " + node, e);
+               }
+       }
+
+       public static Map<String, PropertyDiff> diffProperties(Node reference,
+                       Node observed) {
+               Map<String, PropertyDiff> diffs = new TreeMap<String, PropertyDiff>();
+               diffPropertiesLevel(diffs, null, reference, observed);
+               return diffs;
+       }
+
+       /**
+        * Compare the properties of two nodes. Extends
+        * <code>JcrUtils.diffPropertiesLevel</code> to also track differences in
+        * multiple value properties and sub graph. No property is skipped (among
+        * other all technical jcr:... properties) to be able to track jcr:title and
+        * description properties, among other. Filtering must be applied afterwards
+        * to only keep relevant properties.
+        */
+       static void diffPropertiesLevel(Map<String, PropertyDiff> diffs,
+                       String baseRelPath, Node reference, Node observed) {
+               try {
+                       // check removed and modified
+                       PropertyIterator pit = reference.getProperties();
+                       while (pit.hasNext()) {
+                               Property p = pit.nextProperty();
+                               String name = p.getName();
+                               // if (name.startsWith("jcr:"))
+                               // continue props;
+
+                               if (!observed.hasProperty(name)) {
+                                       String relPath = propertyRelPath(baseRelPath, name);
+                                       PropertyDiff pDiff = new PropertyDiff(PropertyDiff.REMOVED,
+                                                       relPath, p.getValue(), null);
+                                       diffs.put(relPath, pDiff);
+                               } else {
+                                       if (p.isMultiple()) {
+                                               int i = 0;
+
+                                               Value[] refValues = p.getValues();
+                                               Value[] newValues = observed.getProperty(name)
+                                                               .getValues();
+                                               String relPath = propertyRelPath(baseRelPath, name);
+                                               refValues: for (Value refValue : refValues) {
+                                                       for (Value newValue : newValues) {
+                                                               if (refValue.equals(newValue))
+                                                                       continue refValues;
+                                                       }
+                                                       PropertyDiff pDiff = new PropertyDiff(
+                                                                       PropertyDiff.REMOVED, relPath, refValue,
+                                                                       null);
+                                                       diffs.put(relPath + "_" + i++, pDiff);
+                                               }
+
+                                               newValues: for (Value newValue : newValues) {
+                                                       for (Value refValue : refValues) {
+                                                               if (refValue.equals(newValue))
+                                                                       continue newValues;
+                                                       }
+                                                       PropertyDiff pDiff = new PropertyDiff(
+                                                                       PropertyDiff.ADDED, relPath, null, newValue);
+                                                       diffs.put(relPath + "_" + i++, pDiff);
+                                               }
+
+                                       } else {
+                                               Value referenceValue = p.getValue();
+                                               Value newValue = observed.getProperty(name).getValue();
+                                               if (!referenceValue.equals(newValue)) {
+                                                       String relPath = propertyRelPath(baseRelPath, name);
+                                                       PropertyDiff pDiff = new PropertyDiff(
+                                                                       PropertyDiff.MODIFIED, relPath,
+                                                                       referenceValue, newValue);
+                                                       diffs.put(relPath, pDiff);
+                                               }
+                                       }
+                               }
+                       }
+                       // check added
+                       pit = observed.getProperties();
+                       // props:
+                       while (pit.hasNext()) {
+                               Property p = pit.nextProperty();
+                               String name = p.getName();
+                               // if (name.startsWith("jcr:"))
+                               // continue props;
+                               if (!reference.hasProperty(name)) {
+                                       String relPath = propertyRelPath(baseRelPath, name);
+                                       if (p.isMultiple()) {
+                                               Value[] newValues = observed.getProperty(name)
+                                                               .getValues();
+                                               int i = 0;
+                                               for (Value newValue : newValues) {
+                                                       PropertyDiff pDiff = new PropertyDiff(
+                                                                       PropertyDiff.ADDED, relPath, null, newValue);
+                                                       diffs.put(relPath + "_" + i++, pDiff);
+                                               }
+                                       } else {
+                                               PropertyDiff pDiff = new PropertyDiff(
+                                                               PropertyDiff.ADDED, relPath, null, p.getValue());
+                                               diffs.put(relPath, pDiff);
+                                       }
+                               }
+                       }
+               } catch (RepositoryException e) {
+                       throw new AkbException("Cannot diff " + reference + " and "
+                                       + observed, e);
+               }
+       }
+
+       /** Builds a property relPath to be used in the diff. */
+       private static String propertyRelPath(String baseRelPath,
+                       String propertyName) {
+               if (baseRelPath == null)
+                       return propertyName;
+               else
+                       return baseRelPath + '/' + propertyName;
+       }
+
+       /** prevent instantiation by others */
+       private AkbJcrUtils() {
+       }
+
+}
\ No newline at end of file
index 09fed63a8d6fb26428b4a3510f5ff80b04ed2a0f..0e451b3432db01469cd8655aa6107b1f167a3551 100644 (file)
@@ -12,10 +12,10 @@ mixin
 // 
 
 // Abstract description of whole or part of an IT environment
-[akb:envTemplate] > nt:unstructured
+[akb:envTemplate] > nt:unstructured, mix:title
 
 // An environment template (1..1 relationship) applied to a real environment
-[akb:env] > nt:unstructured
+[akb:env] > nt:unstructured, mix:title
 
 
 //