private void setPropertyAsIs()

in src/main/java/org/apache/sling/jackrabbit/usermanager/impl/post/AbstractAuthorizablePostServlet.java [602:740]


    private void setPropertyAsIs(Session session, Authorizable parent,
            RequestProperty prop, List<Modification> changes)
            throws RepositoryException {

        String parentPath;
        if (parent.isGroup()) {
            parentPath = systemUserManagerPaths.getGroupPrefix()
                + parent.getID();
        } else {
            parentPath = systemUserManagerPaths.getUserPrefix()
                + parent.getID();
        }

        // no explicit typehint
        int type = PropertyType.UNDEFINED;
        boolean multiValue = false;
        if (prop.getTypeHint() != null) {
            try {
                type = PropertyType.valueFromName(prop.getTypeHint());
                multiValue = prop.hasMultiValueTypeHint();
            } catch (Exception e) {
                // ignore
            }
        } else {
            // inspect the node type definitions to see if there is a known PropertyDefintion
            //  for the target property that we can get the required type from
            String propParentPath = String.format("%s%s", parent.getPath(), prop.getParentPath());
            if (session.nodeExists(propParentPath)) {
                // try to determine required property type from the NodeType definition
                Node parentNode = session.getNode(propParentPath);
                @Nullable
                PropertyDefinition propDef = resolvePropertyDefinition(prop.getName(), parentNode);
                if (propDef != null) {
                    type = propDef.getRequiredType();
                    multiValue = propDef.isMultiple();
                }
            }
        }
        // remove artificial "/" prepended to the prop path
        String relativePath = prop.getPath().substring(1);

        String[] values = prop.getStringValues();
        if (values == null) {
            // remove property
            boolean removedProp = removePropertyIfExists(parent, relativePath);
            if (removedProp) {
                changes.add(Modification.onDeleted(parentPath + "/"
                    + relativePath));
            }
        } else if (values.length == 0) {
            // do not create new prop here, but clear existing
            if (parent.hasProperty(relativePath)) {
                Value val = session.getValueFactory().createValue("");
                parent.setProperty(relativePath, val);
                changes.add(Modification.onModified(parentPath + "/"
                    + relativePath));
            }
        } else if (values.length == 1) {
            // if the provided value is the empty string, we don't have to do
            // anything.
            if (values[0].length() == 0) {
                boolean removedProp = removePropertyIfExists(parent, relativePath);
                if (removedProp) {
                    changes.add(Modification.onDeleted(parentPath + "/"
                        + relativePath));
                }
            } else {
                // modify property
                if (type == PropertyType.DATE) {
                    // try conversion
                    Calendar c = dateParser.parse(values[0]);
                    if (c != null) {
                        if (multiValue) {
                            final Value[] array = new Value[1];
                            array[0] = session.getValueFactory().createValue(c);
                            parent.setProperty(relativePath, array);
                            changes.add(Modification.onModified(parentPath
                                + "/" + relativePath));
                        } else {
                            Value cVal = session.getValueFactory().createValue(
                                c);
                            parent.setProperty(relativePath, cVal);
                            changes.add(Modification.onModified(parentPath
                                + "/" + relativePath));
                        }
                        return;
                    }
                    // fall back to default behaviour
                }
                if (type == PropertyType.UNDEFINED) {
                    Value val = session.getValueFactory().createValue(
                        values[0], PropertyType.STRING);
                    parent.setProperty(relativePath, val);
                } else {
                    if (multiValue) {
                        final Value[] array = new Value[1];
                        array[0] = session.getValueFactory().createValue(
                            values[0], type);
                        parent.setProperty(relativePath, array);
                    } else {
                        Value val = session.getValueFactory().createValue(
                            values[0], type);
                        parent.setProperty(relativePath, val);
                    }
                }
                changes.add(Modification.onModified(parentPath + "/"
                    + relativePath));
            }
        } else {
            if (type == PropertyType.DATE) {
                // try conversion
                ValueFactory valFac = session.getValueFactory();
                Value[] c = dateParser.parse(values, valFac);
                if (c != null) {
                    parent.setProperty(relativePath, c);
                    changes.add(Modification.onModified(parentPath + "/"
                        + relativePath));
                    return;
                }
                // fall back to default behaviour
            }

            Value[] vals = new Value[values.length];
            if (type == PropertyType.UNDEFINED) {
                for (int i = 0; i < values.length; i++) {
                    vals[i] = session.getValueFactory().createValue(values[i]);
                }
            } else {
                for (int i = 0; i < values.length; i++) {
                    vals[i] = session.getValueFactory().createValue(values[i],
                        type);
                }
            }
            parent.setProperty(relativePath, vals);
            changes.add(Modification.onModified(parentPath + "/"
                + relativePath));
        }

    }