Search in sources :

Example 1 with MetadataExportParams

use of org.hisp.dhis.dxf2.metadata.MetadataExportParams in project dhis2-core by dhis2.

the class DefaultMetadataVersionService method getMetadataExport.

//--------------------------------------------------------------------------
// Private methods
//--------------------------------------------------------------------------
/**
     * Generates the metadata export based on the created date of the current version.
     */
private ByteArrayOutputStream getMetadataExport(Date minDate) {
    ByteArrayOutputStream os = null;
    try {
        MetadataExportParams exportParams = new MetadataExportParams();
        if (minDate != null) {
            List<String> defaultFilterList = new ArrayList<String>();
            defaultFilterList.add("lastUpdated:gte:" + DateUtils.getLongGmtDateString(minDate));
            exportParams.setDefaultFilter(defaultFilterList);
            metadataExportService.validate(exportParams);
        }
        os = new ByteArrayOutputStream(1024);
        RootNode metadata = metadataExportService.getMetadataAsNode(exportParams);
        nodeService.serialize(metadata, "application/json", os);
    } catch (//We have to catch the "Exception" object as no specific exception on the contract.
    Exception ex) {
        String message = "Exception occurred while exporting metadata for capturing a metadata version" + ex.getMessage();
        log.error(message, ex);
        throw new MetadataVersionServiceException(message, ex);
    }
    return os;
}
Also used : MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) RootNode(org.hisp.dhis.node.types.RootNode) MetadataExportParams(org.hisp.dhis.dxf2.metadata.MetadataExportParams) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with MetadataExportParams

use of org.hisp.dhis.dxf2.metadata.MetadataExportParams in project dhis2-core by dhis2.

the class MetadataExportServiceTest method testValidate.

@Test
public void testValidate() {
    MetadataExportParams params = new MetadataExportParams();
    metadataExportService.validate(params);
}
Also used : MetadataExportParams(org.hisp.dhis.dxf2.metadata.MetadataExportParams) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 3 with MetadataExportParams

use of org.hisp.dhis.dxf2.metadata.MetadataExportParams in project dhis2-core by dhis2.

the class DefaultMetadataExportService method getParamsFromMap.

@Override
@SuppressWarnings("unchecked")
public MetadataExportParams getParamsFromMap(Map<String, List<String>> parameters) {
    MetadataExportParams params = new MetadataExportParams();
    Map<Class<? extends IdentifiableObject>, Map<String, List<String>>> map = new HashMap<>();
    if (parameters.containsKey("fields")) {
        params.setDefaultFields(parameters.get("fields"));
        parameters.remove("fields");
    }
    if (parameters.containsKey("filter")) {
        params.setDefaultFilter(parameters.get("filter"));
        parameters.remove("filter");
    }
    if (parameters.containsKey("order")) {
        params.setDefaultOrder(parameters.get("order"));
        parameters.remove("order");
    }
    for (String parameterKey : parameters.keySet()) {
        String[] parameter = parameterKey.split(":");
        Schema schema = schemaService.getSchemaByPluralName(parameter[0]);
        if (schema == null || !schema.isIdentifiableObject()) {
            continue;
        }
        Class<? extends IdentifiableObject> klass = (Class<? extends IdentifiableObject>) schema.getKlass();
        // class is enabled if value = true, or fields/filter/order is present
        if ("true".equalsIgnoreCase(parameters.get(parameterKey).get(0)) || (parameter.length > 1 && ("fields".equalsIgnoreCase(parameter[1]) || "filter".equalsIgnoreCase(parameter[1]) || "order".equalsIgnoreCase(parameter[1])))) {
            if (!map.containsKey(klass))
                map.put(klass, new HashMap<>());
        } else {
            continue;
        }
        if (parameter.length > 1) {
            if ("fields".equalsIgnoreCase(parameter[1])) {
                if (!map.get(klass).containsKey("fields"))
                    map.get(klass).put("fields", new ArrayList<>());
                map.get(klass).get("fields").addAll(parameters.get(parameterKey));
            }
            if ("filter".equalsIgnoreCase(parameter[1])) {
                if (!map.get(klass).containsKey("filter"))
                    map.get(klass).put("filter", new ArrayList<>());
                map.get(klass).get("filter").addAll(parameters.get(parameterKey));
            }
            if ("order".equalsIgnoreCase(parameter[1])) {
                if (!map.get(klass).containsKey("order"))
                    map.get(klass).put("order", new ArrayList<>());
                map.get(klass).get("order").addAll(parameters.get(parameterKey));
            }
        }
    }
    map.keySet().forEach(params::addClass);
    for (Class<? extends IdentifiableObject> klass : map.keySet()) {
        Map<String, List<String>> classMap = map.get(klass);
        Schema schema = schemaService.getDynamicSchema(klass);
        if (classMap.containsKey("fields"))
            params.addFields(klass, classMap.get("fields"));
        if (classMap.containsKey("filter") && classMap.containsKey("order")) {
            OrderParams orderParams = new OrderParams(Sets.newHashSet(classMap.get("order")));
            Query query = queryService.getQueryFromUrl(klass, classMap.get("filter"), orderParams.getOrders(schema));
            query.setDefaultOrder();
            params.addQuery(query);
        } else if (classMap.containsKey("filter")) {
            Query query = queryService.getQueryFromUrl(klass, classMap.get("filter"), new ArrayList<>());
            query.setDefaultOrder();
            params.addQuery(query);
        } else if (classMap.containsKey("order")) {
            OrderParams orderParams = new OrderParams();
            orderParams.setOrder(Sets.newHashSet(classMap.get("order")));
            Query query = queryService.getQueryFromUrl(klass, new ArrayList<>(), orderParams.getOrders(schema));
            query.setDefaultOrder();
            params.addQuery(query);
        }
    }
    return params;
}
Also used : Query(org.hisp.dhis.query.Query) HashMap(java.util.HashMap) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) List(java.util.List) ArrayList(java.util.ArrayList) OrderParams(org.hisp.dhis.dxf2.common.OrderParams) Map(java.util.Map) SetMap(org.hisp.dhis.common.SetMap) HashMap(java.util.HashMap)

