Package org.apache.syncope.core.persistence.beans

Examples of org.apache.syncope.core.persistence.beans.AbstractNormalSchema


        List<AbstractAttrValue> values = getIntValues(
                resource, mapItem, attributables, vAttrsToBeRemoved, vAttrsToBeUpdated, membVAttrsToBeRemoved,
                membVAttrsToBeUpdated);

        AbstractNormalSchema schema = null;
        boolean readOnlyVirSchema = false;
        AttributeSchemaType schemaType;
        final Map.Entry<String, Attribute> result;

        switch (mapItem.getIntMappingType()) {
            case UserSchema:
            case RoleSchema:
            case MembershipSchema:
                final SchemaDAO schemaDAO = context.getBean(SchemaDAO.class);
                schema = schemaDAO.find(mapItem.getIntAttrName(),
                        MappingUtil.getIntMappingTypeClass(mapItem.getIntMappingType()));
                schemaType = schema == null ? AttributeSchemaType.String : schema.getType();
                break;

            case UserVirtualSchema:
            case RoleVirtualSchema:
            case MembershipVirtualSchema:
                VirSchemaDAO virSchemaDAO = context.getBean(VirSchemaDAO.class);
                AbstractVirSchema virSchema = virSchemaDAO.find(mapItem.getIntAttrName(),
                        MappingUtil.getIntMappingTypeClass(mapItem.getIntMappingType()));
                readOnlyVirSchema = (virSchema != null && virSchema.isReadonly());
                schemaType = AttributeSchemaType.String;
                break;

            default:
                schemaType = AttributeSchemaType.String;
        }

        final String extAttrName = mapItem.getExtAttrName();

        LOG.debug("Define mapping for: "
                + "\n* ExtAttrName " + extAttrName
                + "\n* is accountId " + mapItem.isAccountid()
                + "\n* is password " + (mapItem.isPassword() || mapItem.getIntMappingType() == IntMappingType.Password)
                + "\n* mandatory condition " + mapItem.getMandatoryCondition()
                + "\n* Schema " + mapItem.getIntAttrName()
                + "\n* IntMappingType " + mapItem.getIntMappingType().toString()
                + "\n* ClassType " + schemaType.getType().getName()
                + "\n* Values " + values);

        if (readOnlyVirSchema) {
            result = null;
        } else {
            final List<Object> objValues = new ArrayList<Object>();

            for (AbstractAttrValue value : values) {
                if (FrameworkUtil.isSupportedAttributeType(schemaType.getType())) {
                    objValues.add(value.getValue());
                } else {
                    objValues.add(value.getValueAsString());
                }
            }

            if (mapItem.isAccountid()) {
                result = new AbstractMap.SimpleEntry<String, Attribute>(objValues.iterator().next().toString(), null);
            } else if (mapItem.isPassword() && subject instanceof SyncopeUser) {
                String passwordAttrValue = password;
                if (StringUtils.isBlank(passwordAttrValue)) {
                    SyncopeUser user = (SyncopeUser) subject;
                    if (user.canDecodePassword()) {
                        try {
                            passwordAttrValue = ENCRYPTOR.decode(user.getPassword(), user.getCipherAlgorithm());
                        } catch (Exception e) {
                            LOG.error("Could not decode password for {}", user, e);
                        }
                    } else if (resource.isRandomPwdIfNotProvided()) {
                        try {
                            passwordAttrValue = passwordGenerator.generate(user);
                        } catch (InvalidPasswordPolicySpecException e) {
                            LOG.error("Could not generate policy-compliant random password for {}", user, e);
                        }
                    }
                }

                if (passwordAttrValue == null) {
                    result = null;
                } else {
                    result = new AbstractMap.SimpleEntry<String, Attribute>(
                            null,
                            AttributeBuilder.buildPassword(passwordAttrValue.toCharArray()));
                }
            } else {
                if ((schema != null && schema.isMultivalue()) || AttributableUtil.getInstance(subject).getType()
                        != mapItem.getIntMappingType().getAttributableType()) {
                    result = new AbstractMap.SimpleEntry<String, Attribute>(
                            null,
                            AttributeBuilder.build(extAttrName, objValues));
                } else {
View Full Code Here


                        getTemplate(EntityViolationType.MoreThanOneNonNull, "More than one non-null value found")).
                        addPropertyNode(object.getClass().getSimpleName().replaceAll("\\n", " ")).
                        addConstraintViolation();

            } else if (object instanceof AbstractAttrUniqueValue) {
                AbstractNormalSchema uniqueValueSchema = ((AbstractAttrUniqueValue) object).getSchema();
                AbstractNormalSchema attrSchema = object.getAttribute().getSchema();

                isValid = uniqueValueSchema.equals(attrSchema);

                if (!isValid) {
                    LOG.error("Unique value schema for " + object.getClass().getSimpleName() + "[" + object.getId()
                            + "]" + " is " + uniqueValueSchema + ", while owning attribute schema is " + attrSchema);

                    context.disableDefaultConstraintViolation();
                    context.buildConstraintViolationWithTemplate(
                            getTemplate(EntityViolationType.valueOf("Invalid" + attrSchema.getClass().getSimpleName()),
                                    "Unique value schema is " + uniqueValueSchema
                                    + ", while owning attribute schema is " + attrSchema)).addPropertyNode("schema").
                            addConstraintViolation();
                }
            }
View Full Code Here

        switch (accountIdItem.getIntMappingType()) {
            case UserSchema:
            case RoleSchema:
                final AbstractAttrValue value = attrUtil.newAttrValue();

                AbstractNormalSchema schema = schemaDAO.find(accountIdItem.getIntAttrName(), attrUtil.schemaClass());
                if (schema == null) {
                    value.setStringValue(uid);
                } else {
                    try {
                        value.parseValue(schema, uid);
View Full Code Here

                created = (T) binder.getDerSchemaTO(derSchema);
                break;

            case NORMAL:
            default:
                AbstractNormalSchema normalSchema = attrUtil.newSchema();
                binder.create((SchemaTO) schemaTO, normalSchema);
                normalSchema = schemaDAO.save(normalSchema);

                created = (T) binder.getSchemaTO(normalSchema, attrUtil);
        }
View Full Code Here

                read = (T) binder.getDerSchemaTO(derSchema);
                break;

            case NORMAL:
            default:
                AbstractNormalSchema schema = schemaDAO.find(schemaName, attrUtil.schemaClass());
                if (schema == null) {
                    throw new NotFoundException("Schema '" + schemaName + "'");
                }

                read = (T) binder.getSchemaTO(schema, attrUtil);
View Full Code Here

                derSchemaDAO.save(derSchema);
                break;

            case NORMAL:
            default:
                AbstractNormalSchema schema = schemaDAO.find(schemaTO.getName(), attrUtil.schemaClass());
                if (schema == null) {
                    throw new NotFoundException("Schema '" + schemaTO.getName() + "'");
                }

                binder.update((SchemaTO) schemaTO, schema, attrUtil);
View Full Code Here

            LOG.debug("Resources to be added:\n{}", propByRes);
        }

        // 3. attributes to be removed
        for (String attributeToBeRemoved : attributableMod.getAttrsToRemove()) {
            AbstractNormalSchema schema = getNormalSchema(attributeToBeRemoved, attrUtil.schemaClass());
            if (schema != null) {
                AbstractAttr attr = attributable.getAttr(schema.getName());
                if (attr == null) {
                    LOG.debug("No attribute found for schema {}", schema);
                } else {
                    String newValue = null;
                    for (AttributeMod mod : attributableMod.getAttrsToUpdate()) {
                        if (schema.getName().equals(mod.getSchema())) {
                            newValue = mod.getValuesToBeAdded().get(0);
                        }
                    }

                    if (!schema.isUniqueConstraint()
                            || (!attr.getUniqueValue().getStringValue().equals(newValue))) {

                        attributable.removeAttr(attr);
                        attrDAO.delete(attr.getId(), attrUtil.attrClass());
                    }
                }

                if (attributable instanceof AbstractSubject) {
                    for (ExternalResource resource : resourceDAO.findAll()) {
                        for (AbstractMappingItem mapItem : attrUtil.
                                getMappingItems(resource, MappingPurpose.PROPAGATION)) {
                            if (schema.getName().equals(mapItem.getIntAttrName())
                                    && mapItem.getIntMappingType() == attrUtil.intMappingType()
                                    && ((AbstractSubject) attributable).getResources().contains(resource)) {

                                propByRes.add(ResourceOperation.UPDATE, resource.getName());

                                if (mapItem.isAccountid() && attr != null
                                        && !attr.getValuesAsStrings().isEmpty()) {

                                    propByRes.addOldAccountId(resource.getName(),
                                            attr.getValuesAsStrings().iterator().next());
                                }
                            }
                        }
                    }
                }
            }
        }

        LOG.debug("Attributes to be removed:\n{}", propByRes);

        // 4. attributes to be updated
        for (AttributeMod attributeMod : attributableMod.getAttrsToUpdate()) {
            AbstractNormalSchema schema = getNormalSchema(attributeMod.getSchema(), attrUtil.schemaClass());
            AbstractAttr attr = null;
            if (schema != null) {
                attr = attributable.getAttr(schema.getName());
                if (attr == null) {
                    attr = attrUtil.newAttr();
                    setAttrSchema(attributable, attr, schema);
                    if (attr.getSchema() == null) {
                        LOG.debug("Ignoring {} because no valid schema or template was found", attributeMod);
                    } else {
                        attr.setOwner(attributable);
                        attributable.addAttr(attr);
                    }
                }
            }

            if (schema != null && attr != null && attr.getSchema() != null) {
                if (attributable instanceof AbstractSubject) {
                    for (ExternalResource resource : resourceDAO.findAll()) {
                        for (AbstractMappingItem mapItem : attrUtil.
                                getMappingItems(resource, MappingPurpose.PROPAGATION)) {
                            if (schema.getName().equals(mapItem.getIntAttrName())
                                    && mapItem.getIntMappingType() == attrUtil.intMappingType()
                                    && ((AbstractSubject) attributable).getResources().contains(resource)) {

                                propByRes.add(ResourceOperation.UPDATE, resource.getName());
                            }
                        }
                    }
                }

                // 1.1 remove values
                Set<Long> valuesToBeRemoved = new HashSet<Long>();
                for (String valueToBeRemoved : attributeMod.getValuesToBeRemoved()) {
                    if (attr.getSchema().isUniqueConstraint()) {
                        if (attr.getUniqueValue() != null
                                && valueToBeRemoved.equals(attr.getUniqueValue().getValueAsString())) {

                            valuesToBeRemoved.add(attr.getUniqueValue().getId());
                        }
                    } else {
                        for (AbstractAttrValue mav : attr.getValues()) {
                            if (valueToBeRemoved.equals(mav.getValueAsString())) {
                                valuesToBeRemoved.add(mav.getId());
                            }
                        }
                    }
                }
                for (Long attributeValueId : valuesToBeRemoved) {
                    attributeValueDAO.delete(attributeValueId, attrUtil.attrValueClass());
                }

                // 1.2 add values
                List<String> valuesToBeAdded = attributeMod.getValuesToBeAdded();
                if (valuesToBeAdded != null && !valuesToBeAdded.isEmpty()
                        && (!schema.isUniqueConstraint() || attr.getUniqueValue() == null
                        || !valuesToBeAdded.iterator().next().equals(attr.getUniqueValue().getValueAsString()))) {

                    fillAttribute(attributeMod.getValuesToBeAdded(), attrUtil, schema, attr, invalidValues);
                }
View Full Code Here

        SyncopeClientException invalidValues = SyncopeClientException.build(ClientExceptionType.InvalidValues);

        // Only consider attributeTO with values
        for (AttributeTO attributeTO : attributableTO.getAttrs()) {
            if (attributeTO.getValues() != null && !attributeTO.getValues().isEmpty()) {
                AbstractNormalSchema schema = getNormalSchema(attributeTO.getSchema(), attributableUtil.schemaClass());

                if (schema != null) {
                    AbstractAttr attr = attributable.getAttr(schema.getName());
                    if (attr == null) {
                        attr = attributableUtil.newAttr();
                        setAttrSchema(attributable, attr, schema);
                    }
                    if (attr.getSchema() == null) {
View Full Code Here

        for (OrderByClause clause : orderByClauses) {
            OrderBySupport.Item obs = new OrderBySupport.Item();

            Field subjectField = ReflectionUtils.findField(attrUtil.attributableClass(), clause.getField());
            if (subjectField == null) {
                AbstractNormalSchema schema = schemaDAO.find(clause.getField(), attrUtil.schemaClass());
                if (schema != null) {
                    if (schema.isUniqueConstraint()) {
                        orderBySupport.views.add(svs.uniqueAttr());

                        obs.select = new StringBuilder().
                                append(svs.uniqueAttr().alias).append('.').append(svs.fieldName(schema.getType())).
                                append(" AS ").append(clause.getField()).toString();
                        obs.where = new StringBuilder().
                                append(svs.uniqueAttr().alias).
                                append(".schema_name='").append(clause.getField()).append("'").toString();
                        obs.orderBy = clause.getField() + " " + clause.getDirection().name();
                    } else {
                        orderBySupport.views.add(svs.attr());

                        obs.select = new StringBuilder().
                                append(svs.attr().alias).append('.').append(svs.fieldName(schema.getType())).
                                append(" AS ").append(clause.getField()).toString();
                        obs.where = new StringBuilder().
                                append(svs.attr().alias).
                                append(".schema_name='").append(clause.getField()).append("'").toString();
                        obs.orderBy = clause.getField() + " " + clause.getDirection().name();
View Full Code Here

    private String getQuery(final AttributeCond cond, final boolean not, final List<Object> parameters,
            final SubjectType type, final SearchSupport svs) {

        final AttributableUtil attrUtil = AttributableUtil.getInstance(type.asAttributableType());

        AbstractNormalSchema schema = schemaDAO.find(cond.getSchema(), attrUtil.schemaClass());
        if (schema == null) {
            LOG.warn("Ignoring invalid schema '{}'", cond.getSchema());
            return EMPTY_ATTR_QUERY;
        }

        AbstractAttrValue attrValue = attrUtil.newAttrValue();
        try {
            if (cond.getType() != AttributeCond.Type.LIKE && cond.getType() != AttributeCond.Type.ISNULL
                    && cond.getType() != AttributeCond.Type.ISNOTNULL) {

                schema.getValidator().validate(cond.getExpression(), attrValue);
            }
        } catch (ValidationException e) {
            LOG.error("Could not validate expression '" + cond.getExpression() + "'", e);
            return EMPTY_ATTR_QUERY;
        }

        StringBuilder query = new StringBuilder("SELECT DISTINCT subject_id FROM ");
        if (cond.getType() == AttributeCond.Type.ISNOTNULL) {
            query.append(svs.field().name).
                    append(" WHERE subject_id NOT IN (SELECT subject_id FROM ").
                    append(svs.nullAttr().name).
                    append(" WHERE schema_name='").append(schema.getName()).append("')");
        } else {
            if (cond.getType() == AttributeCond.Type.ISNULL) {
                query.append(svs.nullAttr().name).
                        append(" WHERE schema_name='").append(schema.getName()).append("'");
            } else {
                if (schema.isUniqueConstraint()) {
                    query.append(svs.uniqueAttr().name);
                } else {
                    query.append(svs.attr().name);
                }
                query.append(" WHERE schema_name='").append(schema.getName());

                fillAttributeQuery(query, attrValue, schema, cond, not, parameters, svs);
            }
        }
View Full Code Here

TOP

Related Classes of org.apache.syncope.core.persistence.beans.AbstractNormalSchema

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.