Search in sources :

Example 6 with Query

use of org.hisp.dhis.query.Query in project dhis2-core by dhis2.

the class MessageConversationController method getEntityList.

@Override
@SuppressWarnings("unchecked")
protected List<org.hisp.dhis.message.MessageConversation> getEntityList(WebMetadata metadata, WebOptions options, List<String> filters, List<Order> orders) throws QueryParserException {
    List<org.hisp.dhis.message.MessageConversation> messageConversations;
    if (options.getOptions().containsKey("query")) {
        messageConversations = Lists.newArrayList(manager.filter(getEntityClass(), options.getOptions().get("query")));
    } else if (options.hasPaging()) {
        int count = messageService.getMessageConversationCount();
        Pager pager = new Pager(options.getPage(), count, options.getPageSize());
        metadata.setPager(pager);
        messageConversations = new ArrayList<>(messageService.getMessageConversations(pager.getOffset(), pager.getPageSize()));
    } else {
        messageConversations = new ArrayList<>(messageService.getMessageConversations());
    }
    Query query = queryService.getQueryFromUrl(getEntityClass(), filters, orders, options.getRootJunction());
    query.setDefaultOrder();
    query.setObjects(messageConversations);
    return (List<org.hisp.dhis.message.MessageConversation>) queryService.query(query);
}
Also used : Query(org.hisp.dhis.query.Query) MessageConversation(org.hisp.dhis.webapi.webdomain.MessageConversation) Pager(org.hisp.dhis.common.Pager) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with Query

use of org.hisp.dhis.query.Query 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 8 with Query

use of org.hisp.dhis.query.Query 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 9 with Query

use of org.hisp.dhis.query.Query in project dhis2-core by dhis2.

the class OrganisationUnitController method getEntityList.

@Override
@SuppressWarnings("unchecked")
protected List<OrganisationUnit> getEntityList(WebMetadata metadata, WebOptions options, List<String> filters, List<Order> orders) throws QueryParserException {
    List<OrganisationUnit> objects = Lists.newArrayList();
    User currentUser = currentUserService.getCurrentUser();
    boolean anySpecialPropertySet = ObjectUtils.anyIsTrue(options.isTrue("userOnly"), options.isTrue("userDataViewOnly"), options.isTrue("userDataViewFallback"), options.isTrue("levelSorted"));
    boolean anyQueryPropertySet = ObjectUtils.firstNonNull(options.get("query"), options.getInt("level"), options.getInt("maxLevel")) != null || options.isTrue("withinUserHierarchy");
    String memberObject = options.get("memberObject");
    String memberCollection = options.get("memberCollection");
    if (options.isTrue("userOnly")) {
        objects = new ArrayList<>(currentUser.getOrganisationUnits());
    } else if (options.isTrue("userDataViewOnly")) {
        objects = new ArrayList<>(currentUser.getDataViewOrganisationUnits());
    } else if (options.isTrue("userDataViewFallback")) {
        if (currentUser.hasDataViewOrganisationUnit()) {
            objects = new ArrayList<>(currentUser.getDataViewOrganisationUnits());
        } else {
            objects = organisationUnitService.getOrganisationUnitsAtLevel(1);
        }
    } else if (options.isTrue("levelSorted")) {
        objects = new ArrayList<>(manager.getAll(getEntityClass()));
        Collections.sort(objects, OrganisationUnitByLevelComparator.INSTANCE);
    } else if (anyQueryPropertySet) {
        OrganisationUnitQueryParams params = new OrganisationUnitQueryParams();
        params.setQuery(options.get("query"));
        params.setLevel(options.getInt("level"));
        params.setMaxLevels(options.getInt("maxLevel"));
        params.setParents(options.isTrue("withinUserHierarchy") ? currentUser.getOrganisationUnits() : Sets.newHashSet());
        objects = organisationUnitService.getOrganisationUnitsByQuery(params);
    }
    // ---------------------------------------------------------------------
    // Standard Query handling
    // ---------------------------------------------------------------------
    Query query = queryService.getQueryFromUrl(getEntityClass(), filters, orders, options.getRootJunction());
    query.setUser(currentUser);
    query.setDefaultOrder();
    if (anySpecialPropertySet || anyQueryPropertySet) {
        query.setObjects(objects);
    }
    List<OrganisationUnit> list = (List<OrganisationUnit>) queryService.query(query);
    // ---------------------------------------------------------------------
    // Collection member count in hierarchy handling
    // ---------------------------------------------------------------------
    IdentifiableObject member = null;
    if (memberObject != null && memberCollection != null && (member = manager.get(memberObject)) != null) {
        for (OrganisationUnit unit : list) {
            Long count = organisationUnitService.getOrganisationUnitHierarchyMemberCount(unit, member, memberCollection);
            unit.setMemberCount((count != null ? count.intValue() : 0));
        }
    }
    return list;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) Query(org.hisp.dhis.query.Query) ArrayList(java.util.ArrayList) OrganisationUnitQueryParams(org.hisp.dhis.organisationunit.OrganisationUnitQueryParams) ArrayList(java.util.ArrayList) List(java.util.List) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 10 with Query

