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

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

Introduction

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

Prototype

public static AuditProperty<Number> revisionNumber() 

Source Link

Document

Create restrictions, projections and specify order for the revision number, corresponding to an audited entity.

Usage

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.
 * //  ww w . ja v a  2  s  .c o 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   www. j  a v a  2s.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  ww. j a  va  2 s. c o  m*/
 * @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;
}

From source file:org.devgateway.eudevfin.financial.test.storage.RevisionsTest.java

License:Open Source License

/**
 * Showing 2 ways of getting a revision number. The test compares these 2 revision numbers.
 *///from   ww w .  ja v  a  2s .c  o  m
@Test
public void testRevisionNumbers() {
    final Long modifiedTxId = this.storageHelper.modifyOrgAndAmountOfTransaction();

    AuditQuery auditQuery = this.auditReader.createQuery()
            .forRevisionsOfEntity(FinancialTransaction.class, false, false)
            .add(AuditEntity.id().eq(modifiedTxId))
            .add(AuditEntity.property("donorProjectNumber").hasNotChanged())
            .addProjection(AuditEntity.revisionNumber().max());
    final Number num1 = (Number) auditQuery.getSingleResult();
    logger.info("Last revision for a financial tx with unchanged description is: " + num1);

    auditQuery = this.auditReader.createQuery().forRevisionsOfEntity(Organization.class, false, false)
            .add(AuditEntity.property("code").hasChanged()).add(AuditEntity.revisionNumber().maximize());
    final Object[] oArray = (Object[]) auditQuery.getSingleResult();
    final DefaultTrackingModifiedEntitiesRevisionEntity trackingObject = (DefaultTrackingModifiedEntitiesRevisionEntity) oArray[1];
    logger.info("Last revision for an org with changed name is: " + trackingObject.getId());

    assertEquals(num1, trackingObject.getId());

}

From source file:org.gaia.dao.audit.AuditAccessor.java

License:Open Source License

public static List<Trade> getOldVersionList(Trade trade) {
    Session session = HibernateUtil.getSession();
    List<Trade> result = new ArrayList<>();
    try {/*from ww  w  .j a  va  2 s. co m*/
        if (trade != null) {
            result.add(trade);
            AuditReader reader = AuditReaderFactory.get(session);
            List<Object[]> prior_revision = (List<Object[]>) reader.createQuery()
                    .forRevisionsOfEntity(trade.getClass(), false, true)
                    .add(AuditEntity.property("tradeVersion").lt(trade.getTradeVersion()))
                    .add(AuditEntity.id().eq(trade.getId())).addOrder(AuditEntity.revisionNumber().desc())
                    .getResultList();
            for (Object[] objects : prior_revision) {
                Trade version = (Trade) objects[0];
                unProxyObject(version);
                result.add(version);
            }
        }
    } catch (ClassNotFoundException | IllegalAccessException | HibernateException | IllegalArgumentException
            | InvocationTargetException e) {
        logger.error(StringUtils.formatErrorMessage(e));
    } finally {
        session.close();
    }
    return result;
}

From source file:org.jboss.pnc.datastore.repositories.BuildConfigurationAuditedRepositoryImpl.java

License:Open Source License

@Override
public List<BuildConfigurationAudited> findAllByIdOrderByRevDesc(Integer buildConfigurationId) {

    List<Object[]> result = AuditReaderFactory.get(entityManager).createQuery()
            .forRevisionsOfEntity(BuildConfiguration.class, false, false)
            .add(AuditEntity.id().eq(buildConfigurationId)).addOrder(AuditEntity.revisionNumber().desc())
            .getResultList();/*from w  ww  .j  ava 2  s.  c om*/

    List<BuildRecord> buildRecords = getBuildRecords(buildConfigurationId);

    return result.stream().map(o -> createAudited(o[0], o[1], buildRecords)).collect(Collectors.toList());
}

From source file:org.jboss.pnc.datastore.repositories.BuildConfigurationAuditedRepositoryImpl.java

License:Open Source License

@Override
public List<BuildConfigurationAudited> searchForBuildConfigurationName(String buildConfigurationName) {
    List<Object[]> result = AuditReaderFactory.get(entityManager).createQuery()
            .forRevisionsOfEntity(BuildConfiguration.class, false, false)
            .add(AuditEntity.property("name").like(buildConfigurationName))
            .addOrder(AuditEntity.revisionNumber().desc()).getResultList();
    List<BuildRecord> emptyList = Collections.EMPTY_LIST;
    return result.stream().map(o -> createAudited(o[0], o[1], emptyList)).collect(Collectors.toList());
}

From source file:org.jboss.pnc.model.BasicModelTest.java

License:Open Source License

private BuildConfigurationAudited findBuildConfigurationAudited(EntityManager em) {
    List<Object[]> result = AuditReaderFactory.get(em).createQuery()
            .forRevisionsOfEntity(BuildConfiguration.class, false, false)
            .addOrder(AuditEntity.revisionNumber().desc()).getResultList();

    Object[] second = result.get(1);
    BuildConfiguration buildConfiguration = (BuildConfiguration) second[0];
    return BuildConfigurationAudited.fromBuildConfiguration(buildConfiguration,
            ((DefaultRevisionEntity) second[1]).getId());
}

From source file:org.jboss.pnc.model.BuildConfigurationTest.java

License:Open Source License

private BuildConfiguration getByIdRev(Integer buildConfigurationId, Integer revision) {
    return (BuildConfiguration) AuditReaderFactory.get(em).createQuery()
            .forEntitiesAtRevision(BuildConfiguration.class, revision)
            .add(AuditEntity.id().eq(buildConfigurationId)).addOrder(AuditEntity.revisionNumber().desc())
            .getSingleResult();/*from w  ww  . j a v a  2 s  . c o  m*/
}

From source file:org.jboss.pnc.rest.provider.BuildRecordProvider.java

License:Open Source License

public CollectionInfo<BuildRecordRest> getAllForProject(int pageIndex, int pageSize, String sortingRsql,
        String query, Integer projectId) {
    List<Object[]> buildConfigurationRevisions = AuditReaderFactory.get(entityManager).createQuery()
            .forRevisionsOfEntity(BuildConfiguration.class, false, false)
            .add(AuditEntity.relatedId("project").eq(projectId)).addOrder(AuditEntity.revisionNumber().desc())
            .getResultList();//w w  w. java  2 s  .c o  m

    return queryForBuildRecords(pageIndex, pageSize, sortingRsql, query, buildConfigurationRevisions);
}