Example 4 with MetadataExportParams

use of org.hisp.dhis.dxf2.metadata.MetadataExportParams in project dhis2-core by dhis2.

the class DefaultMetadataExportService method getMetadata.

@Override
@SuppressWarnings("unchecked")
public Map<Class<? extends IdentifiableObject>, List<? extends IdentifiableObject>> getMetadata(MetadataExportParams params) {
    Timer timer = new SystemTimer().start();
    Map<Class<? extends IdentifiableObject>, List<? extends IdentifiableObject>> metadata = new HashMap<>();
    if (params.getUser() == null) {
        params.setUser(currentUserService.getCurrentUser());
    }
    if (params.getClasses().isEmpty()) {
        schemaService.getMetadataSchemas().stream().filter(Schema::isIdentifiableObject).forEach(schema -> params.getClasses().add((Class<? extends IdentifiableObject>) schema.getKlass()));
    }
    log.info("(" + params.getUsername() + ") Export:Start");
    for (Class<? extends IdentifiableObject> klass : params.getClasses()) {
        Query query;
        if (params.getQuery(klass) != null) {
            query = params.getQuery(klass);
        } else {
            OrderParams orderParams = new OrderParams(Sets.newHashSet(params.getDefaultOrder()));
            query = queryService.getQueryFromUrl(klass, params.getDefaultFilter(), orderParams.getOrders(schemaService.getDynamicSchema(klass)));
        }
        if (query.getUser() == null) {
            query.setUser(params.getUser());
        }
        query.setDefaultOrder();
        List<? extends IdentifiableObject> objects = queryService.query(query);
        if (!objects.isEmpty()) {
            log.info("(" + params.getUsername() + ") Exported " + objects.size() + " objects of type " + klass.getSimpleName());
            metadata.put(klass, objects);
        }
    }
    log.info("(" + params.getUsername() + ") Export:Done took " + timer.toString());
    return metadata;
}
Also used : SystemTimer(org.hisp.dhis.commons.timer.SystemTimer) Timer(org.hisp.dhis.commons.timer.Timer) Query(org.hisp.dhis.query.Query) HashMap(java.util.HashMap) List(java.util.List) ArrayList(java.util.ArrayList) OrderParams(org.hisp.dhis.dxf2.common.OrderParams) SystemTimer(org.hisp.dhis.commons.timer.SystemTimer) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 5 with MetadataExportParams

use of org.hisp.dhis.dxf2.metadata.MetadataExportParams in project dhis2-core by dhis2.

the class MetadataExportServiceTest method testMetadataExportWithCustomQueries.

@Test
public void testMetadataExportWithCustomQueries() {
    DataElementGroup deg1 = createDataElementGroup('A');
    DataElement de1 = createDataElement('A');
    DataElement de2 = createDataElement('B');
    DataElement de3 = createDataElement('C');
    manager.save(de1);
    manager.save(de2);
    manager.save(de3);
    User user = createUser('A');
    manager.save(user);
    deg1.addDataElement(de1);
    deg1.addDataElement(de2);
    deg1.addDataElement(de3);
    deg1.setUser(user);
    manager.save(deg1);
    Query deQuery = Query.from(schemaService.getDynamicSchema(DataElement.class));
    Disjunction disjunction = deQuery.disjunction();
    disjunction.add(Restrictions.eq("id", de1.getUid()));
    disjunction.add(Restrictions.eq("id", de2.getUid()));
    deQuery.add(disjunction);
    Query degQuery = Query.from(schemaService.getDynamicSchema(DataElementGroup.class));
    degQuery.add(Restrictions.eq("id", "INVALID UID"));
    MetadataExportParams params = new MetadataExportParams();
    params.addQuery(deQuery);
    params.addQuery(degQuery);
    Map<Class<? extends IdentifiableObject>, List<? extends IdentifiableObject>> metadata = metadataExportService.getMetadata(params);
    assertFalse(metadata.containsKey(User.class));
    assertFalse(metadata.containsKey(DataElementGroup.class));
    assertTrue(metadata.containsKey(DataElement.class));
    assertEquals(2, metadata.get(DataElement.class).size());
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Disjunction(org.hisp.dhis.query.Disjunction) User(org.hisp.dhis.user.User) Query(org.hisp.dhis.query.Query) MetadataExportParams(org.hisp.dhis.dxf2.metadata.MetadataExportParams) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) List(java.util.List) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

MetadataExportParams (org.hisp.dhis.dxf2.metadata.MetadataExportParams)8 List (java.util.List)6 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)6 DhisSpringTest (org.hisp.dhis.DhisSpringTest)4 DataElement (org.hisp.dhis.dataelement.DataElement)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 DataElementGroup (org.hisp.dhis.dataelement.DataElementGroup)3 Query (org.hisp.dhis.query.Query)3 User (org.hisp.dhis.user.User)3 HashMap (java.util.HashMap)2 OrderParams (org.hisp.dhis.dxf2.common.OrderParams)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Map (java.util.Map)1 Transformer (javax.xml.transform.Transformer)1