Search in sources :

Example 1 with IdmAttachment

use of eu.bcvsolutions.idm.core.ecm.entity.IdmAttachment in project CzechIdMng by bcvsolutions.

the class DefaultAttachmentManager method toPredicates.

@Override
protected List<Predicate> toPredicates(Root<IdmAttachment> root, CriteriaQuery<?> query, CriteriaBuilder builder, IdmAttachmentFilter filter) {
    List<Predicate> predicates = super.toPredicates(root, query, builder, filter);
    // 
    // text - owner type, owner code
    String text = filter.getText();
    if (StringUtils.isNotEmpty(text)) {
        text = text.toLowerCase();
        predicates.add(builder.or(builder.like(builder.lower(root.get(IdmAttachment_.name)), "%" + text + "%"), builder.like(builder.lower(root.get(IdmAttachment_.description)), "%" + text + "%")));
    }
    // owner id
    UUID ownerId = filter.getOwnerId();
    if (ownerId != null) {
        predicates.add(builder.equal(root.get(IdmAttachment_.ownerId), ownerId));
    }
    // owner type
    String ownerType = filter.getOwnerType();
    if (StringUtils.isNotEmpty(ownerType)) {
        predicates.add(builder.equal(root.get(IdmAttachment_.ownerType), ownerType));
    }
    // name
    String name = filter.getName();
    if (StringUtils.isNotEmpty(name)) {
        predicates.add(builder.equal(root.get(IdmAttachment_.name), name));
    }
    // last version only
    Boolean lastVersionOnly = filter.getLastVersionOnly();
    if (lastVersionOnly != null && lastVersionOnly) {
        predicates.add(builder.isNull(root.get(IdmAttachment_.nextVersion)));
    }
    // versions
    UUID versionsFor = filter.getVersionsFor();
    if (versionsFor != null) {
        CriteriaQuery<IdmAttachment> subCriteriaQuery = builder.createQuery(getEntityClass());
        Subquery<IdmAttachment> subquery = subCriteriaQuery.subquery(getEntityClass());
        Root<IdmAttachment> subRoot = subquery.from(getEntityClass());
        subquery.select(subRoot.get(IdmAttachment_.parent));
        subquery.where(builder.equal(subRoot.get(IdmAttachment_.id), versionsFor));
        predicates.add(builder.or(builder.equal(root.get(IdmAttachment_.parent).get(IdmAttachment_.id), versionsFor), builder.equal(root.get(IdmAttachment_.id), versionsFor), builder.equal(root.get(IdmAttachment_.parent).get(IdmAttachment_.id), subquery), builder.equal(root, subquery)));
    }
    // created before
    ZonedDateTime createdBefore = filter.getCreatedBefore();
    if (createdBefore != null) {
        predicates.add(builder.lessThan(root.get(IdmAttachment_.created), createdBefore));
    }
    // created after
    ZonedDateTime createdAfter = filter.getCreatedAfter();
    if (createdAfter != null) {
        predicates.add(builder.greaterThan(root.get(IdmAttachment_.created), createdAfter));
    }
    String attachmentType = filter.getAttachmentType();
    if (StringUtils.isNotEmpty(attachmentType)) {
        predicates.add(builder.equal(root.get(IdmAttachment_.attachmentType), attachmentType));
    }
    // 
    return predicates;
}
Also used : ZonedDateTime(java.time.ZonedDateTime) UUID(java.util.UUID) Predicate(javax.persistence.criteria.Predicate) IdmAttachment(eu.bcvsolutions.idm.core.ecm.entity.IdmAttachment)

Example 2 with IdmAttachment

use of eu.bcvsolutions.idm.core.ecm.entity.IdmAttachment in project CzechIdMng by bcvsolutions.

the class DefaultAttachmentManagerUnitTest method testAttachmentInputStreamIsClosedAfterUpdate.

@Test
public void testAttachmentInputStreamIsClosedAfterUpdate() throws IOException {
    when(attachmentConfiguration.getStoragePath()).thenReturn("target");
    when(repository.saveAndFlush(any())).thenReturn(new IdmAttachment());
    InputStream inputStreamSpy = Mockito.spy(IOUtils.toInputStream("mock"));
    // 
    IdmProfile owner = new IdmProfile(UUID.randomUUID());
    IdmAttachmentDto attachment = new IdmAttachmentDto();
    attachment.setId(UUID.randomUUID());
    attachment.setName("mock");
    attachment.setInputData(inputStreamSpy);
    attachment.setOwnerId(owner.getId());
    attachment.setOwnerType("mock");
    // 
    attachmentManager.updateAttachment(attachment);
    // 
    Assert.assertNull(attachment.getInputData());
    Mockito.verify(inputStreamSpy).close();
    Mockito.verify(repository).saveAndFlush(Mockito.any(IdmAttachment.class));
    Mockito.verify(repository).findById(Mockito.any(UUID.class));
    Mockito.verify(attachmentConfiguration, times(2)).getStoragePath();
}
Also used : IdmAttachmentDto(eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto) IdmProfile(eu.bcvsolutions.idm.core.model.entity.IdmProfile) InputStream(java.io.InputStream) UUID(java.util.UUID) IdmAttachment(eu.bcvsolutions.idm.core.ecm.entity.IdmAttachment) AbstractVerifiableUnitTest(eu.bcvsolutions.idm.test.api.AbstractVerifiableUnitTest) Test(org.junit.Test)

Aggregations

IdmAttachment (eu.bcvsolutions.idm.core.ecm.entity.IdmAttachment)2 UUID (java.util.UUID)2 IdmAttachmentDto (eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto)1 IdmProfile (eu.bcvsolutions.idm.core.model.entity.IdmProfile)1 AbstractVerifiableUnitTest (eu.bcvsolutions.idm.test.api.AbstractVerifiableUnitTest)1 InputStream (java.io.InputStream)1 ZonedDateTime (java.time.ZonedDateTime)1 Predicate (javax.persistence.criteria.Predicate)1 Test (org.junit.Test)1