001/*
002 * (C) Copyright 2011-2018 Nuxeo (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.api;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.List;
024
025import org.apache.commons.lang3.StringUtils;
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.apidoc.documentation.DocumentationComponent;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.PropertyException;
031import org.nuxeo.ecm.directory.DirectoryException;
032import org.nuxeo.ecm.directory.Session;
033import org.nuxeo.ecm.directory.api.DirectoryService;
034import org.nuxeo.runtime.api.Framework;
035
036public abstract class AbstractDocumentationItem implements DocumentationItem {
037
038    protected static final Log log = LogFactory.getLog(AbstractDocumentationItem.class);
039
040    protected AbstractDocumentationItem(String typeLabel) {
041        this.typeLabel = typeLabel;
042    }
043
044    final String typeLabel;
045
046    @Override
047    public int compareTo(DocumentationItem o) {
048
049        List<String> myVersions = new ArrayList<>(getApplicableVersion());
050        List<String> otherVersions = new ArrayList<>(o.getApplicableVersion());
051
052        Collections.sort(myVersions);
053        Collections.sort(otherVersions);
054        Collections.reverse(myVersions);
055        Collections.reverse(otherVersions);
056
057        if (myVersions.isEmpty()) {
058            if (otherVersions.isEmpty()) {
059                return 0;
060            }
061            return 1;
062        } else if (otherVersions.isEmpty()) {
063            return -1;
064        }
065
066        return myVersions.get(0).compareTo(otherVersions.get(0));
067    }
068
069    @Override
070    public String getTypeLabel() {
071        return typeLabel;
072    }
073
074    public static String typeLabelOf(String type) {
075        if (StringUtils.isBlank(type)) {
076            return "";
077        }
078
079        DirectoryService dm = Framework.getService(DirectoryService.class);
080        try (Session session = dm.open(DocumentationComponent.DIRECTORY_NAME)) {
081            DocumentModel entry = session.getEntry(type);
082            if (entry != null) {
083                return (String) entry.getProperty("vocabulary", "label");
084            }
085        } catch (DirectoryException | PropertyException e) {
086            log.error("Error while resolving typeLabel", e);
087        }
088        return type;
089    }
090
091}