use of org.hisp.dhis.query.Query in project dhis2-core by dhis2.
the class DefaultQueryPlanner method getQuery.
/**
* @param query Query
* @return Query instance
*/
private Query getQuery(Query query, boolean persistedOnly) {
Query pQuery = Query.from(query.getSchema(), query.getRootJunction().getType());
Iterator<Criterion> iterator = query.getCriterions().iterator();
while (iterator.hasNext()) {
org.hisp.dhis.query.Criterion criterion = iterator.next();
if (Junction.class.isInstance(criterion)) {
Junction junction = handleJunction(pQuery, (Junction) criterion, persistedOnly);
if (!junction.getCriterions().isEmpty()) {
pQuery.getAliases().addAll(junction.getAliases());
pQuery.add(junction);
}
if (((Junction) criterion).getCriterions().isEmpty()) {
iterator.remove();
}
} else if (Restriction.class.isInstance(criterion)) {
Restriction restriction = (Restriction) criterion;
restriction.setQueryPath(getQueryPath(query.getSchema(), restriction.getPath()));
if (restriction.getQueryPath().isPersisted() && !restriction.getQueryPath().haveAlias()) {
pQuery.getAliases().addAll(Arrays.asList(((Restriction) criterion).getQueryPath().getAlias()));
pQuery.getCriterions().add(criterion);
iterator.remove();
}
}
}
if (query.ordersPersisted()) {
pQuery.addOrders(query.getOrders());
query.clearOrders();
}
return pQuery;
}
use of org.hisp.dhis.query.Query in project dhis2-core by dhis2.
the class DefaultPreheatService method preheat.
@Override
@SuppressWarnings("unchecked")
public Preheat preheat(PreheatParams params) {
Timer timer = new SystemTimer().start();
Preheat preheat = new Preheat();
preheat.setUser(params.getUser());
preheat.setDefaults(manager.getDefaults());
if (preheat.getUser() == null) {
preheat.setUser(currentUserService.getCurrentUser());
}
preheat.put(PreheatIdentifier.UID, preheat.getUser());
preheat.put(PreheatIdentifier.CODE, preheat.getUser());
for (Class<? extends IdentifiableObject> klass : params.getObjects().keySet()) {
params.getObjects().get(klass).stream().filter(identifiableObject -> StringUtils.isEmpty(identifiableObject.getUid())).forEach(identifiableObject -> ((BaseIdentifiableObject) identifiableObject).setUid(CodeGenerator.generateUid()));
}
Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> uniqueCollectionMap = new HashMap<>();
Set<Class<? extends IdentifiableObject>> klasses = new HashSet<>(params.getObjects().keySet());
if (PreheatMode.ALL == params.getPreheatMode()) {
if (params.getClasses().isEmpty()) {
schemaService.getMetadataSchemas().stream().filter(Schema::isIdentifiableObject).forEach(schema -> params.getClasses().add((Class<? extends IdentifiableObject>) schema.getKlass()));
}
for (Class<? extends IdentifiableObject> klass : params.getClasses()) {
Query query = Query.from(schemaService.getDynamicSchema(klass));
query.setUser(preheat.getUser());
List<? extends IdentifiableObject> objects = queryService.query(query);
if (PreheatIdentifier.UID == params.getPreheatIdentifier() || PreheatIdentifier.AUTO == params.getPreheatIdentifier()) {
preheat.put(PreheatIdentifier.UID, objects);
}
if (PreheatIdentifier.CODE == params.getPreheatIdentifier() || PreheatIdentifier.AUTO == params.getPreheatIdentifier()) {
preheat.put(PreheatIdentifier.CODE, objects);
}
if (klasses.contains(klass) && !objects.isEmpty()) {
uniqueCollectionMap.put(klass, new ArrayList<>(objects));
}
}
} else if (PreheatMode.REFERENCE == params.getPreheatMode()) {
Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> references = collectReferences(params.getObjects());
Map<Class<? extends IdentifiableObject>, Set<String>> uidMap = references.get(PreheatIdentifier.UID);
Map<Class<? extends IdentifiableObject>, Set<String>> codeMap = references.get(PreheatIdentifier.CODE);
if (uidMap != null && (PreheatIdentifier.UID == params.getPreheatIdentifier() || PreheatIdentifier.AUTO == params.getPreheatIdentifier())) {
for (Class<? extends IdentifiableObject> klass : uidMap.keySet()) {
List<List<String>> identifiers = Lists.partition(Lists.newArrayList(uidMap.get(klass)), 20000);
if (!identifiers.isEmpty()) {
for (List<String> ids : identifiers) {
Query query = Query.from(schemaService.getDynamicSchema(klass));
query.setUser(preheat.getUser());
query.add(Restrictions.in("id", ids));
List<? extends IdentifiableObject> objects = queryService.query(query);
preheat.put(PreheatIdentifier.UID, objects);
}
}
}
}
if (codeMap != null && (PreheatIdentifier.CODE == params.getPreheatIdentifier() || PreheatIdentifier.AUTO == params.getPreheatIdentifier())) {
for (Class<? extends IdentifiableObject> klass : codeMap.keySet()) {
List<List<String>> identifiers = Lists.partition(Lists.newArrayList(codeMap.get(klass)), 20000);
if (!identifiers.isEmpty()) {
for (List<String> ids : identifiers) {
Query query = Query.from(schemaService.getDynamicSchema(klass));
query.setUser(preheat.getUser());
query.add(Restrictions.in("code", ids));
List<? extends IdentifiableObject> objects = queryService.query(query);
preheat.put(PreheatIdentifier.CODE, objects);
}
}
}
}
for (Class<? extends IdentifiableObject> klass : klasses) {
Query query = Query.from(schemaService.getDynamicSchema(klass));
query.setUser(preheat.getUser());
List<? extends IdentifiableObject> objects = queryService.query(query);
if (!objects.isEmpty()) {
uniqueCollectionMap.put(klass, new ArrayList<>(objects));
}
}
}
if (uniqueCollectionMap.containsKey(User.class)) {
List<IdentifiableObject> userCredentials = new ArrayList<>();
for (IdentifiableObject identifiableObject : uniqueCollectionMap.get(User.class)) {
User user = (User) identifiableObject;
if (user.getUserCredentials() != null) {
userCredentials.add(user.getUserCredentials());
}
}
uniqueCollectionMap.put(UserCredentials.class, userCredentials);
}
preheat.setUniquenessMap(collectUniqueness(uniqueCollectionMap));
// add preheat placeholders for objects that will be created and set mandatory/unique attributes
for (Class<? extends IdentifiableObject> klass : params.getObjects().keySet()) {
List<IdentifiableObject> objects = params.getObjects().get(klass);
preheat.put(params.getPreheatIdentifier(), objects);
}
handleAttributes(params.getObjects(), preheat);
handleSecurity(params.getObjects(), params.getPreheatIdentifier(), preheat);
periodStore.getAll().forEach(period -> preheat.getPeriodMap().put(period.getName(), period));
periodStore.getAllPeriodTypes().forEach(periodType -> preheat.getPeriodTypeMap().put(periodType.getName(), periodType));
log.info("(" + preheat.getUsername() + ") Import:Preheat[" + params.getPreheatMode() + "] took " + timer.toString());
return preheat;
}
use of org.hisp.dhis.query.Query in project dhis2-core by dhis2.
the class AbstractCrudController method getEntityList.
@SuppressWarnings("unchecked")
protected List<T> getEntityList(WebMetadata metadata, WebOptions options, List<String> filters, List<Order> orders) throws QueryParserException {
List<T> entityList;
Query query = queryService.getQueryFromUrl(getEntityClass(), filters, orders, options.getRootJunction());
query.setDefaultOrder();
if (options.getOptions().containsKey("query")) {
entityList = Lists.newArrayList(manager.filter(getEntityClass(), options.getOptions().get("query")));
} else {
entityList = (List<T>) queryService.query(query);
}
return entityList;
}
use of org.hisp.dhis.query.Query in project dhis2-core by dhis2.
the class DimensionController method getItems.
@SuppressWarnings("unchecked")
@RequestMapping(value = "/{uid}/items", method = RequestMethod.GET)
@ResponseBody
public RootNode getItems(@PathVariable String uid, @RequestParam Map<String, String> parameters, Model model, HttpServletRequest request, HttpServletResponse response) throws QueryParserException {
List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
List<String> filters = Lists.newArrayList(contextService.getParameterValues("filter"));
if (fields.isEmpty()) {
fields.addAll(Preset.defaultPreset().getFields());
}
List<DimensionalItemObject> items = dimensionService.getCanReadDimensionItems(uid);
Query query = queryService.getQueryFromUrl(getEntityClass(), filters, new ArrayList<>());
query.setObjects(items);
query.setDefaultOrder();
items = (List<DimensionalItemObject>) queryService.query(query);
RootNode rootNode = NodeUtils.createMetadata();
CollectionNode collectionNode = rootNode.addChild(fieldFilterService.filter(getEntityClass(), items, fields));
collectionNode.setName("items");
for (Node node : collectionNode.getChildren()) {
((AbstractNode) node).setName("item");
}
return rootNode;
}
use of org.hisp.dhis.query.Query in project dhis2-core by dhis2.
the class DimensionController method getEntityList.
// -------------------------------------------------------------------------
// Controller
// -------------------------------------------------------------------------
@Override
@SuppressWarnings("unchecked")
@ResponseBody
protected List<DimensionalObject> getEntityList(WebMetadata metadata, WebOptions options, List<String> filters, List<Order> orders) throws QueryParserException {
List<DimensionalObject> dimensionalObjects;
Query query = queryService.getQueryFromUrl(DimensionalObject.class, filters, orders, options.getRootJunction());
query.setDefaultOrder();
query.setObjects(dimensionService.getAllDimensions());
dimensionalObjects = (List<DimensionalObject>) queryService.query(query);
return dimensionalObjects;
}
Aggregations