Example usage for org.hibernate.envers.query AuditEntity property

List of usage examples for org.hibernate.envers.query AuditEntity property

Introduction

In this page you can find the example usage for org.hibernate.envers.query AuditEntity property.

Prototype

public static AuditProperty<Object> property(String propertyName) 

Source Link

Document

Create restrictions, projections and specify order for a property of an audited entity.

Usage

From source file:de.rs.scrumit.dao.BaseEntityAuditDao.java

License:Open Source License

@Transactional(readOnly = true)
public <T extends BaseEntityAuditModel> void printChangeHistory(Class<T> modelClass, String code) {
    AuditReader reader = AuditReaderFactory.get(entityManager);

    List<?> results = reader.createQuery().forRevisionsOfEntity(modelClass, false, true)
            .add(AuditEntity.property("code").eq(code)).addOrder(AuditEntity.property("changedAt").asc())
            .getResultList();/*from  w ww. j a  v a2s .co m*/

    SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy hh:mm:ss");
    for (Object entry : results) {
        BaseEntityAuditModel model = (BaseEntityAuditModel) ((Object[]) entry)[0];
        RevisionType type = (RevisionType) ((Object[]) entry)[2];
        String msg = type + " entry with code " + model.getCode() + " at time: "
                + format.format(model.getChangedAt());
        LOGGER.debug(msg);
        System.out.println(msg);
    }
}

From source file:fr.mcc.ginco.audit.utils.AuditHelper.java

License:CeCILL license

/**
 * Gets the preferred term of the given concept at the given revision
 * @param revisionNumber/*from   w ww  .  j a  v a 2s.c  om*/
 * @param conceptId
 * @param lang
 * @return
 */
public ThesaurusTerm getPreferredTermAtRevision(Number revisionNumber, String conceptId, String lang) {
    AuditQuery query = reader.getAuditReader().createQuery()
            .forEntitiesAtRevision(ThesaurusTerm.class, revisionNumber)
            .add(AuditEntity.relatedId("concept").eq(conceptId)).add(AuditEntity.property("prefered").eq(true))
            .addOrder(AuditEntity.revisionNumber().desc()).setMaxResults(1);
    if (lang != null) {
        auditQueryBuilder.addFilterOnLanguage(query, lang);
    }
    List results = query.getResultList();
    if (results.isEmpty()) {
        return null;
    } else {
        return (ThesaurusTerm) results.get(0);
    }
}

From source file:fr.mcc.ginco.audit.utils.AuditQueryBuilder.java

License:CeCILL license

/**
 * Builds a query returning objects property updates revisions
 * /*  w  w w.  jav a  2 s .co m*/
 * @param reader
 * @param thesaurus
 *            the thesaurus on which searched should be performed
 * @param startDate
 *            the start date for the search
 * @param clazz
 *            the object class
 * @param propertyName
 *            the audited property
 * @return
 */
public AuditQuery getPropertyChangedQueryOnUpdate(Thesaurus thesaurus, Date startDate, Class<?> clazz,
        String propertyName) {
    AuditQuery auditQuery = readerService.getAuditReader().createQuery().forRevisionsOfEntity(clazz, false,
            true);
    filterOnDateAndThesaurusId(auditQuery, thesaurus, startDate);
    auditQuery.add(AuditEntity.property(propertyName).hasChanged());
    auditQuery.add(AuditEntity.revisionType().eq(RevisionType.MOD));
    return auditQuery;
}

From source file:fr.mcc.ginco.audit.utils.AuditQueryBuilder.java

License:CeCILL license

/**
 * @param reader/*  w w w.j  a  v a2s  . c o m*/
 * @param clazz
 * @param identifier
 * @param currentRevision
 * @return
 */
public AuditQuery getPreviousPreferredTermQuery(int currentRevision, String conceptId) {
    return readerService.getAuditReader().createQuery().forRevisionsOfEntity(ThesaurusTerm.class, false, true)
            .add(AuditEntity.revisionNumber().lt(currentRevision))
            .add(AuditEntity.property("prefered").eq(true)).add(AuditEntity.relatedId("concept").eq(conceptId));
}

From source file:fr.mcc.ginco.audit.utils.AuditQueryBuilder.java

License:CeCILL license

public void getFilterOnStatus(AuditQuery query, Integer status) {
    query.add(AuditEntity.property("status").eq(status));
}

From source file:org.azafiu.hibernatetest.datainteractor.CommonDAO.java

License:Apache License

