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.util.PortalUtil;
021 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
022
023 import java.sql.Connection;
024 import java.sql.PreparedStatement;
025 import java.sql.ResultSet;
026
027 import java.util.ArrayList;
028 import java.util.List;
029
030
033 public class UpgradeDocumentLibrary extends UpgradeProcess {
034
035 @Override
036 protected void doUpgrade() throws Exception {
037 updateFileEntries();
038 updateFileVersions();
039 }
040
041 protected List<Long> getFileVersionIds(long folderId, String name)
042 throws Exception {
043
044 Connection con = null;
045 PreparedStatement ps = null;
046 ResultSet rs = null;
047
048 try {
049 con = DataAccess.getUpgradeOptimizedConnection();
050
051 ps = con.prepareStatement(
052 "select fileVersionId from DLFileVersion where folderId = ? " +
053 "and name = ? order by version desc");
054
055 ps.setLong(1, folderId);
056 ps.setString(2, name);
057
058 rs = ps.executeQuery();
059
060 List<Long> fileVersionIds = new ArrayList<Long>();
061
062 while (rs.next()) {
063 long fileVersionId = rs.getLong("fileVersionId");
064
065 fileVersionIds.add(fileVersionId);
066 }
067
068 return fileVersionIds;
069 }
070 finally {
071 DataAccess.cleanUp(con, ps, rs);
072 }
073 }
074
075 protected void updateFileEntries() throws Exception {
076 Connection con = null;
077 PreparedStatement ps = null;
078 ResultSet rs = null;
079
080 List<Long> tableIds = new ArrayList<Long>();
081
082 try {
083 long classNameId = PortalUtil.getClassNameId(DLFileEntry.class);
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, String
155 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 }