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.v6_0_0;
16  
17  import com.liferay.documentlibrary.service.DLServiceUtil;
18  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
19  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
20  import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
21  import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
22  import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
23  import com.liferay.portal.kernel.util.StringBundler;
24  import com.liferay.portal.kernel.workflow.StatusConstants;
25  import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryNameUpgradeColumnImpl;
26  import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryTable;
27  import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryTitleUpgradeColumnImpl;
28  import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryVersionUpgradeColumnImpl;
29  import com.liferay.portal.upgrade.v6_0_0.util.DLFileRankTable;
30  import com.liferay.portal.upgrade.v6_0_0.util.DLFileShortcutTable;
31  import com.liferay.portal.upgrade.v6_0_0.util.DLFileVersionTable;
32  import com.liferay.portal.util.PortletKeys;
33  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
34  
35  import java.sql.Connection;
36  import java.sql.PreparedStatement;
37  import java.sql.ResultSet;
38  import java.sql.Timestamp;
39  
40  /**
41   * <a href="UpgradeDocumentLibrary.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Jorge Ferrer
44   * @author Alexander Chow
45   */
46  public class UpgradeDocumentLibrary extends UpgradeProcess {
47  
48      protected void addFileVersion(
49              long groupId, long companyId, long userId, String userName,
50              long folderId, String name, double version, int size)
51          throws Exception {
52  
53          Timestamp now = new Timestamp(System.currentTimeMillis());
54  
55          Connection con = null;
56          PreparedStatement ps = null;
57  
58          try {
59              con = DataAccess.getConnection();
60  
61              StringBundler sb = new StringBundler(5);
62  
63              sb.append("insert into DLFileVersion (fileVersionId, groupId, ");
64              sb.append("companyId, userId, userName, createDate, folderId, ");
65              sb.append("name, version, size_, status, statusByUserId, ");
66              sb.append("statusByUserName, statusDate) values (?, ?, ?, ?, ?, ");
67              sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?)");
68  
69              String sql = sb.toString();
70  
71              ps = con.prepareStatement(sql);
72  
73              ps.setLong(1, increment());
74              ps.setLong(2, groupId);
75              ps.setLong(3, companyId);
76              ps.setLong(4, userId);
77              ps.setString(5, userName);
78              ps.setTimestamp(6, now);
79              ps.setLong(7, folderId);
80              ps.setString(8, name);
81              ps.setDouble(9, version);
82              ps.setInt(10, size);
83              ps.setInt(11, StatusConstants.APPROVED);
84              ps.setLong(12, userId);
85              ps.setString(13, userName);
86              ps.setTimestamp(14, now);
87  
88              ps.executeUpdate();
89          }
90          finally {
91              DataAccess.cleanUp(con, ps);
92          }
93      }
94  
95      protected void doUpgrade() throws Exception {
96          Connection con = null;
97          PreparedStatement ps = null;
98          ResultSet rs = null;
99  
100         try {
101             con = DataAccess.getConnection();
102 
103             ps = con.prepareStatement("select * from DLFileEntry");
104 
105             rs = ps.executeQuery();
106 
107             while (rs.next()) {
108                 long companyId = rs.getLong("companyId");
109                 long groupId = rs.getLong("groupId");
110                 long userId = rs.getLong("userId");
111                 String userName = rs.getString("userName");
112                 long folderId = rs.getLong("folderId");
113                 String name = rs.getString("name");
114                 double version = rs.getDouble("version");
115                 int size = rs.getInt("size_");
116 
117                 String portletId = PortletKeys.DOCUMENT_LIBRARY;
118                 long repositoryId = folderId;
119 
120                 if (repositoryId ==
121                         DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
122 
123                     repositoryId = groupId;
124                 }
125 
126                 String newName = DLFileEntryNameUpgradeColumnImpl.getNewName(
127                     name);
128 
129                 if (!newName.equals(name)) {
130                     DLServiceUtil.updateFile(
131                         companyId, portletId, groupId, repositoryId, name,
132                         newName, false);
133                 }
134 
135                 addFileVersion(
136                     groupId, companyId, userId, userName, folderId, name,
137                     version, size);
138             }
139         }
140         finally {
141             DataAccess.cleanUp(con, ps, rs);
142         }
143 
144         // DLFileEntry
145 
146         UpgradeColumn nameColumn = new DLFileEntryNameUpgradeColumnImpl("name");
147         UpgradeColumn titleColumn = new DLFileEntryTitleUpgradeColumnImpl(
148             nameColumn, "title");
149         UpgradeColumn versionColumn = new DLFileEntryVersionUpgradeColumnImpl(
150             "version");
151 
152         UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
153             DLFileEntryTable.TABLE_NAME, DLFileEntryTable.TABLE_COLUMNS,
154             nameColumn, titleColumn, versionColumn);
155 
156         upgradeTable.setCreateSQL(DLFileEntryTable.TABLE_SQL_CREATE);
157 
158         upgradeTable.updateTable();
159 
160         // DLFileRank
161 
162         upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
163             DLFileRankTable.TABLE_NAME, DLFileRankTable.TABLE_COLUMNS,
164             nameColumn);
165 
166         upgradeTable.setCreateSQL(DLFileRankTable.TABLE_SQL_CREATE);
167 
168         upgradeTable.updateTable();
169 
170         // DLFileShortcut
171 
172         UpgradeColumn toNameColumn = new DLFileEntryNameUpgradeColumnImpl(
173             "toName");
174 
175         upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
176             DLFileShortcutTable.TABLE_NAME, DLFileShortcutTable.TABLE_COLUMNS,
177             toNameColumn);
178 
179         upgradeTable.setCreateSQL(DLFileShortcutTable.TABLE_SQL_CREATE);
180 
181         upgradeTable.updateTable();
182 
183         // DLFileVersion
184 
185         upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
186             DLFileVersionTable.TABLE_NAME, DLFileVersionTable.TABLE_COLUMNS,
187             nameColumn, versionColumn);
188 
189         upgradeTable.setCreateSQL(DLFileVersionTable.TABLE_SQL_CREATE);
190 
191         upgradeTable.updateTable();
192     }
193 
194 }