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