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.Collection;
024import java.util.List;
025
026import org.nuxeo.ecm.core.api.Blob;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.blobholder.DocumentBlobHolder;
029import org.nuxeo.ecm.core.api.model.Property;
030
031public class PictureBlobHolder extends DocumentBlobHolder {
032
033    public PictureBlobHolder(DocumentModel doc, String path) {
034        super(doc, path);
035    }
036
037    @Override
038    public List<Blob> getBlobs() {
039        List<Blob> blobList = new ArrayList<>();
040
041        Blob mainBlob = getBlob();
042        if (mainBlob != null) {
043            blobList.add(getBlob());
044        }
045
046        Collection<Property> views = doc.getProperty("picture:views").getChildren();
047        for (Property property : views) {
048            blobList.add((Blob) property.getValue("content"));
049        }
050        return blobList;
051    }
052
053    public List<Blob> getBlobs(String... viewNames) {
054        List<Blob> blobList = new ArrayList<>();
055        for (String viewName : viewNames) {
056            blobList.add(getBlob(viewName));
057        }
058        return blobList;
059    }
060
061    public Blob getBlob(String title) {
062        PictureResourceAdapter picture = doc.getAdapter(PictureResourceAdapter.class);
063        return picture.getPictureFromTitle(title);
064    }
065
066    @Override
067    public String getHash() {
068
069        Blob blob = getBlob();
070        if (blob != null) {
071            String h = blob.getDigest();
072            if (h != null) {
073                return h;
074            }
075        }
076        return doc.getId() + xPath + getModificationDate().toString();
077    }
078
079}