001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.importexport;
029
030import org.opencms.file.CmsObject;
031import org.opencms.main.CmsEvent;
032import org.opencms.main.I_CmsEventListener;
033import org.opencms.main.OpenCms;
034import org.opencms.report.I_CmsReport;
035import org.opencms.security.CmsRole;
036import org.opencms.security.CmsRoleViolationException;
037import org.opencms.xml.CmsXmlException;
038
039import java.util.Collections;
040import java.util.Iterator;
041import java.util.List;
042
043/**
044 * Holds the functionality to import resources from the file system
045 * or a zip file into the OpenCms VFS.<p>
046 *
047 * @since 6.0.0
048 */
049public class CmsImport {
050
051    /** The cms context. */
052    protected CmsObject m_cms;
053
054    /** The output report. */
055    protected I_CmsReport m_report;
056
057    /** Stores all import interface implementations .*/
058    protected List<I_CmsImport> m_importImplementations;
059
060    /**
061     * Constructs a new uninitialized import, required for special subclass data import.<p>
062     */
063    public CmsImport() {
064
065        // empty
066        super();
067    }
068
069    /**
070     * Constructs a new import object which imports the resources from an OpenCms
071     * export zip file or a folder in the "real" file system.<p>
072     *
073     * @param cms the cms context
074     * @param report the output report
075     *
076     * @throws CmsRoleViolationException if the current user dies not have role permissions to import the database
077     */
078    public CmsImport(CmsObject cms, I_CmsReport report)
079    throws CmsRoleViolationException {
080
081        // check the role permissions
082        OpenCms.getRoleManager().checkRole(cms, CmsRole.DATABASE_MANAGER);
083
084        // set member variables
085        m_importImplementations = OpenCms.getImportExportManager().getImportVersionClasses();
086        m_cms = cms;
087        m_report = report;
088    }
089
090    /**
091     * Imports the resources and writes them to the cms VFS, even if there
092     * already exist files with the same name.<p>
093     *
094     * @param parameters the import parameters
095     *
096     * @throws CmsImportExportException if something goes wrong
097     * @throws CmsXmlException if the manifest of the import file could not be unmarshalled
098     */
099    public void importData(CmsImportParameters parameters) throws CmsImportExportException, CmsXmlException {
100
101        boolean run = false;
102
103        try {
104            // now find the correct import implementation
105            Iterator<I_CmsImport> i = m_importImplementations.iterator();
106            while (i.hasNext()) {
107                I_CmsImport importVersion = i.next();
108                if (importVersion.matches(parameters)) {
109                    m_report.println(
110                        Messages.get().container(
111                            Messages.RPT_IMPORT_VERSION_1,
112                            String.valueOf(importVersion.getVersion())),
113                        I_CmsReport.FORMAT_NOTE);
114                    // this is the correct import version, so call it for the import process
115                    importVersion.importData(m_cms, m_report, parameters);
116                    OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_FLEX_PURGE_JSP_REPOSITORY, null));
117                    run = true;
118                    break;
119                }
120            }
121            if (!run) {
122                m_report.println(
123                    Messages.get().container(Messages.RPT_IMPORT_DB_NO_CLASS_1, parameters.getPath()),
124                    I_CmsReport.FORMAT_WARNING);
125            }
126        } finally {
127            OpenCms.fireCmsEvent(
128                new CmsEvent(I_CmsEventListener.EVENT_CLEAR_OFFLINE_CACHES, Collections.<String, Object> emptyMap()));
129        }
130    }
131}