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