1
14
15 package com.liferay.portal.upgrade.v5_2_0;
16
17 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
18 import com.liferay.portal.kernel.dao.jdbc.SmartResultSet;
19 import com.liferay.portal.kernel.upgrade.UpgradeProcess;
20 import com.liferay.portal.kernel.util.StringPool;
21 import com.liferay.portlet.asset.NoSuchTagException;
22
23 import java.sql.Connection;
24 import java.sql.PreparedStatement;
25 import java.sql.ResultSet;
26 import java.sql.Timestamp;
27
28 import java.util.HashMap;
29 import java.util.Map;
30
31
37 public class UpgradeTags extends UpgradeProcess {
38
39 protected void addEntry(
40 long entryId, long groupId, long companyId, long userId,
41 String userName, Timestamp createDate, Timestamp modifiedDate,
42 long parentEntryId, String name, long vocabularyId)
43 throws Exception {
44
45 Connection con = null;
46 PreparedStatement ps = null;
47
48 try {
49 con = DataAccess.getConnection();
50
51 ps = con.prepareStatement(
52 "insert into TagsEntry (entryId, groupId, companyId, userId, " +
53 "userName, createDate, modifiedDate, parentEntryId, " +
54 "name, vocabularyId) values (?, ?, ?, ?, ?, ?, ?, ?, " +
55 "?, ?)");
56
57 ps.setLong(1, entryId);
58 ps.setLong(2, groupId);
59 ps.setLong(3, companyId);
60 ps.setLong(4, userId);
61 ps.setString(5, userName);
62 ps.setTimestamp(6, createDate);
63 ps.setTimestamp(7, modifiedDate);
64 ps.setLong(8, parentEntryId);
65 ps.setString(9, name);
66 ps.setLong(10, vocabularyId);
67
68 ps.executeUpdate();
69 }
70 finally {
71 DataAccess.cleanUp(con, ps);
72 }
73 }
74
75 protected void addProperty(
76 long propertyId, long companyId, long userId, String userName,
77 Timestamp createDate, Timestamp modifiedDate, long entryId,
78 String key, String value)
79 throws Exception {
80
81 Connection con = null;
82 PreparedStatement ps = null;
83
84 try {
85 con = DataAccess.getConnection();
86
87 ps = con.prepareStatement(
88 "insert into TagsProperty (propertyId, companyId, userId, " +
89 "userName, createDate, modifiedDate, entryId, key_, " +
90 "value) values (?, ?, ?, ?, ?, ?, ?, ?, ?)");
91
92 ps.setLong(1, propertyId);
93 ps.setLong(2, companyId);
94 ps.setLong(3, userId);
95 ps.setString(4, userName);
96 ps.setTimestamp(5, createDate);
97 ps.setTimestamp(6, modifiedDate);
98 ps.setLong(7, entryId);
99 ps.setString(8, key);
100 ps.setString(9, value);
101
102 ps.executeUpdate();
103 }
104 finally {
105 DataAccess.cleanUp(con, ps);
106 }
107 }
108
109 protected long copyEntry(long groupId, long entryId) throws Exception {
110 String key = groupId + StringPool.UNDERLINE + entryId;
111
112 Long newEntryId = _entryIdsMap.get(key);
113
114 if (newEntryId != null) {
115 return newEntryId.longValue();
116 }
117
118 Connection con = null;
119 PreparedStatement ps = null;
120 ResultSet rs = null;
121
122 try {
123 con = DataAccess.getConnection();
124
125 ps = con.prepareStatement(
126 "select * from TagsEntry where entryId = ?",
127 ResultSet.TYPE_SCROLL_INSENSITIVE,
128 ResultSet.CONCUR_READ_ONLY);
129
130 ps.setLong(1, entryId);
131
132 rs = ps.executeQuery();
133
134 while (rs.next()) {
135 long companyId = rs.getLong("companyId");
136 long userId = rs.getLong("userId");
137 String userName = rs.getString("userName");
138 Timestamp createDate = rs.getTimestamp("createDate");
139 Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
140 long parentEntryId = rs.getLong("parentEntryId");
141 String name = rs.getString("name");
142 long vocabularyId = rs.getLong("vocabularyId");
143
144 newEntryId = increment();
145
146 addEntry(
147 newEntryId, groupId, companyId, userId, userName,
148 createDate, modifiedDate, parentEntryId, name,
149 vocabularyId);
150
151 copyProperties(entryId, newEntryId);
152
153 _entryIdsMap.put(key, newEntryId);
154
155 return newEntryId;
156 }
157 }
158 finally {
159 DataAccess.cleanUp(con, ps, rs);
160 }
161
162 throw new NoSuchTagException(
163 "No AssetTag exists with the primary key " + entryId);
164 }
165
166 public void copyProperties(long entryId, long newEntryId) throws Exception {
167 Connection con = null;
168 PreparedStatement ps = null;
169 ResultSet rs = null;
170
171 try {
172 con = DataAccess.getConnection();
173
174 ps = con.prepareStatement(
175 "select * from TagsProperty where entryId = ?",
176 ResultSet.TYPE_SCROLL_INSENSITIVE,
177 ResultSet.CONCUR_READ_ONLY);
178
179 ps.setLong(1, entryId);
180
181 rs = ps.executeQuery();
182
183 while (rs.next()) {
184 long companyId = rs.getLong("companyId");
185 long userId = rs.getLong("userId");
186 String userName = rs.getString("userName");
187 Timestamp createDate = rs.getTimestamp("createDate");
188 Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
189 String key = rs.getString("key_");
190 String value = rs.getString("value");
191
192 long newPropertyId = increment();
193
194 addProperty(
195 newPropertyId, companyId, userId, userName, createDate,
196 modifiedDate, newEntryId, key, value);
197 }
198 }
199 finally {
200 DataAccess.cleanUp(con, ps, rs);
201 }
202 }
203
204 protected void deleteEntries() throws Exception {
205 Connection con = null;
206 PreparedStatement ps = null;
207 ResultSet rs = null;
208
209 try {
210 con = DataAccess.getConnection();
211
212 ps = con.prepareStatement(
213 "select entryId from TagsEntry where groupId = 0");
214
215 rs = ps.executeQuery();
216
217 while (rs.next()) {
218 long entryId = rs.getLong("entryId");
219
220 runSQL(
221 "delete from TagsAssets_TagsEntries where entryId = " +
222 entryId);
223
224 runSQL("delete from TagsProperty where entryId = " + entryId);
225 }
226
227 runSQL("delete from TagsEntry where groupId = 0");
228 }
229 finally {
230 DataAccess.cleanUp(con, ps, rs);
231 }
232 }
233
234 protected void doUpgrade() throws Exception {
235 updateGroupIds();
236 updateAssets();
237 }
238
239 protected void updateAssets() throws Exception {
240 Connection con = null;
241 PreparedStatement ps = null;
242 ResultSet rs = null;
243
244 try {
245 con = DataAccess.getConnection();
246
247 ps = con.prepareStatement(
248 "select resourcePrimKey from JournalArticle where approved " +
249 "= ?");
250
251 ps.setBoolean(1, false);
252
253 rs = ps.executeQuery();
254
255 while (rs.next()) {
256 long resourcePrimKey = rs.getLong("resourcePrimKey");
257
258 runSQL(
259 "update TagsAsset set visible = FALSE where classPK = " +
260 resourcePrimKey);
261 }
262 }
263 finally {
264 DataAccess.cleanUp(con, ps, rs);
265 }
266 }
267
268 protected void updateGroupIds() throws Exception {
269 Connection con = null;
270 PreparedStatement ps = null;
271 ResultSet rs = null;
272
273 try {
274 con = DataAccess.getConnection();
275
276 ps = con.prepareStatement(
277 "select TA.assetId, TA.groupId, TA_TE.entryId from " +
278 "TagsAssets_TagsEntries TA_TE inner join TagsAsset TA on " +
279 "TA.assetId = TA_TE.assetId",
280 ResultSet.TYPE_SCROLL_INSENSITIVE,
281 ResultSet.CONCUR_READ_ONLY);
282
283 rs = ps.executeQuery();
284
285 SmartResultSet srs = new SmartResultSet(rs);
286
287 while (srs.next()) {
288 long assetId = srs.getLong("TA.assetId");
289 long groupId = srs.getLong("TA.groupId");
290 long entryId = srs.getLong("TA_TE.entryId");
291
292 long newEntryId = copyEntry(groupId, entryId);
293
294 runSQL(
295 "insert into TagsAssets_TagsEntries (assetId, entryId) " +
296 "values (" + assetId + ", " + newEntryId + ")");
297 }
298 }
299 finally {
300 DataAccess.cleanUp(con, ps, rs);
301 }
302
303 deleteEntries();
304 }
305
306 private Map<String, Long> _entryIdsMap = new HashMap<String, Long>();
307
308 }