001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thierry Delprat
018 */
019package org.nuxeo.apidoc.snapshot;
020
021import java.util.ArrayList;
022import java.util.Arrays;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027import java.util.Optional;
028
029public class SnapshotResolverHelper {
030
031    protected static final String[] capaliases = { "cap", "Nuxeo Platform", "Nuxeo cap", "Nuxeo DM", "dm" };
032
033    public static String findBestMatch(List<DistributionSnapshot> snaps, String distributionId) {
034
035        if (distributionId == null || "".equals(distributionId.trim())) {
036            return null;
037        }
038        // if ("current".equalsIgnoreCase((distributionId.trim()))) {
039        // return "current";
040        // }
041
042        // exact match
043        for (DistributionSnapshot snap : snaps) {
044            if (snap.getKey().equalsIgnoreCase(distributionId)) {
045                return snap.getKey();
046            }
047        }
048
049        // aliases
050        Optional<DistributionSnapshot> first = snaps.stream()
051                .filter(s -> s.getAliases().contains(distributionId))
052                .findFirst();
053        if (first.isPresent()) {
054            return first.get().getKey();
055        }
056
057        // name match + best version
058        String[] parts = distributionId.split("-");
059        if (parts.length > 1) {
060            String name = parts[0];
061            String version = distributionId.replace(name + "-", "");
062            name = getName(name);
063            List<String> potentialVersions = new ArrayList<>();
064            Map<String, String> dist4Version = new HashMap<>();
065            for (DistributionSnapshot snap : snaps) {
066                if (getName(snap.getName()).equalsIgnoreCase(name)) {
067                    potentialVersions.add(snap.getVersion());
068                    dist4Version.put(snap.getVersion(), snap.getName());
069                    if (snap.getVersion().equals(version)) {
070                        return snap.getKey();
071                    }
072                }
073            }
074
075            potentialVersions.add(version);
076            Collections.sort(potentialVersions);
077            int idx = potentialVersions.indexOf(version);
078
079            String targetVersion = null;
080            if (idx == potentialVersions.size() - 1) {
081                targetVersion = potentialVersions.get(idx - 1);
082            } else if (idx < potentialVersions.size() - 1) {
083                targetVersion = potentialVersions.get(idx + 1);
084            }
085
086            if (targetVersion != null) {
087                return dist4Version.get(targetVersion) + "-" + targetVersion;
088            }
089        }
090        return null;
091    }
092
093    protected static String getName(String name) {
094        Optional<String> first = Arrays.stream(capaliases)
095                .filter(s -> name.toLowerCase().startsWith(s.toLowerCase()))
096                .findFirst();
097        return first.isPresent() ? "cap" : name;
098    }
099
100}