Search in sources :

Example 1 with AttachmentSpec

use of com.icthh.xm.ms.entity.domain.spec.AttachmentSpec in project xm-ms-entity by xm-online.

the class AttachmentServiceImplUnitTest method shouldFailIfMaxSizeIsZero.

@Test
public void shouldFailIfMaxSizeIsZero() {
    AttachmentSpec spec = new AttachmentSpec();
    spec.setMax(0);
    exception.expect(BusinessException.class);
    exception.expect(hasProperty("code", is(AttachmentService.ZERO_RESTRICTION)));
    attachmentService.assertZeroRestriction(spec);
}
Also used : AttachmentSpec(com.icthh.xm.ms.entity.domain.spec.AttachmentSpec) Test(org.junit.Test) AbstractUnitTest(com.icthh.xm.ms.entity.AbstractUnitTest)

Example 2 with AttachmentSpec

use of com.icthh.xm.ms.entity.domain.spec.AttachmentSpec in project xm-ms-entity by xm-online.

the class AttachmentService method save.

/**
 * Save a attachment.
 *
 * @param attachment the entity to save
 * @return the persisted entity
 */
@LogicExtensionPoint("Save")
public Attachment save(Attachment attachment) {
    Objects.nonNull(attachment);
    Objects.nonNull(attachment.getXmEntity());
    Objects.nonNull(attachment.getXmEntity().getId());
    startUpdateDateGenerationStrategy.preProcessStartDate(attachment, attachment.getId(), attachmentRepository, Attachment::setStartDate, Attachment::getStartDate);
    XmEntity entity = xmEntityRepository.findById(attachment.getXmEntity().getId()).orElseThrow(() -> new EntityNotFoundException("No entity found by id: " + attachment.getXmEntity().getId()));
    AttachmentSpec spec = getSpec(entity, attachment);
    // check only for addingNew
    if (attachment.getId() == null && spec.getMax() != null) {
        // forbid to add element if spec.max = 0
        assertZeroRestriction(spec);
        // forbid to add element if spec.max <= addedSize
        assertLimitRestriction(spec, entity);
    }
    attachment.setXmEntity(entity);
    if (attachment.getContent() != null) {
        byte[] content = attachment.getContent().getValue();
        attachment.setContentChecksum(DigestUtils.sha256Hex(content));
    }
    return attachmentRepository.save(attachment);
}
Also used : AttachmentSpec(com.icthh.xm.ms.entity.domain.spec.AttachmentSpec) XmEntity(com.icthh.xm.ms.entity.domain.XmEntity) Attachment(com.icthh.xm.ms.entity.domain.Attachment) EntityNotFoundException(com.icthh.xm.commons.exceptions.EntityNotFoundException) LogicExtensionPoint(com.icthh.xm.commons.lep.LogicExtensionPoint)

Example 3 with AttachmentSpec

use of com.icthh.xm.ms.entity.domain.spec.AttachmentSpec in project xm-ms-entity by xm-online.

the class XmEntitySpecService method getAllKeys.

/**
 * Transforms all XmEntity Specification keys into the thin structure based
 * in maps and sets.
 *
 * @return thin structure based in maps and sets
 */
@IgnoreLogginAspect
public Map<String, Map<String, Set<String>>> getAllKeys() {
    Map<String, Map<String, Set<String>>> result = Maps.newHashMap();
    for (TypeSpec typeSpec : findAllTypes()) {
        Map<String, Set<String>> subKeys = Maps.newHashMap();
        getKeys(subKeys, AttachmentSpec.class, typeSpec.getAttachments(), AttachmentSpec::getKey);
        getKeys(subKeys, CalendarSpec.class, typeSpec.getCalendars(), CalendarSpec::getKey);
        getKeys(subKeys, LinkSpec.class, typeSpec.getLinks(), LinkSpec::getKey);
        getKeys(subKeys, LocationSpec.class, typeSpec.getLocations(), LocationSpec::getKey);
        getKeys(subKeys, RatingSpec.class, typeSpec.getRatings(), RatingSpec::getKey);
        getKeys(subKeys, StateSpec.class, typeSpec.getStates(), StateSpec::getKey);
        getKeys(subKeys, TagSpec.class, typeSpec.getTags(), TagSpec::getKey);
        result.put(typeSpec.getKey(), subKeys);
    }
    return result;
}
Also used : AttachmentSpec(com.icthh.xm.ms.entity.domain.spec.AttachmentSpec) LocationSpec(com.icthh.xm.ms.entity.domain.spec.LocationSpec) Set(java.util.Set) RatingSpec(com.icthh.xm.ms.entity.domain.spec.RatingSpec) StateSpec(com.icthh.xm.ms.entity.domain.spec.StateSpec) TagSpec(com.icthh.xm.ms.entity.domain.spec.TagSpec) LinkSpec(com.icthh.xm.ms.entity.domain.spec.LinkSpec) CalendarSpec(com.icthh.xm.ms.entity.domain.spec.CalendarSpec) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TypeSpec(com.icthh.xm.ms.entity.domain.spec.TypeSpec) IgnoreLogginAspect(com.icthh.xm.commons.logging.aop.IgnoreLogginAspect)

