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