Search in sources :

Example 31 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class DefaultCollectionService method validateUpdate.

private Property validateUpdate(IdentifiableObject object, String propertyName, String message) throws WebMessageException {
    Schema schema = schemaService.getDynamicSchema(HibernateProxyUtils.getRealClass(object));
    if (!aclService.canUpdate(currentUserService.getCurrentUser(), object)) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
    }
    if (!schema.haveProperty(propertyName)) {
        throw new WebMessageException(WebMessageUtils.notFound("Property " + propertyName + " does not exist on " + object.getClass().getName()));
    }
    Property property = schema.getProperty(propertyName);
    if (!property.isCollection() || !property.isIdentifiableObject()) {
        throw new WebMessageException(conflict(message));
    }
    return property;
}
Also used : UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Schema(org.hisp.dhis.schema.Schema) Property(org.hisp.dhis.schema.Property)

Example 32 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class DefaultCollectionService method delNonOwnedCollectionItems.

private void delNonOwnedCollectionItems(IdentifiableObject object, Property property, Collection<String> itemCodes, TypeReport report) {
    Schema owningSchema = schemaService.getDynamicSchema(property.getItemKlass());
    Property owningProperty = owningSchema.propertyByRole(property.getOwningRole());
    updateCollectionItems(property, itemCodes, report, ErrorCode.E1109, item -> {
        Collection<IdentifiableObject> collection = getCollection(item, owningProperty);
        if (collection.contains(object)) {
            validateAndThrowErrors(() -> schemaValidator.validateProperty(owningProperty, item));
            collection.remove(object);
            manager.update(item);
            report.getStats().incDeleted();
        } else {
            report.getStats().incIgnored();
        }
    });
}
Also used : Schema(org.hisp.dhis.schema.Schema) Property(org.hisp.dhis.schema.Property) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 33 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class DefaultCollectionService method replaceCollectionItems.

@Override
@Transactional
public TypeReport replaceCollectionItems(IdentifiableObject object, String propertyName, Collection<? extends IdentifiableObject> objects) throws Exception {
    Property property = validateUpdate(object, propertyName, "Only identifiable object collections can be replaced.");
    TypeReport deletions = delCollectionItems(object, propertyName, getCollection(object, property));
    TypeReport additions = addCollectionItems(object, propertyName, objects);
    return deletions.mergeAllowEmpty(additions);
}
Also used : TypeReport(org.hisp.dhis.feedback.TypeReport) Property(org.hisp.dhis.schema.Property) Transactional(org.springframework.transaction.annotation.Transactional)

Example 34 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class DefaultJobConfigurationService method getProperty.

private static Property getProperty(JobType jobType, Class<?> paramsType, Field field) {
    Class<?> valueType = field.getType();
    Property property = new Property(Primitives.wrap(valueType), null, null);
    property.setName(field.getName());
    property.setFieldName(TextUtils.getPrettyPropertyName(field.getName()));
    try {
        field.setAccessible(true);
        property.setDefaultValue(field.get(jobType.getJobParameters().newInstance()));
    } catch (IllegalAccessException | InstantiationException e) {
        log.error("Fetching default value for JobParameters properties failed for property: " + field.getName(), e);
    }
    String relativeApiElements = jobType.getRelativeApiElements() != null ? jobType.getRelativeApiElements().get(field.getName()) : "";
    if (relativeApiElements != null && !relativeApiElements.equals("")) {
        property.setRelativeApiEndpoint(relativeApiElements);
    }
    if (Collection.class.isAssignableFrom(valueType)) {
        return setPropertyIfCollection(property, field, paramsType);
    }
    if (valueType.isEnum()) {
        property.setConstants(getConstants(valueType));
    }
    return property;
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Property(org.hisp.dhis.schema.Property)

Example 35 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class QueryUtils method convertOrderStrings.

/**
 * Converts the specified string orders (e.g. <code>name:asc</code>) to
 * order objects.
 *
 * @param orders the order strings that should be converted.
 * @param schema the schema that should be used to perform the conversion.
 * @return the converted order.
 */
@Nonnull
public static List<Order> convertOrderStrings(@Nullable Collection<String> orders, @Nonnull Schema schema) {
    if (orders == null) {
        return Collections.emptyList();
    }
    final Map<String, Order> result = new LinkedHashMap<>();
    for (String o : orders) {
        String[] split = o.split(":");
        String direction = "asc";
        if (split.length < 1) {
            continue;
        } else if (split.length == 2) {
            direction = split[1].toLowerCase();
        }
        String propertyName = split[0];
        Property property = schema.getProperty(propertyName);
        if (result.containsKey(propertyName) || !schema.haveProperty(propertyName) || !validProperty(property) || !validDirection(direction)) {
            continue;
        }
        result.put(propertyName, Order.from(direction, property));
    }
    return new ArrayList<>(result.values());
}
Also used : ArrayList(java.util.ArrayList) Property(org.hisp.dhis.schema.Property) LinkedHashMap(java.util.LinkedHashMap) Nonnull(javax.annotation.Nonnull)

Aggregations

Property (org.hisp.dhis.schema.Property)126 Schema (org.hisp.dhis.schema.Schema)69 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)36 ArrayList (java.util.ArrayList)32 HashMap (java.util.HashMap)26 Collection (java.util.Collection)21 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)21 List (java.util.List)20 Map (java.util.Map)16 Test (org.junit.jupiter.api.Test)16 Attribute (org.hisp.dhis.attribute.Attribute)14 ReflectionUtils (org.hisp.dhis.system.util.ReflectionUtils)14 Collectors (java.util.stream.Collectors)13 User (org.hisp.dhis.user.User)13 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)12 EmbeddedObject (org.hisp.dhis.common.EmbeddedObject)12 SimpleNode (org.hisp.dhis.node.types.SimpleNode)12 Query (org.hisp.dhis.query.Query)12 SchemaService (org.hisp.dhis.schema.SchemaService)12 Transactional (org.springframework.transaction.annotation.Transactional)12