Search in sources :

Example 6 with Property

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

the class DefaultPreheatService method handleUniqueProperties.

private Map<String, Map<Object, String>> handleUniqueProperties(Schema schema, List<IdentifiableObject> objects) {
    List<Property> uniqueProperties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && p.isUnique() && p.isSimple()).collect(Collectors.toList());
    Map<String, Map<Object, String>> map = new HashMap<>();
    for (IdentifiableObject object : objects) {
        uniqueProperties.forEach(property -> {
            if (!map.containsKey(property.getName()))
                map.put(property.getName(), new HashMap<>());
            Object value = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
            if (value != null)
                map.get(property.getName()).put(value, object.getUid());
        });
    }
    return map;
}
Also used : ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) BaseAnalyticalObject(org.hisp.dhis.common.BaseAnalyticalObject) Restrictions(org.hisp.dhis.query.Restrictions) PeriodService(org.hisp.dhis.period.PeriodService) MergeService(org.hisp.dhis.schema.MergeService) Autowired(org.springframework.beans.factory.annotation.Autowired) TrackedEntityAttributeDimension(org.hisp.dhis.trackedentity.TrackedEntityAttributeDimension) StringUtils(org.apache.commons.lang3.StringUtils) MergeParams(org.hisp.dhis.schema.MergeParams) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) CategoryDimension(org.hisp.dhis.dataelement.CategoryDimension) UserCredentials(org.hisp.dhis.user.UserCredentials) Map(java.util.Map) Period(org.hisp.dhis.period.Period) Query(org.hisp.dhis.query.Query) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) UserGroup(org.hisp.dhis.user.UserGroup) Collection(java.util.Collection) Set(java.util.Set) SchemaService(org.hisp.dhis.schema.SchemaService) QueryService(org.hisp.dhis.query.QueryService) Property(org.hisp.dhis.schema.Property) Collectors(java.util.stream.Collectors) DataDimensionItem(org.hisp.dhis.common.DataDimensionItem) List(java.util.List) AttributeService(org.hisp.dhis.attribute.AttributeService) Schema(org.hisp.dhis.schema.Schema) LogFactory(org.apache.commons.logging.LogFactory) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) PropertyType(org.hisp.dhis.schema.PropertyType) DataSetElement(org.hisp.dhis.dataset.DataSetElement) HashMap(java.util.HashMap) Attribute(org.hisp.dhis.attribute.Attribute) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) User(org.hisp.dhis.user.User) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) SystemTimer(org.hisp.dhis.commons.timer.SystemTimer) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) TrackedEntityProgramIndicatorDimension(org.hisp.dhis.trackedentity.TrackedEntityProgramIndicatorDimension) Timer(org.hisp.dhis.commons.timer.Timer) CollectionUtils(org.hisp.dhis.commons.collection.CollectionUtils) CurrentUserService(org.hisp.dhis.user.CurrentUserService) PeriodStore(org.hisp.dhis.period.PeriodStore) TrackedEntityDataElementDimension(org.hisp.dhis.trackedentity.TrackedEntityDataElementDimension) Log(org.apache.commons.logging.Log) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Transactional(org.springframework.transaction.annotation.Transactional) HashMap(java.util.HashMap) BaseAnalyticalObject(org.hisp.dhis.common.BaseAnalyticalObject) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Property(org.hisp.dhis.schema.Property) Map(java.util.Map) HashMap(java.util.HashMap) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 7 with Property

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

the class AbstractCrudController method partialUpdateObject.

@RequestMapping(value = "/{uid}", method = RequestMethod.PATCH)
public void partialUpdateObject(@PathVariable("uid") String pvUid, @RequestParam Map<String, String> rpParameters, HttpServletRequest request, HttpServletResponse response) throws Exception {
    WebOptions options = new WebOptions(rpParameters);
    List<T> entities = getEntity(pvUid, options);
    if (entities.isEmpty()) {
        throw new WebMessageException(WebMessageUtils.notFound(getEntityClass(), pvUid));
    }
    T persistedObject = entities.get(0);
    User user = currentUserService.getCurrentUser();
    if (!aclService.canUpdate(user, persistedObject)) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
    }
    String payload = StreamUtils.copyToString(request.getInputStream(), Charset.forName("UTF-8"));
    List<String> properties = new ArrayList<>();
    T object = null;
    if (isJson(request)) {
        properties = getJsonProperties(payload);
        object = renderService.fromJson(payload, getEntityClass());
    } else if (isXml(request)) {
        properties = getXmlProperties(payload);
        object = renderService.fromXml(payload, getEntityClass());
    }
    prePatchEntity(persistedObject, object);
    properties = getPersistedProperties(properties);
    if (properties.isEmpty() || object == null) {
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        return;
    }
    Schema schema = getSchema();
    for (String keyProperty : properties) {
        Property property = schema.getProperty(keyProperty);
        Object value = property.getGetterMethod().invoke(object);
        property.getSetterMethod().invoke(persistedObject, value);
    }
    manager.update(persistedObject);
    postPatchEntity(persistedObject);
}
Also used : User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) Property(org.hisp.dhis.schema.Property) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with Property

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

