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; 024import java.util.List; 025import java.util.stream.Collectors; 026 027import org.nuxeo.ecm.collections.api.CollectionConstants; 028import org.nuxeo.ecm.collections.core.adapter.Collection; 029import org.nuxeo.ecm.core.api.CoreSession; 030import org.nuxeo.ecm.core.api.DocumentModel; 031import org.nuxeo.ecm.core.api.DocumentModelList; 032import org.nuxeo.ecm.core.api.IdRef; 033import org.nuxeo.ecm.core.api.NuxeoException; 034import org.nuxeo.ecm.core.api.PathRef; 035import org.nuxeo.runtime.model.ComponentInstance; 036import org.nuxeo.runtime.model.DefaultComponent; 037 038/** 039 * @author ajubert 040 */ 041public class FSExporter extends DefaultComponent implements FSExporterService { 042 043 protected FSExporterPlugin exporter = new DefaultExporterPlugin(); 044 045 @Override 046 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 047 ExportLogicDescriptor exportLogicDesc = (ExportLogicDescriptor) contribution; 048 if (exportLogicDesc.plugin != null) { 049 try { 050 exporter = exportLogicDesc.plugin.getDeclaredConstructor().newInstance(); 051 } catch (ReflectiveOperationException e) { 052 throw new NuxeoException("Failed to instantiate " + exportLogicDesc.plugin, e); 053 } 054 } 055 } 056 057 @Override 058 public void export(CoreSession session, String rootPath, String fspath, String PageProvider) throws IOException { 059 DocumentModel root = session.getDocument(new PathRef(rootPath)); 060 serializeStructure(session, fspath, root, PageProvider); 061 } 062 063 private void serializeStructure(CoreSession session, String fsPath, DocumentModel doc, String PageProvider) 064 throws IOException { 065 File serialized = exporter.serialize(session, doc, fsPath); 066 067 if (doc.isFolder()) { 068 DocumentModelList children = exporter.getChildren(session, doc, PageProvider); 069 for (DocumentModel child : children) { 070 serializeStructure(session, serialized.getAbsolutePath(), child, PageProvider); 071 } 072 } else if (doc.hasFacet(CollectionConstants.COLLECTION_FACET)) { 073 List<DocumentModel> collectedDocs = doc.getAdapter(Collection.class) 074 .getCollectedDocumentIds() 075 .stream() 076 .map(IdRef::new) 077 .map(session::getDocument) 078 .collect(Collectors.toList()); 079 for (DocumentModel collected : collectedDocs) { 080 serializeStructure(session, serialized.getAbsolutePath(), collected, PageProvider); 081 } 082 } 083 } 084 085 @Override 086 public void exportXML(CoreSession session, String rootName, String fileSystemTarget) { 087 throw new UnsupportedOperationException(); 088 } 089 090}