@Transactional
public Object[] getLatestChangeForItemWithID(final Long id, final Class<?> itemClass) {
    final AuditQuery query = this.getAuditReader().createQuery().forRevisionsOfEntity(itemClass, false, true)
            .addOrder(AuditEntity.property("modified").desc()).add(AuditEntity.id().eq(id)).setMaxResults(1);

    final List<Object[]> resultList = query.getResultList();

    if (resultList != null && resultList.size() > 0) {
        return resultList.get(0);
    }//ww w .ja va 2s  . c om
    return null;
}

From source file:org.azafiu.hibernatetest.datainteractor.ProductDAO.java

License:Apache License

/**
 * Get all products that need to be shown to the checker for approval.
 * /*from w ww  .  j  a va  2 s  .  c  om*/
 * @return a list of Object[]. Each element will be an Object[3] array with
 *         the following items: Object[0] - the {@link ProductEntity} at a
 *         revision ( greater or equal than the one given as parameter)
 *         Object[1] a {@link DefaultRevisionEntity} Object[2] a
 *         {@link RevisionType} object containing information about the
 *         revision
 */
@Transactional
public List<Object[]> getAllProductsWaitingForApproval() {
    /**
     * Get all distinct {@link ProductEntity} objects where the wasChecked
     * property is false order by modified descending
     */
    final AuditQuery query = this.getAuditReader().createQuery()
            .forRevisionsOfEntity(ProductEntity.class, false, true)
            .addOrder(AuditEntity.property("modified").desc())
            .add(AuditEntity.revisionNumber().maximize().computeAggregationInInstanceContext())
            .add(AuditEntity.property("wasChecked").eq(Boolean.FALSE))
            .add(AuditEntity.revisionType().ne(RevisionType.DEL));

    final List<Object[]> resultList = query.getResultList();

    final List<Object[]> result = new ArrayList<>();

    /**
     * for each "changed" object found in the db we need to check if there
     * is a newer revision of it in which the {@link ProductEntity} was
     * approved (wasChecked = true) because we do not need to retrieve
     * already checked objects to the checker.
     */
    for (final Object[] change : resultList) {
        final ProductEntity pe = (ProductEntity) change[0];
        final AuditQuery queryForWasCheckedTrue = this.getAuditReader().createQuery()
                .forRevisionsOfEntity(ProductEntity.class, false, true)
                .addOrder(AuditEntity.property("modified").desc()).add(AuditEntity.id().eq(pe.getId()))
                .add(AuditEntity.property("wasChecked").eq(Boolean.TRUE));

        if (pe.getModified() != null) {
            queryForWasCheckedTrue.add(AuditEntity.property("modified").gt(pe.getModified()));
        }

        try {
            final Object[] trueWasChecked = (Object[]) queryForWasCheckedTrue.getSingleResult();
        } catch (final NoResultException ex) {
            // there is no newer revision where the current product has
            // wasChecked property == true
            result.add(change);
        }

    }

    return result;
}

From source file:org.azafiu.hibernatetest.datainteractor.ProductDAO.java

License:Apache License

/**
 * Retrieve the previous state of a given {@link ProductEntity} which was
 * modified earlier than the given modified date.
 * /*from ww w  . j  av  a2 s. co  m*/
 * @param prodId
 *            the id of the {@link ProductEntity}
 * @param revNumber
 *            the revision number when the productDetails was modified
 * @return a {@link ProductEntity} object
 */
@Transactional
public ProductEntity getPreviousStateForProduct(final Long prodId, final int revNumber) {
    /**
     * Get only the most recent {@link ProductEntity} information from the
     * audit tables where the wasChecked property is true and the
     * modifiedDate is less than the one given as parameter for the given
     * product details object
     */
    final AuditQuery query = this.getAuditReader().createQuery()
            .forRevisionsOfEntity(ProductEntity.class, true, true)
            .addOrder(AuditEntity.property("modified").desc()).add(AuditEntity.id().eq(prodId))
            .add(AuditEntity.property("wasChecked").eq(Boolean.TRUE))
            .add(AuditEntity.revisionNumber().lt(Integer.valueOf(revNumber))).setMaxResults(1);

    final List<ProductEntity> resultList = query.getResultList();

    if (resultList != null && resultList.size() > 0) {
        return resultList.get(0);
    }

    return null;

}

From source file:org.azafiu.hibernatetest.datainteractor.ProductDetailsDAO.java

License:Apache License

