<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>
<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>
<!-- 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
<Import-Package>
*,
org.argeo.eclipse.spring,
+ javax.jcr.nodetype
</Import-Package>
</instructions>
</configuration>
IFolderLayout left = layout.createFolder("left", IPageLayout.LEFT,
0.3f, editorArea);
- left.addView(AkbTemplatesTreeView.ID);
left.addView(AkbDefaultView.ID);
}
}
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;
IFolderLayout left = layout.createFolder("left", IPageLayout.LEFT,
0.3f, editorArea);
+ left.addView(AkbTemplatesTreeView.ID);
left.addView(AkbDefaultView.ID);
}
}
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;
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;
/* 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";
Session session = null;
try {
+ // caches current Page
+ IWorkbenchPage currentPage = HandlerUtil.getActiveWorkbenchWindow(
+ event).getActivePage();
+
session = repository.login();
Node node = null;
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",
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();
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))
public void setRepository(Repository repository) {
this.repository = repository;
}
+
+ public void setAkbService(AkbService akbService) {
+ this.akbService = akbService;
+ }
}
\ No newline at end of file
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
// 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 {
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 */
*/
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;
}
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() {
*/\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
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
// .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
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
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
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
}\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
--- /dev/null
+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;
+}
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 */
} 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;
--- /dev/null
+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
//
// 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
//