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