diff --git a/magnolia-core/src/main/java/info/magnolia/jcr/util/SessionUtil.java b/magnolia-core/src/main/java/info/magnolia/jcr/util/SessionUtil.java index dffc76f..2af5242 100644 --- a/magnolia-core/src/main/java/info/magnolia/jcr/util/SessionUtil.java +++ b/magnolia-core/src/main/java/info/magnolia/jcr/util/SessionUtil.java @@ -36,6 +36,7 @@ import info.magnolia.context.MgnlContext; import info.magnolia.jcr.wrapper.DelegateSessionWrapper; +import javax.jcr.ItemNotFoundException; import javax.jcr.Node; import javax.jcr.RepositoryException; import javax.jcr.Session; @@ -132,4 +133,97 @@ } return res; } + + /* + * Start: New by Christian for review + */ + public static Boolean nodeExists(String workspace, String path) { + if (StringUtils.isBlank(workspace)) { + log.debug("nodeExists returns null because workspace: '{}' is empty", workspace); + return null; + } + if (StringUtils.isBlank(path)) { + log.debug("nodeExists returns null because path: '{}' is empty", path); + return null; + } + + try { + Session session = MgnlContext.getJCRSession(workspace); + return session.nodeExists(path); + } catch (RepositoryException e) { + log.error("Exception: itemExists returns null trying to check node path '{}' in workspace: '{}'", path, workspace, e); + return null; + } + } + + + public static Boolean itemExists(String workspace, String path) { + if (StringUtils.isBlank(workspace)) { + log.debug("itemExists returns null because workspace: '{}' is empty", workspace); + return null; + } + if (StringUtils.isBlank(path)) { + log.debug("itemExists returns null because path: '{}' is empty", path); + return null; + } + + try { + Session session = MgnlContext.getJCRSession(workspace); + return session.nodeExists(path); + } catch (RepositoryException e) { + log.error("Exception: itemExists returns null trying to check item path '{}' in workspace: '{}'", path, workspace, e); + return null; + } + } + + public static Boolean propertyExists(String workspace, String path) { + if (StringUtils.isBlank(workspace)) { + log.debug("propertyExists returns null because workspace: '{}' is empty", workspace); + return null; + } + if (StringUtils.isBlank(path)) { + log.debug("propertyExists returns null because path: '{}' is empty", path); + return null; + } + + try { + Session session = MgnlContext.getJCRSession(workspace); + return session.nodeExists(path); + } catch (RepositoryException e) { + log.error("Exception: propertyExists returns null trying to check property path '{}' in workspace: '{}'", path, workspace, e); + return null; + } + } + + public static Boolean idExists(String id, String workspace) { + if (StringUtils.isBlank(workspace)) { + log.debug("idExists returns null because workspace: '{}' is empty", workspace); + return null; + } + if (StringUtils.isBlank(id)) { + log.debug("idExists returns null because id: '{}' is empty", id); + return null; + } + + try { + Session session = MgnlContext.getJCRSession(workspace); + if (session != null) { + session.getNodeByIdentifier(id); + return true; + } else { + log.error("idExists returns null because no session to workspace '{}'was returend.", workspace); + return null; + } + } catch (ItemNotFoundException e) { + log.error("Exception: idExists return false because item with id '{}' doesn not existing in workspace: '{}'", id, workspace, e); + return false; + + } catch (RepositoryException e) { + log.debug("Exception: idExists returns null while trying to resolve item id '{}' to a node within workspace: '{}'", id, workspace, e); + return null; + } + } + /* + * End: New by Christian for review + */ }