001package org.nuxeo.apidoc.api;
002
003import java.util.ArrayList;
004import java.util.Collections;
005import java.util.List;
006
007import org.apache.commons.logging.Log;
008import org.apache.commons.logging.LogFactory;
009import org.nuxeo.apidoc.documentation.DocumentationComponent;
010import org.nuxeo.ecm.core.api.DocumentModel;
011import org.nuxeo.ecm.core.api.PropertyException;
012import org.nuxeo.ecm.directory.DirectoryException;
013import org.nuxeo.ecm.directory.Session;
014import org.nuxeo.ecm.directory.api.DirectoryService;
015import org.nuxeo.runtime.api.Framework;
016
017public abstract class AbstractDocumentationItem implements DocumentationItem {
018
019    protected static final Log log = LogFactory.getLog(AbstractDocumentationItem.class);
020
021    @Override
022    public int compareTo(DocumentationItem o) {
023
024        List<String> myVersions = new ArrayList<String>(getApplicableVersion());
025        List<String> otherVersions = new ArrayList<String>(o.getApplicableVersion());
026
027        Collections.sort(myVersions);
028        Collections.sort(otherVersions);
029        Collections.reverse(myVersions);
030        Collections.reverse(otherVersions);
031
032        if (myVersions.isEmpty()) {
033            if (otherVersions.isEmpty()) {
034                return 0;
035            }
036            return 1;
037        } else if (otherVersions.isEmpty()) {
038            return -1;
039        }
040
041        return myVersions.get(0).compareTo(otherVersions.get(0));
042    }
043
044    @Override
045    public String getTypeLabel() {
046        String type = getType();
047        if ("".equals(type)) {
048            return "";
049        }
050        if (Framework.isTestModeSet()) {
051            return type;
052        }
053
054        DirectoryService dm = Framework.getService(DirectoryService.class);
055        try (Session session = dm.open(DocumentationComponent.DIRECTORY_NAME)) {
056            try {
057                DocumentModel entry = session.getEntry(type);
058                return (String) entry.getProperty("vocabulary", "label");
059            } catch (PropertyException e) {
060                log.error("Error while resolving typeLabel", e);
061            }
062        }
063        return "";
064    }
065
066}