Search in sources :

Example 11 with Node

use of org.hisp.dhis.node.Node in project dhis2-core by dhis2.

the class PdfNodeSerializer method startWriteRootNode.

@Override
protected void startWriteRootNode(RootNode rootNode) throws Exception {
    for (Node child : rootNode.getChildren()) {
        if (child.isCollection()) {
            PdfPTable table = PDFUtils.getPdfPTable(true, 0.25f, 0.75f);
            boolean haveContent = false;
            for (Node node : child.getChildren()) {
                for (Node property : node.getChildren()) {
                    if (property.isSimple()) {
                        table.addCell(PDFUtils.getItalicCell(property.getName()));
                        table.addCell(PDFUtils.getTextCell(getValue((SimpleNode) property)));
                        haveContent = true;
                    }
                }
                if (haveContent) {
                    table.addCell(PDFUtils.getEmptyCell(2, 15));
                    haveContent = false;
                }
            }
            document.add(table);
        }
    }
}
Also used : PdfPTable(com.lowagie.text.pdf.PdfPTable) CollectionNode(org.hisp.dhis.node.types.CollectionNode) Node(org.hisp.dhis.node.Node) SimpleNode(org.hisp.dhis.node.types.SimpleNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) RootNode(org.hisp.dhis.node.types.RootNode)

Example 12 with Node

use of org.hisp.dhis.node.Node in project dhis2-core by dhis2.

the class AbstractCrudController method getObjectInternal.

@SuppressWarnings("unchecked")
private RootNode getObjectInternal(String uid, Map<String, String> parameters, List<String> filters, List<String> fields, User user) throws Exception {
    WebOptions options = new WebOptions(parameters);
    List<T> entities = getEntity(uid, options);
    if (entities.isEmpty()) {
        throw new WebMessageException(WebMessageUtils.notFound(getEntityClass(), uid));
    }
    Query query = queryService.getQueryFromUrl(getEntityClass(), filters, new ArrayList<>(), options.getRootJunction());
    query.setUser(user);
    query.setObjects(entities);
    entities = (List<T>) queryService.query(query);
    handleLinksAndAccess(entities, fields, true, user);
    for (T entity : entities) {
        postProcessEntity(entity);
        postProcessEntity(entity, options, parameters);
    }
    CollectionNode collectionNode = fieldFilterService.filter(getEntityClass(), entities, fields);
    if (options.isTrue("useWrapper") || entities.size() > 1) {
        RootNode rootNode = NodeUtils.createMetadata(collectionNode);
        rootNode.getConfig().setInclusionStrategy(getInclusionStrategy(parameters.get("inclusionStrategy")));
        return rootNode;
    } else {
        List<Node> children = collectionNode.getChildren();
        RootNode rootNode;
        if (!children.isEmpty()) {
            rootNode = NodeUtils.createRootNode(children.get(0));
        } else {
            rootNode = NodeUtils.createRootNode(new ComplexNode(getSchema().getSingular()));
        }
        rootNode.getConfig().setInclusionStrategy(getInclusionStrategy(parameters.get("inclusionStrategy")));
        return rootNode;
    }
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) Query(org.hisp.dhis.query.Query) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) ComplexNode(org.hisp.dhis.node.types.ComplexNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) SimpleNode(org.hisp.dhis.node.types.SimpleNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) RootNode(org.hisp.dhis.node.types.RootNode) CollectionNode(org.hisp.dhis.node.types.CollectionNode) Node(org.hisp.dhis.node.Node) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) CollectionNode(org.hisp.dhis.node.types.CollectionNode)

Example 13 with Node

use of org.hisp.dhis.node.Node in project dhis2-core by dhis2.

the class AbstractCrudController method getCollectionItem.

