use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class DeletedObjectPostDeleteEventListener method onPostDelete.
@Override
public void onPostDelete(PostDeleteEvent event) {
if (MetadataObject.class.isInstance(event.getEntity())) {
IdentifiableObject identifiableObject = (IdentifiableObject) event.getEntity();
DeletedObject deletedObject = new DeletedObject(identifiableObject);
deletedObject.setDeletedBy(getUsername());
event.getSession().persist(deletedObject);
}
}
use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class DefaultCollectionService method clearCollectionItems.
@Override
@SuppressWarnings("unchecked")
public void clearCollectionItems(IdentifiableObject object, String pvProperty) throws WebMessageException, InvocationTargetException, IllegalAccessException {
Schema schema = schemaService.getDynamicSchema(object.getClass());
if (!schema.haveProperty(pvProperty)) {
throw new WebMessageException(WebMessageUtils.notFound("Property " + pvProperty + " does not exist on " + object.getClass().getName()));
}
Property property = schema.getProperty(pvProperty);
if (!property.isCollection() || !property.isIdentifiableObject()) {
throw new WebMessageException(WebMessageUtils.conflict("Only identifiable collections are allowed to be cleared."));
}
Collection<IdentifiableObject> collection = (Collection<IdentifiableObject>) property.getGetterMethod().invoke(object);
manager.refresh(object);
if (property.isOwner()) {
collection.clear();
manager.update(object);
} else {
for (IdentifiableObject itemObject : collection) {
Schema itemSchema = schemaService.getDynamicSchema(property.getItemKlass());
Property itemProperty = itemSchema.propertyByRole(property.getOwningRole());
Collection<IdentifiableObject> itemCollection = (Collection<IdentifiableObject>) itemProperty.getGetterMethod().invoke(itemObject);
itemCollection.remove(object);
manager.update(itemObject);
manager.refresh(itemObject);
}
}
}
use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class DefaultCollectionService method addCollectionItems.
@Override
@SuppressWarnings("unchecked")
public void addCollectionItems(IdentifiableObject object, String propertyName, List<IdentifiableObject> objects) throws Exception {
Schema schema = schemaService.getDynamicSchema(object.getClass());
if (!aclService.canUpdate(currentUserService.getCurrentUser(), object)) {
throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
}
if (!schema.haveProperty(propertyName)) {
throw new WebMessageException(WebMessageUtils.notFound("Property " + propertyName + " does not exist on " + object.getClass().getName()));
}
Property property = schema.getProperty(propertyName);
if (!property.isCollection() || !property.isIdentifiableObject()) {
throw new WebMessageException(WebMessageUtils.conflict("Only identifiable object collections can be added to."));
}
Collection<String> itemCodes = objects.stream().map(IdentifiableObject::getUid).collect(Collectors.toList());
if (itemCodes.isEmpty()) {
return;
}
List<? extends IdentifiableObject> items = manager.get(((Class<? extends IdentifiableObject>) property.getItemKlass()), itemCodes);
manager.refresh(object);
if (property.isOwner()) {
Collection<IdentifiableObject> collection = (Collection<IdentifiableObject>) property.getGetterMethod().invoke(object);
for (IdentifiableObject item : items) {
if (!collection.contains(item))
collection.add(item);
}
manager.update(object);
} else {
Schema owningSchema = schemaService.getDynamicSchema(property.getItemKlass());
Property owningProperty = owningSchema.propertyByRole(property.getOwningRole());
for (IdentifiableObject item : items) {
try {
Collection<IdentifiableObject> collection = (Collection<IdentifiableObject>) owningProperty.getGetterMethod().invoke(item);
if (!collection.contains(object)) {
collection.add(object);
manager.update(item);
}
} catch (Exception ex) {
}
}
}
dbmsManager.clearSession();
cacheManager.clearCache();
}
use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class DefaultObjectBundleService method handleCreates.
//-----------------------------------------------------------------------------------
// Utility Methods
//-----------------------------------------------------------------------------------
private TypeReport handleCreates(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() + ") Creating " + objects.size() + " object(s) of type " + objects.get(0).getClass().getSimpleName();
log.info(message);
if (bundle.hasTaskId()) {
notifier.notify(bundle.getTaskId(), message);
}
objects.forEach(object -> objectBundleHooks.forEach(hook -> hook.preCreate(object, bundle)));
for (int idx = 0; idx < objects.size(); idx++) {
IdentifiableObject object = objects.get(idx);
if (Preheat.isDefault(object))
continue;
ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
typeReport.addObjectReport(objectReport);
preheatService.connectReferences(object, bundle.getPreheat(), bundle.getPreheatIdentifier());
session.save(object);
if (MetadataObject.class.isInstance(object)) {
deletedObjectService.deleteDeletedObjects(new DeletedObjectQuery(object));
}
bundle.getPreheat().replace(bundle.getPreheatIdentifier(), object);
objectBundleHooks.forEach(hook -> hook.postCreate(object, bundle));
if (log.isDebugEnabled()) {
String msg = "(" + bundle.getUsername() + ") Created 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 DefaultObjectBundleService method handleUpdates.
private TypeReport handleUpdates(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() + ") Updating " + objects.size() + " object(s) of type " + objects.get(0).getClass().getSimpleName();
log.info(message);
if (bundle.hasTaskId()) {
notifier.notify(bundle.getTaskId(), message);
}
objects.forEach(object -> {
IdentifiableObject persistedObject = bundle.getPreheat().get(bundle.getPreheatIdentifier(), object);
objectBundleHooks.forEach(hook -> hook.preUpdate(object, persistedObject, bundle));
});
for (int idx = 0; idx < objects.size(); idx++) {
IdentifiableObject object = objects.get(idx);
IdentifiableObject persistedObject = bundle.getPreheat().get(bundle.getPreheatIdentifier(), object);
if (Preheat.isDefault(object))
continue;
ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
typeReport.addObjectReport(objectReport);
preheatService.connectReferences(object, bundle.getPreheat(), bundle.getPreheatIdentifier());
if (bundle.getMergeMode() != MergeMode.NONE) {
mergeService.merge(new MergeParams<>(object, persistedObject).setMergeMode(bundle.getMergeMode()).setSkipSharing(bundle.isSkipSharing()));
}
session.update(persistedObject);
if (MetadataObject.class.isInstance(object)) {
deletedObjectService.deleteDeletedObjects(new DeletedObjectQuery(object));
}
objectBundleHooks.forEach(hook -> hook.postUpdate(persistedObject, bundle));
bundle.getPreheat().replace(bundle.getPreheatIdentifier(), persistedObject);
if (log.isDebugEnabled()) {
String msg = "(" + bundle.getUsername() + ") Updated object '" + bundle.getPreheatIdentifier().getIdentifiersWithName(persistedObject) + "'";
log.debug(msg);
}
if (FlushMode.OBJECT == bundle.getFlushMode())
session.flush();
}
return typeReport;
}
Aggregations