Search in sources :

Example 11 with Location

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

the class XmEntityGeneratorService method generateLocations.

private Set<Location> generateLocations(LocationSpec locationSpec) {
    Set<Location> result = new HashSet<>();
    List<Location> originalLocationStubs = getLocationStubs();
    val locationStubs = originalLocationStubs.stream().filter(l -> RandomUtils.nextBoolean()).collect(toList());
    if (isEmpty(locationStubs)) {
        int randomLocationPosition = RandomUtils.nextInt(0, originalLocationStubs.size() - 1);
        Location randomLocationStub = originalLocationStubs.get(randomLocationPosition);
        locationStubs.add(randomLocationStub);
    }
    int locationsLimit = min(locationSpec.getMax(), locationStubs.size());
    int countLocations = RandomUtils.nextInt(1, locationsLimit);
    IntStream.range(0, countLocations).forEachOrdered(i -> {
        Location locationStub = locationStubs.get(i);
        updateLocationStubToLocation(locationSpec, locationStub);
        result.add(locationStub);
    });
    return result;
}
Also used : lombok.val(lombok.val) RandomUtils(org.apache.commons.lang3.RandomUtils) IntStream(java.util.stream.IntStream) LocationSpec(com.icthh.xm.ms.entity.domain.spec.LocationSpec) ERR_METHOD_NOT_SUPPORTED(com.icthh.xm.commons.exceptions.ErrorConstants.ERR_METHOD_NOT_SUPPORTED) SneakyThrows(lombok.SneakyThrows) Constants(com.icthh.xm.ms.entity.config.Constants) ClassPathResource(org.springframework.core.io.ClassPathResource) HashMap(java.util.HashMap) Random(java.util.Random) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) Service(org.springframework.stereotype.Service) Map(java.util.Map) Integer.min(java.lang.Integer.min) TagSpec(com.icthh.xm.ms.entity.domain.spec.TagSpec) Collections.emptyMap(java.util.Collections.emptyMap) Collections.emptySet(java.util.Collections.emptySet) StringUtils.isEmpty(org.apache.commons.lang.StringUtils.isEmpty) BusinessException(com.icthh.xm.commons.exceptions.BusinessException) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) lombok.val(lombok.val) Set(java.util.Set) CollectionType(com.fasterxml.jackson.databind.type.CollectionType) TypeSpec(com.icthh.xm.ms.entity.domain.spec.TypeSpec) UUID(java.util.UUID) Tag(com.icthh.xm.ms.entity.domain.Tag) Instant(java.time.Instant) Location(com.icthh.xm.ms.entity.domain.Location) IOUtils(org.apache.commons.io.IOUtils) Collectors.toList(java.util.stream.Collectors.toList) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) XmEntity(com.icthh.xm.ms.entity.domain.XmEntity) CollectionUtils.isEmpty(org.apache.commons.collections.CollectionUtils.isEmpty) XmAuthenticationContextHolder(com.icthh.xm.commons.security.XmAuthenticationContextHolder) NextSpec(com.icthh.xm.ms.entity.domain.spec.NextSpec) InputStream(java.io.InputStream) Location(com.icthh.xm.ms.entity.domain.Location) HashSet(java.util.HashSet)

Example 12 with Location

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

the class XmEntityServiceImpl method saveXmEntity.

/**
 * Save a xmEntity.
 * When you call this method will be run general save LEP
 *
 * @param xmEntity the entity to save
 * @return the persisted entity
 */
@LogicExtensionPoint("Save")
public XmEntity saveXmEntity(XmEntity xmEntity) {
    log.debug("Request to save XmEntity : {}", xmEntity);
    Optional<XmEntity> oldEntity = startUpdateDateGenerationStrategy.preProcessStartUpdateDates(xmEntity, xmEntity.getId(), xmEntityRepository, XmEntity::setStartDate, XmEntity::getStartDate, XmEntity::setUpdateDate);
    if (oldEntity.isPresent()) {
        preventRenameTenant(xmEntity, oldEntity.get());
    } else if (xmEntity.getCreatedBy() == null) {
        xmEntity.setCreatedBy(authContextHolder.getContext().getUserKey().orElse(null));
    }
    // FIXME It is hack to link each tag with entity before persisting. may be there is more elegant solution.
    xmEntity.updateXmEntityReference(xmEntity.getAttachments(), Attachment::setXmEntity);
    xmEntity.updateXmEntityReference(xmEntity.getCalendars(), Calendar::setXmEntity);
    xmEntity.updateXmEntityReference(xmEntity.getLocations(), Location::setXmEntity);
    xmEntity.updateXmEntityReference(xmEntity.getRatings(), Rating::setXmEntity);
    xmEntity.updateXmEntityReference(xmEntity.getTags(), Tag::setXmEntity);
    xmEntity.updateXmEntityReference(xmEntity.getComments(), Comment::setXmEntity);
    xmEntity.updateXmEntityReference(xmEntity.getTargets(), Link::setSource);
    xmEntity.updateXmEntityReference(xmEntity.getSources(), Link::setTarget);
    xmEntity.updateXmEntityReference(xmEntity.getVotes(), Vote::setXmEntity);
    XmEntity result = xmEntityRepository.save(xmEntity);
    xmEntitySearchRepository.save(result);
    return result;
}
Also used : Comment(com.icthh.xm.ms.entity.domain.Comment) Vote(com.icthh.xm.ms.entity.domain.Vote) Calendar(com.icthh.xm.ms.entity.domain.Calendar) Rating(com.icthh.xm.ms.entity.domain.Rating) XmEntity(com.icthh.xm.ms.entity.domain.XmEntity) Attachment(com.icthh.xm.ms.entity.domain.Attachment) Tag(com.icthh.xm.ms.entity.domain.Tag) Link(com.icthh.xm.ms.entity.domain.Link) Location(com.icthh.xm.ms.entity.domain.Location) LogicExtensionPoint(com.icthh.xm.commons.lep.LogicExtensionPoint)

Aggregations

Location (com.icthh.xm.ms.entity.domain.Location)12 XmEntity (com.icthh.xm.ms.entity.domain.XmEntity)6 Tag (com.icthh.xm.ms.entity.domain.Tag)4 Test (org.junit.Test)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 Attachment (com.icthh.xm.ms.entity.domain.Attachment)3 Timed (com.codahale.metrics.annotation.Timed)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 BusinessException (com.icthh.xm.commons.exceptions.BusinessException)2 Calendar (com.icthh.xm.ms.entity.domain.Calendar)2 Comment (com.icthh.xm.ms.entity.domain.Comment)2 Rating (com.icthh.xm.ms.entity.domain.Rating)2 Vote (com.icthh.xm.ms.entity.domain.Vote)2 LocationSpec (com.icthh.xm.ms.entity.domain.spec.LocationSpec)2 TypeSpec (com.icthh.xm.ms.entity.domain.spec.TypeSpec)2 SneakyThrows (lombok.SneakyThrows)2 lombok.val (lombok.val)2 Transactional (org.springframework.transaction.annotation.Transactional)2 CollectionType (com.fasterxml.jackson.databind.type.CollectionType)1 ImmutableList (com.google.common.collect.ImmutableList)1