org.hibernate.envers.AuditReader Java Examples

The following examples show how to use org.hibernate.envers.AuditReader. 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: AbstractHibernateAuditableDao.java    From tutorials with MIT License 6 votes vote down vote up
@Override
public List<T> getRevisions() {
    final AuditReader auditReader = AuditReaderFactory.get(getCurrentSession());
    final AuditQuery query = auditReader.createQuery().forRevisionsOfEntity(clazz, true, true);
    final List<T> resultList = query.getResultList();
    return resultList;
}
 
Example #2
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 #3
Source File: CurrentVariantRollbackService.java    From mojito with Apache License 2.0 5 votes vote down vote up
/**
 * Builds the query to insert new {@link com.box.l10n.mojito.entity.TMTextUnitCurrentVariant}s
 * as they were at the rollback date.
 *
 * @param rollbackDateTime Date at which the {@link TMTextUnitCurrentVariant}s will be rollbacked to
 * @param tmId             ID of the TM the {@link TMTextUnitCurrentVariant}s to be rolled back should belong to
 * @param extraParameters  Extra parameters to filter what to rollback
 * @return The insert audit query
 */
protected AuditQuery buildInsertAuditQuery(DateTime rollbackDateTime, Long tmId, CurrentVariantRollbackParameters extraParameters) {

    logger.trace("Building the insert tmTextUnitCurrentVariants audit query");

    AuditReader auditReader = AuditReaderFactory.get(entityManager);
    Number revNumberAtDate = auditReader.getRevisionNumberForDate(rollbackDateTime.toDate());

    AuditQuery auditQuery = auditReader.createQuery()
            .forEntitiesAtRevision(TMTextUnitCurrentVariant.class, TMTextUnitCurrentVariant.class.getName(), revNumberAtDate, true)
            .add(AuditEntity.property("tm_id").eq(tmId));

    List<Long> localeIdsToRollback = extraParameters.getLocaleIds();
    if (localeIdsToRollback != null && !localeIdsToRollback.isEmpty()) {
        // Using "in" does not work with relatedId() nor property() so using loop instead
        for (Long localeIdToRollback : localeIdsToRollback) {
            auditQuery.add(AuditEntity.relatedId("locale").eq(localeIdToRollback));
        }
    }

    List<Long> tmTextUnitIdsToRollback = extraParameters.getTmTextUnitIds();
    if (tmTextUnitIdsToRollback != null && !tmTextUnitIdsToRollback.isEmpty()) {
        // Using "in" does not work with relatedId() nor property() so using loop instead
        for (Long tmTextUnitIdToRollback : tmTextUnitIdsToRollback) {
            auditQuery.add(AuditEntity.relatedId("tmTextUnit").eq(tmTextUnitIdToRollback));
        }
    }

    return auditQuery;
}
 
Example #4
Source File: AbstractHibernateAuditableDao.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public List<T> getEntitiesModifiedAtRevision(final Number revision) {
    final AuditReader auditReader = AuditReaderFactory.get(getCurrentSession());
    final AuditQuery query = auditReader.createQuery().forEntitiesModifiedAtRevision(clazz, revision);
    final List<T> resultList = query.getResultList();
    return resultList;
}
 
Example #5
Source File: AbstractHibernateAuditableDao.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public List<T> getEntitiesAtRevision(final Number revision) {
    final AuditReader auditReader = AuditReaderFactory.get(getCurrentSession());
    final AuditQuery query = auditReader.createQuery().forEntitiesAtRevision(clazz, revision);
    final List<T> resultList = query.getResultList();
    return resultList;
}
 
Example #6
Source File: EnversDemo.java    From hibernate-demos with Apache License 2.0 5 votes vote down vote up
public List getRevisionProjects(int revisionNumber) {
	final Session s = openSession();
	final AuditReader ar = AuditReaderFactory.get( s );
	return ar.createQuery()
		    .forEntitiesAtRevision(Project.class, revisionNumber)
		    .getResultList();
}
 
Example #7
Source File: EnversDemo.java    From hibernate-demos with Apache License 2.0 5 votes vote down vote up
public List getProjectRevisions(String property) {
	final Session s = openSession();
	final AuditReader ar = AuditReaderFactory.get( s );
	return ar.createQuery()
		    .forRevisionsOfEntity(Project.class, false, true)
		    .add(AuditEntity.property(property).hasChanged())
		    .getResultList();
}
 
