001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.upgrade.v6_0_12_to_6_1_0;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
019    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
020    import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
021    import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
022    import com.liferay.portal.kernel.util.StringUtil;
023    
024    import java.sql.Types;
025    
026    import java.util.ArrayList;
027    import java.util.List;
028    
029    /**
030     * @author Miguel Pastor
031     */
032    public class UpgradeQuartz extends UpgradeProcess {
033    
034            @Override
035            protected void doUpgrade() throws Exception {
036                    DB db = DBFactoryUtil.getDB();
037    
038                    String dbType = db.getType();
039    
040                    if (!dbType.equals(DB.TYPE_POSTGRESQL)) {
041                            return;
042                    }
043    
044                    Class<?> clazz = getClass();
045    
046                    ClassLoader classLoader = clazz.getClassLoader();
047    
048                    String quartzTableSQL = StringUtil.read(
049                            classLoader,
050                            "com/liferay/portal/upgrade/v6_0_12_to_6_1_0/dependencies/" +
051                                    "quartz-tables.sql");
052    
053                    String[] createTableSQLs = StringUtil.split(quartzTableSQL, ";");
054    
055                    for (String createTableSQL : createTableSQLs) {
056                            createTableSQL = createTableSQL.trim();
057    
058                            int x = createTableSQL.indexOf("(");
059    
060                            String tableName = createTableSQL.substring(13, x - 1);
061    
062                            String[] columnSQLs = StringUtil.split(
063                                    createTableSQL.substring(x + 1, createTableSQL.length() - 1));
064    
065                            List<Object[]> columns = new ArrayList<Object[]>();
066    
067                            for (String columnSQL : columnSQLs) {
068                                    if (columnSQL.contains("primary key")) {
069                                            break;
070                                    }
071    
072                                    columnSQL = StringUtil.replace(columnSQL, "\n", "");
073                                    columnSQL = columnSQL.trim();
074    
075                                    x = columnSQL.indexOf(" ");
076    
077                                    int y = columnSQL.indexOf(" ", x + 1);
078    
079                                    if (y == -1) {
080                                            y = columnSQL.length();
081                                    }
082    
083                                    String columnName = columnSQL.substring(0, x);
084                                    String columnType = columnSQL.substring(x + 1, y);
085    
086                                    Object[] column = {columnName, getTypes(columnType)};
087    
088                                    columns.add(column);
089                            }
090    
091                            UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
092                                    tableName, columns.toArray(new Object[columns.size()][]));
093    
094                            upgradeTable.setCreateSQL(createTableSQL);
095    
096                            upgradeTable.updateTable();
097                    }
098            }
099    
100            protected int getTypes(String columnType) {
101                    if (columnType.equals("BOOLEAN")) {
102                            return Types.BOOLEAN;
103                    }
104                    else if (columnType.equals("INTEGER")) {
105                            return Types.INTEGER;
106                    }
107                    else if (columnType.equals("LONG")) {
108                            return Types.BIGINT;
109                    }
110                    else if (columnType.equals("SBLOB")) {
111                            return Types.BLOB;
112                    }
113                    else if (columnType.startsWith("VARCHAR")) {
114                            return Types.VARCHAR;
115                    }
116                    else {
117                            throw new RuntimeException("Invalid column type " + columnType);
118                    }
119            }
120    
121    }