001    /**
002     * Copyright (c) 2000-2012 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 deleteTempDirectory() {
044                    try {
045                            DLStoreUtil.deleteDirectory(0, 0, "liferay_temp/");
046                    }
047                    catch (Exception e) {
048                    }
049            }
050    
051            @Override
052            protected void doUpgrade() throws Exception {
053    
054                    // DLFileRank
055    
056                    try {
057                            runSQL("alter table DLFileRank add userName STRING");
058    
059                            runSQL("alter table DLFileRank add modifiedDate DATE");
060                    }
061                    catch (SQLException sqle) {
062                            upgradeTable(
063                                    DLFileRankTable.TABLE_NAME, DLFileRankTable.TABLE_COLUMNS,
064                                    DLFileRankTable.TABLE_SQL_CREATE,
065                                    DLFileRankTable.TABLE_SQL_ADD_INDEXES);
066                    }
067    
068                    updateFileRanks();
069    
070                    // DLSync
071    
072                    UpgradeColumn createDateColumn = new DateUpgradeColumnImpl(
073                            "createDate");
074                    UpgradeColumn modifiedDateColumn = new DateUpgradeColumnImpl(
075                            "modifiedDate");
076    
077                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
078                            DLSyncTable.TABLE_NAME, DLSyncTable.TABLE_COLUMNS, createDateColumn,
079                            modifiedDateColumn);
080    
081                    upgradeTable.setCreateSQL(DLSyncTable.TABLE_SQL_CREATE);
082                    upgradeTable.setIndexesSQL(DLSyncTable.TABLE_SQL_ADD_INDEXES);
083    
084                    upgradeTable.updateTable();
085    
086                    // Temp directory
087    
088                    deleteTempDirectory();
089            }
090    
091            protected String getUserName(long userId) throws Exception {
092                    Connection con = null;
093                    PreparedStatement ps = null;
094                    ResultSet rs = null;
095    
096                    try {
097                            con = DataAccess.getUpgradeOptimizedConnection();
098    
099                            ps = con.prepareStatement(
100                                    "select firstName, middleName, lastName from User_ where " +
101                                            "userId = ?");
102    
103                            ps.setLong(1, userId);
104    
105                            rs = ps.executeQuery();
106    
107                            if (rs.next()) {
108                                    String firstName = rs.getString("firstName");
109                                    String middleName = rs.getString("middleName");
110                                    String lastName = rs.getString("lastName");
111    
112                                    FullNameGenerator fullNameGenerator =
113                                            FullNameGeneratorFactory.getInstance();
114    
115                                    return fullNameGenerator.getFullName(
116                                            firstName, middleName, lastName);
117                            }
118    
119                            return StringPool.BLANK;
120                    }
121                    finally {
122                            DataAccess.cleanUp(con, ps, rs);
123                    }
124            }
125    
126            protected void updateFileRank(
127                            long fileRankId, long userId, Timestamp modifiedDate)
128                    throws Exception {
129    
130                    Connection con = null;
131                    PreparedStatement ps = null;
132    
133                    try {
134                            con = DataAccess.getUpgradeOptimizedConnection();
135    
136                            ps = con.prepareStatement(
137                                    "update DLFileRank set userName = ?, modifiedDate = ? where " +
138                                            "fileRankId = ?");
139    
140                            ps.setString(1, getUserName(userId));
141                            ps.setTimestamp(2, modifiedDate);
142                            ps.setLong(3, fileRankId);
143    
144                            ps.executeUpdate();
145                    }
146                    finally {
147                            DataAccess.cleanUp(con, ps);
148                    }
149            }
150    
151            protected void updateFileRanks() throws Exception {
152                    Connection con = null;
153                    PreparedStatement ps = null;
154                    ResultSet rs = null;
155    
156                    try {
157                            con = DataAccess.getUpgradeOptimizedConnection();
158    
159                            ps = con.prepareStatement(
160                                    "select fileRankId, userId, createDate from DLFileRank");
161    
162                            rs = ps.executeQuery();
163    
164                            while (rs.next()) {
165                                    long fileRankId = rs.getLong("fileRankId");
166                                    long userId = rs.getLong("userId");
167                                    Timestamp createDate = rs.getTimestamp("createDate");
168    
169                                    updateFileRank(fileRankId, userId, createDate);
170                            }
171                    }
172                    finally {
173                            DataAccess.cleanUp(con, ps, rs);
174                    }
175            }
176    
177    }