001/*
002 * (C) Copyright 2010-2013 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 *     Olivier Grisel
018 */
019package org.nuxeo.ecm.platform.suggestbox.service;
020
021import java.util.ArrayList;
022import java.util.List;
023
024import org.apache.commons.lang.StringUtils;
025import org.nuxeo.ecm.core.api.DocumentLocation;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.IdRef;
028import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl;
029import org.nuxeo.ecm.platform.types.adapter.TypeInfo;
030
031/**
032 * Suggest to navigate to a specific document.
033 */
034public class DocumentSuggestion extends Suggestion {
035
036    private static final long serialVersionUID = 1L;
037
038    private static final String PREFIX = "nxdoc";
039
040    private static final String VIEW_ID = "view_documents";
041
042    protected final DocumentLocation documentLocation;
043
044    public DocumentSuggestion(String id, DocumentLocation documentLocation, String label, String iconURL) {
045        super(id, CommonSuggestionTypes.DOCUMENT, label, iconURL);
046        this.documentLocation = documentLocation;
047    }
048
049    public static Suggestion fromDocumentModel(DocumentModel doc) {
050        TypeInfo typeInfo = doc.getAdapter(TypeInfo.class);
051        String description = doc.getProperty("dc:description").getValue(String.class);
052        String icon = null;
053        if (doc.hasSchema("common")) {
054            icon = (String) doc.getProperty("common", "icon");
055        }
056        if (StringUtils.isEmpty(icon)) {
057            icon = typeInfo.getIcon();
058        }
059        String thumbnailURL = String.format("api/v1/id/%s/@rendition/thumbnail", doc.getId());
060        return new DocumentSuggestion(doc.getId(), new DocumentLocationImpl(doc), doc.getTitle(), icon).withDescription(
061                description).withThumbnailURL(thumbnailURL);
062    }
063
064    public DocumentLocation getDocumentLocation() {
065        return documentLocation;
066    }
067
068    @Override
069    public String getObjectUrl() {
070        if (documentLocation != null) {
071            List<String> items = new ArrayList<String>();
072            items.add(PREFIX);
073            items.add(documentLocation.getServerName());
074            IdRef docRef = documentLocation.getIdRef();
075            if (docRef == null) {
076                return null;
077            }
078            items.add(docRef.toString());
079            items.add(VIEW_ID);
080
081            String uri = StringUtils.join(items, "/");
082            return uri;
083        }
084        return null;
085    }
086}