Example 4 with AttachmentSpec

use of com.icthh.xm.ms.entity.domain.spec.AttachmentSpec in project xm-ms-entity by xm-online.

the class XmEntityServiceImpl method addFileAttachment.

@Override
public XmEntity addFileAttachment(XmEntity entity, MultipartFile file) {
    // save multipart file to storage
    String storedFileName = storageService.store(file, null);
    log.debug("Multipart file stored with name {}", storedFileName);
    String targetTypeKey = entity.getTypeKey();
    List<AttachmentSpec> attachmentSpecs = xmEntitySpecService.getTypeSpecByKey(targetTypeKey).map(TypeSpec::getAttachments).orElseThrow(() -> new IllegalStateException("Attachment type key not found for entity " + targetTypeKey));
    // get first attachment spec type for now
    String attachmentTypeKey = attachmentSpecs.stream().findFirst().orElseThrow(() -> new IllegalArgumentException("Attachment typeKey not found for key " + targetTypeKey)).getKey();
    log.debug("Attachment type key {}", attachmentTypeKey);
    Attachment attachment = attachmentService.save(new Attachment().typeKey(attachmentTypeKey).name(file.getOriginalFilename()).contentUrl(storedFileName).startDate(Instant.now()).valueContentType(file.getContentType()).valueContentSize(file.getSize()).xmEntity(entity));
    log.debug("Attachment stored with id {}", attachment.getId());
    entity.getAttachments().add(attachment);
    return entity;
}
Also used : AttachmentSpec(com.icthh.xm.ms.entity.domain.spec.AttachmentSpec) Attachment(com.icthh.xm.ms.entity.domain.Attachment)

Example 5 with AttachmentSpec

use of com.icthh.xm.ms.entity.domain.spec.AttachmentSpec in project xm-ms-entity by xm-online.

the class AttachmentServiceImplUnitTest method shouldPassIfSpecProvided.

@Test
public void shouldPassIfSpecProvided() {
    XmEntity e = new XmEntity();
    e.setTypeKey("TYPE");
    Attachment a = new Attachment();
    a.setTypeKey("TYPE.A");
    AttachmentSpec spec = new AttachmentSpec();
    spec.setKey("TYPE.A");
    when(xmEntitySpecService.findAttachment("TYPE", "TYPE.A")).thenReturn(Optional.of(spec));
    assertThat(attachmentService.getSpec(e, a).getKey()).isEqualTo("TYPE.A");
}
Also used : AttachmentSpec(com.icthh.xm.ms.entity.domain.spec.AttachmentSpec) XmEntity(com.icthh.xm.ms.entity.domain.XmEntity) Attachment(com.icthh.xm.ms.entity.domain.Attachment) Test(org.junit.Test) AbstractUnitTest(com.icthh.xm.ms.entity.AbstractUnitTest)

Aggregations

AttachmentSpec (com.icthh.xm.ms.entity.domain.spec.AttachmentSpec)9 AbstractUnitTest (com.icthh.xm.ms.entity.AbstractUnitTest)6 Attachment (com.icthh.xm.ms.entity.domain.Attachment)6 XmEntity (com.icthh.xm.ms.entity.domain.XmEntity)6 Test (org.junit.Test)6 Content (com.icthh.xm.ms.entity.domain.Content)3 EntityNotFoundException (com.icthh.xm.commons.exceptions.EntityNotFoundException)1 LogicExtensionPoint (com.icthh.xm.commons.lep.LogicExtensionPoint)1 IgnoreLogginAspect (com.icthh.xm.commons.logging.aop.IgnoreLogginAspect)1 CalendarSpec (com.icthh.xm.ms.entity.domain.spec.CalendarSpec)1 LinkSpec (com.icthh.xm.ms.entity.domain.spec.LinkSpec)1 LocationSpec (com.icthh.xm.ms.entity.domain.spec.LocationSpec)1 RatingSpec (com.icthh.xm.ms.entity.domain.spec.RatingSpec)1 StateSpec (com.icthh.xm.ms.entity.domain.spec.StateSpec)1 TagSpec (com.icthh.xm.ms.entity.domain.spec.TagSpec)1 TypeSpec (com.icthh.xm.ms.entity.domain.spec.TypeSpec)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Set (java.util.Set)1