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.i18n.CmsMessageContainer;
031import org.opencms.main.CmsLog;
032import org.opencms.main.OpenCms;
033import org.opencms.util.CmsDataTypeUtil;
034
035import java.util.ArrayList;
036import java.util.HashMap;
037import java.util.Iterator;
038import java.util.List;
039import java.util.Map;
040
041import org.apache.commons.codec.binary.Base64;
042import org.apache.commons.logging.Log;
043
044import org.dom4j.Element;
045import org.dom4j.Node;
046
047/**
048 * Implementation of the OpenCms Import Interface ({@link org.opencms.importexport.I_CmsImport}) for
049 * the import version 6.<p>
050 *
051 * This import format is used in OpenCms since 6.5.6.<p>
052 *
053 * @since 6.5.6
054 *
055 * @see org.opencms.importexport.A_CmsImport
056 *
057 * @deprecated this import class is no longer in use and should only be used to import old export files
058 */
059@Deprecated
060public class CmsImportVersion6 extends CmsImportVersion5 {
061
062    /** The version number of this import implementation.<p> */
063    public static final int IMPORT_VERSION6 = 6;
064
065    /** The log object for this class. */
066    private static final Log LOG = CmsLog.getLog(CmsImportVersion6.class);
067
068    /**
069     * Creates a new CmsImportVerion7 object.<p>
070     */
071    public CmsImportVersion6() {
072
073        m_convertToXmlPage = true;
074    }
075
076    /**
077     * @see org.opencms.importexport.I_CmsImport#getVersion()
078     */
079    @Override
080    public int getVersion() {
081
082        return CmsImportVersion6.IMPORT_VERSION6;
083    }
084
085    /**
086     * Imports the OpenCms users.<p>
087     *
088     * @throws CmsImportExportException if something goes wrong
089     */
090    @Override
091    protected void importUsers() throws CmsImportExportException {
092
093        try {
094            // getAll user nodes
095            List<Node> userNodes = m_docXml.selectNodes("//" + A_CmsImport.N_USERDATA);
096            // walk threw all groups in manifest
097            for (int i = 0; i < userNodes.size(); i++) {
098                Element currentElement = (Element)userNodes.get(i);
099
100                String name = getChildElementTextValue(currentElement, A_CmsImport.N_NAME);
101                name = OpenCms.getImportExportManager().translateUser(name);
102
103                // decode passwords using base 64 decoder
104                String pwd = getChildElementTextValue(currentElement, A_CmsImport.N_PASSWORD);
105                String password = new String(Base64.decodeBase64(pwd.trim().getBytes()));
106
107                String flags = getChildElementTextValue(currentElement, A_CmsImport.N_FLAGS);
108                String firstname = getChildElementTextValue(currentElement, A_CmsImport.N_FIRSTNAME);
109                String lastname = getChildElementTextValue(currentElement, A_CmsImport.N_LASTNAME);
110                String email = getChildElementTextValue(currentElement, A_CmsImport.N_EMAIL);
111                long dateCreated = Long.parseLong(getChildElementTextValue(currentElement, A_CmsImport.N_DATECREATED));
112
113                // get the userinfo and put it into the additional info map
114                Map<String, Object> userInfo = new HashMap<String, Object>();
115                Iterator<Node> itInfoNodes = currentElement.selectNodes(
116                    "./" + A_CmsImport.N_USERINFO + "/" + A_CmsImport.N_USERINFO_ENTRY).iterator();
117                while (itInfoNodes.hasNext()) {
118                    Element infoEntryNode = (Element)itInfoNodes.next();
119                    String key = infoEntryNode.attributeValue(A_CmsImport.A_NAME);
120                    String type = infoEntryNode.attributeValue(A_CmsImport.A_TYPE);
121                    String value = infoEntryNode.getTextTrim();
122                    userInfo.put(key, CmsDataTypeUtil.dataImport(value, type));
123                }
124
125                // get the groups of the user and put them into the list
126                List<Node> groupNodes = currentElement.selectNodes("*/" + A_CmsImport.N_GROUPNAME);
127                List<String> userGroups = new ArrayList<String>();
128                for (int j = 0; j < groupNodes.size(); j++) {
129                    Element currentGroup = (Element)groupNodes.get(j);
130                    String userInGroup = getChildElementTextValue(currentGroup, A_CmsImport.N_NAME);
131                    userInGroup = OpenCms.getImportExportManager().translateGroup(userInGroup);
132                    userGroups.add(userInGroup);
133                }
134
135                // import this user
136                importUser(name, flags, password, firstname, lastname, email, dateCreated, userInfo, userGroups);
137            }
138        } catch (CmsImportExportException e) {
139            throw e;
140        } catch (Exception e) {
141            m_report.println(e);
142            CmsMessageContainer message = Messages.get().container(Messages.ERR_IMPORTEXPORT_ERROR_IMPORTING_USERS_0);
143            if (LOG.isDebugEnabled()) {
144                LOG.debug(message.key(), e);
145            }
146            throw new CmsImportExportException(message, e);
147        }
148    }
149}