001/*
002 * (C) Copyright 2011 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 *     Thierry Delprat
018 */
019package org.nuxeo.ecm.platform.exalead.ws;
020
021import java.util.ArrayList;
022import java.util.List;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.core.api.Blob;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.IdRef;
030import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
031import org.nuxeo.ecm.core.io.download.DownloadService;
032import org.nuxeo.ecm.platform.api.ws.DocumentBlob;
033import org.nuxeo.ecm.platform.indexing.gateway.adapter.BaseIndexingAdapter;
034import org.nuxeo.ecm.platform.indexing.gateway.adapter.IndexingAdapter;
035import org.nuxeo.runtime.api.Framework;
036
037public class NoteBlobAdapter extends BaseIndexingAdapter implements IndexingAdapter {
038
039    protected static final Log log = LogFactory.getLog(NoteBlobAdapter.class);
040
041    @Override
042    public DocumentBlob[] adaptDocumentBlobs(CoreSession session, String uuid, DocumentBlob[] blobs)
043            {
044
045        DocumentModel doc = session.getDocument(new IdRef(uuid));
046        if ("Note".equals(doc.getType())) {
047
048            BlobHolder bh = doc.getAdapter(BlobHolder.class);
049            if (bh != null && bh.getBlob() != null) {
050                DownloadService downloadService = Framework.getService(DownloadService.class);
051                String filename = bh.getBlob().getFilename();
052                String url = "/" + downloadService.getDownloadUrl(doc, DownloadService.BLOBHOLDER_0, filename);
053                Blob blob = bh.getBlob();
054                DocumentBlob db = new DocumentBlob(blob.getFilename(), blob.getEncoding(), blob.getMimeType(), url);
055                List<DocumentBlob> dbs = new ArrayList<DocumentBlob>();
056                dbs.add(db);
057                if (blobs != null) {
058                    for (DocumentBlob dbi : blobs) {
059                        dbs.add(dbi);
060                    }
061                }
062                return dbs.toArray(new DocumentBlob[dbs.size()]);
063            }
064        }
065        return blobs;
066    }
067}