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.IOException;
021
022import org.nuxeo.ecm.core.api.CoreSession;
023import org.nuxeo.ecm.core.api.DocumentModel;
024import org.nuxeo.ecm.core.api.DocumentModelList;
025import org.nuxeo.ecm.core.api.NuxeoException;
026import org.nuxeo.ecm.core.api.PathRef;
027import org.nuxeo.runtime.model.ComponentInstance;
028import org.nuxeo.runtime.model.DefaultComponent;
029
030/**
031 * @author ajubert
032 */
033public class FSExporter extends DefaultComponent implements FSExporterService {
034
035    protected FSExporterPlugin exporter = new DefaultExporterPlugin();
036
037    @Override
038    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
039        ExportLogicDescriptor exportLogicDesc = (ExportLogicDescriptor) contribution;
040        if (exportLogicDesc.plugin != null) {
041            try {
042                exporter = exportLogicDesc.plugin.newInstance();
043            } catch (IllegalAccessException | InstantiationException e) {
044                throw new NuxeoException("Failed to instantiate " + exportLogicDesc.plugin, e);
045            }
046        }
047    }
048
049    @Override
050    public void export(CoreSession session, String rootPath, String fspath, String PageProvider) throws IOException {
051        DocumentModel root = session.getDocument(new PathRef(rootPath));
052        serializeStructure(session, fspath, root, PageProvider);
053    }
054
055    private void serializeStructure(CoreSession session, String fsPath, DocumentModel doc, String PageProvider)
056            throws IOException {
057        exporter.serialize(session, doc, fsPath);
058
059        if (doc.isFolder()) {
060            DocumentModelList children = exporter.getChildren(session, doc, PageProvider);
061            for (DocumentModel child : children) {
062                serializeStructure(session, fsPath + "/" + doc.getName(), child, PageProvider);
063            }
064        }
065    }
066
067    @Override
068    public void exportXML(CoreSession session, String rootName, String fileSystemTarget) {
069        throw new UnsupportedOperationException();
070    }
071
072}