Search in sources :

Example 1 with RelativePropertyContext

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

the class DefaultGistService method gist.

@Override
public List<?> gist(GistQuery query) {
    GistAccessControl access = createGistAccessControl();
    RelativePropertyContext context = createPropertyContext(query);
    new GistValidator(query, context, access).validateQuery();
    GistBuilder queryBuilder = createFetchBuilder(query, context, access, this::getUserGroupIdsByUserId);
    List<Object[]> rows = fetchWithParameters(query, queryBuilder, getSession().createQuery(queryBuilder.buildFetchHQL(), Object[].class));
    queryBuilder.transform(rows);
    return rows;
}
Also used : RelativePropertyContext(org.hisp.dhis.schema.RelativePropertyContext)

Example 2 with RelativePropertyContext

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

the class DefaultGistService method pager.

@Override
public GistPager pager(GistQuery query, List<?> rows, Map<String, String[]> params) {
    int page = 1 + (query.getPageOffset() / query.getPageSize());
    Schema schema = schemaService.getDynamicSchema(query.getElementType());
    String prev = null;
    String next = null;
    Integer total = null;
    if (query.isTotal()) {
        if (rows.size() < query.getPageSize() && !rows.isEmpty()) {
            // NB. only do this when rows are returned as otherwise the page
            // simply might not exist which leads to zero rows
            total = query.getPageOffset() + rows.size();
        } else {
            GistAccessControl access = createGistAccessControl();
            RelativePropertyContext context = createPropertyContext(query);
            GistBuilder countBuilder = createCountBuilder(query, context, access, this::getUserGroupIdsByUserId);
            total = countWithParameters(countBuilder, getSession().createQuery(countBuilder.buildCountHQL(), Long.class));
        }
    }
    if (schema.haveApiEndpoint()) {
        URI baseURL = GistPager.computeBaseURL(query, params, schemaService::getDynamicSchema);
        if (page > 1) {
            prev = UriComponentsBuilder.fromUri(baseURL).replaceQueryParam("page", page - 1).build().toString();
        }
        if (total != null && query.getPageOffset() + rows.size() < total || total == null && query.getPageSize() == rows.size()) {
            next = UriComponentsBuilder.fromUri(baseURL).replaceQueryParam("page", page + 1).build().toString();
        }
    }
    return new GistPager(page, query.getPageSize(), total, prev, next);
}
Also used : RelativePropertyContext(org.hisp.dhis.schema.RelativePropertyContext) Schema(org.hisp.dhis.schema.Schema) URI(java.net.URI)

Example 3 with RelativePropertyContext

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

the class DefaultGistService method describe.

@Override
public Map<String, ?> describe(GistQuery unplanned) {
    GistAccessControl access = createGistAccessControl();
    GistQuery planned = unplanned;
    Map<String, Object> description = new LinkedHashMap<>();
    description.put("unplanned", unplanned);
    try {
        planned = plan(unplanned);
    } catch (RuntimeException ex) {
        description.put("error.type", ex.getClass().getName());
        description.put("error.message", ex.getMessage());
        description.put("status", "planning-failed");
        return description;
    }
    RelativePropertyContext context = createPropertyContext(planned);
    // describe query
    description.put("planned.summary", planned.getFieldNames());
    description.put("planned", planned);
    // describe validation
    try {
        new GistValidator(planned, context, access).validateQuery();
    } catch (RuntimeException ex) {
        description.put("error.type", ex.getClass().getName());
        description.put("error.message", ex.getMessage());
        description.put("status", "validation-failed");
        return description;
    }
    // describe HQL queries
    if (access.canReadHQL()) {
        if (planned.isTotal()) {
            description.put("hql.count", createCountBuilder(planned, context, access, this::getUserGroupIdsByUserId).buildCountHQL());
        }
        GistBuilder fetchBuilder = createFetchBuilder(planned, context, access, this::getUserGroupIdsByUserId);
        description.put("hql.fetch", fetchBuilder.buildFetchHQL());
        Map<String, Object> params = new LinkedHashMap<>();
        fetchBuilder.addFetchParameters(params::put, this::parseFilterArgument);
        description.put("hql.parameters", params);
    }
    description.put("status", "ok");
    return description;
}
Also used : RelativePropertyContext(org.hisp.dhis.schema.RelativePropertyContext) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with RelativePropertyContext

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

