001    /**
002     * Copyright (c) 2000-2013 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_2_0;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.upgrade.util.DateUpgradeColumnImpl;
020    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
021    import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
022    import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.security.auth.FullNameGenerator;
025    import com.liferay.portal.security.auth.FullNameGeneratorFactory;
026    import com.liferay.portal.upgrade.v6_2_0.util.DLFileRankTable;
027    import com.liferay.portal.upgrade.v6_2_0.util.DLSyncTable;
028    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
029    
030    import java.sql.Connection;
031    import java.sql.PreparedStatement;
032    import java.sql.ResultSet;
033    import java.sql.SQLException;
034    import java.sql.Timestamp;
035    
036    /**
037     * @author Dennis Ju
038     * @author Mate Thurzo
039     * @author Alexander Chow
040     */
041    public class UpgradeDocumentLibrary extends UpgradeProcess {
042    
043            protected void deleteChecksumDirectory() throws Exception {
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 distinct companyId from DLFileEntry");
053    
054                            rs = ps.executeQuery();
055    
056                            while (rs.next()) {
057                                    long companyId = rs.getLong("companyId");
058    
059                                    try {
060                                            DLStoreUtil.deleteDirectory(companyId, 0, "checksum");
061                                    }
062                                    catch (Exception e) {
063                                    }
064                            }
065                    }
066                    finally {
067                            DataAccess.cleanUp(con, ps, rs);
068                    }
069            }
070    
071            protected void deleteTempDirectory() {
072                    try {
073                            DLStoreUtil.deleteDirectory(0, 0, "liferay_temp/");
074                    }
075                    catch (Exception e) {
076                    }
077            }
078    
079            @Override
080            protected void doUpgrade() throws Exception {
081    
082                    // DLFileRank
083    
084                    try {
085                            runSQL("alter table DLFileRank add userName STRING");
086    
087                            runSQL("alter table DLFileRank add modifiedDate DATE");
088                    }
089                    catch (SQLException sqle) {
090                            upgradeTable(
091                                    DLFileRankTable.TABLE_NAME, DLFileRankTable.TABLE_COLUMNS,
092                                    DLFileRankTable.TABLE_SQL_CREATE,
093                                    DLFileRankTable.TABLE_SQL_ADD_INDEXES);
094                    }
095    
096                    updateFileRanks();
097    
098                    // DLSync
099    
100                    UpgradeColumn createDateColumn = new DateUpgradeColumnImpl(
101                            "createDate");
102                    UpgradeColumn modifiedDateColumn = new DateUpgradeColumnImpl(
103                            "modifiedDate");
104    
105                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
106                            DLSyncTable.TABLE_NAME, DLSyncTable.TABLE_COLUMNS, createDateColumn,
107                            modifiedDateColumn);
108    
109                    upgradeTable.setCreateSQL(DLSyncTable.TABLE_SQL_CREATE);
110                    upgradeTable.setIndexesSQL(DLSyncTable.TABLE_SQL_ADD_INDEXES);
111    
112                    upgradeTable.updateTable();
113    
114                    // Checksum directory
115    
116                    deleteChecksumDirectory();
117    
118                    // Temp directory
119    
120                    deleteTempDirectory();
121            }
122    
123            protected String getUserName(long userId) throws Exception {
124                    Connection con = null;
125                    PreparedStatement ps = null;
126                    ResultSet rs = null;
127    
128                    try {
129                            con = DataAccess.getUpgradeOptimizedConnection();
130    
131                            ps = con.prepareStatement(
132                                    "select firstName, middleName, lastName from User_ where " +
133                                            "userId = ?");
134    
135                            ps.setLong(1, userId);
136    
137                            rs = ps.executeQuery();
138    
139                            if (rs.next()) {
140                                    String firstName = rs.getString("firstName");
141                                    String middleName = rs.getString("middleName");
142                                    String lastName = rs.getString("lastName");
143    
144                                    FullNameGenerator fullNameGenerator =
145                                            FullNameGeneratorFactory.getInstance();
146    
147                                    return fullNameGenerator.getFullName(
148                                            firstName, middleName, lastName);
149                            }
150    
151                            return StringPool.BLANK;
152                    }
153                    finally {
154                            DataAccess.cleanUp(con, ps, rs);
155                    }
156            }
157    
158            protected void updateFileRank(
159                            long fileRankId, long userId, Timestamp modifiedDate)
160                    throws Exception {
161    
162                    Connection con = null;
163                    PreparedStatement ps = null;
164    
165                    try {
166                            con = DataAccess.getUpgradeOptimizedConnection();
167    
168                            ps = con.prepareStatement(
169                                    "update DLFileRank set userName = ?, modifiedDate = ? where " +
170                                            "fileRankId = ?");
171    
172                            ps.setString(1, getUserName(userId));
173                            ps.setTimestamp(2, modifiedDate);
174                            ps.setLong(3, fileRankId);
175    
176                            ps.executeUpdate();
177                    }
178                    finally {
179                            DataAccess.cleanUp(con, ps);
180                    }
181            }
182    
183            protected void updateFileRanks() throws Exception {
184                    Connection con = null;
185                    PreparedStatement ps = null;
186                    ResultSet rs = null;
187    
188                    try {
189                            con = DataAccess.getUpgradeOptimizedConnection();
190    
191                            ps = con.prepareStatement(
192                                    "select fileRankId, userId, createDate from DLFileRank");
193    
194                            rs = ps.executeQuery();
195    
196                            while (rs.next()) {
197                                    long fileRankId = rs.getLong("fileRankId");
198                                    long userId = rs.getLong("userId");
199                                    Timestamp createDate = rs.getTimestamp("createDate");
200    
201                                    updateFileRank(fileRankId, userId, createDate);
202                            }
203                    }
204                    finally {
205                            DataAccess.cleanUp(con, ps, rs);
206                    }
207            }
208    
209    }