//--------------------------------------------------------------------------
// Identifiable object collections add, delete
//--------------------------------------------------------------------------
@RequestMapping(value = "/{uid}/{property}/{itemId}", method = RequestMethod.GET)
@ResponseBody
public RootNode getCollectionItem(@PathVariable("uid") String pvUid, @PathVariable("property") String pvProperty, @PathVariable("itemId") String pvItemId, @RequestParam Map<String, String> parameters, TranslateParams translateParams, HttpServletRequest request, HttpServletResponse response) throws Exception {
    User user = currentUserService.getCurrentUser();
    setUserContext(user, translateParams);
    if (!aclService.canRead(user, getEntityClass())) {
        throw new ReadAccessDeniedException("You don't have the proper permissions to read objects of this type.");
    }
    RootNode rootNode = getObjectInternal(pvUid, parameters, Lists.newArrayList(), Lists.newArrayList(pvProperty + "[:all]"), user);
    // TODO optimize this using field filter (collection filtering)
    if (!rootNode.getChildren().isEmpty() && rootNode.getChildren().get(0).isCollection()) {
        rootNode.getChildren().get(0).getChildren().stream().filter(Node::isComplex).forEach(node -> {
            node.getChildren().stream().filter(child -> child.isSimple() && child.getName().equals("id") && !((SimpleNode) child).getValue().equals(pvItemId)).forEach(child -> rootNode.getChildren().get(0).removeChild(node));
        });
    }
    if (rootNode.getChildren().isEmpty() || rootNode.getChildren().get(0).getChildren().isEmpty()) {
        throw new WebMessageException(WebMessageUtils.notFound(pvProperty + " with ID " + pvItemId + " could not be found."));
    }
    return rootNode;
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) PathVariable(org.springframework.web.bind.annotation.PathVariable) Order(org.hisp.dhis.query.Order) RequestParam(org.springframework.web.bind.annotation.RequestParam) ErrorReport(org.hisp.dhis.feedback.ErrorReport) UserContext(org.hisp.dhis.common.UserContext) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) MergeService(org.hisp.dhis.schema.MergeService) RenderService(org.hisp.dhis.render.RenderService) InclusionStrategy(org.hisp.dhis.node.config.InclusionStrategy) UserSettingKey(org.hisp.dhis.user.UserSettingKey) Autowired(org.springframework.beans.factory.annotation.Autowired) WebMessageService(org.hisp.dhis.webapi.service.WebMessageService) NodeUtils(org.hisp.dhis.node.NodeUtils) UserSettingService(org.hisp.dhis.user.UserSettingService) Optional(com.google.common.base.Optional) MetadataImportService(org.hisp.dhis.dxf2.metadata.MetadataImportService) Locale(java.util.Locale) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) Preset(org.hisp.dhis.node.Preset) PagerUtils(org.hisp.dhis.common.PagerUtils) Status(org.hisp.dhis.feedback.Status) Query(org.hisp.dhis.query.Query) ContextService(org.hisp.dhis.webapi.service.ContextService) DefaultRenderService(org.hisp.dhis.render.DefaultRenderService) LinkService(org.hisp.dhis.webapi.service.LinkService) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) FieldFilterService(org.hisp.dhis.fieldfilter.FieldFilterService) MediaType(org.springframework.http.MediaType) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) SchemaService(org.hisp.dhis.schema.SchemaService) QueryService(org.hisp.dhis.query.QueryService) Property(org.hisp.dhis.schema.Property) Collectors(java.util.stream.Collectors) ImportReportMode(org.hisp.dhis.dxf2.metadata.feedback.ImportReportMode) MetadataExportService(org.hisp.dhis.dxf2.metadata.MetadataExportService) SimpleNode(org.hisp.dhis.node.types.SimpleNode) ObjectTranslation(org.hisp.dhis.translation.ObjectTranslation) List(java.util.List) ComplexNode(org.hisp.dhis.node.types.ComplexNode) Type(java.lang.reflect.Type) AclService(org.hisp.dhis.security.acl.AclService) Schema(org.hisp.dhis.schema.Schema) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) RootNode(org.hisp.dhis.node.types.RootNode) Joiner(com.google.common.base.Joiner) HibernateCacheManager(org.hisp.dhis.cache.HibernateCacheManager) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) CollectionNode(org.hisp.dhis.node.types.CollectionNode) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) CreateAccessDeniedException(org.hisp.dhis.hibernate.exception.CreateAccessDeniedException) HashMap(java.util.HashMap) ApiVersion(org.hisp.dhis.webapi.mvc.annotation.ApiVersion) Enums(com.google.common.base.Enums) TypeReport(org.hisp.dhis.feedback.TypeReport) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) Lists(com.google.common.collect.Lists) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) Charset(java.nio.charset.Charset) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) WebMetadata(org.hisp.dhis.webapi.webdomain.WebMetadata) User(org.hisp.dhis.user.User) ErrorCode(org.hisp.dhis.feedback.ErrorCode) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) WebMessageUtils(org.hisp.dhis.dxf2.webmessage.WebMessageUtils) ObjectReport(org.hisp.dhis.feedback.ObjectReport) QueryParserException(org.hisp.dhis.query.QueryParserException) IdentifiableObjects(org.hisp.dhis.common.IdentifiableObjects) ReadAccessDeniedException(org.hisp.dhis.hibernate.exception.ReadAccessDeniedException) StreamUtils(org.springframework.util.StreamUtils) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) ContextUtils(org.hisp.dhis.webapi.utils.ContextUtils) Node(org.hisp.dhis.node.Node) DeleteAccessDeniedException(org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException) Pager(org.hisp.dhis.common.Pager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) CollectionService(org.hisp.dhis.dxf2.metadata.collection.CollectionService) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) HttpStatus(org.springframework.http.HttpStatus) OrderParams(org.hisp.dhis.dxf2.common.OrderParams) ParameterizedType(java.lang.reflect.ParameterizedType) CurrentUserService(org.hisp.dhis.user.CurrentUserService) TranslateParams(org.hisp.dhis.dxf2.common.TranslateParams) StringUtils(org.springframework.util.StringUtils) RootNode(org.hisp.dhis.node.types.RootNode) User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) ReadAccessDeniedException(org.hisp.dhis.hibernate.exception.ReadAccessDeniedException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 14 with Node

use of org.hisp.dhis.node.Node in project dhis2-core by dhis2.

the class PluckNodeTransformerTest method withoutArg.

@Test
void withoutArg() {
    Node result = transformer.transform(collectionNode, null);
    Assertions.assertTrue(result instanceof CollectionNode);
    CollectionNode collection = (CollectionNode) result;
    Assertions.assertEquals("organisationUnits", collection.getName());
    Assertions.assertEquals("testUrn", collection.getNamespace());
    Assertions.assertEquals(2, collection.getUnorderedChildren().size());
    Assertions.assertEquals("id", collection.getUnorderedChildren().get(0).getName());
    Assertions.assertTrue(collection.getUnorderedChildren().get(0) instanceof SimpleNode);
    Assertions.assertEquals("abc1", ((SimpleNode) collection.getUnorderedChildren().get(0)).getValue());
    Assertions.assertEquals("id", collection.getUnorderedChildren().get(1).getName());
    Assertions.assertTrue(collection.getUnorderedChildren().get(1) instanceof SimpleNode);
    Assertions.assertEquals("abc2", ((SimpleNode) collection.getUnorderedChildren().get(1)).getValue());
}
Also used : CollectionNode(org.hisp.dhis.node.types.CollectionNode) 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) Test(org.junit.jupiter.api.Test)

