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