the class GistBuilder method createPluckTransformerHQL.

private String createPluckTransformerHQL(int index, Field field, Property property) {
    String tableName = "t_" + index;
    RelativePropertyContext itemContext = context.switchedTo(property.getItemKlass());
    String propertyName = determineReferenceProperty(field, itemContext, true);
    if (propertyName == null || property.getItemKlass() == Period.class) {
        // give up
        return createSizeTransformerHQL(index, field, property, "");
    }
    String accessFilter = createAccessFilterHQL(itemContext, tableName);
    return String.format("(select array_agg(%1$s.%2$s) from %3$s %1$s where %1$s in elements(e.%4$s) and %5$s)", tableName, propertyName, property.getItemKlass().getSimpleName(), getMemberPath(field.getPropertyPath()), accessFilter);
}
Also used : RelativePropertyContext(org.hisp.dhis.schema.RelativePropertyContext) Period(org.hisp.dhis.period.Period)

Example 5 with RelativePropertyContext

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

the class GistBuilder method createReferenceFieldHQL.

private String createReferenceFieldHQL(int index, Field field) {
    String tableName = "t_" + index;
    String path = field.getPropertyPath();
    Property property = context.resolveMandatory(path);
    RelativePropertyContext fieldContext = context.switchedTo(property.getKlass());
    String propertyName = determineReferenceProperty(field, fieldContext, false);
    Schema propertySchema = fieldContext.getHome();
    if (propertyName == null || propertySchema.getRelativeApiEndpoint() == null) {
        // embed the object directly
        if (!property.isRequired()) {
            return String.format("(select %1$s from %2$s %1$s where %1$s = e.%3$s)", tableName, property.getKlass().getSimpleName(), getMemberPath(path));
        }
        return "e." + getMemberPath(path);
    }
    if (property.isIdentifiableObject()) {
        String endpointRoot = getEndpointRoot(property);
        if (endpointRoot != null && query.isReferences()) {
            int refIndex = fieldIndexByPath.get(Field.REFS_PATH);
            addTransformer(row -> addEndpointURL(row, refIndex, field, isNullOrEmpty(row[index]) ? null : toEndpointURL(endpointRoot, row[index])));
        }
    }
    if (field.getTransformation() == Transform.ID_OBJECTS) {
        addTransformer(row -> row[index] = toIdObject(row[index]));
    }
    if (property.isRequired()) {
        return "e." + getMemberPath(path) + "." + propertyName;
    }
    return String.format("(select %1$s.%2$s from %3$s %1$s where %1$s = e.%4$s)", tableName, propertyName, property.getKlass().getSimpleName(), getMemberPath(path));
}
Also used : RelativePropertyContext(org.hisp.dhis.schema.RelativePropertyContext) Schema(org.hisp.dhis.schema.Schema) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) GistLogic.isAccessProperty(org.hisp.dhis.gist.GistLogic.isAccessProperty) Property(org.hisp.dhis.schema.Property) GistLogic.isHrefProperty(org.hisp.dhis.gist.GistLogic.isHrefProperty)

Aggregations

RelativePropertyContext (org.hisp.dhis.schema.RelativePropertyContext)6 Schema (org.hisp.dhis.schema.Schema)2 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 URI (java.net.URI)1 LinkedHashMap (java.util.LinkedHashMap)1 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)1 GistLogic.isAccessProperty (org.hisp.dhis.gist.GistLogic.isAccessProperty)1 GistLogic.isHrefProperty (org.hisp.dhis.gist.GistLogic.isHrefProperty)1 Period (org.hisp.dhis.period.Period)1 Property (org.hisp.dhis.schema.Property)1