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]);
}
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.");
}
}
}
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);
}
Aggregations