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

io.resys.wrench.assets.bundle.spi.dt.DtServiceBuilder Maven / Gradle / Ivy

There is a newer version: 3.1.4
Show newest version
package io.resys.wrench.assets.bundle.spi.dt;

import java.util.Optional;

/*-
 * #%L
 * wrench-component-assets
 * %%
 * Copyright (C) 2016 - 2017 Copyright 2016 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 org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import io.resys.wrench.assets.bundle.api.repositories.AssetServiceRepository.Service;
import io.resys.wrench.assets.bundle.api.repositories.AssetServiceRepository.ServiceBuilder;
import io.resys.wrench.assets.bundle.api.repositories.AssetServiceRepository.ServiceDataModel;
import io.resys.wrench.assets.bundle.api.repositories.AssetServiceRepository.ServiceIdGen;
import io.resys.wrench.assets.bundle.spi.builders.ImmutableServiceBuilder;
import io.resys.wrench.assets.bundle.spi.builders.TemplateServiceBuilder;
import io.resys.wrench.assets.bundle.spi.clock.ClockRepository;
import io.resys.wrench.assets.dt.api.DecisionTableRepository;
import io.resys.wrench.assets.dt.api.DecisionTableRepository.DecisionTableFormat;
import io.resys.wrench.assets.dt.api.model.DecisionTable;

public class DtServiceBuilder extends TemplateServiceBuilder {

  private final ServiceIdGen serviceStore;
  private final DecisionTableRepository decisionTableRepository;
  private final ClockRepository clockRepository;
  private final String defaultContent;
  private boolean rename;
  
  public DtServiceBuilder(
      ServiceIdGen serviceStore,
      DecisionTableRepository decisionTableRepository,
      ClockRepository clockRepository,
      String defaultContent) {
    super();
    this.serviceStore = serviceStore;
    this.clockRepository = clockRepository;
    this.decisionTableRepository = decisionTableRepository;
    this.defaultContent = defaultContent;
  }

  @Override
  public Service build() {
    if(StringUtils.isEmpty(src) ) {
      Assert.isTrue(!StringUtils.isEmpty(name), "Decision table name must be defined!");
    }
    String content = StringUtils.isEmpty(src) ? defaultContent.replace("{{name}}", name) : src;
    DecisionTable decisionTable = decisionTableRepository.createBuilder()
        .format(DecisionTableFormat.JSON)
        .src(content)
        .rename(rename ? Optional.of(name) : Optional.empty())
        .build();

    String serviceId = id == null ? serviceStore.nextId() : id;
    ServiceDataModel dataModel = new DtServiceDataModelBuilder().build(serviceId, decisionTable);
    String pointer = serviceId + ".json";

    return ImmutableServiceBuilder.newDt()
        .setId(serviceId)
        .setRev(decisionTable.getRev())
        .setName(decisionTable.getId())
        .setDescription(decisionTable.getDescription())
        .setSrc(decisionTable.getSrc())
        .setPointer(pointer)
        .setModel(dataModel)
        .setExecution(() -> new DtServiceExecution(decisionTableRepository, decisionTable))
        .build();
  }

  @Override
  public ServiceBuilder rename() {
    rename = true;
    return this;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy