Search in sources :

Example 1 with AuditDisjunction

use of org.hibernate.envers.query.criteria.AuditDisjunction in project hibernate-orm by hibernate.

the class StoreDeletedData method testMaximizeInDisjunction.

@Test
@TestForIssue(jiraKey = "HHH-7800")
public void testMaximizeInDisjunction() {
    List<Integer> queryIds = Arrays.asList(id2, id3);
    AuditDisjunction disjunction = AuditEntity.disjunction();
    for (Integer id : queryIds) {
        AuditCriterion crit = AuditEntity.revisionNumber().maximize().add(AuditEntity.id().eq(id)).add(AuditEntity.revisionType().ne(RevisionType.DEL));
        disjunction.add(crit);
    // Workaround: using this line instead works correctly:
    // disjunction.add(AuditEntity.conjunction().add(crit));
    }
    List<?> beforeDeletionRevisions = getAuditReader().createQuery().forRevisionsOfEntity(StrIntTestEntity.class, false, false).add(disjunction).addOrder(AuditEntity.property("id").asc()).getResultList();
    Assert.assertEquals(2, beforeDeletionRevisions.size());
    Object[] result1 = (Object[]) beforeDeletionRevisions.get(0);
    Object[] result2 = (Object[]) beforeDeletionRevisions.get(1);
    Assert.assertEquals(new StrIntTestEntity("b", 20, id2), result1[0]);
    // Making sure that we have received an entity added at revision 3.
    Assert.assertEquals(3, ((SequenceIdRevisionEntity) result1[1]).getId());
    Assert.assertEquals(RevisionType.ADD, result1[2]);
    Assert.assertEquals(new StrIntTestEntity("c", 30, id3), result2[0]);
    // Making sure that we have received an entity added at revision 3.
    Assert.assertEquals(3, ((SequenceIdRevisionEntity) result2[1]).getId());
    Assert.assertEquals(RevisionType.ADD, result2[2]);
}
Also used : StrIntTestEntity(org.hibernate.envers.test.entities.StrIntTestEntity) AuditDisjunction(org.hibernate.envers.query.criteria.AuditDisjunction) AuditCriterion(org.hibernate.envers.query.criteria.AuditCriterion) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with AuditDisjunction

use of org.hibernate.envers.query.criteria.AuditDisjunction in project hibernate-orm by hibernate.

the class MaximalizePropertyQuery method testMaximizeInDisjunction.

@Test
@TestForIssue(jiraKey = "HHH-7800")
public void testMaximizeInDisjunction() {
    List<Integer> idsToQuery = Arrays.asList(id1, id3);
    AuditDisjunction disjunction = AuditEntity.disjunction();
    for (Integer id : idsToQuery) {
        disjunction.add(AuditEntity.revisionNumber().maximize().add(AuditEntity.id().eq(id)));
    }
    List result = getAuditReader().createQuery().forRevisionsOfEntity(StrIntTestEntity.class, true, true).add(disjunction).getResultList();
    Set<Integer> idsSeen = new HashSet<Integer>();
    for (Object o : result) {
        StrIntTestEntity entity = (StrIntTestEntity) o;
        Integer id = entity.getId();
        Assert.assertTrue("Entity with ID " + id + " returned but not queried for.", idsToQuery.contains(id));
        if (!idsSeen.add(id)) {
            Assert.fail("Multiple revisions returned with ID " + id + "; expected only one.");
        }
    }
}
Also used : StrIntTestEntity(org.hibernate.envers.test.entities.StrIntTestEntity) AuditDisjunction(org.hibernate.envers.query.criteria.AuditDisjunction) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 3 with AuditDisjunction

use of org.hibernate.envers.query.criteria.AuditDisjunction in project CzechIdMng by bcvsolutions.

the class DefaultAuditService method findLogin.

