]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java
Add a command to get node property type as cnd like string
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jcr / src / main / java / org / argeo / jcr / JcrUtils.java
index 853555d98226d9c8dc47c5d18149558d62a86875..f62196cafe3663de56e1e2ce544832c02ad40fce 100644 (file)
@@ -164,19 +164,24 @@ public class JcrUtils implements ArgeoJcrConstants {
         */
        public static String dateAsPath(Calendar cal, Boolean addHour) {
                StringBuffer buf = new StringBuffer(14);
-               buf.append('Y').append(cal.get(Calendar.YEAR));
+               buf.append('Y');
+               buf.append(cal.get(Calendar.YEAR));
                buf.append('/');
+
                int month = cal.get(Calendar.MONTH) + 1;
                buf.append('M');
                if (month < 10)
                        buf.append(0);
                buf.append(month);
                buf.append('/');
+
                int day = cal.get(Calendar.DAY_OF_MONTH);
+               buf.append('D');
                if (day < 10)
                        buf.append(0);
-               buf.append('D').append(day);
+               buf.append(day);
                buf.append('/');
+
                if (addHour) {
                        int hour = cal.get(Calendar.HOUR_OF_DAY);
                        buf.append('H');
@@ -741,8 +746,13 @@ public class JcrUtils implements ArgeoJcrConstants {
 
        /** Logs out the session, not throwing any exception, even if it is null. */
        public static void logoutQuietly(Session session) {
-               if (session != null)
-                       session.logout();
+               try {
+                       if (session != null)
+                               if (session.isLive())
+                                       session.logout();
+               } catch (Exception e) {
+                       // silent
+               }
        }
 
        /** Returns the home node of the session user or null if none was found. */
@@ -837,15 +847,35 @@ public class JcrUtils implements ArgeoJcrConstants {
                        if (session.hasPendingChanges())
                                throw new ArgeoException(
                                                "Session has pending changes, save them first");
+
                        String homePath = homeBasePath + '/'
                                        + firstCharsToPath(username, 2) + '/' + username;
-                       Node userHome = JcrUtils.mkdirs(session, homePath);
 
-                       Node userProfile = userHome.addNode(ArgeoNames.ARGEO_PROFILE);
-                       userProfile.addMixin(ArgeoTypes.ARGEO_USER_PROFILE);
-                       userProfile.setProperty(ArgeoNames.ARGEO_USER_ID, username);
-                       session.save();
-                       // we need to save the profile before adding the user home type
+                       if (session.itemExists(homePath)) {
+                               try {
+                                       throw new ArgeoException(
+                                                       "Trying to create a user home that already exists");
+                               } catch (Exception e) {
+                                       // we use this workaround to be sure to get the stack trace
+                                       // to identify the sink of the bug.
+                                       log.warn("trying to create an already existing userHome at path:"
+                                                       + homePath + ". Stack trace : ");
+                                       e.printStackTrace();
+                               }
+                       }
+
+                       Node userHome = JcrUtils.mkdirs(session, homePath);
+                       Node userProfile;
+                       if (userHome.hasNode(ArgeoNames.ARGEO_PROFILE)) {
+                               log.warn("userProfile node already exists for userHome path: "
+                                               + homePath + ". We do not add a new one");
+                       } else {
+                               userProfile = userHome.addNode(ArgeoNames.ARGEO_PROFILE);
+                               userProfile.addMixin(ArgeoTypes.ARGEO_USER_PROFILE);
+                               userProfile.setProperty(ArgeoNames.ARGEO_USER_ID, username);
+                               session.save();
+                               // we need to save the profile before adding the user home type
+                       }
                        userHome.addMixin(ArgeoTypes.ARGEO_USER_HOME);
                        // see
                        // http://jackrabbit.510166.n4.nabble.com/Jackrabbit-2-0-beta-6-Problem-adding-a-Mixin-type-with-mandatory-properties-after-setting-propertiesn-td1290332.html
@@ -911,4 +941,31 @@ public class JcrUtils implements ArgeoJcrConstants {
                        throw new ArgeoException("Cannot update last modified", e);
                }
        }
+
+       /**
+        * Returns a String representing the short version (see <a
+        * href="http://jackrabbit.apache.org/node-type-notation.html"> Node type
+        * Notation </a> attributes grammar) of the main business attributes of this
+        * property definition
+        * 
+        * @param prop
+        */
+       public static String getPropertyDefinitionAsString(Property prop) {
+               StringBuffer sbuf = new StringBuffer();
+               try {
+                       if (prop.getDefinition().isAutoCreated())
+                               sbuf.append("a");
+                       if (prop.getDefinition().isMandatory())
+                               sbuf.append("m");
+                       if (prop.getDefinition().isProtected())
+                               sbuf.append("p");
+                       if (prop.getDefinition().isMultiple())
+                               sbuf.append("*");
+               } catch (RepositoryException re) {
+                       throw new ArgeoException(
+                                       "unexpected error while getting property definition as String",
+                                       re);
+               }
+               return sbuf.toString();
+       }
 }