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