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.Collections;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027public class SnapshotResolverHelper {
028
029    protected static final String[] capaliases = { "cap", "Nuxeo Platform", "Nuxeo cap", "Nuxeo DM", "dm" };
030
031    public static String findBestMatch(List<DistributionSnapshot> snaps, String distributionId) {
032
033        if (distributionId == null || "".equals(distributionId.trim())) {
034            return null;
035        }
036        if ("current".equalsIgnoreCase((distributionId.trim()))) {
037            return "current";
038        }
039
040        // exact match
041        for (DistributionSnapshot snap : snaps) {
042            if (snap.getKey().equalsIgnoreCase(distributionId)) {
043                return snap.getKey();
044            }
045        }
046
047        // name match + best version
048        String[] parts = distributionId.split("-");
049        if (parts.length > 1) {
050            String name = parts[0];
051            String version = distributionId.replace(name + "-", "");
052            name = getName(name);
053            List<String> potentialVersions = new ArrayList<String>();
054            Map<String, String> dist4Version = new HashMap<String, String>();
055            for (DistributionSnapshot snap : snaps) {
056                if (getName(snap.getName()).equalsIgnoreCase(name)) {
057                    potentialVersions.add(snap.getVersion());
058                    dist4Version.put(snap.getVersion(), snap.getName());
059                    if (snap.getVersion().equals(version)) {
060                        return snap.getKey();
061                    }
062                }
063            }
064
065            potentialVersions.add(version);
066            Collections.sort(potentialVersions);
067            int idx = potentialVersions.indexOf(version);
068
069            String targetVersion = null;
070            if (idx == potentialVersions.size() - 1) {
071                targetVersion = potentialVersions.get(idx - 1);
072            } else if (idx < potentialVersions.size() - 1) {
073                targetVersion = potentialVersions.get(idx + 1);
074            }
075
076            if (targetVersion != null) {
077                return dist4Version.get(targetVersion) + "-" + targetVersion;
078            }
079        }
080        return null;
081    }
082
083    protected static String getName(String name) {
084        for (String dname : capaliases) {
085            if (dname.equalsIgnoreCase(name)) {
086                return "cap";
087            }
088        }
089        return name;
090    }
091
092}