1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.upgrade.util;
16  
17  import com.liferay.portal.kernel.dao.db.DB;
18  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.upgrade.UpgradeException;
22  import com.liferay.portal.kernel.util.FileUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  
25  /**
26   * <a href="BaseUpgradeTableImpl.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Alexander Chow
29   * @author Brian Wing Shun Chan
30   */
31  public abstract class BaseUpgradeTableImpl extends Table {
32  
33      public BaseUpgradeTableImpl(String tableName) {
34          super(tableName);
35      }
36  
37      public BaseUpgradeTableImpl(String tableName, Object[][] columns) {
38          super(tableName, columns);
39      }
40  
41      public void setCreateSQL(String createSQL) throws Exception {
42          if (_calledUpdateTable) {
43              throw new UpgradeException(
44                  "setCreateSQL is called after updateTable");
45          }
46  
47          super.setCreateSQL(createSQL);
48      }
49  
50      public void updateTable() throws Exception {
51          _calledUpdateTable = true;
52  
53          String tempFileName = generateTempFile();
54  
55          try {
56              DB db = DBFactoryUtil.getDB();
57  
58              String createSQL = getCreateSQL();
59  
60              if (Validator.isNotNull(createSQL)) {
61                  db.runSQL("drop table " + getTableName());
62  
63                  db.runSQL(createSQL);
64              }
65  
66              if (Validator.isNotNull(tempFileName)) {
67                  String deleteSQL = getDeleteSQL();
68  
69                  db.runSQL(deleteSQL);
70  
71                  populateTable(tempFileName);
72              }
73          }
74          finally {
75              if (Validator.isNotNull(tempFileName)) {
76                  FileUtil.delete(tempFileName);
77              }
78          }
79      }
80  
81      static Log _log = LogFactoryUtil.getLog(BaseUpgradeTableImpl.class);
82  
83      private boolean _calledUpdateTable;
84  
85  }