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;
 }
  /*
   * (non-Javadoc)
   * @see org.springframework.data.envers.repository.support.EnversRevisionRepository#findRevision(java.io.Serializable, java.lang.Number)
   */
  @Override
  public Revision<N, T> findRevision(ID id, N revisionNumber) {

    Assert.notNull(id, "Identifier must not be null!");
    Assert.notNull(revisionNumber, "Revision number must not be null!");

    return getEntityForRevision(revisionNumber, id, AuditReaderFactory.get(entityManager));
  }
  /*
   * (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);
  }
Ejemplo n.º 4
0
  @Override
  public GeoObject getByRevision(Long id, Integer revId) {
    AuditQuery query =
        AuditReaderFactory.get(getCurrentSession())
            .createQuery()
            .forEntitiesAtRevision(GeoObject.class, revId);
    query.add(AuditEntity.id().eq(id));
    GeoObject object = (GeoObject) query.getSingleResult();

    AuditQuery queryTags =
        AuditReaderFactory.get(getCurrentSession())
            .createQuery()
            .forEntitiesAtRevision(GeoObjectTag.class, revId);
    // queryTags.add(AuditEntity.property("geoObject").eq(id));
    queryTags.add(AuditEntity.property("geoObject").eq(object));
    List tags = queryTags.getResultList();

    object.setTags(new HashSet<GeoObjectTag>(tags));
    return object;
  }
Ejemplo n.º 5
0
 @Override
 public List<Object[]> getHistoryList(Long id) {
   AuditQueryCreator query = AuditReaderFactory.get(getCurrentSession()).createQuery();
   List<Object[]> resultList =
       query
           .forRevisionsOfEntity(GeoObject.class, false, false)
           .add(AuditEntity.id().eq(id))
           .getResultList();
   Collections.reverse(resultList);
   return resultList;
 }
Ejemplo n.º 6
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.º 7
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;
 }
  /*
   * (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));
  }
  protected AuditReader getAuditReader() {
    EntityManager entityManager = getOrCreateEntityManager();
    SessionImplementor sessionImplementor = entityManager.unwrap(SessionImplementor.class);

    if (sessionImplementor.getTransactionCoordinator().getTransactionCoordinatorBuilder().isJta()) {
      if (!JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager())) {
        try {
          TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    } else if (!entityManager.getTransaction().isActive()) {
      entityManager.getTransaction().begin();
    }

    if (auditReader != null) {
      return auditReader;
    }

    return auditReader = AuditReaderFactory.get(entityManager);
  }