use of org.hisp.dhis.query.Query in project dhis2-core by dhis2.

the class UserController method getEntityList.

// -------------------------------------------------------------------------
// GET
// -------------------------------------------------------------------------
@Override
@SuppressWarnings("unchecked")
protected List<User> getEntityList(WebMetadata metadata, WebOptions options, List<String> filters, List<Order> orders) throws QueryParserException {
    UserQueryParams params = new UserQueryParams();
    params.setQuery(options.get("query"));
    params.setPhoneNumber(options.get("phoneNumber"));
    params.setCanManage(options.isTrue("canManage"));
    params.setAuthSubset(options.isTrue("authSubset"));
    params.setLastLogin(options.getDate("lastLogin"));
    params.setInactiveMonths(options.getInt("inactiveMonths"));
    params.setInactiveSince(options.getDate("inactiveSince"));
    params.setSelfRegistered(options.isTrue("selfRegistered"));
    params.setInvitationStatus(UserInvitationStatus.fromValue(options.get("invitationStatus")));
    String ou = options.get("ou");
    boolean includeOrgUnitChildren = Boolean.valueOf(options.get("includeChildren"));
    if (ou != null) {
        params.setOrganisationUnit(organisationUnitService.getOrganisationUnit(ou));
        params.setIncludeOrgUnitChildren(includeOrgUnitChildren);
    }
    if (options.isManage()) {
        params.setCanManage(true);
        params.setAuthSubset(true);
    }
    int count = userService.getUserCount(params);
    if (options.hasPaging() && filters.isEmpty()) {
        Pager pager = new Pager(options.getPage(), count, options.getPageSize());
        metadata.setPager(pager);
        params.setFirst(pager.getOffset());
        params.setMax(pager.getPageSize());
    }
    List<User> users = userService.getUsers(params);
    Query query = queryService.getQueryFromUrl(getEntityClass(), filters, orders, options.getRootJunction());
    query.setDefaultOrder();
    query.setObjects(users);
    return (List<User>) queryService.query(query);
}
Also used : User(org.hisp.dhis.user.User) Query(org.hisp.dhis.query.Query) Pager(org.hisp.dhis.common.Pager) List(java.util.List) UserQueryParams(org.hisp.dhis.user.UserQueryParams)

Aggregations

Query (org.hisp.dhis.query.Query)16 List (java.util.List)8 ArrayList (java.util.ArrayList)6 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)5 Pager (org.hisp.dhis.common.Pager)4 RootNode (org.hisp.dhis.node.types.RootNode)4 Schema (org.hisp.dhis.schema.Schema)4 User (org.hisp.dhis.user.User)4 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)4 HashMap (java.util.HashMap)3 WebOptions (org.hisp.dhis.webapi.webdomain.WebOptions)3 Map (java.util.Map)2 SystemTimer (org.hisp.dhis.commons.timer.SystemTimer)2 Timer (org.hisp.dhis.commons.timer.Timer)2 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)2 OrderParams (org.hisp.dhis.dxf2.common.OrderParams)2 Order (org.hisp.dhis.query.Order)2 WebMetadata (org.hisp.dhis.webapi.webdomain.WebMetadata)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1