Java Code Examples for org.hibernate.envers.AuditReader#getRevisions()

The following examples show how to use org.hibernate.envers.AuditReader#getRevisions() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProcessResultDAOImpl.java    From Asqatasun with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public List<DefiniteResult> getHistoryChanges(ProcessResult processResultImpl) {

    List<DefiniteResult> history = new ArrayList<>();
    AuditReader auditReader = AuditReaderFactory.get(this.entityManager);
    Long id = processResultImpl.getId();
    if (id == null) {
        return new ArrayList<>();
    }
    List<Number> revisions = auditReader.getRevisions(processResultImpl.getClass(), id);
    DefiniteResult find;
    for (int i = 0; i < revisions.size(); i++) {
        Number revision = revisions.get(i);
        find = auditReader.find(DefiniteResultImpl.class, id, revision);
        history.add(find);
    }

    return history;

}
 
Example 2
Source File: EnversTestValidationResource.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@POST
public String save(String name) {
    try {
        transaction.begin();
        MyAuditedEntity entity = new MyAuditedEntity();
        entity.setName("initial");
        em.persist(entity);
        transaction.commit();

        transaction.begin();
        entity.setName(name);
        em.merge(entity);
        em.flush();
        transaction.commit();

        AuditReader auditReader = AuditReaderFactory.get(em);
        List<Number> revisions = auditReader.getRevisions(MyAuditedEntity.class, entity.getId());
        if (revisions.size() != 2) {
            throw new IllegalStateException(String.format("found {} revisions", revisions.size()));
        }

        MyRevisionEntity revEntity = auditReader.findRevision(MyRevisionEntity.class, revisions.get(0));
        if (revEntity.getListenerValue() == null) {
            throw new IllegalStateException("revision listener failed to update revision entity");
        }

        return "OK";
    } catch (Exception exception) {
        return exception.getMessage();
    }
}
 
Example 3
Source File: BuildConfigurationTest.java    From pnc with Apache License 2.0 4 votes vote down vote up
@Test
public void testRetrieveAuditedGenericParameters() {
    // given
    String key = "key";
    String initialValue = "initialValue";
    String updatedValue = "updatedValue";
    Map<String, String> initialParameters = new HashMap<>();
    initialParameters.put(key, initialValue);

    Map<String, String> updatedParameters = new HashMap<>();
    updatedParameters.put(key, updatedValue);

    // when
    BuildConfiguration buildConfiguration = createBc("auditing test", "description", initialParameters);
    em.getTransaction().begin();
    em.persist(buildConfiguration);
    em.getTransaction().commit();

    buildConfiguration.setGenericParameters(updatedParameters);
    buildConfiguration.setDescription("updated description");
    em.getTransaction().begin();
    em.persist(buildConfiguration);
    em.getTransaction().commit();

    // then
    BuildConfiguration obtained = em.find(BuildConfiguration.class, buildConfiguration.getId());

    AuditReader reader = AuditReaderFactory.get(em);
    List<Number> revisions = reader.getRevisions(BuildConfiguration.class, obtained.getId());

    assertEquals(2, revisions.size());

    Number firstRevision = revisions.get(0);
    BuildConfiguration oldBuildConfiguration = reader
            .find(BuildConfiguration.class, obtained.getId(), firstRevision);
    Number secondRevision = revisions.get(1);
    BuildConfiguration newBuildConfiguration = reader
            .find(BuildConfiguration.class, obtained.getId(), secondRevision);

    Assert.assertEquals(oldBuildConfiguration.getGenericParameters().get(key), initialValue);
    Assert.assertEquals(newBuildConfiguration.getGenericParameters().get(key), updatedValue);

    BuildConfiguration buildConfigurationOld = getByIdRev(buildConfiguration.getId(), firstRevision.intValue());
    BuildConfigurationAudited auditedOld = BuildConfigurationAudited
            .fromBuildConfiguration(buildConfigurationOld, firstRevision.intValue());

    Assert.assertEquals(auditedOld.getGenericParameters().get(key), initialValue);

    BuildConfiguration buildConfigurationNew = getByIdRev(buildConfiguration.getId(), secondRevision.intValue());
    BuildConfigurationAudited auditedNew = BuildConfigurationAudited
            .fromBuildConfiguration(buildConfigurationNew, secondRevision.intValue());

    Assert.assertEquals(auditedNew.getGenericParameters().get(key), updatedValue);

}
 
Example 4
Source File: DataPointServiceImpl.java    From hibernate-demos with Apache License 2.0 4 votes vote down vote up
public Map<Number, DefaultRevisionEntity> getRevisions(long id) {
	Session s = hibernateUtil.getSession();
	AuditReader reader = AuditReaderFactory.get(s);
	List<Number> revisionNums = reader.getRevisions( DataPoint.class, id );
	return reader.findRevisions( DefaultRevisionEntity.class, new HashSet<Number>(revisionNums) );
}