Search in sources :

Example 1 with DeletedObject

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) DeletedObject(org.hisp.dhis.deletedobject.DeletedObject) Predicate(javax.persistence.criteria.Predicate)

Example 2 with DeletedObject

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();
        }
    }
}
Also used : StatelessSession(org.hibernate.StatelessSession) DeletedObjectQuery(org.hisp.dhis.deletedobject.DeletedObjectQuery) DeletedObject(org.hisp.dhis.deletedobject.DeletedObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 3 with DeletedObject

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();
        }
    }
}
Also used : StatelessSession(org.hibernate.StatelessSession) DeletedObject(org.hisp.dhis.deletedobject.DeletedObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 4 with DeletedObject

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Pager(org.hisp.dhis.common.Pager) DeletedObject(org.hisp.dhis.deletedobject.DeletedObject) Predicate(javax.persistence.criteria.Predicate)

Example 5 with DeletedObject

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;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) DeletedObject(org.hisp.dhis.deletedobject.DeletedObject) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

DeletedObject (org.hisp.dhis.deletedobject.DeletedObject)5 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 Predicate (javax.persistence.criteria.Predicate)2 StatelessSession (org.hibernate.StatelessSession)2 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)2 Pager (org.hisp.dhis.common.Pager)1 DeletedObjectQuery (org.hisp.dhis.deletedobject.DeletedObjectQuery)1 FieldFilterParams (org.hisp.dhis.fieldfilter.FieldFilterParams)1 RootNode (org.hisp.dhis.node.types.RootNode)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1