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