001/*
002 * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.picture.api.adapters;
021
022import java.util.ArrayList;
023import java.util.List;
024
025import org.nuxeo.ecm.core.api.Blob;
026import org.nuxeo.ecm.core.api.CoreInstance;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.DocumentModelList;
030import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
031import org.nuxeo.ecm.core.api.blobholder.DocumentBlobHolder;
032
033public class PictureBookBlobHolder extends DocumentBlobHolder {
034
035    private CoreSession session;
036
037    public PictureBookBlobHolder(DocumentModel doc, String xPath) {
038        super(doc, xPath);
039    }
040
041    @Override
042    public Blob getBlob() {
043        CoreSession session = getSession();
044        boolean sessionOpened = false;
045        if (session == null) {
046            sessionOpened = true;
047            session = CoreInstance.openCoreSession(doc.getRepositoryName());
048        }
049        try {
050            DocumentModelList docs = session.getChildren(doc.getRef(), "Picture");
051            if (docs.isEmpty()) {
052                return null;
053            }
054            DocumentModel documentModel = docs.get(0);
055            if (documentModel == null) {
056                return null;
057            }
058            BlobHolder bh = documentModel.getAdapter(BlobHolder.class);
059            return bh.getBlob();
060        } finally {
061            if (sessionOpened) {
062                session.close();
063            }
064        }
065
066    }
067
068    @Override
069    public List<Blob> getBlobs() {
070        return getBlobs("Original");
071    }
072
073    public List<Blob> getBlobs(String title) {
074        CoreSession session = getSession();
075        boolean sessionOpened = false;
076        if (session == null) {
077            sessionOpened = true;
078            session = CoreInstance.openCoreSession(doc.getRepositoryName());
079        }
080        try {
081            DocumentModelList docList = session.getChildren(doc.getRef(), "Picture");
082            List<Blob> blobList = new ArrayList<Blob>(docList.size());
083            for (DocumentModel documentModel : docList) {
084                if ("Original".equals(title)) {
085                    BlobHolder bh = documentModel.getAdapter(BlobHolder.class);
086                    blobList.add(bh.getBlob());
087                } else {
088                    PictureResourceAdapter picture = documentModel.getAdapter(PictureResourceAdapter.class);
089                    blobList.add(picture.getPictureFromTitle(title));
090                }
091            }
092            return blobList;
093        } finally {
094            if (sessionOpened) {
095                session.close();
096            }
097        }
098    }
099
100    @Override
101    public String getHash() {
102        Blob blob = getBlob();
103        if (blob != null) {
104            String h = blob.getDigest();
105            if (h != null) {
106                return h;
107            }
108        }
109        return doc.getId() + xPath + getModificationDate().toString();
110    }
111
112    protected CoreSession getSession() {
113        if (session == null && doc != null) {
114            session = doc.getCoreSession();
115        }
116        return session;
117    }
118
119}