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 */
017
018package org.nuxeo.io.fsexporter;
019
020import java.io.File;
021import java.io.IOException;
022
023import org.nuxeo.ecm.core.api.Blob;
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
027import org.nuxeo.ecm.core.io.DocumentPipe;
028import org.nuxeo.ecm.core.io.impl.DocumentPipeImpl;
029import org.nuxeo.ecm.core.io.impl.plugins.SingleDocumentReader;
030import org.nuxeo.ecm.core.io.impl.plugins.XMLDocumentWriter;
031
032public class CustomExporterPlugin extends DefaultExporterPlugin {
033
034    @Override
035    public File serialize(CoreSession session, DocumentModel docfrom, String fsPath) throws IOException {
036        // code to export XML
037        BlobHolder myblobholder = docfrom.getAdapter(BlobHolder.class);
038        String FileXMLNameToExport = "";
039        File folder = new File(fsPath);
040
041        if (myblobholder != null) {
042            java.util.List<Blob> listblobs = myblobholder.getBlobs();
043            int i = 1;
044            for (Blob blob : listblobs) {
045                // call the method to determine the name of the exported file
046                FileXMLNameToExport = getFileName(blob, docfrom, folder, i);
047                i++;
048            }
049            exportFileInXML(session, docfrom, fsPath + "/" + FileXMLNameToExport);
050        }
051        // export with default exporter all the documents
052        return super.serialize(session, docfrom, fsPath);
053    }
054
055    protected void exportFileInXML(CoreSession session, DocumentModel docfrom, String pathtoexport) throws IOException {
056        DocumentPipe pipe = new DocumentPipeImpl(10);
057        SingleDocumentReader reader = new SingleDocumentReader(session, docfrom);
058        pipe.setReader(reader);
059        XMLDocumentWriter writer = new XMLDocumentWriter(new File(pathtoexport + ".xml"));
060        pipe.setWriter(writer);
061        pipe.run();
062    }
063
064}