PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/portal-impl/src/com/liferay/portal/upgrade/v6_1_0/UpgradeDocumentLibrary.java

https://github.com/viktorkovacs/liferay-portal-trunk
Java | 259 lines | 183 code | 57 blank | 19 comment | 6 complexity | e3e006c4012a005783e8b6c19c769c68 MD5 | raw file
  1. /**
  2. * Copyright (c) 2000-2011 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. package com.liferay.portal.upgrade.v6_1_0;
  15. import com.liferay.portal.kernel.dao.jdbc.DataAccess;
  16. import com.liferay.portal.kernel.upgrade.UpgradeProcess;
  17. import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
  18. import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
  19. import com.liferay.portal.kernel.util.MimeTypesUtil;
  20. import com.liferay.portal.upgrade.v6_1_0.util.DLFileVersionTable;
  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. /**
  25. * @author Brian Wing Shun Chan
  26. * @author Douglas Wong
  27. * @author Alexander Chow
  28. * @author Minhchau Dang
  29. */
  30. public class UpgradeDocumentLibrary extends UpgradeProcess {
  31. protected void doUpgrade() throws Exception {
  32. updateFileEntries();
  33. updateFileRanks();
  34. updateFileShortcuts();
  35. updateFileVersions();
  36. }
  37. protected long getFileEntryId(long groupId, long folderId, String name)
  38. throws Exception {
  39. Connection con = null;
  40. PreparedStatement ps = null;
  41. ResultSet rs = null;
  42. try {
  43. con = DataAccess.getConnection();
  44. ps = con.prepareStatement(
  45. "select fileEntryId from DLFileEntry where groupId = ? and " +
  46. "folderId = ? and name = ?");
  47. ps.setLong(1, groupId);
  48. ps.setLong(2, folderId);
  49. ps.setString(3, name);
  50. rs = ps.executeQuery();
  51. if (rs.next()) {
  52. return rs.getLong("fileEntryId");
  53. }
  54. return 0;
  55. }
  56. finally {
  57. DataAccess.cleanUp(con, ps, rs);
  58. }
  59. }
  60. protected long getGroupId(long folderId) throws Exception {
  61. Connection con = null;
  62. PreparedStatement ps = null;
  63. ResultSet rs = null;
  64. long groupId = 0;
  65. try {
  66. con = DataAccess.getConnection();
  67. ps = con.prepareStatement(
  68. "select groupId from DLFolder where folderId = ?");
  69. ps.setLong(1, folderId);
  70. rs = ps.executeQuery();
  71. if (rs.next()) {
  72. groupId = rs.getLong("groupId");
  73. }
  74. }
  75. finally {
  76. DataAccess.cleanUp(con, ps, rs);
  77. }
  78. return groupId;
  79. }
  80. protected void updateFileEntries() throws Exception {
  81. Connection con = null;
  82. PreparedStatement ps = null;
  83. ResultSet rs = null;
  84. try {
  85. con = DataAccess.getConnection();
  86. ps = con.prepareStatement(
  87. "select fileEntryId, extension from DLFileEntry");
  88. rs = ps.executeQuery();
  89. while (rs.next()) {
  90. long fileEntryId = rs.getLong("fileEntryId");
  91. String extension = rs.getString("extension");
  92. String mimeType = MimeTypesUtil.getContentType(
  93. "A." + extension);
  94. runSQL(
  95. "update DLFileEntry set mimeType = '" + mimeType +
  96. "' where fileEntryId = " + fileEntryId);
  97. }
  98. }
  99. finally {
  100. DataAccess.cleanUp(con, ps, rs);
  101. }
  102. }
  103. protected void updateFileRanks() throws Exception {
  104. Connection con = null;
  105. PreparedStatement ps = null;
  106. ResultSet rs = null;
  107. try {
  108. con = DataAccess.getConnection();
  109. ps = con.prepareStatement(
  110. "select groupId, fileRankId, folderId, name from DLFileRank");
  111. rs = ps.executeQuery();
  112. while (rs.next()) {
  113. long groupId = rs.getLong("groupId");
  114. long fileRankId = rs.getLong("fileRankId");
  115. long folderId = rs.getLong("folderId");
  116. String name = rs.getString("name");
  117. long fileEntryId = getFileEntryId(groupId, folderId, name);
  118. runSQL(
  119. "update DLFileRank set fileEntryId = " + fileEntryId +
  120. " where fileRankId = " + fileRankId);
  121. }
  122. }
  123. finally {
  124. DataAccess.cleanUp(con, ps, rs);
  125. }
  126. runSQL("alter table DLFileRank drop column folderId");
  127. runSQL("alter table DLFileRank drop column name");
  128. }
  129. protected void updateFileShortcuts() throws Exception {
  130. Connection con = null;
  131. PreparedStatement ps = null;
  132. ResultSet rs = null;
  133. try {
  134. con = DataAccess.getConnection();
  135. ps = con.prepareStatement(
  136. "select fileShortcutId, toFolderId, toName from " +
  137. "DLFileShortcut");
  138. rs = ps.executeQuery();
  139. while (rs.next()) {
  140. long fileShortcutId = rs.getLong("fileShortcutId");
  141. long toFolderId = rs.getLong("toFolderId");
  142. String toName = rs.getString("toName");
  143. long groupId = getGroupId(toFolderId);
  144. long toFileEntryId = getFileEntryId(
  145. groupId, toFolderId, toName);
  146. runSQL(
  147. "update DLFileShortcut set toFileEntryId = " +
  148. toFileEntryId + " where fileShortcutId = " +
  149. fileShortcutId);
  150. }
  151. }
  152. finally {
  153. DataAccess.cleanUp(con, ps, rs);
  154. }
  155. runSQL("alter table DLFileShortcut drop column toFolderId");
  156. runSQL("alter table DLFileShortcut drop column toName");
  157. }
  158. protected void updateFileVersions() throws Exception {
  159. Connection con = null;
  160. PreparedStatement ps = null;
  161. ResultSet rs = null;
  162. try {
  163. con = DataAccess.getConnection();
  164. ps = con.prepareStatement(
  165. "select groupId, fileVersionId, folderId, name, extension " +
  166. "from DLFileVersion");
  167. rs = ps.executeQuery();
  168. while (rs.next()) {
  169. long groupId = rs.getLong("groupId");
  170. long fileVersionId = rs.getLong("fileVersionId");
  171. long folderId = rs.getLong("folderId");
  172. String name = rs.getString("name");
  173. String extension = rs.getString("extension");
  174. String mimeType = MimeTypesUtil.getContentType(
  175. "A." + extension);
  176. long fileEntryId = getFileEntryId(groupId, folderId, name);
  177. runSQL(
  178. "update DLFileVersion set fileEntryId = " + fileEntryId +
  179. ", mimeType = '" + mimeType +
  180. "' where fileVersionId = " + fileVersionId);
  181. }
  182. }
  183. finally {
  184. DataAccess.cleanUp(con, ps, rs);
  185. }
  186. try {
  187. runSQL("alter_column_type DLFileVersion extraSettings TEXT null");
  188. runSQL("alter_column_type DLFileVersion title VARCHAR(255) null");
  189. runSQL("alter table DLFileVersion drop column folderId");
  190. runSQL("alter table DLFileVersion drop column name");
  191. }
  192. catch (Exception e) {
  193. UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
  194. DLFileVersionTable.TABLE_NAME,
  195. DLFileVersionTable.TABLE_COLUMNS);
  196. upgradeTable.setCreateSQL(DLFileVersionTable.TABLE_SQL_CREATE);
  197. upgradeTable.setIndexesSQL(
  198. DLFileVersionTable.TABLE_SQL_ADD_INDEXES);
  199. upgradeTable.updateTable();
  200. }
  201. }
  202. }