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