Example usage for org.hibernate.envers AuditReader find

List of usage examples for org.hibernate.envers AuditReader find

Introduction

In this page you can find the example usage for org.hibernate.envers AuditReader find.

Prototype

<T> T find(Class<T> cls, Object primaryKey, Date date) throws IllegalArgumentException, NotAuditedException,
        RevisionDoesNotExistException, IllegalStateException;

Source Link

Document

Find an entity by primary key on the given date.

Usage

From source file:TestConsole.java

License:Open Source License

private void printPersonAtRevision(StringBuilder sb, int personId, int revision) {
    AuditReader reader = AuditReaderFactory.get(entityManager);

    Person p = reader.find(Person.class, personId, revision);
    if (p == null) {
        sb.append("This person does not exist at that revision.");
    } else {//from  w w w . ja v  a 2 s . c  o m
        printPerson(sb, p);
    }
}

From source file:TestConsole.java

License:Open Source License

private void printAddressAtRevision(StringBuilder sb, int addressId, int revision) {
    AuditReader reader = AuditReaderFactory.get(entityManager);

    Address a = reader.find(Address.class, addressId, revision);
    if (a == null) {
        sb.append("This address does not exist at that revision.");
    } else {//from w  ww  .jav  a2s.c o m
        printAddress(sb, a);
    }
}

From source file:br.com.munif.bereja.repositorio.Repositorio.java

public List<RevisaoEObjeto> listaVersoes(Long id) {
    List<RevisaoEObjeto> aRetornar = new ArrayList<>();
    AuditReader auditReader = AuditReaderFactory.get(Persistencia.getInstancia().getEntityManager());
    List<Number> revisoes = auditReader.getRevisions(clazz, id);
    for (Number n : revisoes) {
        AuditoriaRevisao auditoriaRevisao = Persistencia.getInstancia().getEntityManager()
                .find(AuditoriaRevisao.class, n.longValue());
        Object object = auditReader.find(clazz, id, n.longValue());
        aRetornar.add(new RevisaoEObjeto(auditoriaRevisao, object));
    }//from w  ww.  ja va  2s  .c  o m
    return aRetornar;
}

From source file:ch.puzzle.itc.mobiliar.business.database.control.AmwAuditReader.java

License:Open Source License

