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    @SuppressWarnings("resource") // session closed only if we opened it
044    @Override
045    public Blob getBlob() {
046        CoreSession session = getSession();
047        if (session == null) {
048            session = CoreInstance.getCoreSession(doc.getRepositoryName());
049        }
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
061    }
062
063    @Override
064    public List<Blob> getBlobs() {
065        return getBlobs("Original");
066    }
067
068    @SuppressWarnings("resource") // session closed only if we opened it
069    public List<Blob> getBlobs(String title) {
070        CoreSession session = getSession();
071        if (session == null) {
072            session = CoreInstance.getCoreSession(doc.getRepositoryName());
073        }
074        DocumentModelList docList = session.getChildren(doc.getRef(), "Picture");
075        List<Blob> blobList = new ArrayList<>(docList.size());
076        for (DocumentModel documentModel : docList) {
077            if ("Original".equals(title)) {
078                BlobHolder bh = documentModel.getAdapter(BlobHolder.class);
079                blobList.add(bh.getBlob());
080            } else {
081                PictureResourceAdapter picture = documentModel.getAdapter(PictureResourceAdapter.class);
082                blobList.add(picture.getPictureFromTitle(title));
083            }
084        }
085        return blobList;
086    }
087
088    @Override
089    public String getHash() {
090        Blob blob = getBlob();
091        if (blob != null) {
092            String h = blob.getDigest();
093            if (h != null) {
094                return h;
095            }
096        }
097        return doc.getId() + xPath + getModificationDate().toString();
098    }
099
100    protected CoreSession getSession() {
101        if (session == null && doc != null) {
102            session = doc.getCoreSession();
103        }
104        return session;
105    }
106
107}