001package org.nuxeo.ecm.platform.exalead.ws;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import org.apache.commons.logging.Log;
007import org.apache.commons.logging.LogFactory;
008import org.nuxeo.ecm.core.api.Blob;
009import org.nuxeo.ecm.core.api.CoreSession;
010import org.nuxeo.ecm.core.api.DocumentModel;
011import org.nuxeo.ecm.core.api.IdRef;
012import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
013import org.nuxeo.ecm.core.io.download.DownloadService;
014import org.nuxeo.ecm.platform.api.ws.DocumentBlob;
015import org.nuxeo.ecm.platform.indexing.gateway.adapter.BaseIndexingAdapter;
016import org.nuxeo.ecm.platform.indexing.gateway.adapter.IndexingAdapter;
017import org.nuxeo.runtime.api.Framework;
018
019public class NoteBlobAdapter extends BaseIndexingAdapter implements IndexingAdapter {
020
021    protected static final Log log = LogFactory.getLog(NoteBlobAdapter.class);
022
023    @Override
024    public DocumentBlob[] adaptDocumentBlobs(CoreSession session, String uuid, DocumentBlob[] blobs)
025            {
026
027        DocumentModel doc = session.getDocument(new IdRef(uuid));
028        if ("Note".equals(doc.getType())) {
029
030            BlobHolder bh = doc.getAdapter(BlobHolder.class);
031            if (bh != null && bh.getBlob() != null) {
032                DownloadService downloadService = Framework.getService(DownloadService.class);
033                String filename = bh.getBlob().getFilename();
034                String url = "/" + downloadService.getDownloadUrl(doc, DownloadService.BLOBHOLDER_0, filename);
035                Blob blob = bh.getBlob();
036                DocumentBlob db = new DocumentBlob(blob.getFilename(), blob.getEncoding(), blob.getMimeType(), url);
037                List<DocumentBlob> dbs = new ArrayList<DocumentBlob>();
038                dbs.add(db);
039                if (blobs != null) {
040                    for (DocumentBlob dbi : blobs) {
041                        dbs.add(dbi);
042                    }
043                }
044                return dbs.toArray(new DocumentBlob[dbs.size()]);
045            }
046        }
047        return blobs;
048    }
049}