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