001/*
002 * (C) Copyright 2006-2009 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 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.picture.api.adapters;
023
024import java.util.ArrayList;
025import java.util.List;
026
027import org.nuxeo.ecm.core.api.Blob;
028import org.nuxeo.ecm.core.api.CoreInstance;
029import org.nuxeo.ecm.core.api.CoreSession;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.DocumentModelList;
032import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
033import org.nuxeo.ecm.core.api.blobholder.DocumentBlobHolder;
034
035public class PictureBookBlobHolder extends DocumentBlobHolder {
036
037    private CoreSession session;
038
039    public PictureBookBlobHolder(DocumentModel doc, String xPath) {
040        super(doc, xPath);
041    }
042
043    @Override
044    public Blob getBlob() {
045        CoreSession session = getSession();
046        boolean sessionOpened = false;
047        if (session == null) {
048            sessionOpened = true;
049            session = CoreInstance.openCoreSession(doc.getRepositoryName());
050        }
051        try {
052            DocumentModelList docs = session.getChildren(doc.getRef(), "Picture");
053            if (docs.isEmpty()) {
054                return null;
055            }
056            DocumentModel documentModel = docs.get(0);
057            if (documentModel == null) {
058                return null;
059            }
060            BlobHolder bh = documentModel.getAdapter(BlobHolder.class);
061            return bh.getBlob();
062        } finally {
063            if (sessionOpened) {
064                session.close();
065            }
066        }
067
068    }
069
070    @Override
071    public List<Blob> getBlobs() {
072        return getBlobs("Original");
073    }
074
075    public List<Blob> getBlobs(String title) {
076        CoreSession session = getSession();
077        boolean sessionOpened = false;
078        if (session == null) {
079            sessionOpened = true;
080            session = CoreInstance.openCoreSession(doc.getRepositoryName());
081        }
082        try {
083            DocumentModelList docList = session.getChildren(doc.getRef(), "Picture");
084            List<Blob> blobList = new ArrayList<Blob>(docList.size());
085            for (DocumentModel documentModel : docList) {
086                if ("Original".equals(title)) {
087                    BlobHolder bh = documentModel.getAdapter(BlobHolder.class);
088                    blobList.add(bh.getBlob());
089                } else {
090                    PictureResourceAdapter picture = documentModel.getAdapter(PictureResourceAdapter.class);
091                    blobList.add(picture.getPictureFromTitle(title));
092                }
093            }
094            return blobList;
095        } finally {
096            if (sessionOpened) {
097                session.close();
098            }
099        }
100    }
101
102    @Override
103    public String getHash() {
104        Blob blob = getBlob();
105        if (blob != null) {
106            String h = blob.getDigest();
107            if (h != null) {
108                return h;
109            }
110        }
111        return doc.getId() + xPath + getModificationDate().toString();
112    }
113
114    protected CoreSession getSession() {
115        if (session == null && doc != null) {
116            session = doc.getCoreSession();
117        }
118        return session;
119    }
120
121}