001    /**
002     * Copyright (c) 2000-2010 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.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.upgrade.UpgradeException;
022    import com.liferay.portal.kernel.util.FileUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    
025    /**
026     * @author Alexander Chow
027     * @author Brian Wing Shun Chan
028     */
029    public abstract class BaseUpgradeTableImpl extends Table {
030    
031            public BaseUpgradeTableImpl(String tableName) {
032                    super(tableName);
033            }
034    
035            public BaseUpgradeTableImpl(String tableName, Object[][] columns) {
036                    super(tableName, columns);
037            }
038    
039            public void setCreateSQL(String createSQL) throws Exception {
040                    if (_calledUpdateTable) {
041                            throw new UpgradeException(
042                                    "setCreateSQL is called after updateTable");
043                    }
044    
045                    super.setCreateSQL(createSQL);
046            }
047    
048            public void updateTable() throws Exception {
049                    _calledUpdateTable = true;
050    
051                    String tempFileName = generateTempFile();
052    
053                    try {
054                            DB db = DBFactoryUtil.getDB();
055    
056                            String createSQL = getCreateSQL();
057    
058                            if (Validator.isNotNull(createSQL)) {
059                                    db.runSQL("drop table " + getTableName());
060    
061                                    db.runSQL(createSQL);
062                            }
063    
064                            if (Validator.isNotNull(tempFileName)) {
065                                    String deleteSQL = getDeleteSQL();
066    
067                                    db.runSQL(deleteSQL);
068    
069                                    populateTable(tempFileName);
070                            }
071                    }
072                    finally {
073                            if (Validator.isNotNull(tempFileName)) {
074                                    FileUtil.delete(tempFileName);
075                            }
076                    }
077            }
078    
079            static Log _log = LogFactoryUtil.getLog(BaseUpgradeTableImpl.class);
080    
081            private boolean _calledUpdateTable;
082    
083    }