Search in sources :

Example 16 with Schema

use of org.hisp.dhis.schema.Schema 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 17 with Schema

use of org.hisp.dhis.schema.Schema 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 18 with Schema

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

the class DataElementCategoryControllerDocumentation method testGetByIdOk.

@Test
@Override
public void testGetByIdOk() throws Exception {
    DataElementCategory cat = createDataElementCategory('A');
    manager.save(cat);
    MockHttpSession session = getSession("ALL");
    Schema schema = schemaService.getSchema(DataElementCategory.class);
    Set<FieldDescriptor> fieldDescriptors = TestUtils.getFieldDescriptors(schema);
    mvc.perform(get("/" + CategorySchemaDescriptor.PLURAL + "/{id}", cat.getUid()).session(session).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$.name").value("DataElementCategoryA")).andExpect(jsonPath("$.shortName").value("DataElementCategoryA")).andDo(documentPrettyPrint(ENDPOINT + "/id", responseFields(fieldDescriptors.toArray(new FieldDescriptor[fieldDescriptors.size()]))));
}
Also used : Schema(org.hisp.dhis.schema.Schema) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) MockHttpSession(org.springframework.mock.web.MockHttpSession) FieldDescriptor(org.springframework.restdocs.payload.FieldDescriptor) Test(org.junit.Test) AbstractWebApiTest(org.hisp.dhis.webapi.documentation.controller.AbstractWebApiTest)

Example 19 with Schema

use of org.hisp.dhis.schema.Schema 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 20 with Schema

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

the class DefaultFieldFilterService method buildNode.

private AbstractNode buildNode(FieldMap fieldMap, Class<?> klass, Object object, String nodeName) {
    Schema schema = schemaService.getDynamicSchema(klass);
    ComplexNode complexNode = new ComplexNode(nodeName);
    complexNode.setNamespace(schema.getNamespace());
    if (object == null) {
        return new SimpleNode(schema.getName(), null);
    }
    updateFields(fieldMap, schema.getKlass());
    for (String fieldKey : fieldMap.keySet()) {
        AbstractNode child;
        Property property = schema.getProperty(fieldKey);
        if (property == null || !property.isReadable()) {
            // throw new FieldFilterException( fieldKey, schema );
            log.debug("Unknown field property `" + fieldKey + "`, available fields are " + schema.getPropertyMap().keySet());
            continue;
        }
        Object returnValue = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
        Schema propertySchema = schemaService.getDynamicSchema(property.getKlass());
        FieldMap fieldValue = fieldMap.get(fieldKey);
        if (returnValue == null && property.isCollection()) {
            continue;
        }
        if (property.isCollection()) {
            updateFields(fieldValue, property.getItemKlass());
        } else {
            updateFields(fieldValue, property.getKlass());
        }
        if (fieldValue.isEmpty()) {
            List<String> fields = Preset.defaultAssociationPreset().getFields();
            if (property.isCollection()) {
                Collection<?> collection = (Collection<?>) returnValue;
                child = new CollectionNode(property.getCollectionName());
                child.setNamespace(property.getNamespace());
                if (property.isIdentifiableObject() && isProperIdObject(property.getItemKlass())) {
                    for (Object collectionObject : collection) {
                        child.addChild(getProperties(property, collectionObject, fields));
                    }
                } else if (!property.isSimple()) {
                    FieldMap map = getFullFieldMap(schemaService.getDynamicSchema(property.getItemKlass()));
                    for (Object collectionObject : collection) {
                        Node node = buildNode(map, property.getItemKlass(), collectionObject);
                        if (!node.getChildren().isEmpty()) {
                            child.addChild(node);
                        }
                    }
                } else {
                    if (collection != null) {
                        for (Object collectionObject : collection) {
                            SimpleNode simpleNode = child.addChild(new SimpleNode(property.getName(), collectionObject));
                            simpleNode.setProperty(property);
                        }
                    }
                }
            } else if (property.isIdentifiableObject() && isProperIdObject(property.getKlass())) {
                child = getProperties(property, returnValue, fields);
            } else {
                if (propertySchema.getProperties().isEmpty()) {
                    SimpleNode simpleNode = new SimpleNode(fieldKey, returnValue);
                    simpleNode.setAttribute(property.isAttribute());
                    simpleNode.setNamespace(property.getNamespace());
                    child = simpleNode;
                } else {
                    child = buildNode(getFullFieldMap(propertySchema), property.getKlass(), returnValue);
                }
            }
        } else {
            if (property.isCollection()) {
                child = new CollectionNode(property.getCollectionName());
                child.setNamespace(property.getNamespace());
                for (Object collectionObject : (Collection<?>) returnValue) {
                    Node node = buildNode(fieldValue, property.getItemKlass(), collectionObject, property.getName());
                    if (!node.getChildren().isEmpty()) {
                        child.addChild(node);
                    }
                }
            } else {
                child = buildNode(fieldValue, property.getKlass(), returnValue);
            }
        }
        if (child != null) {
            child.setName(fieldKey);
            child.setProperty(property);
            // TODO fix ugly hack, will be replaced by custom field serializer/deserializer
            if (child.isSimple() && PeriodType.class.isInstance((((SimpleNode) child).getValue()))) {
                child = new SimpleNode(child.getName(), ((PeriodType) ((SimpleNode) child).getValue()).getName());
            }
            complexNode.addChild(fieldValue.getPipeline().process(child));
        }
    }
    return complexNode;
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) AbstractNode(org.hisp.dhis.node.AbstractNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) Schema(org.hisp.dhis.schema.Schema) CollectionNode(org.hisp.dhis.node.types.CollectionNode) AbstractNode(org.hisp.dhis.node.AbstractNode) Node(org.hisp.dhis.node.Node) SimpleNode(org.hisp.dhis.node.types.SimpleNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) CollectionNode(org.hisp.dhis.node.types.CollectionNode) SimpleNode(org.hisp.dhis.node.types.SimpleNode) Collection(java.util.Collection) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) Property(org.hisp.dhis.schema.Property)

Aggregations

Schema (org.hisp.dhis.schema.Schema)149 Authority (org.hisp.dhis.security.Authority)65 Property (org.hisp.dhis.schema.Property)29 ArrayList (java.util.ArrayList)20 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)20 Test (org.junit.Test)16 Collection (java.util.Collection)14 List (java.util.List)13 HashMap (java.util.HashMap)12 DhisSpringTest (org.hisp.dhis.DhisSpringTest)12 EmbeddedObject (org.hisp.dhis.common.EmbeddedObject)12 Map (java.util.Map)10 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)10 User (org.hisp.dhis.user.User)10 AnalyticalObject (org.hisp.dhis.common.AnalyticalObject)9 BaseAnalyticalObject (org.hisp.dhis.common.BaseAnalyticalObject)9 UserCredentials (org.hisp.dhis.user.UserCredentials)9 HashSet (java.util.HashSet)8 Set (java.util.Set)8 Log (org.apache.commons.logging.Log)8