All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.thestencil.client.spi.builders.CreateBuilderImpl Maven / Gradle / Ivy

There is a newer version: 4.278.132
Show newest version
package io.thestencil.client.spi.builders;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/*-
 * #%L
 * stencil-persistence
 * %%
 * Copyright (C) 2021 Copyright 2021 ReSys OÜ
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.util.Optional;

import io.smallrye.mutiny.Uni;
import io.thestencil.client.api.CreateBuilder;
import io.thestencil.client.api.ImmutableArticle;
import io.thestencil.client.api.ImmutableEntity;
import io.thestencil.client.api.ImmutableLink;
import io.thestencil.client.api.ImmutableLocale;
import io.thestencil.client.api.ImmutableLocaleLabel;
import io.thestencil.client.api.ImmutablePage;
import io.thestencil.client.api.ImmutableRelease;
import io.thestencil.client.api.ImmutableTemplate;
import io.thestencil.client.api.ImmutableWorkflow;
import io.thestencil.client.api.StencilClient;
import io.thestencil.client.api.StencilClient.Article;
import io.thestencil.client.api.StencilClient.Entity;
import io.thestencil.client.api.StencilClient.EntityBody;
import io.thestencil.client.api.StencilClient.EntityType;
import io.thestencil.client.api.StencilClient.Link;
import io.thestencil.client.api.StencilClient.Locale;
import io.thestencil.client.api.StencilClient.Page;
import io.thestencil.client.api.StencilClient.Release;
import io.thestencil.client.api.StencilClient.Template;
import io.thestencil.client.api.StencilClient.Workflow;
import io.thestencil.client.api.StencilComposer.SiteContentType;
import io.thestencil.client.api.StencilComposer.SiteState;
import io.thestencil.client.spi.StencilAssert;
import io.thestencil.client.spi.exceptions.ConstraintException;
import lombok.RequiredArgsConstructor;


@RequiredArgsConstructor
public class CreateBuilderImpl implements CreateBuilder {
  private final StencilClient client;

  @Override
  public Uni>> batch(BatchSite batch) {
    final Uni query = client.getStore().query().head();
    return query.onItem().transformToUni(state -> client.getStore().batch(new BatchSiteCommandVisitor(state, client).visit(batch)));
  }
  @Override
  public Uni> article(CreateArticle init) {
    final Uni query = client.getStore().query().head();
    return query.onItem().transformToUni(state -> client.getStore().create(article(init, state, client)));
  }
  @Override
  public Uni> template(CreateTemplate init) {
    final Uni query = client.getStore().query().head();
    return query.onItem().transformToUni(state -> client.getStore().create(template(init, state, client)));
  }
  @Override
  public Uni> release(CreateRelease init) {
    final Uni query = client.getStore().query().head();
    return query.onItem().transformToUni(state -> client.getStore().create(release(init, state, client)));
  }
  @Override
  public Uni> locale(CreateLocale init) {
    final Uni query = client.getStore().query().head();
    return query.onItem().transformToUni(state -> client.getStore().create(locale(init, state, client)));
  }
  @Override
  public Uni> page(CreatePage init) {
    final Uni query = client.getStore().query().head();
    return query.onItem().transformToUni(state -> client.getStore().create(page(init, state, client)));
  }
  @Override
  public Uni> link(CreateLink init) {
    final Uni query = client.getStore().query().head();
    return query.onItem().transformToUni(state -> client.getStore().create(link(init, state, client)));
  }
  @Override
  public Uni> workflow(CreateWorkflow init) {
    final Uni query = client.getStore().query().head();
    return query.onItem().transformToUni(state -> client.getStore().create(workflow(init, state, client)));
  }

  public static Entity release(CreateRelease init, SiteState state, StencilClient client) {
    StencilAssert.isTrue(state.getContentType() != SiteContentType.NOT_CREATED, () -> "Can't create release because ref state query failed!");
    final var gid = client.getStore().gid(EntityType.RELEASE);
    final var release = new CreateReleaseVisitor(state)
        .visit(ImmutableRelease.builder()
          .name(init.getName())
          .created(LocalDateTime.now())
          .note(Optional.ofNullable(init.getNote()).orElse(""))
          .parentCommit(state.getCommit())
        ).build();
  
    final Entity entity = ImmutableEntity.builder()
        .id(gid)
        .type(EntityType.RELEASE)
        .body(release)
        .build();
    return assertUniqueId(entity, state);
  }
  
  public static Entity