Search in sources :

Example 1 with TypeSpec

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);
}
Also used : XmEntity(com.icthh.xm.ms.entity.domain.XmEntity) TypeSpec(com.icthh.xm.ms.entity.domain.spec.TypeSpec)

Example 2 with TypeSpec

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));
    }
}
Also used : XmEntityService(com.icthh.xm.ms.entity.service.XmEntityService) StorageService(com.icthh.xm.ms.entity.service.StorageService) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) XmEntityPermittedRepository(com.icthh.xm.ms.entity.repository.XmEntityPermittedRepository) LogicExtensionPoint(com.icthh.xm.commons.lep.LogicExtensionPoint) Collectors.toMap(java.util.stream.Collectors.toMap) Vote(com.icthh.xm.ms.entity.domain.Vote) Arrays.asList(java.util.Arrays.asList) FileFormatEnum(com.icthh.xm.ms.entity.domain.FileFormatEnum) EntityToCsvConverterUtils(com.icthh.xm.ms.entity.domain.converter.EntityToCsvConverterUtils) Map(java.util.Map) EntityToExcelConverterUtils(com.icthh.xm.ms.entity.domain.converter.EntityToExcelConverterUtils) Pageable(org.springframework.data.domain.Pageable) FindWithPermission(com.icthh.xm.commons.permission.annotation.FindWithPermission) URI(java.net.URI) Resource(org.springframework.core.io.Resource) Link(com.icthh.xm.ms.entity.domain.Link) LifecycleLepStrategy(com.icthh.xm.ms.entity.service.LifecycleLepStrategy) BusinessException(com.icthh.xm.commons.exceptions.BusinessException) Collections.emptyList(java.util.Collections.emptyList) PageRequest(org.springframework.data.domain.PageRequest) Set(java.util.Set) TypeSpec(com.icthh.xm.ms.entity.domain.spec.TypeSpec) LinkService(com.icthh.xm.ms.entity.service.LinkService) XmEntitySearchRepository(com.icthh.xm.ms.entity.repository.search.XmEntitySearchRepository) Page(org.springframework.data.domain.Page) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) HttpEntity(org.springframework.http.HttpEntity) List(java.util.List) ProfileService(com.icthh.xm.ms.entity.service.ProfileService) XmEntitySpecService(com.icthh.xm.ms.entity.service.XmEntitySpecService) XmEntity(com.icthh.xm.ms.entity.domain.XmEntity) Specification(org.springframework.data.jpa.domain.Specification) Comment(com.icthh.xm.ms.entity.domain.Comment) Optional(java.util.Optional) XmAuthenticationContextHolder(com.icthh.xm.commons.security.XmAuthenticationContextHolder) CustomCollectionUtils.nullSafe(com.icthh.xm.ms.entity.util.CustomCollectionUtils.nullSafe) Constants(com.icthh.xm.ms.entity.config.Constants) XmEntityTypeKeyResolver(com.icthh.xm.ms.entity.lep.keyresolver.XmEntityTypeKeyResolver) XmEntityIdKeyTypeKey(com.icthh.xm.ms.entity.projection.XmEntityIdKeyTypeKey) Callable(java.util.concurrent.Callable) LepService(com.icthh.xm.commons.lep.spring.LepService) ModelMapper(org.modelmapper.ModelMapper) XmEntityStateProjection(com.icthh.xm.ms.entity.projection.XmEntityStateProjection) Propagation(org.springframework.transaction.annotation.Propagation) SimpleExportXmEntityDto(com.icthh.xm.ms.entity.domain.SimpleExportXmEntityDto) EntityNotFoundException(com.icthh.xm.commons.exceptions.EntityNotFoundException) IdOrKey(com.icthh.xm.ms.entity.domain.ext.IdOrKey) AttachmentService(com.icthh.xm.ms.entity.service.AttachmentService) SEARCH_BUILDER_TYPE(com.icthh.xm.ms.entity.domain.spec.LinkSpec.SEARCH_BUILDER_TYPE) Rating(com.icthh.xm.ms.entity.domain.Rating) Iterator(java.util.Iterator) Optional.ofNullable(java.util.Optional.ofNullable) ErrorConstants(com.icthh.xm.commons.exceptions.ErrorConstants) LifecycleLepStrategyFactory(com.icthh.xm.ms.entity.service.LifecycleLepStrategyFactory) ObjectUtils(org.springframework.util.ObjectUtils) LinkSpec(com.icthh.xm.ms.entity.domain.spec.LinkSpec) Attachment(com.icthh.xm.ms.entity.domain.Attachment) Tag(com.icthh.xm.ms.entity.domain.Tag) Location(com.icthh.xm.ms.entity.domain.Location) Consumer(java.util.function.Consumer) StateSpec(com.icthh.xm.ms.entity.domain.spec.StateSpec) XmEntityPermittedSearchRepository(com.icthh.xm.ms.entity.repository.search.XmEntityPermittedSearchRepository) XmEntityRepository(com.icthh.xm.ms.entity.repository.XmEntityRepository) Calendar(com.icthh.xm.ms.entity.domain.Calendar) MultipartFile(org.springframework.web.multipart.MultipartFile) NEW_BUILDER_TYPE(com.icthh.xm.ms.entity.domain.spec.LinkSpec.NEW_BUILDER_TYPE) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) PageRequest(org.springframework.data.domain.PageRequest) BusinessException(com.icthh.xm.commons.exceptions.BusinessException) SimpleExportXmEntityDto(com.icthh.xm.ms.entity.domain.SimpleExportXmEntityDto) XmEntity(com.icthh.xm.ms.entity.domain.XmEntity) ModelMapper(org.modelmapper.ModelMapper) LogicExtensionPoint(com.icthh.xm.commons.lep.LogicExtensionPoint)