@Override
public Page<IdmAuditDto> findLogin(IdmAuditFilter filter, Pageable pageable) {
    // 
    if (pageable == null) {
        // pageable is required noe
        pageable = PageRequest.of(0, Integer.MAX_VALUE);
    }
    // Create audit query for specific login audit
    // Conjunction solve connection between successful and failed query
    AuditConjunction conjunction = AuditEntity.conjunction();
    // Disjunctions solves connection between each query condition, there are two conditions for successful and failed logins
    AuditConjunction conjunctionForSuccessful = AuditEntity.conjunction();
    AuditConjunction conjunctionForFailed = AuditEntity.conjunction();
    AuditDisjunction disjunction = AuditEntity.disjunction();
    conjunctionForFailed.add(AuditEntity.revisionProperty(IdmAudit_.changedAttributes.getName()).ilike(IdmPassword_.unsuccessfulAttempts.getName(), MatchMode.ANYWHERE));
    conjunctionForFailed.add(AuditEntity.property(IdmPassword_.modifier.getName()).eq(SecurityService.GUEST_NAME));
    conjunctionForSuccessful.add(AuditEntity.revisionProperty(IdmAudit_.changedAttributes.getName()).ilike(IdmPassword_.lastSuccessfulLogin.getName(), MatchMode.ANYWHERE));
    // Self created relation, created by expression
    conjunctionForSuccessful.add(new IdmPasswordSelfRelationWithOwnerExpression());
    disjunction.add(conjunctionForSuccessful);
    disjunction.add(conjunctionForFailed);
    conjunction.add(disjunction);
    if (StringUtils.isNotEmpty(filter.getOwnerId())) {
        // 'coleration' attribute for connection with IdmAudit entity - ownerId
        conjunction.add(AuditEntity.revisionProperty(IdmAudit_.ownerId.getName()).eq(filter.getOwnerId()));
    }
    if (StringUtils.isNotEmpty(filter.getOwnerCode())) {
        // 'coleration' attribute for connection with IdmAudit entity - ownerCode
        conjunction.add(AuditEntity.revisionProperty(IdmAudit_.ownerCode.getName()).eq(filter.getOwnerCode()));
    }
    if (filter.getFrom() != null) {
        conjunction.add(AuditEntity.revisionProperty(IdmAudit_.timestamp.getName()).ge(filter.getFrom().toInstant().toEpochMilli()));
    }
    if (filter.getTill() != null) {
        conjunction.add(AuditEntity.revisionProperty(IdmAudit_.timestamp.getName()).le(filter.getTill().toInstant().toEpochMilli()));
    }
    // Count is for pageable and check if is required made query
    Object count = this.getAuditReader().createQuery().forRevisionsOfEntity(IdmPassword.class, false, true).add(conjunction).addProjection(AuditEntity.id().count()).getSingleResult();
    Long countAsLong = null;
    if (count instanceof Long) {
        countAsLong = (Long) count;
    }
    // Count is zero. Count is for queries better than real query
    if (countAsLong == null || countAsLong == 0) {
        return new PageImpl<IdmAuditDto>(Collections.emptyList(), pageable, 0);
    }
    // Create final query and solve pagination and order
    AuditQuery query = this.getAuditReader().createQuery().forRevisionsOfEntity(IdmPassword.class, false, true).add(conjunction);
    int maxResults = pageable.getPageSize();
    int firstResult = pageable.getPageSize() * pageable.getPageNumber();
    query.setMaxResults(maxResults).setFirstResult(firstResult);
    Sort sort = pageable.getSort();
    if (sort != null) {
        sort.forEach(order -> {
            AuditProperty<Object> property = AuditEntity.revisionProperty(order.getProperty());
            if (order.isAscending()) {
                query.addOrder(property.asc());
            } else {
                query.addOrder(property.desc());
            }
        });
    }
    // Returned list contains three object IdmAudit, Version (IdmPassword) and type of modification
    @SuppressWarnings("unchecked") List<Object[]> resultList = query.getResultList();
    // We doesn't need made again get for version, because version is in result form audit query
    filter.setWithVersion(Boolean.FALSE);
    // Iterate over all result and transform it into dtos
    List<IdmAuditDto> result = new ArrayList<>(resultList.size());
    for (Object[] object : resultList) {
        Object version = object[PROPERTY_AUDIT_VERSION];
        IdmAudit entity = (IdmAudit) object[PROPERTY_AUDIT_ENTITY];
        IdmAuditEntityDto newDto = (IdmAuditEntityDto) super.toDto(entity, new IdmAuditEntityDto(), filter);
        newDto.setEntity(getValuesFromVersion(version));
        result.add(newDto);
    }
    return new PageImpl<IdmAuditDto>(result, pageable, countAsLong);
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) IdmPassword(eu.bcvsolutions.idm.core.model.entity.IdmPassword) AuditQuery(org.hibernate.envers.query.AuditQuery) IdmAuditEntityDto(eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditEntityDto) AuditConjunction(org.hibernate.envers.query.criteria.AuditConjunction) AuditDisjunction(org.hibernate.envers.query.criteria.AuditDisjunction) IdmAudit(eu.bcvsolutions.idm.core.audit.entity.IdmAudit) IdmAuditDto(eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditDto) ArrayList(java.util.ArrayList) IdmPasswordSelfRelationWithOwnerExpression(eu.bcvsolutions.idm.core.api.audit.criteria.IdmPasswordSelfRelationWithOwnerExpression) Sort(org.springframework.data.domain.Sort)

Aggregations

AuditDisjunction (org.hibernate.envers.query.criteria.AuditDisjunction)3 StrIntTestEntity (org.hibernate.envers.test.entities.StrIntTestEntity)2 TestForIssue (org.hibernate.testing.TestForIssue)2 Test (org.junit.Test)2 IdmPasswordSelfRelationWithOwnerExpression (eu.bcvsolutions.idm.core.api.audit.criteria.IdmPasswordSelfRelationWithOwnerExpression)1 IdmAuditDto (eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditDto)1 IdmAuditEntityDto (eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditEntityDto)1 IdmAudit (eu.bcvsolutions.idm.core.audit.entity.IdmAudit)1 IdmPassword (eu.bcvsolutions.idm.core.model.entity.IdmPassword)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 AuditQuery (org.hibernate.envers.query.AuditQuery)1 AuditConjunction (org.hibernate.envers.query.criteria.AuditConjunction)1 AuditCriterion (org.hibernate.envers.query.criteria.AuditCriterion)1 PageImpl (org.springframework.data.domain.PageImpl)1 Sort (org.springframework.data.domain.Sort)1