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

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

The newest version!
package io.thestencil.client.spi.builders;

/*-
 * #%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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import io.smallrye.mutiny.Uni;
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.ImmutablePage;
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.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.Template;
import io.thestencil.client.api.StencilClient.Workflow;
import io.thestencil.client.api.StencilComposer.SiteState;
import io.thestencil.client.api.StencilConfig.EntityState;
import io.thestencil.client.api.UpdateBuilder;
import io.thestencil.client.spi.exceptions.ConstraintException;
import lombok.RequiredArgsConstructor;


@RequiredArgsConstructor
public class UpdateBuilderImpl implements UpdateBuilder {

  private final StencilClient client;
  

  @Override
  public Uni> article(ArticleMutator changes) {
    // Get the article
    final Uni query = client.getStore().query().head();
    
    // Change the article
    return query.onItem().transformToUni(state -> changeArticle(state, changes));
  }
  
  private Uni> changeArticle(SiteState site, ArticleMutator changes) {
    Entity
start = site.getArticles().get(changes.getArticleId()); List> additionalChanges = new ArrayList<>(); if(changes.getArticleId().equals(changes.getParentId())) { throw new ConstraintException(start, "Article: '" + changes.getName() + "' parent can't be itself!"); } final var duplicate = site.getArticles().values().stream() .filter(p -> !p.getId().equals(changes.getArticleId())) .filter(p -> p.getBody().getName().equals(changes.getName())) .findFirst(); if(duplicate.isPresent()) { throw new ConstraintException(start, "Article: '" + changes.getName() + "' already exists!"); } // update article links if(changes.getLinks() != null) { for(Entity link : site.getLinks().values()) { final var isArticleInLink = link.getBody().getArticles().contains(changes.getArticleId()); final var isLinkInChanges = changes.getLinks().contains(link.getId()); // link already defined for article if(isArticleInLink && isLinkInChanges) { continue; } // add link if(isLinkInChanges && !isArticleInLink) { final var newLink = ImmutableEntity.builder().from(link) .body(ImmutableLink.builder().from(link.getBody()) .addArticles(changes.getArticleId()) .build()) .build(); additionalChanges.add(newLink); } // remove link if(isArticleInLink && !isLinkInChanges) { final var articles = new ArrayList<>(link.getBody().getArticles()); articles.remove(changes.getArticleId()); final var newLink = ImmutableEntity.builder().from(link) .body(ImmutableLink.builder().from(link.getBody()) .articles(articles) .build()) .build(); additionalChanges.add(newLink); } } } // update article workflows if(changes.getWorkflows() != null) { for(Entity workflow : site.getWorkflows().values()) { final var isArticleInWorkflow = workflow.getBody().getArticles().contains(changes.getArticleId()); final var isWorkflowInChanges = changes.getWorkflows().contains(workflow.getId()); // workflow already defined for article if(isArticleInWorkflow && isWorkflowInChanges) { continue; } // add workflow if(isWorkflowInChanges && !isArticleInWorkflow) { final var newWorkflow = ImmutableEntity.builder().from(workflow) .body(ImmutableWorkflow.builder().from(workflow.getBody()) .addArticles(changes.getArticleId()) .build()) .build(); additionalChanges.add(newWorkflow); } // remove link if(isArticleInWorkflow && !isWorkflowInChanges) { final var articles = new ArrayList<>(workflow.getBody().getArticles()); articles.remove(changes.getArticleId()); final var newWorkflow = ImmutableEntity.builder().from(workflow) .body(ImmutableWorkflow.builder().from(workflow.getBody()) .articles(articles) .build()) .build(); additionalChanges.add(newWorkflow); } } } final var result = ImmutableEntity.
builder() .from(start) .body(ImmutableArticle.builder() .from(start.getBody()) .devMode(changes.getDevMode()) .name(changes.getName()) .order(changes.getOrder()) .parentId(changes.getParentId()) .build()) .build(); final var allChanges = new ArrayList>(); allChanges.add(result); allChanges.addAll(additionalChanges); return client.getStore().saveAll(allChanges).onItem().transform(e -> result); } @Override public Uni> locale(LocaleMutator changes) { final Uni query = client.getStore().query().head(); // Change the locale return query.onItem().transformToUni(state -> client.getStore().save(changeLocale(state, changes))); } private Entity changeLocale(SiteState site, LocaleMutator changes) { final Entity start = site.getLocales().get(changes.getLocaleId()); final var duplicate = site.getLocales().values().stream() .filter(p -> !p.getId().equals(changes.getLocaleId())) .filter(p -> p.getBody().getValue().equals(changes.getValue())) .findFirst(); if(duplicate.isPresent()) { throw new ConstraintException(start, "Locale: '" + changes.getValue() + "' already exists!"); } return ImmutableEntity.builder() .from(start) .body(ImmutableLocale.builder().from(start.getBody()) .value(changes.getValue()) .enabled(changes.getEnabled()) .build()) .build(); } @Override public Uni> template(TemplateMutator changes) { final Uni query = client.getStore().query().head(); // Change the template return query.onItem().transformToUni(state -> client.getStore().save(changeTemplate(state, changes))); } private Entity