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

io.deepsense.neptune.clientlibrary.models.impl.job.JobImpl Maven / Gradle / Ivy

There is a newer version: 1.6.1
Show newest version
/**
 * Copyright (c) 2016, CodiLime Inc.
 */

package io.deepsense.neptune.clientlibrary.models.impl.job;

import com.google.common.base.Preconditions;
import io.deepsense.neptune.apiclient.ApiException;
import io.deepsense.neptune.clientlibrary.exceptions.channels.DuplicatedChannelNameException;
import io.deepsense.neptune.clientlibrary.exceptions.charts.DuplicatedChartNameException;
import io.deepsense.neptune.clientlibrary.exceptions.common.NeptuneRuntimeException;
import io.deepsense.neptune.clientlibrary.models.Action;
import io.deepsense.neptune.clientlibrary.models.Channel;
import io.deepsense.neptune.clientlibrary.models.Chart;
import io.deepsense.neptune.clientlibrary.models.ChartSeriesCollection;
import io.deepsense.neptune.clientlibrary.models.Job;
import io.deepsense.neptune.clientlibrary.models.JobProperties;
import io.deepsense.neptune.clientlibrary.models.JobState;
import io.deepsense.neptune.clientlibrary.models.NeptuneImage;
import io.deepsense.neptune.clientlibrary.models.Tags;
import io.deepsense.neptune.clientlibrary.models.impl.channels.BaseChannelImpl;
import io.deepsense.neptune.clientlibrary.models.impl.channels.ImageChannelImpl;
import io.deepsense.neptune.clientlibrary.models.impl.channels.NumericChannelImpl;
import io.deepsense.neptune.clientlibrary.models.impl.channels.TextChannelImpl;
import io.deepsense.neptune.clientlibrary.models.impl.charts.ChartImpl;
import io.deepsense.neptune.clientlibrary.models.impl.charts.ChartSeriesCollectionImpl;
import io.deepsense.neptune.clientlibrary.models.impl.properties.JobPropertiesImpl;
import io.deepsense.neptune.clientlibrary.models.impl.tags.TagsImpl;
import io.deepsense.neptune.clientlibrary.services.apimodelconverter.ApiModelConverter;
import io.deepsense.neptune.clientlibrary.services.apiservice.ApiService;
import io.deepsense.neptune.clientlibrary.services.channelvaluesender.ChannelValueSendingProxy;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Predicate;

public final class JobImpl implements Job {

    private final ApiService apiService;

    private final ApiModelConverter apiModelConverter;

    private final ChannelValueSendingProxy channelValueSendingProxy;

    private final long maxChannelValueSize;

    private final UUID id;

    private JobState state;

    private final List channels = new LinkedList<>();

    private final List charts = new LinkedList<>();

    private final Tags tags;

    private final JobProperties properties;

    JobImpl(
            ApiService apiService,
            ApiModelConverter apiModelConverter,
            ChannelValueSendingProxy channelValueSendingProxy,
            long maxChannelValueSize,
            UUID id,
            JobState state) {
        this.apiService = Preconditions.checkNotNull(apiService);
        this.apiModelConverter = Preconditions.checkNotNull(apiModelConverter);
        this.channelValueSendingProxy = Preconditions.checkNotNull(channelValueSendingProxy);
        this.maxChannelValueSize = maxChannelValueSize;
        this.id = Preconditions.checkNotNull(id);
        this.state = Preconditions.checkNotNull(state);
        this.tags = new TagsImpl(apiService, id);
        this.properties = new JobPropertiesImpl(apiService, id);
    }

    @Override
    public Channel createNumericChannel(String name, boolean isHistoryPersisted, boolean isLastValueExposed) {
        assertChannelCanBeAdded(name);
        try {
            return registeredChannel(new NumericChannelImpl(
                    apiModelConverter,
                    channelValueSendingProxy,
                    maxChannelValueSize,
                    name,
                    isHistoryPersisted,
                    isLastValueExposed));
        } catch (ApiException exc) {
            throw new NeptuneRuntimeException(String.format("Failed to create channel '%s'!", name), exc);
        }
    }

    @Override
    public Channel createNumericChannel(String name) {
        return createNumericChannel(name, true, true);
    }

    @Override
    public Channel createTextChannel(String name, boolean isHistoryPersisted, boolean isLastValueExposed) {
        assertChannelCanBeAdded(name);
        try {
            return registeredChannel(new TextChannelImpl(
                    apiModelConverter,
                    channelValueSendingProxy,
                    maxChannelValueSize,
                    name,
                    isHistoryPersisted,
                    isLastValueExposed));
        } catch (ApiException exc) {
            throw new NeptuneRuntimeException(String.format("Failed to create channel '%s'!", name), exc);
        }
    }

    @Override
    public Channel createTextChannel(String name) {
        return createTextChannel(name, true, true);
    }

    @Override
    public Channel createImageChannel(String name) {
        assertChannelCanBeAdded(name);
        try {
            return registeredChannel(new ImageChannelImpl(
                    apiModelConverter,
                    channelValueSendingProxy,
                    maxChannelValueSize,
                    name,
                    true,
                    false));
        } catch (ApiException exc) {
            throw new NeptuneRuntimeException(String.format("Failed to create channel '%s'!", name), exc);
        }
    }

    @Override
    public Chart createChart(String name, ChartSeriesCollection series) {
        assertChartCanBeAdded(name);
        try {
            return registeredChart(new ChartImpl(name, series));
        } catch (ApiException exc) {
            throw new NeptuneRuntimeException(String.format("Failed to create chart '%s'!", name), exc);
        }
    }

    @Override
    public ChartSeriesCollection createChartSeriesCollection() {
        return new ChartSeriesCollectionImpl();
    }

    @Override
    public Action registerAction(String name, Function handler) {
        throw new UnsupportedOperationException();
    }

    @Override
    public UUID getId() {
        return id;
    }

    @Override
    public JobState getJobState() {
        return state;
    }

    @Override
    public Tags getTags() {
        return tags;
    }

    @Override
    public JobProperties getProperties() {
        return properties;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        JobImpl job = (JobImpl) o;
        return Objects.equals(id, job.id)
                && state == job.state;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, state);
    }

    private  Channel registeredChannel(BaseChannelImpl channel)
            throws ApiException {
        channels.add(channel);
        apiService.updateChannels(id, channels);
        return channel;
    }

    private Chart registeredChart(ChartImpl chart)  throws ApiException {
        charts.add(chart);
        apiService.updateCharts(id, charts);
        return chart;
    }

    private void assertChannelCanBeAdded(String channelName) {
        if (channelNameAlreadyExists(channelName)) {
            throw new DuplicatedChannelNameException(channelName);
        }
    }

    private void assertChartCanBeAdded(String chartName) {
        if (chartNameAlreadyExists(chartName)) {
            throw new DuplicatedChartNameException(chartName);
        }
    }

    private boolean channelNameAlreadyExists(String channelName) {
        return entityNameAlreadyExists(channels, Channel::getName, channelName);
    }

    private boolean chartNameAlreadyExists(String chartName) {
        return entityNameAlreadyExists(charts, Chart::getName, chartName);
    }

    private static  boolean entityNameAlreadyExists(
            Collection entitiesCollection,
            Function entityNameGetter,
            String newEntityName) {

        return entitiesCollection.stream()
                .map(entityNameGetter)
                .filter(Predicate.isEqual(newEntityName))
                .findAny()
                .isPresent();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy