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, 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.file.CmsResource;
032import org.opencms.i18n.CmsMessageContainer;
033import org.opencms.main.CmsLog;
034import org.opencms.module.CmsModuleImportData;
035import org.opencms.module.CmsResourceImportData;
036import org.opencms.report.I_CmsReport;
037
038import org.apache.commons.logging.Log;
039
040import com.google.common.collect.Lists;
041
042/**
043 * Subclass which doesn't actually import anything, but just reads the module data into a
044 * data structure which can then be used by the module updater.<p>
045 */
046public class CmsImportResourceDataReader extends CmsImportVersion10 {
047
048    /** The logger instance for this class. */
049    private static final Log LOG = CmsLog.getLog(CmsImportResourceDataReader.class);
050
051    /** The module data object to be filled. */
052    private CmsModuleImportData m_moduleData;
053
054    /**
055     * Creates a new instance.<p>
056     *
057     * @param moduleData the module data object to be filled
058     */
059    public CmsImportResourceDataReader(CmsModuleImportData moduleData) {
060
061        super();
062        m_moduleData = moduleData;
063    }
064
065    /**
066     * @see org.opencms.importexport.CmsImportVersion10#importAccessControlEntries()
067     */
068    @Override
069    public void importAccessControlEntries() {
070        // do nothing, ACLS handled by module updater
071    }
072
073    /**
074     * @see org.opencms.importexport.CmsImportVersion10#importData(org.opencms.file.CmsObject, org.opencms.report.I_CmsReport, org.opencms.importexport.CmsImportParameters)
075     */
076    @Override
077    public void importData(CmsObject cms, I_CmsReport report, CmsImportParameters parameters) {
078
079        try {
080            // iniitializes the import helper, but we aren't interested in the method return value
081            matches(parameters);
082        } catch (Exception e) {
083            throw new RuntimeException(e);
084        }
085        super.importData(cms, report, parameters);
086    }
087
088    /**
089     * @see org.opencms.importexport.CmsImportVersion10#importRelations()
090     */
091    @Override
092    public void importRelations() {
093        // do nothing, relations handled by module updater
094
095    }
096
097    /**
098     * @see org.opencms.importexport.CmsImportVersion10#importResource()
099     */
100    @Override
101    public void importResource() {
102
103        try {
104            if (m_throwable != null) {
105                getReport().println(m_throwable);
106                getReport().addError(m_throwable);
107
108                CmsMessageContainer message = Messages.get().container(
109                    Messages.ERR_IMPORTEXPORT_ERROR_IMPORTING_RESOURCES_0);
110                if (LOG.isDebugEnabled()) {
111                    LOG.debug(message.key(), m_throwable);
112                }
113                m_throwable = null;
114                m_importACEs = false;
115                m_resource = null;
116                return;
117            }
118            // apply name translation and import path
119            String translatedName = getRequestContext().addSiteRoot(m_parameters.getDestinationPath() + m_destination);
120            boolean resourceImmutable = checkImmutable(translatedName);
121            translatedName = getRequestContext().removeSiteRoot(translatedName);
122            if (!resourceImmutable) {
123                byte[] content = null;
124                if (m_source != null) {
125                    content = m_helper.getFileBytes(m_source);
126                }
127                int size = 0;
128                if (content != null) {
129                    size = content.length;
130                }
131                setDefaultsForEmptyResourceFields();
132                // create a new CmsResource
133                CmsResource resource = createResourceObjectFromFields(translatedName, size);
134
135                CmsResourceImportData resData = new CmsResourceImportData(
136                    resource,
137                    translatedName,
138                    content,
139                    Lists.newArrayList(m_properties.values()),
140                    m_aces,
141                    m_relationsForResource,
142                    m_hasStructureId,
143                    m_hasDateLastModified);
144                m_moduleData.addResource(resData);
145            }
146        } catch (Exception e) {
147            LOG.error(e.getLocalizedMessage(), e);
148            m_report.println(e);
149        }
150
151    }
152
153    /**
154     * @see org.opencms.importexport.CmsImportVersion10#rewriteParseables()
155     */
156    @Override
157    public void rewriteParseables() {
158        // do nothing , parseables handled by module updater
159    }
160
161}