TreeUtil { public static PropertyState autoCreateProperty(@NotNull String name, @NotNull Tree definition, @Nullable String userID) { if (JCR_UUID.equals(name)) { String uuid = UUIDUtils.generateUUID(); return PropertyStates.createProperty(name, uuid, STRING); } else if (JCR_CREATED.equals(name)) { String now = ISO8601.format(Calendar.getInstance()); return PropertyStates.createProperty(name, now, DATE); } else if (JCR_CREATEDBY.equals(name)) { return PropertyStates.createProperty(name, Strings.nullToEmpty(userID), STRING); } else if (JCR_LASTMODIFIED.equals(name)) { String now = ISO8601.format(Calendar.getInstance()); return PropertyStates.createProperty(name, now, DATE); } else if (JCR_LASTMODIFIEDBY.equals(name)) { return PropertyStates.createProperty(name, Strings.nullToEmpty(userID), STRING); } PropertyState values = definition.getProperty(JCR_DEFAULTVALUES); if (values != null) { Type<?> type = values.getType(); if (getBoolean(definition, JCR_MULTIPLE)) { return PropertyStates.createProperty( name, values.getValue(type), type); } else if (values.count() > 0) { type = type.getBaseType(); return PropertyStates.createProperty( name, values.getValue(type, 0), type); } } return null; } private  TreeUtil(); @Nullable static String getPrimaryTypeName(@NotNull Tree tree); @Nullable static String getPrimaryTypeName(@NotNull Tree tree, @NotNull LazyValue<Tree> readOnlyTree); @NotNull static Iterable<String> getMixinTypeNames(@NotNull Tree tree); @NotNull static Iterable<String> getMixinTypeNames(@NotNull Tree tree, @NotNull LazyValue<Tree> readOnlyTree); @Nullable static Iterable<String> getStrings(@NotNull Tree tree, @NotNull String propertyName); @Nullable static String getString(@NotNull Tree tree, @NotNull String propertyName); @Nullable static String getString(@NotNull Tree tree, @NotNull String name, @Nullable String defaultValue); static boolean getBoolean(@NotNull Tree tree, @NotNull String propertyName); @Nullable static String getName(@NotNull Tree tree, @NotNull String name); @NotNull static Iterable<String> getNames(@NotNull Tree tree, @NotNull String name); static long getLong(@NotNull Tree tree, @NotNull String name, long defaultValue); @Nullable static Tree getTree(@NotNull Tree tree, @NotNull String path); static Tree addChild(
            @NotNull Tree parent, @NotNull String name,
            @Nullable String typeName, @NotNull Tree typeRoot,
            @Nullable String userID); @NotNull static Tree addChild(@NotNull Tree tree, @NotNull String childName, @NotNull String primaryTypeName); @NotNull static Tree getOrAddChild(@NotNull Tree tree, @NotNull String childName, @NotNull String primaryTypeName); static void addMixin(@NotNull Tree tree, @NotNull String mixinName, @NotNull Tree typeRoot, @Nullable String userID); static void autoCreateItems(@NotNull Tree tree, @NotNull Tree type, @NotNull Tree typeRoot, @Nullable String userID); static PropertyState autoCreateProperty(@NotNull String name,
                                                   @NotNull Tree definition,
                                                   @Nullable String userID); static String getDefaultChildType(
            Tree typeRoot, Tree parent, String childName); static List<Tree> getEffectiveType(Tree tree, Tree typeRoot); static String findDefaultPrimaryType(Tree definitions, boolean sns); static boolean isNodeType(Tree tree, String typeName, Tree typeRoot); static boolean isReadOnlyTree(@NotNull Tree tree);  }
@Test public void testAutoCreatePropertyBuiltIn() { assertNull(TreeUtil.autoCreateProperty("anyName", propDef, "userId")); String uuid = TreeUtil.autoCreateProperty(JCR_UUID, propDef, null).getValue(Type.STRING); assertTrue(UUIDUtils.isValidUUID(uuid)); assertEquals("userId", TreeUtil.autoCreateProperty(NodeTypeConstants.JCR_CREATEDBY, propDef, "userId").getValue(Type.STRING)); assertTrue(TreeUtil.autoCreateProperty(NodeTypeConstants.JCR_CREATEDBY, propDef, null).getValue(Type.STRING).isEmpty()); assertEquals("userId", TreeUtil.autoCreateProperty(NodeTypeConstants.JCR_LASTMODIFIEDBY, propDef, "userId").getValue(Type.STRING)); assertTrue(TreeUtil.autoCreateProperty(NodeTypeConstants.JCR_LASTMODIFIEDBY, propDef, null).getValue(Type.STRING).isEmpty()); assertEquals(Type.DATE, TreeUtil.autoCreateProperty(JcrConstants.JCR_CREATED, propDef, null).getType()); assertEquals(Type.DATE, TreeUtil.autoCreateProperty(JcrConstants.JCR_LASTMODIFIED, propDef, null).getType()); }
@Test public void testAutoCreatePropertyFromEmptyDefaultValues() { PropertyState defaultSingleValue = PropertyStates.createProperty(JCR_DEFAULTVALUES, ImmutableList.of(), Type.DATES); when(propDef.getProperty(JCR_DEFAULTVALUES)).thenReturn(defaultSingleValue); when(propDef.getProperty(JCR_MULTIPLE)).thenReturn(PropertyStates.createProperty(JCR_MULTIPLE, false)); PropertyState autocreated = TreeUtil.autoCreateProperty("anyName", propDef, "userId"); assertNull(autocreated); }