001/* 002 * (C) Copyright 2014-2015 Nuxeo SA (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-2.1.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 * annejubert 016 */ 017package org.nuxeo.io.fsexporter; 018 019import java.io.File; 020import java.io.IOException; 021import java.io.Serializable; 022import java.util.Date; 023import java.util.HashMap; 024import java.util.List; 025import java.util.Map; 026 027import org.apache.commons.lang.StringUtils; 028 029import org.nuxeo.ecm.core.api.Blob; 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.impl.DocumentModelListImpl; 035import org.nuxeo.ecm.platform.query.api.PageProvider; 036import org.nuxeo.ecm.platform.query.api.PageProviderService; 037import org.nuxeo.ecm.platform.query.core.CoreQueryPageProviderDescriptor; 038import org.nuxeo.ecm.platform.query.nxql.CoreQueryDocumentPageProvider; 039import org.nuxeo.runtime.api.Framework; 040 041public class DefaultExporterPlugin implements FSExporterPlugin { 042 043 @Override 044 public DocumentModelList getChildren(CoreSession session, DocumentModel doc, String customQuery) { 045 Map<String, Serializable> props = new HashMap<>(); 046 props.put(CoreQueryDocumentPageProvider.CORE_SESSION_PROPERTY, (Serializable) session); 047 048 String query = ""; 049 // if the user gives a query, we build a new Page Provider with the query provided 050 if (StringUtils.isNotBlank(customQuery)) { 051 if (customQuery.toLowerCase().contains(" where")) { 052 query = customQuery + " AND ecm:parentId = ?"; 053 } else { 054 query = customQuery + " where ecm:parentId = ?"; 055 } 056 } else { 057 query = "SELECT * FROM Document WHERE ecm:parentId = ? AND ecm:mixinType !='HiddenInNavigation' AND ecm:isCheckedInVersion = 0 AND ecm:currentLifeCycleState !='deleted'"; 058 } 059 CoreQueryPageProviderDescriptor desc = new CoreQueryPageProviderDescriptor(); 060 desc.setPattern(query); 061 062 PageProviderService ppService = Framework.getService(PageProviderService.class); 063 @SuppressWarnings("unchecked") 064 PageProvider<DocumentModel> pp = (PageProvider<DocumentModel>) ppService.getPageProvider("customPP", desc, 065 null, null, null, null, props, new Object[] { doc.getId() }); 066 067 int countPages = 1; 068 // get all the documents of the first page 069 DocumentModelList children = new DocumentModelListImpl(pp.getCurrentPage()); 070 // if there is more than one page, get the children of all the other pages and put into one list 071 if (pp.getNumberOfPages() > 1) { 072 while (countPages < pp.getNumberOfPages()) { 073 pp.nextPage(); 074 List<DocumentModel> childrenTemp = pp.getCurrentPage(); 075 for (DocumentModel childTemp : childrenTemp) { 076 children.add(childTemp); 077 } 078 countPages++; 079 } 080 } 081 // return the complete list of documents 082 return children; 083 } 084 085 @Override 086 public File serialize(CoreSession session, DocumentModel docfrom, String fsPath) throws IOException { 087 File folder = null; 088 File newFolder = null; 089 folder = new File(fsPath); 090 091 // if target directory doesn't exist, create it 092 if (!folder.exists()) { 093 folder.mkdir(); 094 } 095 096 if (docfrom.hasFacet("Folderish")) { 097 newFolder = new File(fsPath + "/" + docfrom.getName()); 098 newFolder.mkdir(); 099 } 100 101 // get all the blobs of the blob holder 102 BlobHolder myblobholder = docfrom.getAdapter(BlobHolder.class); 103 if (myblobholder != null) { 104 java.util.List<Blob> listblobs = myblobholder.getBlobs(); 105 int i = 1; 106 for (Blob blob : listblobs) { 107 // call the method to determine the name of the exported file 108 String FileNameToExport = getFileName(blob, docfrom, folder, i); 109 // export the file to the target file system 110 File target = new File(folder, FileNameToExport); 111 blob.transferTo(target); 112 i++; 113 } 114 } 115 if (newFolder != null) { 116 folder = newFolder; 117 } 118 return folder; 119 } 120 121 protected String getFileName(Blob blob, DocumentModel docfrom, File folder, int i) { 122 String prefix = ""; 123 // if not principal file, prefix = name of the file which contains the blobs 124 if (i != 1) { 125 prefix = docfrom.getName() + "-"; 126 } 127 128 // if already existing file, prefix with "timestamp" 129 File alreadyExistingBlob = new File(folder, prefix + blob.getFilename()); 130 131 if (alreadyExistingBlob.exists()) { 132 prefix = String.valueOf(new Date().getTime()) + "-" + prefix; 133 } 134 135 return prefix + blob.getFilename(); 136 } 137}