/**
 * @param clazz/*from  w w  w . ja v a2  s.  co m*/
 *             - the entity class which shall be found in the history of the database
 * @param date
 *             - the date for which the result shall be loaded in the history of data
 * @param id
 *             - the id of the entity class that shall be loaded
 * @return the entity-instance with its state at the given date or null if the entity didn't exist at
 *         that time
 * @throws IllegalArgumentException
 *              - if the date is null (re-thrown from {@link AuditReader#getRevisionNumberForDate(Date)})
 */
public <T> T getByDate(final Class<T> clazz, final Date date, final Integer id)
        throws IllegalArgumentException {
    final AuditReader r = getAuditReader();
    Number currentRevisionNumber = null;
    try {
        // find the revision number for the given date
        currentRevisionNumber = r.getRevisionNumberForDate(date);
    } catch (final RevisionDoesNotExistException e) {
        return null;
    }
    return r.find(clazz, id, currentRevisionNumber);
}

From source file:ch.puzzle.itc.mobiliar.test.testrunner.example.ExamplePersistenceEnversTest.java

License:Open Source License

@Test
public void shouldFindPropertyDescriptorEntity_inDifferentVersions() throws Exception {
    // when/*from w w w.  ja  va2s . co  m*/
    PropertyDescriptorEntity result = entityManager.find(PropertyDescriptorEntity.class, 1);

    AuditReader auditReader = AuditReaderFactory.get(entityManager);

    List<Number> revisions = auditReader.getRevisions(PropertyDescriptorEntity.class, 1);
    PropertyDescriptorEntity revision1 = auditReader.find(PropertyDescriptorEntity.class, 1, 1);
    PropertyDescriptorEntity revision2 = auditReader.find(PropertyDescriptorEntity.class, 1, 2);

    // then
    assertNotNull(revisions);
    assertNotNull(revision1);
    assertNotNull(result);
    assertEquals(2, revisions.size());
    assertEquals("comment2", result.getPropertyComment());
    assertEquals("comment", revision1.getPropertyComment());
    assertEquals("comment2", revision2.getPropertyComment());
}

From source file:com.actelion.research.spiritcore.services.dao.DAORevision.java

License:Open Source License

/**
 * Cancel the change, ie. go to the version minus one for each object in the revision.
 *
 * If this revision is a deletion->insert older version
 * If this revision is a insert->delete
 * If this revision is an update->update older version
 *
 * @param revision//from   w w  w .ja va 2 s.  com
 * @param user
 * @param comments
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public static void revert(Revision revision, SpiritUser user) throws Exception {
    EntityManager session = JPAUtil.getManager();
    EntityTransaction txn = session.getTransaction();

    try {
        Date now = JPAUtil.getCurrentDateFromDatabase();
        int revId = revision.getRevId();

        AuditReader reader = AuditReaderFactory.get(session);

        //Query modified entities during this revision
        Map<String, IObject> mapMerged = new HashMap<>();
        txn.begin();

        for (Class<IObject> claz : new Class[] { Biotype.class, Test.class, Study.class, Location.class,
                Biosample.class, Result.class }) {
            List<Object[]> res = queryForRevisions(reader, Collections.singleton(claz), revId, revId, null, -1,
                    null);

            List<IObject> toDelete = new ArrayList<>();
            List<IObject> toMerge = new ArrayList<>();

            for (Object[] a : res) {
                IObject entity = (IObject) a[0];
                RevisionType type = (RevisionType) a[2];
                if (revision.getType() == type) {
                    if (type == RevisionType.ADD) {
                        //Attach the entity to be deleted
                        IObject obj = session.merge(entity);
                        toDelete.add(obj);
                    } else {
                        //Attach the revision to be restored
                        IObject obj = reader.find(entity.getClass(), entity.getId(), revId - 1);
                        if (obj == null)
                            throw new Exception("The " + entity.getClass().getSimpleName() + " "
                                    + entity.getId() + " could not be found at rev=" + (revId - 1));
                        toMerge.add(obj);
                    }
                    mapMerged.put(entity.getClass() + "_" + entity.getId(), null);
                }
            }

            LoggerFactory.getLogger(DAORevision.class)
                    .debug(claz + " >  toMerge=" + toMerge.size() + " toDelete=" + toDelete.size());
            int step = 0;
            while (toMerge.size() > 0 && step++ < 10) {
                for (IObject o : new ArrayList<>(toMerge)) {
                    int id = o.getId();
                    boolean success = remap(session, o, now, user, mapMerged);
                    if (!success)
                        continue;
                    toMerge.remove(o);

                    LoggerFactory.getLogger(DAORevision.class)
                            .debug("merge " + o.getClass().getSimpleName() + " " + o.getId() + ":" + o);
                    mapMerged.put(o.getClass() + "_" + id, session.merge(o));
                }
            }
            for (IObject o : toDelete) {
                LoggerFactory.getLogger(DAORevision.class)
                        .debug("remove " + o.getClass().getSimpleName() + " " + o.getId() + ":" + o);
                session.remove(o);
            }
        }
        txn.commit();
        txn = null;
    } catch (Exception e) {
        e.printStackTrace();
        if (txn != null && txn.isActive())
            txn.rollback();
        throw e;
    }
}

From source file:com.home.ln_spring.ch10.service.springjpa.ContactAuditServiceImpl.java

@Override
@Transactional(readOnly = true)/*w w  w  . ja va  2 s . com*/
public ContactAudit findAuditByRevision(Long id, int revision) {
    AuditReader auditReader = AuditReaderFactory.get(entityManager);
    return auditReader.find(ContactAudit.class, id, revision);
}

From source file:com.intuit.tank.dao.BaseDao.java

License:Open Source License

/**
 * gets the entity at the specified revision
 * /*  w ww .j  a  v  a 2  s . co  m*/
 * @param id
 *            the id of the entity to fetch
 * @param revisionNumber
 *            the revision number
 * @return the entity or null if no entity can be found
 */
@Nullable
public T_ENTITY findRevision(int id, int revisionNumber) {
    T_ENTITY result = null;
    try {
        begin();
        AuditReader reader = AuditReaderFactory.get(getEntityManager());
        result = reader.find(entityClass, id, revisionNumber);
        commit();
    } catch (NoResultException e) {
        LOG.warn("No result for revision " + revisionNumber + " with id of " + id);
    } finally {
        cleanup();

    }
    return result;
}

From source file:com.intuit.tank.dao.JobNotificationDao.java

License:Open Source License

/**
 * gets the entity at the specified revision
 * //w w w  .jav  a  2  s.com
 * @param id
 *            the id of the entity to fetch
 * @param revisionNumber
 *            the revision number
 * @return the entity or null if no entity can be found
 */
@Nullable
@Override
public JobNotification findRevision(int id, int revisionNumber) {
    JobNotification result = null;
    try {
        begin();
        AuditReader reader = AuditReaderFactory.get(getEntityManager());
        result = reader.find(JobNotification.class, id, revisionNumber);
        Hibernate.initialize(result.getLifecycleEvents());
        result.getLifecycleEvents().contains(JobLifecycleEvent.QUEUE_ADD);
        commit();
    } catch (NoResultException e) {
        LOG.warn("No result for revision " + revisionNumber + " with id of " + id);
    } finally {
        cleanup();
    }
    return result;
}

From source file:com.lvg.springtest.services.springdatajpa.impl.ContactAuditServiceSpringDataJpaImpl.java

@Override
@Transactional(readOnly = true)/*from  ww w.  j  a v  a 2s  .  co m*/
public ContactAudit findAuditByRevision(Long id, int revision) {
    AuditReader auditReader = AuditReaderFactory.get(em);
    return auditReader.find(ContactAudit.class, id, revision);
}