001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.upgrade.v6_1_0.util;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.dao.db.DBManagerUtil;
019    import com.liferay.portal.kernel.dao.jdbc.AutoBatchPreparedStatementUtil;
020    import com.liferay.portal.kernel.util.LoggingTimer;
021    import com.liferay.portal.kernel.util.StringBundler;
022    
023    import java.sql.Connection;
024    import java.sql.PreparedStatement;
025    import java.sql.ResultSet;
026    import java.sql.Timestamp;
027    
028    /**
029     * @author Cristina Gonz??lez
030     */
031    public class UpdateSyncUtil {
032    
033            public static void updateSyncs(Connection connection) throws Exception {
034                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
035                            StringBundler sb1 = new StringBundler(11);
036    
037                            sb1.append("select DLFileEntry.fileEntryId as fileId, ");
038                            sb1.append("DLFileEntry.groupId as groupId, ");
039                            sb1.append("DLFileEntry.companyId as companyId, ");
040                            sb1.append("DLFileEntry.createDate as createDate, ");
041                            sb1.append("DLFileEntry.folderId as parentFolderId, 'file' as ");
042                            sb1.append("type from DLFileEntry union all select ");
043                            sb1.append("DLFolder.folderId as fileId, DLFolder.groupId as ");
044                            sb1.append("groupId, DLFolder.companyId as companyId, ");
045                            sb1.append("DLFolder.createDate as createDate, ");
046                            sb1.append("DLFolder.parentFolderId as parentFolderId, 'folder' ");
047                            sb1.append("as type from DLFolder");
048    
049                            StringBundler sb2 = new StringBundler(3);
050    
051                            sb2.append("insert into DLSync (syncId, companyId, createDate, ");
052                            sb2.append("modifiedDate, fileId, repositoryId, parentFolderId, ");
053                            sb2.append("event, type_) values (?, ?, ?, ?, ?, ?, ?, ?, ?)");
054    
055                            try (PreparedStatement ps1 = connection.prepareStatement(
056                                            sb1.toString());
057                                    PreparedStatement ps2 =
058                                            AutoBatchPreparedStatementUtil.concurrentAutoBatch(
059                                                    connection, sb2.toString());
060                                    ResultSet rs = ps1.executeQuery()) {
061    
062                                    while (rs.next()) {
063                                            long fileId = rs.getLong("fileId");
064                                            long groupId = rs.getLong("groupId");
065                                            long companyId = rs.getLong("companyId");
066                                            Timestamp createDate = rs.getTimestamp("createDate");
067                                            long parentFolderId = rs.getLong("parentFolderId");
068                                            String type = rs.getString("type");
069    
070                                            ps2.setLong(1, _increment());
071                                            ps2.setLong(2, companyId);
072                                            ps2.setTimestamp(3, createDate);
073                                            ps2.setTimestamp(4, createDate);
074                                            ps2.setLong(5, fileId);
075                                            ps2.setLong(6, groupId);
076                                            ps2.setLong(7, parentFolderId);
077                                            ps2.setString(8, "add");
078                                            ps2.setString(9, type);
079    
080                                            ps2.addBatch();
081                                    }
082    
083                                    ps2.executeBatch();
084                            }
085                    }
086            }
087    
088            private static long _increment() {
089                    DB db = DBManagerUtil.getDB();
090    
091                    return db.increment();
092            }
093    
094    }