Search in sources :

Example 76 with Property

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

the class TestUtils method getFieldDescriptors.

public static Set<FieldDescriptor> getFieldDescriptors(Schema schema) {
    Set<FieldDescriptor> fieldDescriptors = new HashSet<>();
    Map<String, Property> persistedPropertyMap = schema.getPersistedProperties();
    Iterator<?> persistedItr = persistedPropertyMap.keySet().iterator();
    while (persistedItr.hasNext()) {
        Property p = persistedPropertyMap.get(persistedItr.next());
        String pName = p.isCollection() ? p.getCollectionName() : p.getName();
        FieldDescriptor f = fieldWithPath(pName).description(TestUtils.getFieldDescription(p));
        if (!p.isRequired()) {
            f.optional().type(p.getPropertyType());
        }
        fieldDescriptors.add(f);
    }
    Map<String, Property> nonPersistedPropertyMap = schema.getNonPersistedProperties();
    Iterator<?> nonPersistedItr = nonPersistedPropertyMap.keySet().iterator();
    while (nonPersistedItr.hasNext()) {
        Property p = nonPersistedPropertyMap.get(nonPersistedItr.next());
        String pName = p.isCollection() ? p.getCollectionName() : p.getName();
        FieldDescriptor f = fieldWithPath(pName).description(TestUtils.getFieldDescription(p));
        if (!p.isRequired()) {
            f.optional().type(p.getPropertyType());
        }
        fieldDescriptors.add(f);
    }
    return fieldDescriptors;
}
Also used : Property(org.hisp.dhis.schema.Property) HashSet(java.util.HashSet) FieldDescriptor(org.springframework.restdocs.payload.FieldDescriptor)

Example 77 with Property

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

the class OrderParams method getOrders.

public List<Order> getOrders(Schema schema) {
    Map<String, Order> orders = new LinkedHashMap<>();
    for (String o : order) {
        String[] split = o.split(":");
        //Using ascending as default direction. 
        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 (orders.containsKey(propertyName) || !schema.haveProperty(propertyName) || !validProperty(property) || !validDirection(direction)) {
            continue;
        }
        orders.put(propertyName, Order.from(direction, property));
    }
    return new ArrayList<>(orders.values());
}
Also used : Order(org.hisp.dhis.query.Order) ArrayList(java.util.ArrayList) Property(org.hisp.dhis.schema.Property) LinkedHashMap(java.util.LinkedHashMap)

Example 78 with Property

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

the class DefaultCollectionService method clearCollectionItems.

@Override
@SuppressWarnings("unchecked")
public void clearCollectionItems(IdentifiableObject object, String pvProperty) throws WebMessageException, InvocationTargetException, IllegalAccessException {
    Schema schema = schemaService.getDynamicSchema(object.getClass());
    if (!schema.haveProperty(pvProperty)) {
        throw new WebMessageException(WebMessageUtils.notFound("Property " + pvProperty + " does not exist on " + object.getClass().getName()));
    }
    Property property = schema.getProperty(pvProperty);
    if (!property.isCollection() || !property.isIdentifiableObject()) {
        throw new WebMessageException(WebMessageUtils.conflict("Only identifiable collections are allowed to be cleared."));
    }
    Collection<IdentifiableObject> collection = (Collection<IdentifiableObject>) property.getGetterMethod().invoke(object);
    manager.refresh(object);
    if (property.isOwner()) {
        collection.clear();
        manager.update(object);
    } else {
        for (IdentifiableObject itemObject : collection) {
            Schema itemSchema = schemaService.getDynamicSchema(property.getItemKlass());
            Property itemProperty = itemSchema.propertyByRole(property.getOwningRole());
            Collection<IdentifiableObject> itemCollection = (Collection<IdentifiableObject>) itemProperty.getGetterMethod().invoke(itemObject);
            itemCollection.remove(object);
            manager.update(itemObject);
            manager.refresh(itemObject);
        }
    }
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Schema(org.hisp.dhis.schema.Schema) Collection(java.util.Collection) Property(org.hisp.dhis.schema.Property) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 79 with Property

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

the class DefaultCollectionService method addCollectionItems.

@Override
@SuppressWarnings("unchecked")
public void addCollectionItems(IdentifiableObject object, String propertyName, List<IdentifiableObject> objects) throws Exception {
    Schema schema = schemaService.getDynamicSchema(object.getClass());
    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(WebMessageUtils.conflict("Only identifiable object collections can be added to."));
    }
    Collection<String> itemCodes = objects.stream().map(IdentifiableObject::getUid).collect(Collectors.toList());
    if (itemCodes.isEmpty()) {
        return;
    }
    List<? extends IdentifiableObject> items = manager.get(((Class<? extends IdentifiableObject>) property.getItemKlass()), itemCodes);
    manager.refresh(object);
    if (property.isOwner()) {
        Collection<IdentifiableObject> collection = (Collection<IdentifiableObject>) property.getGetterMethod().invoke(object);
        for (IdentifiableObject item : items) {
            if (!collection.contains(item))
                collection.add(item);
        }
        manager.update(object);
    } else {
        Schema owningSchema = schemaService.getDynamicSchema(property.getItemKlass());
        Property owningProperty = owningSchema.propertyByRole(property.getOwningRole());
        for (IdentifiableObject item : items) {
            try {
                Collection<IdentifiableObject> collection = (Collection<IdentifiableObject>) owningProperty.getGetterMethod().invoke(item);
                if (!collection.contains(object)) {
                    collection.add(object);
                    manager.update(item);
                }
            } catch (Exception ex) {
            }
        }
    }
    dbmsManager.clearSession();
    cacheManager.clearCache();
}
Also used : UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Schema(org.hisp.dhis.schema.Schema) Collection(java.util.Collection) Property(org.hisp.dhis.schema.Property) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 80 with Property

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

