use of com.icthh.xm.ms.entity.domain.Attachment 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) {
startUpdateDateGenerationStrategy.preProcessStartDate(attachment, attachment.getId(), attachmentRepository, Attachment::setStartDate, Attachment::getStartDate);
Attachment result = attachmentRepository.save(attachment);
attachmentSearchRepository.save(result);
return result;
}
use of com.icthh.xm.ms.entity.domain.Attachment 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();
TypeSpec typeSpec = xmEntitySpecService.findTypeByKey(targetTypeKey);
if (typeSpec == null || ObjectUtils.isEmpty(typeSpec.getAttachments())) {
throw new IllegalStateException("Attachment type key not found for entity " + targetTypeKey);
}
// get first attachment spec type for now
String attachmentTypeKey = typeSpec.getAttachments().stream().findFirst().get().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.Attachment in project xm-ms-entity by xm-online.
the class XmEntityResourceExtendedIntTest method testAttachmentStartDate.
@Test
@Transactional
public void testAttachmentStartDate() throws Exception {
Attachment attachment = new Attachment().typeKey("A").name("1");
XmEntity entity = new XmEntity().name(" ").key(randomUUID()).typeKey("TEST_DELETE").attachments(asSet(attachment));
MutableObject<Instant> startDate = new MutableObject<>();
MutableObject<XmEntity> entityHolder = new MutableObject<>();
byte[] content = TestUtil.convertObjectToJsonBytes(entity);
restXmEntityMockMvc.perform(post("/api/xm-entities").contentType(TestUtil.APPLICATION_JSON_UTF8).content(content)).andDo(r -> entityHolder.setValue(readValue(r))).andDo(r -> log.info(r.getResponse().getContentAsString())).andExpect(status().is2xxSuccessful());
Long id = entityHolder.getValue().getId();
restXmEntityMockMvc.perform(get("/api/xm-entities/" + id)).andDo(r -> startDate.setValue(readValue(r).getAttachments().iterator().next().getStartDate())).andDo(r -> log.info(r.getResponse().getContentAsString())).andExpect(status().is2xxSuccessful());
assertNotNull(startDate.getValue());
content = TestUtil.convertObjectToJsonBytes(entityHolder.getValue());
restXmEntityMockMvc.perform(put("/api/xm-entities").contentType(TestUtil.APPLICATION_JSON_UTF8).content(content)).andDo(r -> log.info(r.getResponse().getContentAsString())).andExpect(status().is2xxSuccessful());
restXmEntityMockMvc.perform(get("/api/xm-entities/" + id)).andDo(r -> log.info(r.getResponse().getContentAsString())).andDo(r -> assertEquals(startDate.getValue(), readValue(r).getAttachments().iterator().next().getStartDate())).andExpect(status().is2xxSuccessful());
}
use of com.icthh.xm.ms.entity.domain.Attachment in project xm-ms-entity by xm-online.
the class XmEntityResourceExtendedIntTest method createEntityComplexIncoming.
/**
* Creates incoming Entity as from HTTP request.
* Potentially cam be moved to DTO
*
* @param em - Entity manager
* @return XmEntity for incoming request
*/
public static XmEntity createEntityComplexIncoming(EntityManager em) {
XmEntity entity = createEntity(em);
entity.getTags().add(new Tag().typeKey(DEFAULT_TAG_KEY).startDate(DEFAULT_START_DATE).name(DEFAULT_TAG_NAME));
entity.getAttachments().add(new Attachment().typeKey(DEFAULT_ATTACHMENT_KEY).name(DEFAULT_ATTACHMENT_NAME).startDate(DEFAULT_ATTACHMENT_START_DATE).endDate(DEFAULT_ATTACHMENT_END_DATE).valueContentType(DEFAULT_ATTACHMENT_CONTENT_TYPE).content(new Content().value(DEFAULT_ATTACHMENT_CONTENT_VALUE.getBytes())).contentUrl(DEFAULT_ATTACHMENT_URL).description(DEFAULT_ATTACHMENT_DESCRIPTION));
entity.getLocations().add(new Location().typeKey(DEFAULT_LOCATION_KEY).name(DEFAULT_LOCATION_NAME).countryKey(DEFAULT_LOCATION_COUNTRY_KEY));
return entity;
}
use of com.icthh.xm.ms.entity.domain.Attachment in project xm-ms-entity by xm-online.
the class AttachmentResourceExtendedIntTest method createAttachmentWithEntryDate.
@Test
@Transactional
public void createAttachmentWithEntryDate() throws Exception {
int databaseSizeBeforeCreate = attachmentRepository.findAll().size();
// Create the Attachment
restAttachmentMockMvc.perform(post("/api/attachments").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(attachment))).andExpect(status().isCreated()).andExpect(jsonPath("$.startDate").value(MOCKED_START_DATE.toString()));
// Validate the Attachment in the database
List<Attachment> voteList = attachmentRepository.findAll();
assertThat(voteList).hasSize(databaseSizeBeforeCreate + 1);
Attachment testAttachment = voteList.get(voteList.size() - 1);
assertThat(testAttachment.getStartDate()).isEqualTo(MOCKED_START_DATE);
// Validate the Attachment in Elasticsearch
Attachment attachmentEs = attachmentSearchRepository.findOne(testAttachment.getId());
assertThat(attachmentEs).isEqualToComparingFieldByField(testAttachment);
}
Aggregations