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.setup.db.update6to7.oracle;
029
030import org.opencms.db.oracle.CmsUserDriver;
031import org.opencms.setup.CmsSetupDBWrapper;
032import org.opencms.setup.CmsSetupDb;
033import org.opencms.util.CmsDataTypeUtil;
034
035import java.io.IOException;
036import java.io.OutputStream;
037import java.sql.SQLException;
038import java.util.ArrayList;
039import java.util.HashMap;
040import java.util.Iterator;
041import java.util.List;
042import java.util.Map;
043
044/**
045 * Oracle implementation of the generic update class for the Users.<p>
046 *
047 * @since 7.0.0
048 */
049public class CmsUpdateDBCmsUsers extends org.opencms.setup.db.update6to7.CmsUpdateDBCmsUsers {
050
051    /** Constant for the query to insert the new user data into the new table CMS_USERDATA.<p> */
052    private static final String QUERY_ORACLE_USERDATA_UPDATE = "Q_ORACLE_USERDATA_UPDATE";
053
054    /** Constant for the SQL query properties.<p> */
055    private static final String QUERY_PROPERTY_FILE = "cms_users_queries.properties";
056
057    /** Constant for the replacement in the sql query. */
058    private static final String REPLACEMENT_TABLEINDEX_SPACE = "${indexTablespace}";
059
060    /**
061     * Constructor.<p>
062     *
063     * @throws IOException if the sql queries properties file could not be read
064     */
065    public CmsUpdateDBCmsUsers()
066    throws IOException {
067
068        super();
069        loadQueryProperties(getPropertyFileLocation() + QUERY_PROPERTY_FILE);
070    }
071
072    /**
073     * @see org.opencms.setup.db.update6to7.CmsUpdateDBCmsUsers#createUserDataTable(org.opencms.setup.CmsSetupDb)
074     */
075    @Override
076    protected void createUserDataTable(CmsSetupDb dbCon) throws SQLException {
077
078        String indexTablespace = m_poolData.get("indexTablespace");
079
080        Map<String, String> replacer = new HashMap<String, String>();
081        replacer.put(REPLACEMENT_TABLEINDEX_SPACE, indexTablespace);
082
083        String createStatement = readQuery(QUERY_CREATE_TABLE_USERDATA);
084        dbCon.updateSqlStatement(createStatement, replacer, null);
085
086        // create indices
087        List<String> indexElements = new ArrayList<String>();
088        indexElements.add("CMS_USERDATA_01_IDX_INDEX");
089        indexElements.add("CMS_USERDATA_02_IDX_INDEX");
090
091        Iterator<String> iter = indexElements.iterator();
092        while (iter.hasNext()) {
093            String stmt = readQuery(iter.next());
094            try {
095                // Create the index
096                dbCon.updateSqlStatement(stmt, replacer, null);
097            } catch (SQLException e) {
098                e.printStackTrace();
099            }
100        }
101    }
102
103    /**
104     * @see org.opencms.setup.db.update6to7.CmsUpdateDBCmsUsers#writeUserInfo(org.opencms.setup.CmsSetupDb, java.lang.String, java.lang.String, java.lang.Object)
105     */
106    @Override
107    protected void writeUserInfo(CmsSetupDb dbCon, String id, String key, Object value) {
108
109        String query = readQuery(QUERY_INSERT_CMS_USERDATA);
110
111        try {
112            // Generate the list of parameters to add into the user info table
113            List<Object> params = new ArrayList<Object>();
114            params.add(id);
115            params.add(key);
116            params.add(value.getClass().getName());
117
118            dbCon.updateSqlStatement(query, null, params);
119        } catch (SQLException e) {
120            e.printStackTrace();
121        }
122
123        // update user_info in this special way because of using blob
124
125        boolean wasInTransaction = false;
126
127        CmsSetupDBWrapper db = null;
128
129        try {
130
131            wasInTransaction = !dbCon.getConnection().getAutoCommit();
132            if (!wasInTransaction) {
133                dbCon.getConnection().setAutoCommit(false);
134            }
135
136            String stmt = readQuery(QUERY_ORACLE_USERDATA_UPDATE);
137
138            // Generate the list of parameters to add into the user info table
139            List<Object> params = new ArrayList<Object>();
140            params.add(id);
141            params.add(key);
142
143            db = dbCon.executeSqlStatement(stmt, null, params);
144            if (db.getResultSet().next()) {
145
146                // write serialized user info
147                OutputStream output = CmsUserDriver.getOutputStreamFromBlob(db.getResultSet(), "DATA_VALUE");
148                output.write(CmsDataTypeUtil.dataSerialize(value));
149                output.close();
150
151            } else {
152                System.out.println("Could not insert blob");
153            }
154
155            if (!wasInTransaction) {
156                CmsSetupDBWrapper db2 = null;
157                try {
158                    String commit = readQuery("Q_COMMIT");
159                    db2 = dbCon.executeSqlStatement(commit, null);
160                } finally {
161                    if (db2 != null) {
162                        db2.close();
163                    }
164                }
165            }
166
167        } catch (Exception e) {
168            e.printStackTrace();
169        } finally {
170
171            // close result set
172            if (db != null) {
173                db.close();
174            }
175
176            // rollback
177            CmsSetupDBWrapper db2 = null;
178            try {
179                if (!wasInTransaction) {
180                    String rollback = readQuery("Q_ROLLBACK");
181                    db2 = dbCon.executeSqlStatement(rollback, null);
182                }
183            } catch (Exception e) {
184                e.printStackTrace();
185            } finally {
186                if (db2 != null) {
187                    db2.close();
188                }
189            }
190
191            // set auto commit back to original value
192            try {
193                if (!wasInTransaction) {
194                    dbCon.getConnection().setAutoCommit(true);
195                }
196            } catch (Exception e) {
197                e.printStackTrace();
198            }
199        }
200    }
201}