001
014
015 package com.liferay.portal.upgrade.v6_0_3;
016
017 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018 import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019 import com.liferay.portal.kernel.util.FileUtil;
020 import com.liferay.portal.kernel.util.PortalUtil;
021
022 import java.sql.Connection;
023 import java.sql.PreparedStatement;
024 import java.sql.ResultSet;
025
026 import java.util.ArrayList;
027 import java.util.List;
028
029
032 public class UpgradeDocumentLibrary extends UpgradeProcess {
033
034 @Override
035 protected void doUpgrade() throws Exception {
036 updateFileEntries();
037 updateFileVersions();
038 }
039
040 protected List<Long> getFileVersionIds(long folderId, String name)
041 throws Exception {
042
043 Connection con = null;
044 PreparedStatement ps = null;
045 ResultSet rs = null;
046
047 try {
048 con = DataAccess.getUpgradeOptimizedConnection();
049
050 ps = con.prepareStatement(
051 "select fileVersionId from DLFileVersion where folderId = ? " +
052 "and name = ? order by version desc");
053
054 ps.setLong(1, folderId);
055 ps.setString(2, name);
056
057 rs = ps.executeQuery();
058
059 List<Long> fileVersionIds = new ArrayList<>();
060
061 while (rs.next()) {
062 long fileVersionId = rs.getLong("fileVersionId");
063
064 fileVersionIds.add(fileVersionId);
065 }
066
067 return fileVersionIds;
068 }
069 finally {
070 DataAccess.cleanUp(con, ps, rs);
071 }
072 }
073
074 protected void updateFileEntries() throws Exception {
075 Connection con = null;
076 PreparedStatement ps = null;
077 ResultSet rs = null;
078
079 List<Long> tableIds = new ArrayList<>();
080
081 try {
082 long classNameId = PortalUtil.getClassNameId(
083 "com.liferay.portlet.documentlibrary.model.DLFileEntry");
084
085 con = DataAccess.getUpgradeOptimizedConnection();
086
087 ps = con.prepareStatement(
088 "select tableId from ExpandoTable where classNameId = " +
089 classNameId);
090
091 rs = ps.executeQuery();
092
093 while (rs.next()) {
094 long tableId = rs.getLong("tableId");
095
096 tableIds.add(tableId);
097 }
098 }
099 finally {
100 DataAccess.cleanUp(con, ps, rs);
101 }
102
103 try {
104 con = DataAccess.getUpgradeOptimizedConnection();
105
106 ps = con.prepareStatement(
107 "select uuid_, fileEntryId, groupId, folderId, name, title " +
108 "from DLFileEntry");
109
110 rs = ps.executeQuery();
111
112 while (rs.next()) {
113 String uuid_ = rs.getString("uuid_");
114 long fileEntryId = rs.getLong("fileEntryId");
115 long groupId = rs.getLong("groupId");
116 long folderId = rs.getLong("folderId");
117 String name = rs.getString("name");
118 String title = rs.getString("title");
119
120 String extension = FileUtil.getExtension(title);
121
122 runSQL(
123 "update DLFileEntry set extension = '" + extension +
124 "' where uuid_ = '" + uuid_ + "' and groupId = " +
125 groupId);
126
127 long latestFileVersionId = 0;
128
129 List<Long> fileVersionIds = getFileVersionIds(folderId, name);
130
131 if (!fileVersionIds.isEmpty()) {
132 latestFileVersionId = fileVersionIds.get(0);
133 }
134
135 for (long tableId : tableIds) {
136 runSQL(
137 "update ExpandoRow set classPK = " +
138 latestFileVersionId + " where tableId = " +
139 tableId + " and classPK = " + fileEntryId);
140
141 runSQL(
142 "update ExpandoValue set classPK = " +
143 latestFileVersionId + " where tableId = " +
144 tableId + " and classPK = " + fileEntryId);
145 }
146 }
147 }
148 finally {
149 DataAccess.cleanUp(con, ps, rs);
150 }
151 }
152
153 protected void updateFileVersion(
154 long fileVersionId, String extension, String title,
155 String description, String extraSettings)
156 throws Exception {
157
158 Connection con = null;
159 PreparedStatement ps = null;
160
161 try {
162 con = DataAccess.getUpgradeOptimizedConnection();
163
164 ps = con.prepareStatement(
165 "update DLFileVersion set extension = ?, title = ?, " +
166 "description = ?, extraSettings = ? where fileVersionId " +
167 "= ?");
168
169 ps.setString(1, extension);
170 ps.setString(2, title);
171 ps.setString(3, description);
172 ps.setString(4, extraSettings);
173 ps.setLong(5, fileVersionId);
174
175 ps.executeUpdate();
176 }
177 finally {
178 DataAccess.cleanUp(con, ps);
179 }
180 }
181
182 protected void updateFileVersions() throws Exception {
183 Connection con = null;
184 PreparedStatement ps = null;
185 ResultSet rs = null;
186
187 try {
188 con = DataAccess.getUpgradeOptimizedConnection();
189
190 ps = con.prepareStatement(
191 "select folderId, name, extension, title, description, " +
192 "extraSettings from DLFileEntry");
193
194 rs = ps.executeQuery();
195
196 while (rs.next()) {
197 long folderId = rs.getLong("folderId");
198 String name = rs.getString("name");
199 String extension = rs.getString("extension");
200 String title = rs.getString("title");
201 String description = rs.getString("description");
202 String extraSettings = rs.getString("extraSettings");
203
204 List<Long> fileVersionIds = getFileVersionIds(folderId, name);
205
206 for (long fileVersionId : fileVersionIds) {
207 updateFileVersion(
208 fileVersionId, extension, title, description,
209 extraSettings);
210 }
211 }
212 }
213 finally {
214 DataAccess.cleanUp(con, ps, rs);
215 }
216 }
217
218 }