Search in sources :

Example 1 with TagEditionVo

use of org.ligoj.app.plugin.prov.TagEditionVo in project plugin-prov by ligoj.

the class ProvQuoteUploadResource method persist.

/**
 * Validate the input object, do a lookup, then create the {@link ProvQuoteInstance} and the
 * {@link ProvQuoteStorage} entities.
 */
private <V extends AbstractQuoteVmEditionVo> void persist(final VmUpload upload, final int subscription, final BiFunction<V, UploadContext, Integer> merger, final UploadContext context, final V vo, final ObjIntConsumer<QuoteStorageEditionVo> diskConsumer, final ResourceType resourceType) {
    // Create the quote instance from the validated inputs
    final var id = merger.apply(vo, context);
    if (id == null) {
        // Do not continue
        return;
    }
    // Storage part
    final var disks = IntStream.range(0, upload.getDisk().size()).filter(index -> upload.getDisk().get(index) > 0).mapToObj(index -> {
        final var size = upload.getDisk().get(index).intValue();
        final var sizeMax = upload.getDiskMax().size() > index ? upload.getDiskMax().get(index).intValue() : null;
        // Size is provided, propagate the upload properties
        final var svo = new QuoteStorageEditionVo();
        svo.setName(vo.getName() + (index == 0 ? "" : index));
        diskConsumer.accept(svo, id);
        svo.setSize(size);
        svo.setSizeMax(sizeMax);
        svo.setLatency(getItem(upload.getLatency(), index));
        svo.setOptimized(getItem(upload.getOptimized(), index));
        // Find the nicest storage
        svo.setType(storageResource.lookup(context.quote, svo).stream().findFirst().orElseThrow(() -> new ValidationJsonException("storage", "NotNull")).getPrice().getType().getCode());
        // Default the storage name to the instance name
        svo.setSubscription(subscription);
        return storageResource.create(svo).getId();
    }).collect(Collectors.toList());
    // Tags part
    Arrays.stream(StringUtils.split(ObjectUtils.defaultIfNull(upload.getTags(), ""), ",;")).map(StringUtils::trimToNull).filter(Objects::nonNull).forEach(t -> {
        // Instance tags
        final var tag = new TagEditionVo();
        final var parts = StringUtils.splitPreserveAllTokens(t + ":", ':');
        tag.setName(parts[0].trim());
        tag.setValue(StringUtils.trimToNull(parts[1]));
        tag.setResource(id);
        tag.setType(resourceType);
        tagResource.create(subscription, tag);
        // Storage tags
        tag.setType(ResourceType.STORAGE);
        disks.forEach(d -> {
            tag.setResource(d);
            tagResource.create(subscription, tag);
        });
    });
}
Also used : CsvForBean(org.ligoj.bootstrap.core.csv.CsvForBean) Arrays(java.util.Arrays) Produces(javax.ws.rs.Produces) BiFunction(java.util.function.BiFunction) Path(javax.ws.rs.Path) ProvUsageRepository(org.ligoj.app.plugin.prov.dao.ProvUsageRepository) Autowired(org.springframework.beans.factory.annotation.Autowired) ProvQuoteStorage(org.ligoj.app.plugin.prov.model.ProvQuoteStorage) StringUtils(org.apache.commons.lang3.StringUtils) ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException) MediaType(javax.ws.rs.core.MediaType) ByteArrayInputStream(java.io.ByteArrayInputStream) Consumes(javax.ws.rs.Consumes) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Multipart(org.apache.cxf.jaxrs.ext.multipart.Multipart) ProvQuoteDatabase(org.ligoj.app.plugin.prov.model.ProvQuoteDatabase) ProvQuoteStorageResource(org.ligoj.app.plugin.prov.quote.storage.ProvQuoteStorageResource) Transactional(javax.transaction.Transactional) ResourceType(org.ligoj.app.plugin.prov.model.ResourceType) Collectors(java.util.stream.Collectors) AbstractProvQuoteVmResource(org.ligoj.app.plugin.prov.AbstractProvQuoteVmResource) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) Objects(java.util.Objects) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) QuoteStorageEditionVo(org.ligoj.app.plugin.prov.quote.storage.QuoteStorageEditionVo) Slf4j(lombok.extern.slf4j.Slf4j) QuoteDatabaseEditionVo(org.ligoj.app.plugin.prov.quote.database.QuoteDatabaseEditionVo) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) ProvQuoteDatabaseResource(org.ligoj.app.plugin.prov.quote.database.ProvQuoteDatabaseResource) IntStream(java.util.stream.IntStream) PathParam(javax.ws.rs.PathParam) QuoteInstanceEditionVo(org.ligoj.app.plugin.prov.quote.instance.QuoteInstanceEditionVo) HashMap(java.util.HashMap) ArrayUtils(org.apache.commons.lang3.ArrayUtils) ObjIntConsumer(java.util.function.ObjIntConsumer) ProvQuoteInstance(org.ligoj.app.plugin.prov.model.ProvQuoteInstance) Function(java.util.function.Function) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ObjectUtils(org.apache.commons.lang3.ObjectUtils) Service(org.springframework.stereotype.Service) EntityNotFoundException(javax.persistence.EntityNotFoundException) AbstractQuoteVmEditionVo(org.ligoj.app.plugin.prov.AbstractQuoteVmEditionVo) ProvUsage(org.ligoj.app.plugin.prov.model.ProvUsage) ProvQuoteInstanceResource(org.ligoj.app.plugin.prov.quote.instance.ProvQuoteInstanceResource) MapUtils(org.apache.commons.collections4.MapUtils) POST(javax.ws.rs.POST) SequenceInputStream(java.io.SequenceInputStream) ProvQuoteInstanceRepository(org.ligoj.app.plugin.prov.dao.ProvQuoteInstanceRepository) IOException(java.io.IOException) SubscriptionResource(org.ligoj.app.resource.subscription.SubscriptionResource) InputStreamReader(java.io.InputStreamReader) ProvTagResource(org.ligoj.app.plugin.prov.ProvTagResource) TagEditionVo(org.ligoj.app.plugin.prov.TagEditionVo) StringReader(java.io.StringReader) ConstraintViolationException(javax.validation.ConstraintViolationException) ProvQuoteDatabaseRepository(org.ligoj.app.plugin.prov.dao.ProvQuoteDatabaseRepository) ProvResource(org.ligoj.app.plugin.prov.ProvResource) ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote) BufferedReader(java.io.BufferedReader) Hibernate(org.hibernate.Hibernate) InputStream(java.io.InputStream) StringUtils(org.apache.commons.lang3.StringUtils) ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException) QuoteStorageEditionVo(org.ligoj.app.plugin.prov.quote.storage.QuoteStorageEditionVo) TagEditionVo(org.ligoj.app.plugin.prov.TagEditionVo)

Aggregations

BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 SequenceInputStream (java.io.SequenceInputStream)1 Serializable (java.io.Serializable)1 StringReader (java.io.StringReader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 BiFunction (java.util.function.BiFunction)1 Function (java.util.function.Function)1