001/* 002 * (C) Copyright 2006-2016 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 */ 019package org.nuxeo.ecm.core.api.blobholder; 020 021import java.io.Serializable; 022import java.util.Calendar; 023import java.util.Iterator; 024import java.util.List; 025import java.util.Map; 026 027import org.nuxeo.ecm.core.api.Blob; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.core.utils.BlobsExtractor; 030 031/** 032 * {@link BlobHolder} implementation based on a {@link DocumentModel} and a XPath. 033 * 034 * @author tiry 035 */ 036public class DocumentBlobHolder extends AbstractBlobHolder { 037 038 protected final DocumentModel doc; 039 040 protected final String xPath; 041 042 protected List<Blob> blobList = null; 043 044 public DocumentBlobHolder(DocumentModel doc, String xPath) { 045 this.doc = doc; 046 this.xPath = xPath; 047 } 048 049 @Override 050 protected String getBasePath() { 051 return doc.getPathAsString(); 052 } 053 054 @Override 055 public Blob getBlob() { 056 return(Blob) doc.getPropertyValue(xPath); 057 } 058 059 @Override 060 public void setBlob(Blob blob) { 061 doc.getProperty(xPath).setValue(blob); 062 } 063 064 @Override 065 public Calendar getModificationDate() { 066 return (Calendar) doc.getProperty("dublincore", "modified"); 067 } 068 069 @Override 070 public String getHash() { 071 Blob blob = getBlob(); 072 if (blob != null) { 073 String h = blob.getDigest(); 074 if (h != null) { 075 return h; 076 } 077 } 078 return doc.getId() + xPath + String.valueOf(getModificationDate()); 079 } 080 081 @Override 082 public Serializable getProperty(String name) { 083 return null; 084 } 085 086 @Override 087 public Map<String, Serializable> getProperties() { 088 return null; 089 } 090 091 @Override 092 public List<Blob> getBlobs() { 093 if (blobList == null) { 094 List<Blob> blobs = new BlobsExtractor().getBlobs(doc); 095 Blob main = getBlob(); 096 if (main != null) { 097 // be sure the "main" blob is always in first position 098 Iterator<Blob> bi = blobs.iterator(); 099 while (bi.hasNext()) { 100 Blob blob = bi.next(); 101 if (blob.getDigest() != null) { 102 if (blob.getDigest().equals(main.getDigest())) { 103 bi.remove(); 104 break; 105 } 106 } else if (blob.getFilename() != null) { 107 if (blob.getFilename().equals(main.getFilename())) { 108 bi.remove(); 109 break; 110 } 111 } 112 } 113 blobs.add(0, main); 114 } 115 blobList = blobs; 116 } 117 return blobList; 118 } 119 120 /** 121 * @since 7.3 122 */ 123 public String getXpath() { 124 return xPath; 125 } 126 127 /** 128 * @since 7.4 129 */ 130 public DocumentModel getDocument() { 131 return doc; 132 } 133}