Example #8
Source File: EnversDemo.java    From hibernate-demos with Apache License 2.0 5 votes vote down vote up
public List getProjectRevisions() {
	final Session s = openSession();
	final AuditReader ar = AuditReaderFactory.get( s );
	return ar.createQuery()
		    .forRevisionsOfEntity(Project.class, false, true)
		    .getResultList();
}
 
Example #9
Source File: ConfigurationManager.java    From ankush with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Gets the configuration.
 * 
 * @param clusterId
 *            the cluster id
 * @return the configuration
 */
public List getConfiguration(Long clusterId) {
	try {
		AuditReader reader = AuditReaderFactory.get(HibernateUtils
				.getEntityManager());
		AuditQuery query = reader.createQuery().forRevisionsOfEntity(
				Configuration.class, false, true);

		// filter results besed on cluster id.
		query.add(AuditEntity.property(
				com.impetus.ankush2.constant.Constant.Keys.CLUSTERID).eq(
				clusterId));
		query.addOrder(AuditEntity.revisionProperty(
				com.impetus.ankush2.constant.Constant.Keys.TIMESTAMP)
				.desc());

		// Getting Result list.
		List list = query.getResultList();

		// Creating List Object.
		List result = new ArrayList();
		for (Object object : list) {
			Object[] obj = (Object[]) object;
			Map map = new HashMap();
			// Mapping Revision Entity.
			DefaultRevisionEntity ri = (DefaultRevisionEntity) obj[1];
			map.putAll(JsonMapperUtil.mapFromObject(obj[0]));
			map.put(com.impetus.ankush2.constant.Constant.Keys.DATE,
					ri.getRevisionDate());
			map.put(com.impetus.ankush2.constant.Constant.Keys.REVISIONID,
					ri.getId());
			map.put(com.impetus.ankush2.constant.Constant.Keys.TYPE, obj[2]);
			result.add(map);
		}
		return result;
	} catch (Exception e) {
		LOG.error(e.getMessage(), e);
	}
	return null;

}
 
Example #10
Source File: EnversDemo.java    From HibernateDemos with The Unlicense 5 votes vote down vote up
public List getRevisionProjects(int revisionNumber) {
	final Session s = openSession();
	final AuditReader ar = AuditReaderFactory.get( s );
	return ar.createQuery()
		    .forEntitiesAtRevision(Project.class, revisionNumber)
		    .getResultList();
}
 
Example #11
Source File: EnversDemo.java    From HibernateDemos with The Unlicense 5 votes vote down vote up
public List getProjectRevisions(String property) {
	final Session s = openSession();
	final AuditReader ar = AuditReaderFactory.get( s );
	return ar.createQuery()
		    .forRevisionsOfEntity(Project.class, false, true)
		    .add(AuditEntity.property(property).hasChanged())
		    .getResultList();
}
 
Example #12
Source File: EnversDemo.java    From HibernateDemos with The Unlicense 5 votes vote down vote up
public List getProjectRevisions() {
	final Session s = openSession();
	final AuditReader ar = AuditReaderFactory.get( s );
	return ar.createQuery()
		    .forRevisionsOfEntity(Project.class, false, true)
		    .getResultList();
}
 
Example #13
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 #14
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 #15
Source File: JpaConfiguration.java    From pnc with Apache License 2.0 4 votes vote down vote up
@Produces
public AuditReader auditReader(EntityManager entityManager) {
    return AuditReaderFactory.get(entityManager);
}
 
Example #16
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) );
}
 
Example #17
Source File: AbstractAuditRepository.java    From pnc with Apache License 2.0 4 votes vote down vote up
public AbstractAuditRepository(AuditReader auditReader, Class<Entity> entityClass) {
    this.auditReader = auditReader;
    this.entityClass = entityClass;
}
 
Example #18
Source File: AuditedBuildConfigurationRepository.java    From pnc with Apache License 2.0 4 votes vote down vote up
@Inject
public AuditedBuildConfigurationRepository(AuditReader auditReader) {
    super(auditReader, BuildConfiguration.class);
}