001    /**
002     * Copyright (c) 2000-present 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.util;
016    
017    import com.liferay.portal.events.StartupHelperUtil;
018    import com.liferay.portal.kernel.dao.db.DB;
019    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.upgrade.UpgradeException;
023    import com.liferay.portal.kernel.util.FileUtil;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    
027    /**
028     * @author Alexander Chow
029     * @author Brian Wing Shun Chan
030     */
031    public abstract class BaseUpgradeTableImpl extends Table {
032    
033            public BaseUpgradeTableImpl(String tableName) {
034                    super(tableName);
035            }
036    
037            public BaseUpgradeTableImpl(String tableName, Object[][] columns) {
038                    super(tableName, columns);
039            }
040    
041            public String[] getIndexesSQL() throws Exception {
042                    return _indexesSQL;
043            }
044    
045            public boolean isAllowUniqueIndexes() throws Exception {
046                    return _allowUniqueIndexes;
047            }
048    
049            public boolean isDeleteTempFile() {
050                    return _deleteTempFile;
051            }
052    
053            public void setAllowUniqueIndexes(boolean allowUniqueIndexes)
054                    throws Exception {
055    
056                    _allowUniqueIndexes = allowUniqueIndexes;
057            }
058    
059            @Override
060            public void setCreateSQL(String createSQL) throws Exception {
061                    if (_calledUpdateTable) {
062                            throw new UpgradeException(
063                                    "setCreateSQL is called after updateTable");
064                    }
065    
066                    super.setCreateSQL(createSQL);
067            }
068    
069            public void setDeleteTempFile(boolean deleteTempFile) {
070                    _deleteTempFile = deleteTempFile;
071            }
072    
073            public void setIndexesSQL(String[] indexesSQL) throws Exception {
074                    _indexesSQL = indexesSQL;
075            }
076    
077            public void updateTable() throws Exception {
078                    _calledUpdateTable = true;
079    
080                    generateTempFile();
081    
082                    String tempFileName = getTempFileName();
083    
084                    try {
085                            DB db = DBFactoryUtil.getDB();
086    
087                            if (Validator.isNotNull(tempFileName)) {
088                                    String deleteSQL = getDeleteSQL();
089    
090                                    db.runSQL(deleteSQL);
091                            }
092    
093                            String createSQL = getCreateSQL();
094    
095                            if (Validator.isNotNull(createSQL)) {
096                                    db.runSQL("drop table " + getTableName());
097    
098                                    db.runSQL(createSQL);
099                            }
100    
101                            populateTable();
102    
103                            String[] indexesSQL = getIndexesSQL();
104    
105                            boolean dropIndexes = false;
106    
107                            for (String indexSQL : indexesSQL) {
108                                    if (!isAllowUniqueIndexes()) {
109                                            if (indexSQL.contains("create unique index")) {
110                                                    indexSQL = StringUtil.replace(
111                                                            indexSQL, "create unique index ", "create index ");
112    
113                                                    dropIndexes = true;
114                                            }
115                                    }
116    
117                                    try {
118                                            db.runSQL(indexSQL);
119                                    }
120                                    catch (Exception e) {
121                                            if (_log.isWarnEnabled()) {
122                                                    _log.warn(e.getMessage() + ": " + indexSQL);
123                                            }
124                                    }
125                            }
126    
127                            if (dropIndexes) {
128                                    StartupHelperUtil.setDropIndexes(true);
129                            }
130                    }
131                    finally {
132                            if (Validator.isNotNull(tempFileName) && _deleteTempFile) {
133                                    FileUtil.delete(tempFileName);
134                            }
135                    }
136            }
137    
138            private static final Log _log = LogFactoryUtil.getLog(
139                    BaseUpgradeTableImpl.class);
140    
141            private boolean _allowUniqueIndexes;
142            private boolean _calledUpdateTable;
143            private boolean _deleteTempFile;
144            private String[] _indexesSQL = new String[0];
145    
146    }