use of eu.bcvsolutions.idm.core.model.entity.IdmPassword 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