List of usage examples for org.hibernate.envers.query AuditEntity id
public static AuditId id()
From source file:org.azafiu.hibernatetest.datainteractor.ProductDAO.java
License:Apache License
/** * Retrieve the previous state of a given {@link ProductEntity} which was * modified earlier than the given modified date. * //from ww w .j ava 2 s .c o m * @param prodId * the id of the {@link ProductEntity} * @param revNumber * the revision number when the productDetails was modified * @return a {@link ProductEntity} object */ @Transactional public ProductEntity getPreviousStateForProduct(final Long prodId, final int revNumber) { /** * Get only the most recent {@link ProductEntity} information from the * audit tables where the wasChecked property is true and the * modifiedDate is less than the one given as parameter for the given * product details object */ final AuditQuery query = this.getAuditReader().createQuery() .forRevisionsOfEntity(ProductEntity.class, true, true) .addOrder(AuditEntity.property("modified").desc()).add(AuditEntity.id().eq(prodId)) .add(AuditEntity.property("wasChecked").eq(Boolean.TRUE)) .add(AuditEntity.revisionNumber().lt(Integer.valueOf(revNumber))).setMaxResults(1); final List<ProductEntity> resultList = query.getResultList(); if (resultList != null && resultList.size() > 0) { return resultList.get(0); } return null; }
From source file:org.azafiu.hibernatetest.datainteractor.ProductDetailsDAO.java
License:Apache License
/** * Query for all {@link ProductDetailsEntity} objects linked to a product at * a given revision.//from ww w .jav a 2s.c o m * * @param productId * the id of the related product * @param revisionDate * the date of the product change revision * @return a list of Object[]. Each element will be an Object[3] array with * the following items: Object[0] - the {@link ProductDetailsEntity} * at a revision ( greater or equal than the one given as parameter) * Object[1] a {@link DefaultRevisionEntity} Object[2] a * {@link RevisionType} object containing information about the * revision */ @Transactional public List<Object[]> getAllProductDetailsForProductAtRevision(final Long productId, final Date revisionDate) { // get the revision number based on the revision date final Number revNumber = this.getAuditReader().getRevisionNumberForDate(revisionDate); /** * Query the audit table for {@link ProductDetailsEntity}, order the * results descending based on modified property of the * {@link ProductDetailsEntity} object, get the list of objects with * revision greater or equal than the one given as parameter (the * {@link ProductDetailsEntity} may have been added in a different * revision(after the product was persisted in the database) than the * {@link ProductEntity} and we need to retrieve it) with the foreign * key for the product set to the one given as parameter and with the * wasChecked property set to false */ final AuditQuery query = this.getAuditReader().createQuery() .forRevisionsOfEntity(ProductDetailsEntity.class, false, false) .addOrder(AuditEntity.property("modified").desc()).add(AuditEntity.revisionNumber().ge(revNumber)) .add(AuditEntity.property("fkProduct").eq(productId)) .add(AuditEntity.property("wasChecked").eq(Boolean.FALSE)); final List<Object[]> resultList = query.getResultList(); final List<Object[]> result = new ArrayList<>(); /** * for each "changed" object found in the db we need to check if there * is a newer revision of it in which the {@link ProductDetailsEntity} * was approved (wasChecked = true) because we do not need to retrieve * already checked objects to the checker. */ for (final Object[] change : resultList) { final ProductDetailsEntity pe = (ProductDetailsEntity) change[0]; final AuditQuery queryForWasCheckedTrue = this.getAuditReader().createQuery() .forRevisionsOfEntity(ProductDetailsEntity.class, false, true) .addOrder(AuditEntity.property("modified").desc()).add(AuditEntity.id().eq(pe.getId())) .add(AuditEntity.property("wasChecked").eq(Boolean.TRUE)); if (pe.getModified() != null) { queryForWasCheckedTrue.add(AuditEntity.property("modified").gt(pe.getModified())); } try { final Object[] trueWasChecked = (Object[]) queryForWasCheckedTrue.getSingleResult(); } catch (final NoResultException ex) { // there is no newer revision where the current product has // wasChecked property == true result.add(change); } } return result; }
From source file:org.azafiu.hibernatetest.datainteractor.ProductDetailsDAO.java
License:Apache License
/** * Retrieve the previous state of a given {@link ProductDetailsEntity} which * was modified earlier than the given modified date. * //from ww w . j a v a2s.c om * @param prodDetailsId * the id of the {@link ProductDetailsEntity} * @param modifiedDate * the date when the productDetails was modified * @return a {@link ProductDetailsEntity} object */ @Transactional public ProductDetailsEntity getPreviousStateForProductDetails(final Long prodDetailsId, final int revNumber) { /** * Get only the most recent {@link ProductDetailsEntity} information * from the audit tables where the wasChecked property is true and the * modifiedDate is less than the one given as parameter for the given * product details object */ final AuditQuery query = this.getAuditReader().createQuery() .forRevisionsOfEntity(ProductDetailsEntity.class, true, true) .addOrder(AuditEntity.property("modified").desc()).add(AuditEntity.id().eq(prodDetailsId)) .add(AuditEntity.property("wasChecked").eq(Boolean.TRUE)) .add(AuditEntity.revisionNumber().lt(Integer.valueOf(revNumber))).setMaxResults(1); final List<ProductDetailsEntity> resultList = query.getResultList(); if (resultList != null && resultList.size() > 0) { return resultList.get(0); } return null; }
From source file:org.cast.cwm.data.models.HibernateAuditObjectModel.java
License:Open Source License
@Override @SuppressWarnings("unchecked") protected T load() { AuditReader auditReader = AuditReaderFactory.get(Databinder.getHibernateSession()); AuditQuery query = auditReader.createQuery().forEntitiesAtRevision(objectClass, revision) .add(AuditEntity.id().eq(entityId)); return (T) query.getSingleResult(); }
From source file:org.devgateway.eudevfin.financial.test.storage.RevisionsTest.java
License:Open Source License
@Test public void testQueryRevByTypeAndId() { final Long modifiedTxId = this.storageHelper.modifyOrgAndAmountOfTransaction(); // Querying for the revisions when a FinancialTransaction with id modifiedTxId was modified final AuditQuery auditQuery = this.auditReader.createQuery() .forRevisionsOfEntity(FinancialTransaction.class, false, false) .add(AuditEntity.id().eq(modifiedTxId)); final List<Object[]> auditObjects = auditQuery.getResultList(); assertTrue(auditObjects.size() > 0); for (final Object[] oArray : auditObjects) { final FinancialTransaction fTransaction = (FinancialTransaction) oArray[0]; assertNotNull(fTransaction.getExtendingAgency()); final DefaultTrackingModifiedEntitiesRevisionEntity trackingObject = (DefaultTrackingModifiedEntitiesRevisionEntity) oArray[1]; logger.info(String.format( "For transaction with id %d and amount %f and organization %s.Revision numbers is %d. Modified entities: %s ", fTransaction.getId(), fTransaction.getAmount().doubleValue(), fTransaction.getExtendingAgency().getName(), trackingObject.getId(), trackingObject.getModifiedEntityNames())); assertTrue(trackingObject.getModifiedEntityNames().contains(FinancialTransaction.class.getName())); }//from ww w. jav a 2s .c o m final DefaultTrackingModifiedEntitiesRevisionEntity trackingObject = (DefaultTrackingModifiedEntitiesRevisionEntity) auditObjects .get(auditObjects.size() - 1)[1]; // Last revision should show that also an Organization entity was modified in the same revision assertTrue(trackingObject.getModifiedEntityNames().contains(Organization.class.getName())); }
From source file:org.devgateway.eudevfin.financial.test.storage.RevisionsTest.java
License:Open Source License
/** * Showing 2 ways of getting a revision number. The test compares these 2 revision numbers. *///from w w w . j ava2s . c om @Test public void testRevisionNumbers() { final Long modifiedTxId = this.storageHelper.modifyOrgAndAmountOfTransaction(); AuditQuery auditQuery = this.auditReader.createQuery() .forRevisionsOfEntity(FinancialTransaction.class, false, false) .add(AuditEntity.id().eq(modifiedTxId)) .add(AuditEntity.property("donorProjectNumber").hasNotChanged()) .addProjection(AuditEntity.revisionNumber().max()); final Number num1 = (Number) auditQuery.getSingleResult(); logger.info("Last revision for a financial tx with unchanged description is: " + num1); auditQuery = this.auditReader.createQuery().forRevisionsOfEntity(Organization.class, false, false) .add(AuditEntity.property("code").hasChanged()).add(AuditEntity.revisionNumber().maximize()); final Object[] oArray = (Object[]) auditQuery.getSingleResult(); final DefaultTrackingModifiedEntitiesRevisionEntity trackingObject = (DefaultTrackingModifiedEntitiesRevisionEntity) oArray[1]; logger.info("Last revision for an org with changed name is: " + trackingObject.getId()); assertEquals(num1, trackingObject.getId()); }
From source file:org.gaia.dao.audit.AuditAccessor.java
License:Open Source License
public static List<Trade> getOldVersionList(Trade trade) { Session session = HibernateUtil.getSession(); List<Trade> result = new ArrayList<>(); try {// w w w .j a va 2s.co m 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; }
From source file:org.jboss.pnc.datastore.repositories.BuildConfigurationAuditedRepositoryImpl.java
License:Open Source License
@Override public List<BuildConfigurationAudited> findAllByIdOrderByRevDesc(Integer buildConfigurationId) { List<Object[]> result = AuditReaderFactory.get(entityManager).createQuery() .forRevisionsOfEntity(BuildConfiguration.class, false, false) .add(AuditEntity.id().eq(buildConfigurationId)).addOrder(AuditEntity.revisionNumber().desc()) .getResultList();// w w w . ja v a 2s . c o m List<BuildRecord> buildRecords = getBuildRecords(buildConfigurationId); return result.stream().map(o -> createAudited(o[0], o[1], buildRecords)).collect(Collectors.toList()); }
From source file:org.jboss.pnc.model.BuildConfigurationTest.java
License:Open Source License
private BuildConfiguration getByIdRev(Integer buildConfigurationId, Integer revision) { return (BuildConfiguration) AuditReaderFactory.get(em).createQuery() .forEntitiesAtRevision(BuildConfiguration.class, revision) .add(AuditEntity.id().eq(buildConfigurationId)).addOrder(AuditEntity.revisionNumber().desc()) .getSingleResult();/*from w w w . j a v a2 s . c o m*/ }
From source file:org.jboss.pressgang.ccms.model.contentspec.ContentSpecToPropertyTag.java
License:Open Source License
@Override protected boolean testUnique(final EntityManager entityManager, final Number revision) { if (propertyTag.getPropertyTagIsUnique()) { /*/* w w w. j av a 2 s.c o m*/ * Since having to iterate over thousands of entities is slow, use a HQL query to find the count for us. */ final Long count; if (revision == null) { final String query = ContentSpecToPropertyTag.SELECT_SIZE_QUERY + " WHERE contentSpecToPropertyTag.propertyTag" + ".propertyTagId = :propertyTagId AND contentSpecToPropertyTag.value = :value"; final Query entityQuery = entityManager.createQuery(query); entityQuery.setParameter("value", getValue()); entityQuery.setParameter("propertyTagId", getPropertyTag().getId()); count = (Long) entityQuery.getSingleResult(); } else { final AuditReader reader = AuditReaderFactory.get(entityManager); final AuditQueryCreator queryCreator = reader.createQuery(); final AuditQuery query = queryCreator .forEntitiesAtRevision(ContentSpecToPropertyTag.class, revision) .addProjection(AuditEntity.id().count("contentSpecToPropertyTagId")) .add(AuditEntity.relatedId("propertyTag").eq(getPropertyTag().getId())) .add(AuditEntity.property("value").eq(getValue())); query.setCacheable(true); count = (Long) query.getSingleResult(); } if (count > 1) return false; } return true; }