001/*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2024-2026 The Enola <https://enola.dev> Authors
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *     https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package dev.enola.thing.io;
019
020import com.google.common.collect.Iterables;
021
022import dev.enola.common.convert.ConversionException;
023import dev.enola.common.convert.ConverterInto;
024import dev.enola.common.function.MoreStreams;
025import dev.enola.thing.Thing;
026import dev.enola.thing.repo.ThingMemoryRepositoryROBuilder;
027import dev.enola.thing.repo.ThingRepositoryStore;
028
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032import java.io.IOException;
033import java.net.URI;
034import java.util.stream.Stream;
035
036public class Loader implements ConverterInto<Stream<URI>, ThingRepositoryStore> {
037
038    // TODO Move Glob-based loading from CommandWithModel into here!
039
040    // TODO Load resources multithreaded, in parallel...
041
042    private static final Logger LOG = LoggerFactory.getLogger(Loader.class);
043
044    private final UriIntoThingConverters uriIntoThingConverters;
045
046    public Loader(UriIntoThingConverters uriIntoThingConverters) {
047        this.uriIntoThingConverters = uriIntoThingConverters;
048    }
049
050    @Override
051    public boolean convertInto(Stream<URI> stream, ThingRepositoryStore store)
052            throws ConversionException, IOException {
053
054        MoreStreams.forEach(stream, resource -> load(resource, store));
055        // TODO Should check if at least one URI successfully loaded anything?
056        return true;
057    }
058
059    public boolean load(String uri, ThingRepositoryStore store) throws IOException {
060        return load(URI.create(uri), store);
061    }
062
063    public boolean load(URI uri, ThingRepositoryStore store) throws IOException {
064        LOG.info("Loading {}...", uri);
065        return uriIntoThingConverters.convertInto(uri, store);
066    }
067
068    // TODO The load() vs. load[AtLeastOne]Thing/s duality is strange... remove this again:
069
070    private Iterable<Thing> loadThings(URI uri) throws IOException {
071        var store = new ThingMemoryRepositoryROBuilder();
072        load(uri, store);
073        return store.build().list();
074    }
075
076    public Iterable<Thing> loadAtLeastOneThing(URI uri) throws IOException {
077        var things = loadThings(uri);
078        if (Iterables.isEmpty(things)) throw new ConversionException("Nothing loaded from: " + uri);
079        return things;
080    }
081}