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.net.URL;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.ecm.core.api.Blob;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.impl.blob.URLBlob;
028import org.nuxeo.ecm.platform.content.template.service.PostContentCreationHandler;
029import org.nuxeo.runtime.api.Framework;
030
031/**
032 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a>
033 * @since 7.10
034 */
035public class InitListener implements PostContentCreationHandler {
036
037    private static final Log log = LogFactory.getLog(InitListener.class);
038
039    private static final String SHOWCASE_CONTENT = "showcase_content.zip";
040
041    @Override
042    public void execute(CoreSession session) {
043        if (Framework.isTestModeSet()) {
044            return;
045        }
046
047        try {
048            URL url = getClass().getClassLoader().getResource(SHOWCASE_CONTENT);
049            if (url == null) {
050                throw new IOException("Unable to found " + SHOWCASE_CONTENT + " resource.");
051            }
052
053            Blob blob = new URLBlob(url);
054            new ShowcaseContentImporter(session).create(blob);
055            log.info("Showcase content imported.");
056        } catch (IOException e) {
057            log.error(e, e);
058        }
059    }
060}