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        return new DocumentSuggestion(doc.getId(), new DocumentLocationImpl(doc), doc.getTitle(), icon).withDescription(description);
060    }
061
062    public DocumentLocation getDocumentLocation() {
063        return documentLocation;
064    }
065
066    @Override
067    public String getObjectUrl() {
068        if (documentLocation != null) {
069            List<String> items = new ArrayList<String>();
070            items.add(PREFIX);
071            items.add(documentLocation.getServerName());
072            IdRef docRef = documentLocation.getIdRef();
073            if (docRef == null) {
074                return null;
075            }
076            items.add(docRef.toString());
077            items.add(VIEW_ID);
078
079            String uri = StringUtils.join(items, "/");
080            return uri;
081        }
082        return null;
083    }
084}