use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class DefaultQueryPlanner method getQueryPath.
@Override
public QueryPath getQueryPath(Schema schema, String path) {
Schema curSchema = schema;
Property curProperty = null;
boolean persisted = true;
List<String> alias = new ArrayList<>();
String[] pathComponents = path.split("\\.");
if (pathComponents.length == 0) {
return null;
}
for (int idx = 0; idx < pathComponents.length; idx++) {
String name = pathComponents[idx];
curProperty = curSchema.getProperty(name);
if (curProperty == null) {
throw new RuntimeException("Invalid path property: " + name);
}
if (!curProperty.isPersisted()) {
persisted = false;
}
if ((!curProperty.isSimple() && idx == pathComponents.length - 1)) {
return new QueryPath(curProperty, persisted, alias.toArray(new String[] {}));
}
if (curProperty.isCollection()) {
curSchema = schemaService.getDynamicSchema(curProperty.getItemKlass());
alias.add(curProperty.getFieldName());
} else if (!curProperty.isSimple()) {
curSchema = schemaService.getDynamicSchema(curProperty.getKlass());
alias.add(curProperty.getFieldName());
} else {
return new QueryPath(curProperty, persisted, alias.toArray(new String[] {}));
}
}
return new QueryPath(curProperty, persisted, alias.toArray(new String[] {}));
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class DefaultQueryPlanner method getQueryPath.
@Override
public Path<?> getQueryPath(Root<?> root, Schema schema, String path) {
Schema curSchema = schema;
Property curProperty;
String[] pathComponents = path.split("\\.");
Path<?> currentPath = root;
if (pathComponents.length == 0) {
return null;
}
for (int idx = 0; idx < pathComponents.length; idx++) {
String name = pathComponents[idx];
curProperty = curSchema.getProperty(name);
if (curProperty == null) {
throw new RuntimeException("Invalid path property: " + name);
}
if ((!curProperty.isSimple() && idx == pathComponents.length - 1)) {
return root.join(curProperty.getFieldName());
}
if (curProperty.isCollection()) {
currentPath = root.join(curProperty.getFieldName());
curSchema = schemaService.getDynamicSchema(curProperty.getItemKlass());
} else if (!curProperty.isSimple()) {
curSchema = schemaService.getDynamicSchema(curProperty.getKlass());
currentPath = root.join(curProperty.getFieldName());
} else {
return currentPath.get(curProperty.getFieldName());
}
}
return currentPath;
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class DefaultPreheatService method collectObjectReferences.
@SuppressWarnings("unchecked")
private Map<Class<?>, Map<String, Map<String, Object>>> collectObjectReferences(Map<Class<?>, List<?>> objects) {
Map<Class<?>, Map<String, Map<String, Object>>> map = new HashMap<>();
if (objects.isEmpty()) {
return map;
}
// clone objects list, we don't want to modify it
Map<Class<?>, List<?>> targets = new HashMap<>(objects);
collectScanTargets(targets);
for (Class<?> objectClass : targets.keySet()) {
Schema schema = schemaService.getDynamicSchema(objectClass);
if (!schema.isIdentifiableObject()) {
continue;
}
List<Property> properties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType())).collect(Collectors.toList());
List<IdentifiableObject> identifiableObjects = (List<IdentifiableObject>) targets.get(objectClass);
Map<String, Map<String, Object>> refMap = new HashMap<>();
map.put(objectClass, refMap);
for (IdentifiableObject object : identifiableObjects) {
refMap.put(object.getUid(), new HashMap<>());
properties.forEach(p -> {
if (!p.isCollection()) {
IdentifiableObject reference = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
if (reference != null) {
try {
IdentifiableObject identifiableObject = (IdentifiableObject) p.getKlass().newInstance();
mergeService.merge(new MergeParams<>(reference, identifiableObject));
refMap.get(object.getUid()).put(p.getName(), identifiableObject);
} catch (InstantiationException | IllegalAccessException ignored) {
}
}
} else {
Collection<IdentifiableObject> refObjects = ReflectionUtils.newCollectionInstance(p.getKlass());
Collection<IdentifiableObject> references = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
if (references != null) {
for (IdentifiableObject reference : references) {
if (reference == null) {
continue;
}
try {
IdentifiableObject identifiableObject = (IdentifiableObject) p.getItemKlass().newInstance();
mergeService.merge(new MergeParams<>(reference, identifiableObject));
refObjects.add(identifiableObject);
} catch (InstantiationException | IllegalAccessException ignored) {
}
}
}
refMap.get(object.getUid()).put(p.getCollectionName(), refObjects);
}
});
}
}
return map;
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class DefaultPatchService method calculateMutation.
@SuppressWarnings("unchecked")
private List<Mutation> calculateMutation(String path, Property property, Object source, Object target) {
Object sourceValue = ReflectionUtils.invokeMethod(source, property.getGetterMethod());
Object targetValue = ReflectionUtils.invokeMethod(target, property.getGetterMethod());
List<Mutation> mutations = new ArrayList<>();
if (sourceValue == null && targetValue == null) {
return mutations;
}
if (targetValue == null || sourceValue == null) {
return Lists.newArrayList(new Mutation(path, targetValue));
}
if (property.isCollection() && property.isIdentifiableObject() && !property.isEmbeddedObject()) {
Collection<Object> addCollection = ReflectionUtils.newCollectionInstance(property.getKlass());
Collection<Object> sourceCollection = ((Collection<Object>) sourceValue).stream().filter(Objects::nonNull).map(o -> ((IdentifiableObject) o).getUid()).collect(Collectors.toList());
Collection<Object> targetCollection = ((Collection<Object>) targetValue).stream().filter(Objects::nonNull).map(o -> ((IdentifiableObject) o).getUid()).collect(Collectors.toList());
for (Object o : targetCollection) {
if (!sourceCollection.contains(o)) {
addCollection.add(o);
} else {
sourceCollection.remove(o);
}
}
if (!addCollection.isEmpty()) {
mutations.add(new Mutation(path, addCollection));
}
Collection<Object> delCollection = ReflectionUtils.newCollectionInstance(property.getKlass());
delCollection.addAll(sourceCollection);
if (!delCollection.isEmpty()) {
mutations.add(new Mutation(path, delCollection, Mutation.Operation.DELETION));
}
} else if (property.isCollection() && !property.isEmbeddedObject() && !property.isIdentifiableObject()) {
List<Object> sourceCollection = new ArrayList<>((Collection<Object>) sourceValue);
Collection<Object> targetCollection = (Collection<Object>) targetValue;
Collection<Object> addCollection = ReflectionUtils.newCollectionInstance(property.getKlass());
for (Object o : targetCollection) {
if (!sourceCollection.contains(o)) {
addCollection.add(o);
} else {
sourceCollection.remove(o);
}
}
if (!addCollection.isEmpty()) {
mutations.add(new Mutation(path, addCollection));
}
Collection<Object> delCollection = ReflectionUtils.newCollectionInstance(property.getKlass());
delCollection.addAll(sourceCollection);
if (!delCollection.isEmpty()) {
mutations.add(new Mutation(path, delCollection, Mutation.Operation.DELETION));
}
} else if (property.isSimple() || property.isEmbeddedObject()) {
if (!targetValue.equals(sourceValue)) {
return Lists.newArrayList(new Mutation(path, targetValue));
}
}
return mutations;
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class DefaultPatchService method applyMutation.
private void applyMutation(Mutation mutation, Schema schema, Object target) {
String path = mutation.getPath();
String[] paths = path.split("\\.");
Schema currentSchema = schema;
Property currentProperty = null;
Object currentTarget = target;
for (int i = 0; i < paths.length; i++) {
if (!currentSchema.haveProperty(paths[i])) {
return;
}
currentProperty = currentSchema.getProperty(paths[i]);
if (currentProperty == null) {
return;
}
if ((currentProperty.isSimple() && !currentProperty.isCollection()) && i != (paths.length - 1)) {
return;
}
if (currentProperty.isCollection()) {
currentSchema = schemaService.getDynamicSchema(currentProperty.getItemKlass());
} else {
currentSchema = schemaService.getDynamicSchema(currentProperty.getKlass());
}
if (i < (paths.length - 1)) {
currentTarget = ReflectionUtils.invokeMethod(currentTarget, currentProperty.getGetterMethod());
}
}
if (currentSchema != null && currentProperty != null) {
applyMutation(mutation, currentProperty, currentTarget);
}
}
Aggregations