Ejemplo n.º 1
0
 public static List<Trade> getOldVersionList(Trade trade) {
   Session session = HibernateUtil.getSession();
   List<Trade> result = new ArrayList<>();
   try {
     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;
 }
  /**
   * Returns an entity in the given revision for the given entity-id.
   *
   * @param revisionNumber
   * @param id
   * @param reader
   * @return
   */
  @SuppressWarnings("unchecked")
  private Revision<N, T> getEntityForRevision(N revisionNumber, ID id, AuditReader reader) {

    Class<?> type = revisionEntityInformation.getRevisionEntityClass();

    T revision = (T) reader.findRevision(type, revisionNumber);
    Object entity = reader.find(entityInformation.getJavaType(), id, revisionNumber);

    return new Revision<N, T>((RevisionMetadata<N>) getRevisionMetadata(revision), (T) entity);
  }
  /*
   * (non-Javadoc)
   * @see org.springframework.data.repository.history.RevisionRepository#findRevisions(java.io.Serializable)
   */
  @SuppressWarnings("unchecked")
  public Revisions<N, T> findRevisions(ID id) {

    Class<T> type = entityInformation.getJavaType();
    AuditReader reader = AuditReaderFactory.get(entityManager);
    List<? extends Number> revisionNumbers = reader.getRevisions(type, id);

    return revisionNumbers.isEmpty()
        ? new Revisions<N, T>(Collections.EMPTY_LIST)
        : getEntitiesForRevisions((List<N>) revisionNumbers, id, reader);
  }
  @Test
  public void testFindRevision() {
    AuditReader vr = getAuditReader();

    long rev1Timestamp =
        vr.findRevision(CustomPropertyAccessRevEntity.class, 1).getCustomTimestamp();
    assert rev1Timestamp > timestamp1;
    assert rev1Timestamp <= timestamp2;

    long rev2Timestamp =
        vr.findRevision(CustomPropertyAccessRevEntity.class, 2).getCustomTimestamp();
    assert rev2Timestamp > timestamp2;
    assert rev2Timestamp <= timestamp3;
  }
Ejemplo n.º 5
0
 /**
  * gets the entity at the specified revision
  *
  * @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;
 }
Ejemplo n.º 6
0
 /**
  * @param id
  * @return
  */
 public int getHeadRevisionNumber(int id) {
   int result = 0;
   try {
     begin();
     AuditReader reader = AuditReaderFactory.get(getEntityManager());
     List<Number> revisions = reader.getRevisions(entityClass, id);
     if (!revisions.isEmpty()) {
       result = revisions.get(revisions.size() - 1).intValue();
     }
     commit();
   } catch (NoResultException e) {
     LOG.warn("No result for revision with id of " + id);
   } finally {
     cleanup();
   }
   return result;
 }
  /**
   * Returns the entities in the given revisions for the entitiy with the given id.
   *
   * @param revisionNumbers
   * @param id
   * @param reader
   * @return
   */
  @SuppressWarnings("unchecked")
  private Revisions<N, T> getEntitiesForRevisions(
      List<N> revisionNumbers, ID id, AuditReader reader) {

    Class<T> type = entityInformation.getJavaType();
    Map<N, T> revisions = new HashMap<N, T>(revisionNumbers.size());

    Class<?> revisionEntityClass = revisionEntityInformation.getRevisionEntityClass();
    Map<Number, Object> revisionEntities =
        (Map<Number, Object>)
            reader.findRevisions(revisionEntityClass, new HashSet<Number>(revisionNumbers));

    for (Number number : revisionNumbers) {
      revisions.put((N) number, reader.find(type, id, number));
    }

    return new Revisions<N, T>(toRevisions(revisions, revisionEntities));
  }
  /*
   * (non-Javadoc)
   * @see org.springframework.data.repository.history.RevisionRepository#findRevisions(java.io.Serializable, org.springframework.data.domain.Pageable)
   */
  @SuppressWarnings("unchecked")
  public Page<Revision<N, T>> findRevisions(ID id, Pageable pageable) {

    Class<T> type = entityInformation.getJavaType();
    AuditReader reader = AuditReaderFactory.get(entityManager);
    List<Number> revisionNumbers = reader.getRevisions(type, id);

    if (pageable.getOffset() > revisionNumbers.size()) {
      return new PageImpl<Revision<N, T>>(Collections.<Revision<N, T>>emptyList(), pageable, 0);
    }

    int upperBound = pageable.getOffset() + pageable.getPageSize();
    upperBound = upperBound > revisionNumbers.size() ? revisionNumbers.size() : upperBound;

    List<? extends Number> subList = revisionNumbers.subList(pageable.getOffset(), upperBound);
    Revisions<N, T> revisions = getEntitiesForRevisions((List<N>) subList, id, reader);

    return new PageImpl<Revision<N, T>>(revisions.getContent(), pageable, revisionNumbers.size());
  }
  /*
   * (non-Javadoc)
   * @see org.springframework.data.repository.history.RevisionRepository#findLastChangeRevision(java.io.Serializable)
   */
  @SuppressWarnings("unchecked")
  public Revision<N, T> findLastChangeRevision(ID id) {

    Class<T> type = entityInformation.getJavaType();
    AuditReader reader = AuditReaderFactory.get(entityManager);

    List<Number> revisions = reader.getRevisions(type, id);

    if (revisions.isEmpty()) {
      return null;
    }

    N latestRevision = (N) revisions.get(revisions.size() - 1);

    Class<?> revisionEntityClass = revisionEntityInformation.getRevisionEntityClass();

    Object revisionEntity = reader.findRevision(revisionEntityClass, latestRevision);
    RevisionMetadata<N> metadata = (RevisionMetadata<N>) getRevisionMetadata(revisionEntity);
    return new Revision<N, T>(metadata, reader.find(type, id, latestRevision));
  }
Ejemplo n.º 10
0
  @Test
  public void testRevisionsForDates() {
    AuditReader vr = getAuditReader();

    assert vr.getRevisionDate(vr.getRevisionNumberForDate(new Date(timestamp2))).getTime()
        <= timestamp2;
    assert vr.getRevisionDate(vr.getRevisionNumberForDate(new Date(timestamp2)).intValue() + 1)
            .getTime()
        > timestamp2;

    assert vr.getRevisionDate(vr.getRevisionNumberForDate(new Date(timestamp3))).getTime()
        <= timestamp3;
  }
Ejemplo n.º 11
0
 @Test
 public void testDatesForRevisions() {
   AuditReader vr = getAuditReader();
   assert vr.getRevisionNumberForDate(vr.getRevisionDate(1)).intValue() == 1;
   assert vr.getRevisionNumberForDate(vr.getRevisionDate(2)).intValue() == 2;
 }