the class DataElementCategoryControllerDocumentation method testAddDeleteCollectionItem.

@Test
public void testAddDeleteCollectionItem() throws Exception {
    MockHttpSession session = getSession("ALL");
    DataElementCategory category = createDataElementCategory('A');
    manager.save(category);
    Schema schema = schemaService.getSchema(DataElementCategory.class);
    List<Property> properties = schema.getProperties();
    for (Property property : properties) {
        if (property.isCollection()) {
            String collectionName = property.getCollectionName();
            IdentifiableObject item = createTestObject(property.getItemKlass(), 'A', category);
            if (item == null) {
                continue;
            } else {
                manager.save(item);
            }
            mvc.perform(post("/" + ENDPOINT + "/" + category.getUid() + "/" + collectionName + "/" + item.getUid()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8)).andDo(documentPrettyPrint(ENDPOINT + "/add" + collectionName)).andExpect(status().isNoContent());
            mvc.perform(delete("/" + ENDPOINT + "/" + category.getUid() + "/" + collectionName + "/" + item.getUid()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8)).andDo(documentPrettyPrint(ENDPOINT + "/delete" + collectionName)).andExpect(status().isNoContent());
        }
    }
}
Also used : Schema(org.hisp.dhis.schema.Schema) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) MockHttpSession(org.springframework.mock.web.MockHttpSession) Property(org.hisp.dhis.schema.Property) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.Test) AbstractWebApiTest(org.hisp.dhis.webapi.documentation.controller.AbstractWebApiTest)

Example 9 with Property

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

the class DataElementControllerDocumentation method testAddDeleteCollectionItem.

@Test
public void testAddDeleteCollectionItem() throws Exception {
    MockHttpSession session = getSession("ALL");
    DataElement de = createDataElement('A');
    manager.save(de);
    Schema schema = schemaService.getSchema(DataElement.class);
    List<Property> properties = schema.getProperties();
    for (Property property : properties) {
        if (property.isCollection()) {
            String collectionName = property.getCollectionName();
            IdentifiableObject item = createTestObject(property.getItemKlass(), 'A');
            if (item == null) {
                continue;
            } else {
                manager.save(item);
            }
            mvc.perform(post("/dataElements/" + de.getUid() + "/" + collectionName + "/" + item.getUid()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8)).andDo(documentPrettyPrint("data-elements/add" + collectionName)).andExpect(status().isNoContent());
            mvc.perform(delete("/dataElements/" + de.getUid() + "/" + collectionName + "/" + item.getUid()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8)).andDo(documentPrettyPrint("data-elements/delete" + collectionName)).andExpect(status().isNoContent());
        }
    }
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Schema(org.hisp.dhis.schema.Schema) MockHttpSession(org.springframework.mock.web.MockHttpSession) Property(org.hisp.dhis.schema.Property) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.Test) AbstractWebApiTest(org.hisp.dhis.webapi.documentation.controller.AbstractWebApiTest)

Example 10 with Property

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

the class EmbeddedObjectObjectBundleHook method handleEmbeddedObjects.

private <T extends IdentifiableObject> void handleEmbeddedObjects(T object, ObjectBundle bundle, Collection<Property> properties) {
    for (Property property : properties) {
        if (property.isCollection()) {
            Collection<?> objects = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
            objects.forEach(o -> {
                if (property.isIdentifiableObject()) {
                    ((BaseIdentifiableObject) o).setAutoFields();
                }
                preheatService.connectReferences(o, bundle.getPreheat(), bundle.getPreheatIdentifier());
            });
        } else {
            Object o = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
            if (property.isIdentifiableObject()) {
                ((BaseIdentifiableObject) o).setAutoFields();
            }
            preheatService.connectReferences(o, bundle.getPreheat(), bundle.getPreheatIdentifier());
        }
    }
}
Also used : BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) 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