/**
 * Query for all {@link ProductDetailsEntity} objects linked to a product at
 * a given revision.//from ww  w . jav  a 2 s .  c o m
 * 
 * @param productId
 *            the id of the related product
 * @param revisionDate
 *            the date of the product change revision
 * @return a list of Object[]. Each element will be an Object[3] array with
 *         the following items: Object[0] - the {@link ProductDetailsEntity}
 *         at a revision ( greater or equal than the one given as parameter)
 *         Object[1] a {@link DefaultRevisionEntity} Object[2] a
 *         {@link RevisionType} object containing information about the
 *         revision
 */
@Transactional
public List<Object[]> getAllProductDetailsForProductAtRevision(final Long productId, final Date revisionDate) {

    // get the revision number based on the revision date
    final Number revNumber = this.getAuditReader().getRevisionNumberForDate(revisionDate);

    /**
     * Query the audit table for {@link ProductDetailsEntity}, order the
     * results descending based on modified property of the
     * {@link ProductDetailsEntity} object, get the list of objects with
     * revision greater or equal than the one given as parameter (the
     * {@link ProductDetailsEntity} may have been added in a different
     * revision(after the product was persisted in the database) than the
     * {@link ProductEntity} and we need to retrieve it) with the foreign
     * key for the product set to the one given as parameter and with the
     * wasChecked property set to false
     */
    final AuditQuery query = this.getAuditReader().createQuery()
            .forRevisionsOfEntity(ProductDetailsEntity.class, false, false)
            .addOrder(AuditEntity.property("modified").desc()).add(AuditEntity.revisionNumber().ge(revNumber))
            .add(AuditEntity.property("fkProduct").eq(productId))
            .add(AuditEntity.property("wasChecked").eq(Boolean.FALSE));

    final List<Object[]> resultList = query.getResultList();

    final List<Object[]> result = new ArrayList<>();

    /**
     * for each "changed" object found in the db we need to check if there
     * is a newer revision of it in which the {@link ProductDetailsEntity}
     * was approved (wasChecked = true) because we do not need to retrieve
     * already checked objects to the checker.
     */
    for (final Object[] change : resultList) {
        final ProductDetailsEntity pe = (ProductDetailsEntity) change[0];
        final AuditQuery queryForWasCheckedTrue = this.getAuditReader().createQuery()
                .forRevisionsOfEntity(ProductDetailsEntity.class, false, true)
                .addOrder(AuditEntity.property("modified").desc()).add(AuditEntity.id().eq(pe.getId()))
                .add(AuditEntity.property("wasChecked").eq(Boolean.TRUE));

        if (pe.getModified() != null) {
            queryForWasCheckedTrue.add(AuditEntity.property("modified").gt(pe.getModified()));
        }

        try {
            final Object[] trueWasChecked = (Object[]) queryForWasCheckedTrue.getSingleResult();
        } catch (final NoResultException ex) {
            // there is no newer revision where the current product has
            // wasChecked property == true
            result.add(change);
        }

    }

    return result;
}

From source file:org.azafiu.hibernatetest.datainteractor.ProductDetailsDAO.java

License:Apache License

/**
 * Retrieve the previous state of a given {@link ProductDetailsEntity} which
 * was modified earlier than the given modified date.
 * //from  w  w  w. j  a  va2 s . c  om
 * @param prodDetailsId
 *            the id of the {@link ProductDetailsEntity}
 * @param modifiedDate
 *            the date when the productDetails was modified
 * @return a {@link ProductDetailsEntity} object
 */
@Transactional
public ProductDetailsEntity getPreviousStateForProductDetails(final Long prodDetailsId, final int revNumber) {
    /**
     * Get only the most recent {@link ProductDetailsEntity} information
     * from the audit tables where the wasChecked property is true and the
     * modifiedDate is less than the one given as parameter for the given
     * product details object
     */
    final AuditQuery query = this.getAuditReader().createQuery()
            .forRevisionsOfEntity(ProductDetailsEntity.class, true, true)
            .addOrder(AuditEntity.property("modified").desc()).add(AuditEntity.id().eq(prodDetailsId))
            .add(AuditEntity.property("wasChecked").eq(Boolean.TRUE))
            .add(AuditEntity.revisionNumber().lt(Integer.valueOf(revNumber))).setMaxResults(1);

    final List<ProductDetailsEntity> resultList = query.getResultList();

    if (resultList != null && resultList.size() > 0) {
        return resultList.get(0);
    }

    return null;
}