Example 15 with Node

use of org.hisp.dhis.node.Node in project dhis2-core by dhis2.

the class AbstractFullReadOnlyController method getCollectionItem.

@GetMapping("/{uid}/{property}/{itemId}")
@ResponseBody
public RootNode getCollectionItem(@PathVariable("uid") String pvUid, @PathVariable("property") String pvProperty, @PathVariable("itemId") String pvItemId, @RequestParam Map<String, String> parameters, TranslateParams translateParams, HttpServletResponse response, @CurrentUser User currentUser) throws Exception {
    setUserContext(currentUser, translateParams);
    try {
        if (!aclService.canRead(currentUser, getEntityClass())) {
            throw new ReadAccessDeniedException("You don't have the proper permissions to read objects of this type.");
        }
        RootNode rootNode = getObjectInternal(pvUid, parameters, Lists.newArrayList(), Lists.newArrayList(pvProperty + "[:all]"), currentUser);
        // TODO optimize this using field filter (collection filtering)
        if (!rootNode.getChildren().isEmpty() && rootNode.getChildren().get(0).isCollection()) {
            rootNode.getChildren().get(0).getChildren().stream().filter(Node::isComplex).forEach(node -> {
                node.getChildren().stream().filter(child -> child.isSimple() && child.getName().equals("id") && !((SimpleNode) child).getValue().equals(pvItemId)).forEach(child -> rootNode.getChildren().get(0).removeChild(node));
            });
        }
        if (rootNode.getChildren().isEmpty() || rootNode.getChildren().get(0).getChildren().isEmpty()) {
            throw new WebMessageException(notFound(pvProperty + " with ID " + pvItemId + " could not be found."));
        }
        cachePrivate(response);
        return rootNode;
    } finally {
        UserContext.reset();
    }
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) Order(org.hisp.dhis.query.Order) RequestParam(org.springframework.web.bind.annotation.RequestParam) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) UserContext(org.hisp.dhis.common.UserContext) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) InclusionStrategy(org.hisp.dhis.node.config.InclusionStrategy) Pagination(org.hisp.dhis.query.Pagination) UserSettingKey(org.hisp.dhis.user.UserSettingKey) Autowired(org.springframework.beans.factory.annotation.Autowired) CurrentUser(org.hisp.dhis.user.CurrentUser) PaginationUtils(org.hisp.dhis.webapi.utils.PaginationUtils) NodeUtils(org.hisp.dhis.node.NodeUtils) UserSettingService(org.hisp.dhis.user.UserSettingService) Locale(java.util.Locale) Optional(com.google.common.base.Optional) Map(java.util.Map) Preset(org.hisp.dhis.node.Preset) Query(org.hisp.dhis.query.Query) ContextService(org.hisp.dhis.webapi.service.ContextService) LinkService(org.hisp.dhis.webapi.service.LinkService) FieldFilterService(org.hisp.dhis.fieldfilter.FieldFilterService) CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) CacheControl.noCache(org.springframework.http.CacheControl.noCache) QueryService(org.hisp.dhis.query.QueryService) Property(org.hisp.dhis.schema.Property) Defaults(org.hisp.dhis.fieldfilter.Defaults) SimpleNode(org.hisp.dhis.node.types.SimpleNode) List(java.util.List) Include(org.hisp.dhis.node.config.InclusionStrategy.Include) ComplexNode(org.hisp.dhis.node.types.ComplexNode) AttributeService(org.hisp.dhis.attribute.AttributeService) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) AclService(org.hisp.dhis.security.acl.AclService) Schema(org.hisp.dhis.schema.Schema) RootNode(org.hisp.dhis.node.types.RootNode) Joiner(com.google.common.base.Joiner) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) WebMessageUtils.notFound(org.hisp.dhis.dxf2.webmessage.WebMessageUtils.notFound) CollectionNode(org.hisp.dhis.node.types.CollectionNode) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) HashMap(java.util.HashMap) ApiVersion(org.hisp.dhis.webapi.mvc.annotation.ApiVersion) ArrayList(java.util.ArrayList) Enums(com.google.common.base.Enums) HttpServletRequest(javax.servlet.http.HttpServletRequest) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) WebMetadata(org.hisp.dhis.webapi.webdomain.WebMetadata) User(org.hisp.dhis.user.User) GetMapping(org.springframework.web.bind.annotation.GetMapping) QueryParserException(org.hisp.dhis.query.QueryParserException) ReadAccessDeniedException(org.hisp.dhis.hibernate.exception.ReadAccessDeniedException) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) ContextUtils(org.hisp.dhis.webapi.utils.ContextUtils) Node(org.hisp.dhis.node.Node) Pager(org.hisp.dhis.common.Pager) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) HttpStatus(org.springframework.http.HttpStatus) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) Collectors.toList(java.util.stream.Collectors.toList) OrderParams(org.hisp.dhis.dxf2.common.OrderParams) CurrentUserService(org.hisp.dhis.user.CurrentUserService) TranslateParams(org.hisp.dhis.dxf2.common.TranslateParams) RootNode(org.hisp.dhis.node.types.RootNode) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) ReadAccessDeniedException(org.hisp.dhis.hibernate.exception.ReadAccessDeniedException) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

Node (org.hisp.dhis.node.Node)16 CollectionNode (org.hisp.dhis.node.types.CollectionNode)16 SimpleNode (org.hisp.dhis.node.types.SimpleNode)14 ComplexNode (org.hisp.dhis.node.types.ComplexNode)13 RootNode (org.hisp.dhis.node.types.RootNode)11 Query (org.hisp.dhis.query.Query)6 WebOptions (org.hisp.dhis.webapi.webdomain.WebOptions)5 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)4 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)3 FieldFilterParams (org.hisp.dhis.fieldfilter.FieldFilterParams)3 AbstractNode (org.hisp.dhis.node.AbstractNode)3 Order (org.hisp.dhis.query.Order)3 Property (org.hisp.dhis.schema.Property)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 CsvSchema (com.fasterxml.jackson.dataformat.csv.CsvSchema)2 Enums (com.google.common.base.Enums)2 Joiner (com.google.common.base.Joiner)2