use of org.hisp.dhis.deletedobject.DeletedObject in project dhis2-core by dhis2.
the class HibernateDeletedObjectStore method count.
@Override
public int count(DeletedObjectQuery query) {
CriteriaBuilder builder = getCurrentSession().getCriteriaBuilder();
CriteriaQuery<Long> criteriaQuery = builder.createQuery(Long.class);
Root<DeletedObject> root = criteriaQuery.from(DeletedObject.class);
Predicate predicate = buildCriteria(builder, root, query);
criteriaQuery.select(builder.countDistinct(root));
if (!predicate.getExpressions().isEmpty())
criteriaQuery.where(predicate);
Query<Long> typedQuery = getCurrentSession().createQuery(criteriaQuery);
return typedQuery.getSingleResult().intValue();
}
use of org.hisp.dhis.deletedobject.DeletedObject in project dhis2-core by dhis2.
the class DeletedObjectPostInsertEventListener method onPostInsert.
@Override
public void onPostInsert(PostInsertEvent event) {
if (IdentifiableObject.class.isInstance(event.getEntity()) && MetadataObject.class.isInstance(event.getEntity()) && !EmbeddedObject.class.isInstance(event.getEntity())) {
StatelessSession session = event.getPersister().getFactory().openStatelessSession();
session.beginTransaction();
try {
List<DeletedObject> deletedObjects = deletedObjectService.getDeletedObjects(new DeletedObjectQuery((IdentifiableObject) event.getEntity()));
deletedObjects.forEach(session::delete);
knownTransactionsService.registerEvent(event);
session.getTransaction().commit();
} catch (Exception ex) {
log.error("Failed to delete DeletedObject for:" + event.getEntity());
session.getTransaction().rollback();
} finally {
session.close();
}
}
}
use of org.hisp.dhis.deletedobject.DeletedObject in project dhis2-core by dhis2.
the class DeletedObjectPostDeleteEventListener method onPostDelete.
@Override
public void onPostDelete(PostDeleteEvent event) {
if (IdentifiableObject.class.isInstance(event.getEntity()) && MetadataObject.class.isInstance(event.getEntity()) && !EmbeddedObject.class.isInstance(event.getEntity())) {
IdentifiableObject identifiableObject = (IdentifiableObject) event.getEntity();
DeletedObject deletedObject = new DeletedObject(identifiableObject);
deletedObject.setDeletedBy(getUsername());
StatelessSession session = event.getPersister().getFactory().openStatelessSession();
session.beginTransaction();
knownTransactionsService.registerEvent(event);
try {
session.insert(deletedObject);
session.getTransaction().commit();
} catch (Exception ex) {
log.error("Failed to save DeletedObject: " + deletedObject);
session.getTransaction().rollback();
} finally {
session.close();
}
}
}
use of org.hisp.dhis.deletedobject.DeletedObject in project dhis2-core by dhis2.
the class HibernateDeletedObjectStore method query.
@Override
public List<DeletedObject> query(DeletedObjectQuery query) {
CriteriaBuilder builder = getCurrentSession().getCriteriaBuilder();
CriteriaQuery<DeletedObject> criteriaQuery = builder.createQuery(DeletedObject.class);
Root<DeletedObject> root = criteriaQuery.from(DeletedObject.class);
Predicate predicate = buildCriteria(builder, root, query);
criteriaQuery.select(root);
if (!predicate.getExpressions().isEmpty())
criteriaQuery.where(predicate);
Query<DeletedObject> typedQuery = getCurrentSession().createQuery(criteriaQuery);
if (!query.isSkipPaging()) {
Pager pager = query.getPager();
typedQuery.setFirstResult(pager.getOffset());
typedQuery.setMaxResults(pager.getPageSize());
}
return typedQuery.list();
}
use of org.hisp.dhis.deletedobject.DeletedObject in project dhis2-core by dhis2.
the class DeletedObjectController method getDeletedObjects.
@GetMapping
@PreAuthorize("hasRole('ALL')")
public RootNode getDeletedObjects(DeletedObjectQuery query) {
List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
int totalDeletedObjects = deletedObjectService.countDeletedObjects(query);
query.setTotal(totalDeletedObjects);
if (fields.isEmpty()) {
fields.addAll(Preset.ALL.getFields());
}
List<DeletedObject> deletedObjects = deletedObjectService.getDeletedObjects(query);
RootNode rootNode = NodeUtils.createMetadata();
if (!query.isSkipPaging()) {
query.setTotal(totalDeletedObjects);
rootNode.addChild(NodeUtils.createPager(query.getPager()));
}
rootNode.addChild(fieldFilterService.toCollectionNode(DeletedObject.class, new FieldFilterParams(deletedObjects, fields)));
return rootNode;
}
Aggregations