001/* 002 * (C) Copyright 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 * Nuxeo 016 */ 017 018package org.nuxeo.ecm.showcase.content; 019 020import java.io.IOException; 021import java.util.Calendar; 022import java.util.Collections; 023import java.util.zip.ZipFile; 024 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.nuxeo.common.utils.Path; 028import org.nuxeo.ecm.core.api.Blob; 029import org.nuxeo.ecm.core.api.CloseableFile; 030import org.nuxeo.ecm.core.api.CoreSession; 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.core.api.DocumentRef; 033import org.nuxeo.ecm.core.api.IdRef; 034import org.nuxeo.ecm.core.api.PathRef; 035import org.nuxeo.ecm.core.io.DocumentPipe; 036import org.nuxeo.ecm.core.io.DocumentReader; 037import org.nuxeo.ecm.core.io.DocumentWriter; 038import org.nuxeo.ecm.core.io.ExportedDocument; 039import org.nuxeo.ecm.core.io.impl.DocumentPipeImpl; 040import org.nuxeo.ecm.core.io.impl.plugins.NuxeoArchiveReader; 041import org.nuxeo.ecm.platform.audit.api.AuditLogger; 042import org.nuxeo.ecm.platform.audit.api.LogEntry; 043import org.nuxeo.ecm.platform.audit.api.Logs; 044import org.nuxeo.ecm.platform.filemanager.api.FileManager; 045import org.nuxeo.ecm.platform.filemanager.service.extension.ExportedZipImporter; 046import org.nuxeo.runtime.api.Framework; 047 048/** 049 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a> 050 * @since 7.10 051 */ 052public class ShowcaseContentImporter { 053 public static final String INITIALIZED_EVENT = "ShowcaseContentImported"; 054 055 private static final Log log = LogFactory.getLog(ShowcaseContentImporter.class); 056 057 protected CoreSession session; 058 059 public ShowcaseContentImporter(CoreSession session) { 060 this.session = session; 061 } 062 063 public DocumentModel create(Blob blob) throws IOException { 064 if (isImported()) { 065 log.debug("Showcase Content already imported."); 066 return null; 067 } 068 069 FileManager importer = Framework.getLocalService(FileManager.class); 070 DocumentModel doc = create(session, blob, getImportPathRoot(), true); 071 072 markImportDone(); 073 return doc; 074 } 075 076 protected DocumentModel create(CoreSession documentManager, Blob content, String path, boolean overwrite) 077 throws IOException { 078 try (CloseableFile source = content.getCloseableFile(".zip")) { 079 ZipFile zip = ExportedZipImporter.getArchiveFileIfValid(source.getFile()); 080 if (zip == null) { 081 return null; 082 } 083 zip.close(); 084 085 boolean importWithIds = false; 086 DocumentReader reader = new NuxeoArchiveReader(source.getFile()); 087 ExportedDocument root = reader.read(); 088 IdRef rootRef = new IdRef(root.getId()); 089 090 if (documentManager.exists(rootRef)) { 091 DocumentModel target = documentManager.getDocument(rootRef); 092 if (target.getPath().removeLastSegments(1).equals(new Path(path))) { 093 importWithIds = true; 094 } 095 } 096 097 DocumentWriter writer = new ShowcaseWriter(documentManager, path, 10); 098 reader.close(); 099 reader = new NuxeoArchiveReader(source.getFile()); 100 101 DocumentRef resultingRef; 102 if (overwrite && importWithIds) { 103 resultingRef = rootRef; 104 } else { 105 String rootName = root.getPath().lastSegment(); 106 resultingRef = new PathRef(path, rootName); 107 } 108 109 try { 110 DocumentPipe pipe = new DocumentPipeImpl(10); 111 pipe.setReader(reader); 112 pipe.setWriter(writer); 113 pipe.run(); 114 } catch (IOException e) { 115 log.warn(e, e); 116 } finally { 117 reader.close(); 118 writer.close(); 119 } 120 return documentManager.getDocument(resultingRef); 121 } 122 } 123 124 protected boolean isImported() { 125 return Framework.getService(Logs.class).getEventsCount(INITIALIZED_EVENT) > 0; 126 } 127 128 protected String getImportPathRoot() { 129 return session.query("Select * from Domain").get(0).getPathAsString(); 130 } 131 132 protected void markImportDone() { 133 AuditLogger logger = Framework.getLocalService(AuditLogger.class); 134 LogEntry entry = logger.newLogEntry(); 135 entry.setEventId(INITIALIZED_EVENT); 136 entry.setEventDate(Calendar.getInstance().getTime()); 137 138 logger.addLogEntries(Collections.singletonList(entry)); 139 } 140}