001/* 002 * (C) Copyright 2012 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.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 * Mariana Cedica 016 */ 017package org.nuxeo.ecm.platform.routing.core.persistence; 018 019import java.io.IOException; 020import java.util.zip.ZipFile; 021 022import org.nuxeo.common.utils.Path; 023import org.nuxeo.ecm.core.api.Blob; 024import org.nuxeo.ecm.core.api.CloseableFile; 025import org.nuxeo.ecm.core.api.CoreSession; 026import org.nuxeo.ecm.core.api.DocumentModel; 027import org.nuxeo.ecm.core.api.DocumentRef; 028import org.nuxeo.ecm.core.api.PathRef; 029import org.nuxeo.ecm.core.api.security.ACP; 030import org.nuxeo.ecm.core.io.DocumentPipe; 031import org.nuxeo.ecm.core.io.DocumentReader; 032import org.nuxeo.ecm.core.io.DocumentWriter; 033import org.nuxeo.ecm.core.io.ExportedDocument; 034import org.nuxeo.ecm.core.io.impl.DocumentPipeImpl; 035import org.nuxeo.ecm.core.io.impl.plugins.DocumentModelWriter; 036import org.nuxeo.ecm.core.io.impl.plugins.NuxeoArchiveReader; 037import org.nuxeo.ecm.platform.filemanager.service.extension.ExportedZipImporter; 038import org.nuxeo.ecm.platform.types.TypeManager; 039 040/** 041 * Imports a route document from a zip archive using the IO core service . Existing route model with the same path as 042 * the are one to be imported is deleted before import. 043 * 044 * @since 5.6 045 */ 046public class RouteModelsZipImporter extends ExportedZipImporter { 047 048 private static final long serialVersionUID = 1L; 049 050 @Override 051 public DocumentModel create(CoreSession session, Blob content, String path, boolean overwrite, String filename, 052 TypeManager typeService) throws IOException { 053 try (CloseableFile source = content.getCloseableFile()) { 054 ZipFile zip = getArchiveFileIfValid(source.getFile()); 055 if (zip == null) { 056 return null; 057 } 058 zip.close(); 059 060 boolean overWrite = false; 061 DocumentReader reader = new NuxeoArchiveReader(source.getFile()); 062 ExportedDocument root = reader.read(); 063 PathRef rootRef = new PathRef(path, root.getPath().toString()); 064 ACP currentRouteModelACP = null; 065 if (session.exists(rootRef)) { 066 DocumentModel target = session.getDocument(rootRef); 067 if (target.getPath().removeLastSegments(1).equals(new Path(path))) { 068 overWrite = true; 069 // clean up existing route before import 070 DocumentModel routeModel = session.getDocument(rootRef); 071 currentRouteModelACP = routeModel.getACP(); 072 session.removeDocument(rootRef); 073 } 074 } 075 076 DocumentWriter writer = new DocumentModelWriter(session, path, 10); 077 reader.close(); 078 reader = new NuxeoArchiveReader(source.getFile()); 079 080 DocumentRef resultingRef; 081 if (overwrite && overWrite) { 082 resultingRef = rootRef; 083 } else { 084 String rootName = root.getPath().lastSegment(); 085 resultingRef = new PathRef(path, rootName); 086 } 087 088 try { 089 DocumentPipe pipe = new DocumentPipeImpl(10); 090 pipe.setReader(reader); 091 pipe.setWriter(writer); 092 pipe.run(); 093 } finally { 094 reader.close(); 095 writer.close(); 096 } 097 098 DocumentModel newRouteModel = session.getDocument(resultingRef); 099 if (currentRouteModelACP != null && overwrite && overWrite) { 100 newRouteModel.setACP(currentRouteModelACP, true); 101 } 102 return session.saveDocument(newRouteModel); 103 } 104 } 105}