001    /**
002     * Copyright (c) 2000-2013 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 String getTempFileName() {
046                    return _tempFileName;
047            }
048    
049            public boolean isAllowUniqueIndexes() throws Exception {
050                    return _allowUniqueIndexes;
051            }
052    
053            public boolean isDeleteTempFile() {
054                    return _deleteTempFile;
055            }
056    
057            public void setAllowUniqueIndexes(boolean allowUniqueIndexes)
058                    throws Exception {
059    
060                    _allowUniqueIndexes = allowUniqueIndexes;
061            }
062    
063            @Override
064            public void setCreateSQL(String createSQL) throws Exception {
065                    if (_calledUpdateTable) {
066                            throw new UpgradeException(
067                                    "setCreateSQL is called after updateTable");
068                    }
069    
070                    super.setCreateSQL(createSQL);
071            }
072    
073            public void setDeleteTempFile(boolean deleteTempFile) {
074                    _deleteTempFile = deleteTempFile;
075            }
076    
077            public void setIndexesSQL(String[] indexesSQL) throws Exception {
078                    _indexesSQL = indexesSQL;
079            }
080    
081            public void updateTable() throws Exception {
082                    _calledUpdateTable = true;
083    
084                    _tempFileName = generateTempFile();
085    
086                    try {
087                            DB db = DBFactoryUtil.getDB();
088    
089                            if (Validator.isNotNull(_tempFileName)) {
090                                    String deleteSQL = getDeleteSQL();
091    
092                                    db.runSQL(deleteSQL);
093                            }
094    
095                            String createSQL = getCreateSQL();
096    
097                            if (Validator.isNotNull(createSQL)) {
098                                    db.runSQL("drop table " + getTableName());
099    
100                                    db.runSQL(createSQL);
101                            }
102    
103                            if (Validator.isNotNull(_tempFileName)) {
104                                    populateTable(_tempFileName);
105                            }
106    
107                            String[] indexesSQL = getIndexesSQL();
108    
109                            boolean dropIndexes = false;
110    
111                            for (String indexSQL : indexesSQL) {
112                                    if (!isAllowUniqueIndexes()) {
113                                            if (indexSQL.contains("create unique index")) {
114                                                    indexSQL = StringUtil.replace(
115                                                            indexSQL, "create unique index ", "create index ");
116    
117                                                    dropIndexes = true;
118                                            }
119                                    }
120    
121                                    try {
122                                            db.runSQL(indexSQL);
123                                    }
124                                    catch (Exception e) {
125                                            if (_log.isWarnEnabled()) {
126                                                    _log.warn(e.getMessage() + ": " + indexSQL);
127                                            }
128                                    }
129                            }
130    
131                            if (dropIndexes) {
132                                    StartupHelperUtil.setDropIndexes(true);
133                            }
134                    }
135                    finally {
136                            if (Validator.isNotNull(_tempFileName) && _deleteTempFile) {
137                                    FileUtil.delete(_tempFileName);
138                            }
139                    }
140            }
141    
142            private static Log _log = LogFactoryUtil.getLog(BaseUpgradeTableImpl.class);
143    
144            private boolean _allowUniqueIndexes;
145            private boolean _calledUpdateTable;
146            private boolean _deleteTempFile;
147            private String[] _indexesSQL = new String[0];
148            private String _tempFileName;
149    
150    }