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

com.metaeffekt.mirror.query.EolIndexQuery Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2021-2024 the original author or authors.
 *
 * 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.
 */
package com.metaeffekt.mirror.query;

import com.metaeffekt.mirror.contents.eol.EolCycle;
import com.metaeffekt.mirror.contents.eol.EolLifecycle;
import com.metaeffekt.mirror.index.IndexSearch;
import com.metaeffekt.mirror.index.advisor.MsrcProductIndex;
import com.metaeffekt.mirror.index.other.EolIndex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public class EolIndexQuery extends IndexQuery {

    private final static Logger LOG = LoggerFactory.getLogger(EolIndexQuery.class);

    public EolIndexQuery(File baseMirrorDirectory) {
        super(baseMirrorDirectory, EolIndex.class);
    }

    public EolIndexQuery(MsrcProductIndex index) {
        super(index);
    }

    public EolLifecycle findCyclesByProduct(String product) {
        return findCyclesBySearcher(new IndexSearch().fieldEquals("product", product));
    }

    public EolLifecycle findCyclesBySearcher(IndexSearch search) {
        final List cycles = super.index.findDocuments(search).stream()
                .map(EolCycle::fromDocument)
                .sorted()
                .collect(Collectors.toList());

        if (cycles.stream().map(EolCycle::getProduct).distinct().count() > 1) {
            LOG.error("Multiple products [{}] found for search: {}", cycles.stream().map(EolCycle::getProduct).distinct().collect(Collectors.joining(", ")), search);
        }
        if (cycles.isEmpty()) {
            return null;
        }

        return new EolLifecycle(cycles.get(0).getProduct(), cycles);
    }

    public List findProducts() {
        final Set products = new HashSet<>();
        super.index.findAndProcessAllDocuments(doc -> products.add(doc.get("product")));
        return products.stream().sorted().collect(Collectors.toList());
    }

    public Optional findCycleByProductAndCycle(String product, String cycle) {
        final List cycles = super.index.findDocuments(new IndexSearch().fieldEquals("product", product).fieldEquals("cycle", cycle)).stream()
                .map(EolCycle::fromDocument)
                .sorted()
                .collect(Collectors.toList());

        if (cycles.size() > 1) {
            LOG.error("Multiple cycles [{}] found for product {} and cycle {}", cycles.stream().map(EolCycle::getCycle).distinct().collect(Collectors.joining(", ")), product, cycle);
        }

        return cycles.stream().findFirst();
    }

    public List findAll() {
        return super.index.findAllDocuments().stream()
                .map(EolCycle::fromDocument)
                .collect(Collectors.groupingBy(EolCycle::getProduct))
                .entrySet().stream()
                .map(e -> new EolLifecycle(e.getKey(), e.getValue()))
                .collect(Collectors.toList());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy