use of org.hisp.dhis.common.IdentifiableObject 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;
}
use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class DefaultObjectBundleService method commit.
@Override
public ObjectBundleCommitReport commit(ObjectBundle bundle) {
Map<Class<?>, TypeReport> typeReports = new HashMap<>();
ObjectBundleCommitReport commitReport = new ObjectBundleCommitReport(typeReports);
if (ObjectBundleMode.VALIDATE == bundle.getObjectBundleMode()) {
// skip if validate only
return commitReport;
}
List<Class<? extends IdentifiableObject>> klasses = getSortedClasses(bundle);
Session session = sessionFactory.getCurrentSession();
objectBundleHooks.forEach(hook -> hook.preCommit(bundle));
for (Class<? extends IdentifiableObject> klass : klasses) {
List<IdentifiableObject> nonPersistedObjects = bundle.getObjects(klass, false);
List<IdentifiableObject> persistedObjects = bundle.getObjects(klass, true);
objectBundleHooks.forEach(hook -> hook.preTypeImport(klass, nonPersistedObjects, bundle));
if (bundle.getImportMode().isCreateAndUpdate()) {
TypeReport typeReport = new TypeReport(klass);
typeReport.merge(handleCreates(session, klass, nonPersistedObjects, bundle));
typeReport.merge(handleUpdates(session, klass, persistedObjects, bundle));
typeReports.put(klass, typeReport);
} else if (bundle.getImportMode().isCreate()) {
typeReports.put(klass, handleCreates(session, klass, nonPersistedObjects, bundle));
} else if (bundle.getImportMode().isUpdate()) {
typeReports.put(klass, handleUpdates(session, klass, persistedObjects, bundle));
} else if (bundle.getImportMode().isDelete()) {
typeReports.put(klass, handleDeletes(session, klass, persistedObjects, bundle));
}
objectBundleHooks.forEach(hook -> hook.postTypeImport(klass, persistedObjects, bundle));
if (FlushMode.AUTO == bundle.getFlushMode())
session.flush();
}
if (!bundle.getImportMode().isDelete()) {
objectBundleHooks.forEach(hook -> hook.postCommit(bundle));
}
dbmsManager.clearSession();
cacheManager.clearCache();
bundle.setObjectBundleStatus(ObjectBundleStatus.COMMITTED);
return commitReport;
}
use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class DefaultObjectBundleService method handleDeletes.
private TypeReport handleDeletes(Session session, Class<? extends IdentifiableObject> klass, List<IdentifiableObject> objects, ObjectBundle bundle) {
TypeReport typeReport = new TypeReport(klass);
if (objects.isEmpty()) {
return typeReport;
}
String message = "(" + bundle.getUsername() + ") Deleting " + objects.size() + " object(s) of type " + objects.get(0).getClass().getSimpleName();
log.info(message);
if (bundle.hasTaskId()) {
notifier.notify(bundle.getTaskId(), message);
}
List<IdentifiableObject> persistedObjects = bundle.getPreheat().getAll(bundle.getPreheatIdentifier(), objects);
for (int idx = 0; idx < persistedObjects.size(); idx++) {
IdentifiableObject object = persistedObjects.get(idx);
ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
typeReport.addObjectReport(objectReport);
objectBundleHooks.forEach(hook -> hook.preDelete(object, bundle));
manager.delete(object, bundle.getUser());
bundle.getPreheat().remove(bundle.getPreheatIdentifier(), object);
if (log.isDebugEnabled()) {
String msg = "(" + bundle.getUsername() + ") Deleted object '" + bundle.getPreheatIdentifier().getIdentifiersWithName(object) + "'";
log.debug(msg);
}
if (FlushMode.OBJECT == bundle.getFlushMode())
session.flush();
}
return typeReport;
}
use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class DefaultAttributeService method validateAttributeValues.
@Override
public <T extends IdentifiableObject> List<ErrorReport> validateAttributeValues(T object, Set<AttributeValue> attributeValues) {
List<ErrorReport> errorReports = new ArrayList<>();
if (attributeValues.isEmpty()) {
return errorReports;
}
Map<String, AttributeValue> attributeValueMap = attributeValues.stream().filter(av -> av.getAttribute() != null).collect(Collectors.toMap(av -> av.getAttribute().getUid(), av -> av));
Iterator<AttributeValue> iterator = object.getAttributeValues().iterator();
List<Attribute> mandatoryAttributes = getMandatoryAttributes(object.getClass());
while (iterator.hasNext()) {
AttributeValue attributeValue = iterator.next();
if (attributeValue.getAttribute() != null && attributeValueMap.containsKey(attributeValue.getAttribute().getUid())) {
AttributeValue av = attributeValueMap.get(attributeValue.getAttribute().getUid());
if (attributeValue.isUnique()) {
if (!manager.isAttributeValueUnique(object.getClass(), object, attributeValue.getAttribute(), av.getValue())) {
errorReports.add(new ErrorReport(Attribute.class, ErrorCode.E4009, attributeValue.getAttribute().getUid(), av.getValue()).setMainId(attributeValue.getAttribute().getUid()).setErrorProperty("value"));
}
}
attributeValueMap.remove(attributeValue.getAttribute().getUid());
mandatoryAttributes.remove(attributeValue.getAttribute());
}
}
for (String uid : attributeValueMap.keySet()) {
AttributeValue attributeValue = attributeValueMap.get(uid);
if (attributeValue.getAttribute() != null && !attributeValue.getAttribute().getSupportedClasses().contains(object.getClass())) {
errorReports.add(new ErrorReport(Attribute.class, ErrorCode.E4010, attributeValue.getAttribute().getUid(), object.getClass().getSimpleName()).setMainId(uid).setErrorProperty("id"));
} else {
mandatoryAttributes.remove(attributeValue.getAttribute());
}
}
mandatoryAttributes.forEach(att -> errorReports.add(new ErrorReport(Attribute.class, ErrorCode.E4011, att.getUid()).setMainId(att.getUid())));
return errorReports;
}
use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class DefaultRenderService method fromMetadata.
@Override
@SuppressWarnings("unchecked")
public Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> fromMetadata(InputStream inputStream, RenderFormat format) throws IOException {
Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> map = new HashMap<>();
ObjectMapper mapper;
if (RenderFormat.JSON == format) {
mapper = jsonMapper;
} else if (RenderFormat.XML == format) {
throw new IllegalArgumentException("XML format is not supported.");
} else {
return map;
}
JsonNode rootNode = mapper.readTree(inputStream);
Iterator<String> fieldNames = rootNode.fieldNames();
while (fieldNames.hasNext()) {
String fieldName = fieldNames.next();
JsonNode node = rootNode.get(fieldName);
Schema schema = schemaService.getSchemaByPluralName(fieldName);
if (schema == null || !schema.isIdentifiableObject()) {
log.info("Skipping unknown property '" + fieldName + "'.");
continue;
}
if (!schema.isMetadata()) {
log.debug("Skipping non-metadata property `" + fieldName + "`.");
continue;
}
List<IdentifiableObject> collection = new ArrayList<>();
for (JsonNode item : node) {
IdentifiableObject value = mapper.treeToValue(item, (Class<? extends IdentifiableObject>) schema.getKlass());
if (value != null)
collection.add(value);
}
map.put((Class<? extends IdentifiableObject>) schema.getKlass(), collection);
}
return map;
}
Aggregations