Example 3 with TypeSpec

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;
}
Also used : Attachment(com.icthh.xm.ms.entity.domain.Attachment) TypeSpec(com.icthh.xm.ms.entity.domain.spec.TypeSpec)

Example 4 with TypeSpec

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());
}
Also used : StateSpec(com.icthh.xm.ms.entity.domain.spec.StateSpec) TypeSpec(com.icthh.xm.ms.entity.domain.spec.TypeSpec)

Example 5 with TypeSpec

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));
    }
}
Also used : lombok.val(lombok.val) LocationSpec(com.icthh.xm.ms.entity.domain.spec.LocationSpec) XmEntity(com.icthh.xm.ms.entity.domain.XmEntity) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Location(com.icthh.xm.ms.entity.domain.Location) TypeSpec(com.icthh.xm.ms.entity.domain.spec.TypeSpec) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) SneakyThrows(lombok.SneakyThrows)

Aggregations

TypeSpec (com.icthh.xm.ms.entity.domain.spec.TypeSpec)9 XmEntity (com.icthh.xm.ms.entity.domain.XmEntity)4 Test (org.junit.Test)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Attachment (com.icthh.xm.ms.entity.domain.Attachment)2 Link (com.icthh.xm.ms.entity.domain.Link)2 Location (com.icthh.xm.ms.entity.domain.Location)2 Tag (com.icthh.xm.ms.entity.domain.Tag)2 StateSpec (com.icthh.xm.ms.entity.domain.spec.StateSpec)2 SneakyThrows (lombok.SneakyThrows)2 lombok.val (lombok.val)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 Timed (com.codahale.metrics.annotation.Timed)1 BusinessException (com.icthh.xm.commons.exceptions.BusinessException)1 EntityNotFoundException (com.icthh.xm.commons.exceptions.EntityNotFoundException)1 ErrorConstants (com.icthh.xm.commons.exceptions.ErrorConstants)1 LogicExtensionPoint (com.icthh.xm.commons.lep.LogicExtensionPoint)1 LepService (com.icthh.xm.commons.lep.spring.LepService)1 FindWithPermission (com.icthh.xm.commons.permission.annotation.FindWithPermission)1 XmAuthenticationContextHolder (com.icthh.xm.commons.security.XmAuthenticationContextHolder)1