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