001/*
002 * (C) Copyright 2010-2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Olivier Grisel
016 */
017package org.nuxeo.ecm.platform.suggestbox.service;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.apache.commons.lang.StringUtils;
023import org.nuxeo.ecm.core.api.DocumentLocation;
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.core.api.IdRef;
026import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl;
027import org.nuxeo.ecm.platform.types.adapter.TypeInfo;
028
029/**
030 * Suggest to navigate to a specific document.
031 */
032public class DocumentSuggestion extends Suggestion {
033
034    private static final long serialVersionUID = 1L;
035
036    private static final String PREFIX = "nxdoc";
037
038    private static final String VIEW_ID = "view_documents";
039
040    protected final DocumentLocation documentLocation;
041
042    public DocumentSuggestion(String id, DocumentLocation documentLocation, String label, String iconURL) {
043        super(id, CommonSuggestionTypes.DOCUMENT, label, iconURL);
044        this.documentLocation = documentLocation;
045    }
046
047    public static Suggestion fromDocumentModel(DocumentModel doc) {
048        TypeInfo typeInfo = doc.getAdapter(TypeInfo.class);
049        String description = doc.getProperty("dc:description").getValue(String.class);
050        String icon = null;
051        if (doc.hasSchema("common")) {
052            icon = (String) doc.getProperty("common", "icon");
053        }
054        if (StringUtils.isEmpty(icon)) {
055            icon = typeInfo.getIcon();
056        }
057        return new DocumentSuggestion(doc.getId(), new DocumentLocationImpl(doc), doc.getTitle(), icon).withDescription(description);
058    }
059
060    public DocumentLocation getDocumentLocation() {
061        return documentLocation;
062    }
063
064    @Override
065    public String getObjectUrl() {
066        if (documentLocation != null) {
067            List<String> items = new ArrayList<String>();
068            items.add(PREFIX);
069            items.add(documentLocation.getServerName());
070            IdRef docRef = documentLocation.getIdRef();
071            if (docRef == null) {
072                return null;
073            }
074            items.add(docRef.toString());
075            items.add(VIEW_ID);
076
077            String uri = StringUtils.join(items, "/");
078            return uri;
079        }
080        return null;
081    }
082}