use of com.icthh.xm.ms.entity.domain.spec.TypeSpec in project xm-ms-entity by xm-online.
the class XmEntityGeneratorService method generateXmEntity.
public XmEntity generateXmEntity(String rootTypeKey) {
TypeSpec typeSpec = getRandomTypeSpec(rootTypeKey);
String name = typeSpec.getName().entrySet().iterator().next().getValue();
if (!Constants.TENANT_TYPE_KEY.equals(rootTypeKey)) {
name = name.concat(" name");
} else {
name = name.concat(String.valueOf(System.currentTimeMillis()));
}
XmEntity xmEntity = new XmEntity().key(UUID.randomUUID().toString()).typeKey(typeSpec.getKey()).stateKey(generateXmEntityState(xmEntitySpecService, typeSpec)).name(name).startDate(generateXmEntityStartDate()).updateDate(Instant.now()).endDate(generateXmEntityEndDate()).description(String.format("Generated [%s] Generator version [%s]", typeSpec.getName().entrySet().iterator().next().getValue(), GENERATOR_VERSION)).avatarUrl(generateXmEntityAvatarUrl()).data(generateXmEntityData(typeSpec)).locations(generateLocations(typeSpec.getLocations())).tags(generateTags(typeSpec)).createdBy(authContextHolder.getContext().getRequiredUserKey());
return xmEntityService.save(xmEntity);
}
use of com.icthh.xm.ms.entity.domain.spec.TypeSpec in project xm-ms-entity by xm-online.
the class XmEntityServiceImpl method exportEntities.
@LogicExtensionPoint("Export")
@Override
public byte[] exportEntities(String fileFormat, String typeKey) {
Set<String> typeKeys = xmEntitySpecService.findNonAbstractTypesByPrefix(typeKey).stream().map(TypeSpec::getKey).collect(Collectors.toSet());
List<XmEntity> xmEntities = xmEntityRepository.findAllByTypeKeyIn(new PageRequest(0, Integer.MAX_VALUE), typeKeys).getContent();
ModelMapper modelMapper = new ModelMapper();
List<SimpleExportXmEntityDto> simpleEntities = xmEntities.stream().map(entity -> modelMapper.map(entity, SimpleExportXmEntityDto.class)).collect(Collectors.toList());
switch(FileFormatEnum.valueOf(fileFormat.toUpperCase())) {
case CSV:
return EntityToCsvConverterUtils.toCsv(simpleEntities, SimpleExportXmEntityDto.class);
case XLSX:
return EntityToExcelConverterUtils.toExcel(simpleEntities, typeKey);
default:
throw new BusinessException(ErrorConstants.ERR_VALIDATION, String.format("Converter doesn't support '%s' file format", fileFormat));
}
}
use of com.icthh.xm.ms.entity.domain.spec.TypeSpec 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.spec.TypeSpec in project xm-ms-entity by xm-online.
the class StateKeyValidator method isValid.
@Override
public boolean isValid(XmEntity value, ConstraintValidatorContext context) {
TypeSpec typeSpec = xmEntitySpecService.findTypeByKey(value.getTypeKey());
if (typeSpec == null) {
return true;
}
if (isEmpty(typeSpec.getStates()) && value.getStateKey() == null) {
return true;
}
List<StateSpec> stateSpecs = typeSpec.getStates();
stateSpecs = (stateSpecs != null) ? stateSpecs : Collections.emptyList();
Set<String> stateKeys = stateSpecs.stream().map(StateSpec::getKey).collect(toSet());
log.debug("Type specification states {}, checked state {}", stateKeys, value.getStateKey());
return stateKeys.contains(value.getStateKey());
}
use of com.icthh.xm.ms.entity.domain.spec.TypeSpec in project xm-ms-entity by xm-online.
the class XmEntityGeneratorServiceIntTest method generateXmEntityWithLocations.
@Test
@SneakyThrows
public void generateXmEntityWithLocations() {
XmEntity generatedEntity = xmEntityGeneratorService.generateXmEntity(ENTITY_TYPE_WITH_TAGS_AND_LOCATIONS_KEY);
log.info(new ObjectMapper().writeValueAsString(generatedEntity));
Set<Location> locations = generatedEntity.getLocations();
assertFalse("No locations generated", isEmpty(locations));
TypeSpec specType = xmEntitySpecService.findTypeByKey("TYPE1.SUBTYPE1");
List<LocationSpec> locationSpecs = specType.getLocations();
Set<String> locationSpecKeys = locationSpecs.stream().map(LocationSpec::getKey).collect(toSet());
Set<String> locationKeys = locations.stream().map(Location::getTypeKey).collect(toSet());
assertTrue("Tag type not from locations specification", locationSpecKeys.containsAll(locationKeys));
for (val location : locations) {
assertFalse("Name of location is empty", isBlank(location.getName()));
assertFalse("Coordinates and address is empty", isBlank(location.getAddressLine1()) && (location.getLatitude() == null || location.getLongitude() == null));
}
}
Aggregations