X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=server%2Fruntime%2Forg.argeo.server.jcr%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fjcr%2FJcrUtils.java;h=3a65de567d4316e888adacf8d47e5ae60bd0655e;hb=e687e711998015552b9caa1eeb36033f673e853b;hp=66c6a9388f1f6c1df93e79de91beb6e5a117e832;hpb=484dcb1507e4e35cc282e50522ea7eac7e99a7f9;p=lgpl%2Fargeo-commons.git diff --git a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java index 66c6a9388..3a65de567 100644 --- a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java +++ b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java @@ -51,6 +51,7 @@ import javax.jcr.nodetype.NodeType; import javax.jcr.observation.EventListener; import javax.jcr.query.Query; import javax.jcr.query.QueryResult; +import javax.jcr.version.VersionManager; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; @@ -59,6 +60,7 @@ import org.argeo.ArgeoException; /** Utility methods to simplify common JCR operations. */ public class JcrUtils implements ArgeoJcrConstants { + private final static Log log = LogFactory.getLog(JcrUtils.class); /** @@ -920,6 +922,23 @@ public class JcrUtils implements ArgeoJcrConstants { } } + /** + * Convenient method to add a listener. uuids passed as null, deep=true, + * local=true, only one node type + */ + public static void addListener(Session session, EventListener listener, + int eventTypes, String basePath, String nodeType) { + try { + session.getWorkspace() + .getObservationManager() + .addEventListener(listener, eventTypes, basePath, true, + null, new String[] { nodeType }, true); + } catch (RepositoryException e) { + throw new ArgeoException("Cannot add JCR listener " + listener + + " to session " + session, e); + } + } + /** Removes a listener without throwing exception */ public static void removeListenerQuietly(Session session, EventListener listener) { @@ -941,7 +960,7 @@ public class JcrUtils implements ArgeoJcrConstants { /** User home path is NOT configurable */ public static String getUserHomePath(String username) { - String homeBasePath = "/home"; + String homeBasePath = DEFAULT_HOME_BASE_PATH; return homeBasePath + '/' + firstCharsToPath(username, 2) + '/' + username; } @@ -1025,8 +1044,37 @@ public class JcrUtils implements ArgeoJcrConstants { return userProfile; } catch (RepositoryException e) { discardQuietly(session); - throw new ArgeoException("Cannot create home for " + username - + " in workspace " + session.getWorkspace().getName(), e); + throw new ArgeoException("Cannot create user profile for " + + username + " in workspace " + + session.getWorkspace().getName(), e); + } + } + + /** + * Create user profile if needed, the session IS saved. + * + * @return the user profile + */ + public static Node createUserProfileIfNeeded(Session securitySession, + String username) { + try { + Node userHome = JcrUtils.createUserHomeIfNeeded(securitySession, + username); + Node userProfile = userHome.hasNode(ArgeoNames.ARGEO_PROFILE) ? userHome + .getNode(ArgeoNames.ARGEO_PROFILE) : JcrUtils + .createUserProfile(securitySession, username); + if (securitySession.hasPendingChanges()) + securitySession.save(); + VersionManager versionManager = securitySession.getWorkspace() + .getVersionManager(); + if (versionManager.isCheckedOut(userProfile.getPath())) + versionManager.checkin(userProfile.getPath()); + return userProfile; + } catch (RepositoryException e) { + discardQuietly(securitySession); + throw new ArgeoException("Cannot create user profile for " + + username + " in workspace " + + securitySession.getWorkspace().getName(), e); } } @@ -1109,11 +1157,7 @@ public class JcrUtils implements ArgeoJcrConstants { /** * @return null if not found * - * @deprecated will soon be removed. Call instead - * getUserHome().getNode(ARGEO_PROFILE) on the security - * workspace. */ - @Deprecated public static Node getUserProfile(Session session, String username) { try { Node userHome = getUserHome(session, username); @@ -1131,12 +1175,7 @@ public class JcrUtils implements ArgeoJcrConstants { /** * Get the profile of the user attached to this session. - * - * @deprecated will soon be removed. Call instead - * getUserHome().getNode(ARGEO_PROFILE) on the security - * workspace. */ - @Deprecated public static Node getUserProfile(Session session) { String userID = session.getUserID(); return getUserProfile(session, userID);