use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method checkDuplicateIds.
private TypeReport checkDuplicateIds(Class<? extends IdentifiableObject> klass, List<IdentifiableObject> persistedObjects, List<IdentifiableObject> nonPersistedObjects, Preheat preheat, PreheatIdentifier identifier) {
TypeReport typeReport = new TypeReport(klass);
if (persistedObjects.isEmpty() && nonPersistedObjects.isEmpty()) {
return typeReport;
}
Map<Class<?>, String> idMap = new HashMap<>();
Iterator<IdentifiableObject> iterator = persistedObjects.iterator();
int idx = 0;
while (iterator.hasNext()) {
IdentifiableObject object = iterator.next();
if (idMap.containsKey(object.getClass()) && idMap.get(object.getClass()).equals(object.getUid())) {
ErrorReport errorReport = new ErrorReport(object.getClass(), ErrorCode.E5004, object.getUid(), object.getClass()).setMainId(object.getUid()).setErrorProperty("id");
ObjectReport objectReport = new ObjectReport(object.getClass(), idx);
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReport(errorReport);
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
} else {
idMap.put(object.getClass(), object.getUid());
}
idx++;
}
iterator = nonPersistedObjects.iterator();
idx = 0;
while (iterator.hasNext()) {
IdentifiableObject object = iterator.next();
if (idMap.containsKey(object.getClass()) && idMap.get(object.getClass()).equals(object.getUid())) {
ErrorReport errorReport = new ErrorReport(object.getClass(), ErrorCode.E5004, object.getUid(), object.getClass()).setMainId(object.getUid()).setErrorProperty("id");
ObjectReport objectReport = new ObjectReport(object.getClass(), idx);
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReport(errorReport);
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
} else {
idMap.put(object.getClass(), object.getUid());
}
idx++;
}
return typeReport;
}
use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method checkUniqueness.
private List<ErrorReport> checkUniqueness(Class<? extends IdentifiableObject> klass, IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier) {
List<ErrorReport> errorReports = new ArrayList<>();
if (object == null || Preheat.isDefault(object))
return errorReports;
if (!preheat.getUniquenessMap().containsKey(object.getClass())) {
preheat.getUniquenessMap().put(object.getClass(), new HashMap<>());
}
Map<String, Map<Object, String>> uniquenessMap = preheat.getUniquenessMap().get(object.getClass());
Schema schema = schemaService.getDynamicSchema(object.getClass());
List<Property> uniqueProperties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && p.isUnique() && p.isSimple()).collect(Collectors.toList());
uniqueProperties.forEach(property -> {
if (!uniquenessMap.containsKey(property.getName())) {
uniquenessMap.put(property.getName(), new HashMap<>());
}
Object value = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
if (value != null) {
String persistedUid = uniquenessMap.get(property.getName()).get(value);
if (persistedUid != null) {
if (!object.getUid().equals(persistedUid)) {
errorReports.add(new ErrorReport(object.getClass(), ErrorCode.E5003, property.getName(), value, identifier.getIdentifiersWithName(object), persistedUid).setMainId(persistedUid).setErrorProperty(property.getName()));
}
} else {
uniquenessMap.get(property.getName()).put(value, object.getUid());
}
}
});
return errorReports;
}
use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method checkMandatoryAttributes.
private TypeReport checkMandatoryAttributes(Class<? extends IdentifiableObject> klass, List<IdentifiableObject> objects, Preheat preheat, PreheatIdentifier identifier) {
TypeReport typeReport = new TypeReport(klass);
Schema schema = schemaService.getDynamicSchema(klass);
if (objects.isEmpty() || !schema.havePersistedProperty("attributeValues")) {
return typeReport;
}
Iterator<IdentifiableObject> iterator = objects.iterator();
int idx = 0;
while (iterator.hasNext()) {
IdentifiableObject object = iterator.next();
List<ErrorReport> errorReports = checkMandatoryAttributes(klass, object, preheat, identifier);
if (!errorReports.isEmpty()) {
ObjectReport objectReport = new ObjectReport(object.getClass(), idx);
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReports(errorReports);
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
}
idx++;
}
return typeReport;
}
use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class DefaultMetadataExportService method getMetadataWithDependenciesAsNode.
@Override
public RootNode getMetadataWithDependenciesAsNode(IdentifiableObject object) {
RootNode rootNode = NodeUtils.createMetadata();
rootNode.addChild(new SimpleNode("date", new Date(), true));
SetMap<Class<? extends IdentifiableObject>, IdentifiableObject> metadata = getMetadataWithDependencies(object);
for (Class<? extends IdentifiableObject> klass : metadata.keySet()) {
rootNode.addChild(fieldFilterService.filter(klass, Lists.newArrayList(metadata.get(klass)), Lists.newArrayList(":owner")));
}
return rootNode;
}
use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class DefaultMetadataExportService method getMetadataAsNode.
@Override
public RootNode getMetadataAsNode(MetadataExportParams params) {
RootNode rootNode = NodeUtils.createMetadata();
SystemInfo systemInfo = systemService.getSystemInfo();
ComplexNode system = rootNode.addChild(new ComplexNode("system"));
system.addChild(new SimpleNode("id", systemInfo.getSystemId()));
system.addChild(new SimpleNode("rev", systemInfo.getRevision()));
system.addChild(new SimpleNode("version", systemInfo.getVersion()));
system.addChild(new SimpleNode("date", systemInfo.getServerDate()));
Map<Class<? extends IdentifiableObject>, List<? extends IdentifiableObject>> metadata = getMetadata(params);
for (Class<? extends IdentifiableObject> klass : metadata.keySet()) {
rootNode.addChild(fieldFilterService.filter(klass, metadata.get(klass), params.getFields(klass)));
}
return rootNode;
}
Aggregations