the class DefaultObjectBundleValidationService method checkUniqueness.

private List<ErrorReport> checkUniqueness(Class<? extends IdentifiableObject> klass, IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier) {
    List<ErrorReport> errorReports = new ArrayList<>();
    if (object == null || Preheat.isDefault(object))
        return errorReports;
    if (!preheat.getUniquenessMap().containsKey(object.getClass())) {
        preheat.getUniquenessMap().put(object.getClass(), new HashMap<>());
    }
    Map<String, Map<Object, String>> uniquenessMap = preheat.getUniquenessMap().get(object.getClass());
    Schema schema = schemaService.getDynamicSchema(object.getClass());
    List<Property> uniqueProperties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && p.isUnique() && p.isSimple()).collect(Collectors.toList());
    uniqueProperties.forEach(property -> {
        if (!uniquenessMap.containsKey(property.getName())) {
            uniquenessMap.put(property.getName(), new HashMap<>());
        }
        Object value = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
        if (value != null) {
            String persistedUid = uniquenessMap.get(property.getName()).get(value);
            if (persistedUid != null) {
                if (!object.getUid().equals(persistedUid)) {
                    errorReports.add(new ErrorReport(object.getClass(), ErrorCode.E5003, property.getName(), value, identifier.getIdentifiersWithName(object), persistedUid).setMainId(persistedUid).setErrorProperty(property.getName()));
                }
            } else {
                uniquenessMap.get(property.getName()).put(value, object.getUid());
            }
        }
    });
    return errorReports;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) AtomicMode(org.hisp.dhis.dxf2.metadata.AtomicMode) ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) PropertyType(org.hisp.dhis.schema.PropertyType) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) AttributeValue(org.hisp.dhis.attribute.AttributeValue) IdentifiableObjectUtils(org.hisp.dhis.common.IdentifiableObjectUtils) ErrorReport(org.hisp.dhis.feedback.ErrorReport) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier) Preheat(org.hisp.dhis.preheat.Preheat) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.hisp.dhis.attribute.Attribute) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) TypeReport(org.hisp.dhis.feedback.TypeReport) UserCredentials(org.hisp.dhis.user.UserCredentials) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Service(org.springframework.stereotype.Service) Map(java.util.Map) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) User(org.hisp.dhis.user.User) ErrorCode(org.hisp.dhis.feedback.ErrorCode) ObjectReport(org.hisp.dhis.feedback.ObjectReport) Period(org.hisp.dhis.period.Period) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) SystemTimer(org.hisp.dhis.commons.timer.SystemTimer) UserService(org.hisp.dhis.user.UserService) Iterator(java.util.Iterator) Collection(java.util.Collection) Set(java.util.Set) Timer(org.hisp.dhis.commons.timer.Timer) SchemaService(org.hisp.dhis.schema.SchemaService) Property(org.hisp.dhis.schema.Property) Collectors(java.util.stream.Collectors) SchemaValidator(org.hisp.dhis.schema.validation.SchemaValidator) List(java.util.List) AclService(org.hisp.dhis.security.acl.AclService) PeriodType(org.hisp.dhis.period.PeriodType) Log(org.apache.commons.logging.Log) Schema(org.hisp.dhis.schema.Schema) LogFactory(org.apache.commons.logging.LogFactory) Transactional(org.springframework.transaction.annotation.Transactional) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) HashMap(java.util.HashMap) Map(java.util.Map) Property(org.hisp.dhis.schema.Property)

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