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