Example usage for org.hibernate.envers AuditReaderFactory get

List of usage examples for org.hibernate.envers AuditReaderFactory get

Introduction

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

Prototype

public static AuditReader get(EntityManager entityManager) throws AuditException 

Source Link

Document

Create an audit reader associated with an open entity manager.

Usage

From source file:com.confighub.core.store.Store.java

License:Open Source License

public RepoFile getRepoFile(final UserAccount user, final Repository repository, final Long fileId,
        final Date time) throws ConfigException {
    if (!repository.hasReadAccess(user)) {
        throw new ConfigException(Error.Code.USER_ACCESS_DENIED);
    }/* w  w w .jav  a2 s  . c o  m*/

    if (null == time) {
        RepoFile file = get(RepoFile.class, fileId);
        if (file.getRepository().equals(repository)) {
            return file;
        } else {
            throw new ConfigException(Error.Code.FILE_NOT_FOUND);
        }
    }

    AuditReader reader = AuditReaderFactory.get(em);
    Number rev = reader.getRevisionNumberForDate(time);

    AuditQuery query = reader.createQuery().forEntitiesAtRevision(RepoFile.class, rev);
    query.add(AuditEntity.property("repository").eq(repository));
    query.add(AuditEntity.id().eq(fileId));

    try {
        return (RepoFile) query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    } catch (Exception e) {
        handleException(e);
        return null;
    }
}

From source file:com.confighub.core.store.Store.java

License:Open Source License

public Collection<RepoFile> getRepoFilesForAPI(final Repository repository, final Date date)
        throws ConfigException {
    if (null == repository) {
        throw new ConfigException(Error.Code.REPOSITORY_NOT_FOUND);
    }//from  w w w . j  av  a  2 s  .c  o  m

    if (null == date) {
        return repository.getFiles();
    }

    try {
        AuditReader reader = AuditReaderFactory.get(em);
        Number rev = reader.getRevisionNumberForDate(date);

        AuditQuery query = reader.createQuery().forEntitiesAtRevision(RepoFile.class, rev)
                .add(AuditEntity.property("repository").eq(repository));

        return query.getResultList();
    } catch (NoResultException e) {
        return null;
    } catch (Exception e) {
        handleException(e);
        return null;
    }
}

From source file:com.confighub.core.store.Store.java

License:Open Source License

public Collection<RepoFile> getRepoFiles(final Repository repository, final UserAccount user,
        final String searchTerm, final Date date) throws ConfigException {
    if (Utils.anyNull(repository)) {
        throw new ConfigException(Error.Code.MISSING_PARAMS);
    }//from  w w  w  .  j a v  a2  s . c  om

    //        if (!repository.isDemo() && null == user)
    //            throw new ConfigException(Error.Code.MISSING_PARAMS);
    //
    if (!repository.isDemo() && !repository.hasReadAccess(user)) {
        throw new ConfigException(Error.Code.USER_ACCESS_DENIED);
    }

    if (null == date) {
        if (Utils.isBlank(searchTerm)) {
            return repository.getFiles();
        } else {
            try {
                return em.createNamedQuery("RepoFile.search").setLockMode(LockModeType.NONE)
                        .setParameter("repository", repository)
                        .setParameter("searchTerm", "%" + searchTerm + "%").getResultList();
            } catch (NoResultException e) {
                return null;
            } catch (Exception e) {
                handleException(e);
                return null;
            }
        }
    } else {
        try {
            AuditReader reader = AuditReaderFactory.get(em);
            Number rev = reader.getRevisionNumberForDate(date);

            AuditQuery query = reader.createQuery().forEntitiesAtRevision(RepoFile.class, rev)
                    .add(AuditEntity.property("repository").eq(repository));

            if (!Utils.isBlank(searchTerm)) {
                query.add(AuditEntity.property("content").like("%" + searchTerm + "%"));
            }

            return query.getResultList();
        } catch (NoResultException e) {
            return null;
        } catch (Exception e) {
            handleException(e);
            return null;
        }
    }
}

From source file:com.confighub.core.store.Store.java

License:Open Source License

public RepoFile getAuditConfigFile(final UserAccount user, final Repository repository, final Long fileId,
        final Long revId) throws ConfigException {
    try {//from ww w . jav a 2  s. co  m
        if (!repository.hasReadAccess(user)) {
            throw new ConfigException(Error.Code.USER_ACCESS_DENIED);
        }

        AuditReader reader = AuditReaderFactory.get(em);

        AuditQuery query = reader.createQuery().forRevisionsOfEntity(RepoFile.class, true, true)
                .add(AuditEntity.revisionNumber().ge(revId)).add(AuditEntity.revisionNumber().le(revId))
                .add(AuditEntity.property("repository").eq(repository)).add(AuditEntity.id().eq(fileId));

        return (RepoFile) query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    } catch (Exception e) {
        handleException(e);
        return null;
    }
}

From source file:com.confighub.core.store.Store.java

License:Open Source License

public List<PropertyKey> getKeys(final UserAccount user, final Repository repository,
        final Collection<String> keys, final Date time) throws ConfigException {
    if (Utils.anyNull(repository, keys)) {
        throw new ConfigException(Error.Code.MISSING_PARAMS);
    }//from  ww  w .ja  v a  2 s.c  o  m

    if (!repository.hasReadAccess(user)) {
        throw new ConfigException(Error.Code.USER_ACCESS_DENIED);
    }

    if (null == time) {
        Collection<String> upperKeys = new ArrayList<>();
        keys.forEach(k -> upperKeys.add(k.toUpperCase()));

        try {
            return em.createNamedQuery("Key.getKeys").setLockMode(LockModeType.NONE)
                    .setParameter("repository", repository).setParameter("keys", upperKeys).getResultList();
        } catch (NoResultException e) {
            return Collections.EMPTY_LIST;
        } catch (Exception e) {
            handleException(e);
            return Collections.EMPTY_LIST;
        }
    }

    AuditReader reader = AuditReaderFactory.get(em);
    Number rev = reader.getRevisionNumberForDate(time);

    AuditQuery query = reader.createQuery().forEntitiesAtRevision(PropertyKey.class, rev);
    query.add(AuditEntity.property("repository").eq(repository));
    query.add(AuditEntity.property("key").in(keys));

    try {
        return query.getResultList();
    } catch (NoResultException e) {
        return null;
    } catch (Exception e) {
        handleException(e);
        return null;
    }
}

From source file:com.griffinslogistics.dao.DAO.java

@Override
public List getRevisions(Class aClass, Long id) {
    AuditReader auditReader = AuditReaderFactory.get(this.sessionFactory.getCurrentSession());
    AuditQuery query = auditReader.createQuery().forRevisionsOfEntity(aClass, false, true)
            .add(AuditEntity.property("id").eq(id)).addOrder(AuditEntity.revisionNumber().desc());
    List result = query.getResultList();

    return result;
}

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

@Override
@Transactional(readOnly = true)/*from w w w  .ja  v a2  s. c  o m*/
public ContactAudit findAuditByRevision(Long id, int revision) {
    AuditReader auditReader = AuditReaderFactory.get(entityManager);
    return auditReader.find(ContactAudit.class, id, revision);
}

From source file:com.impetus.ankush.common.service.ConfigurationManager.java

License:Open Source License

/**
 * Gets the configuration./*from   ww  w .ja va  2s . co m*/
 * 
 * @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;

}

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

License:Open Source License

/**
 * /* w w  w.  j a  v a2  s.c  o  m*/
 * @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;
}

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

License:Open Source License

/**
 * gets the entity at the specified revision
 * /*from   w w w  . ja  va2  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
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;
}