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.util.GetterUtil;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.LocalizationUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
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.DLFileEntryTypeTable;
027    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
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    
035    import java.util.HashMap;
036    import java.util.Locale;
037    import java.util.Map;
038    
039    /**
040     * @author Dennis Ju
041     * @author Mate Thurzo
042     * @author Alexander Chow
043     * @author Roberto D??az
044     */
045    public class UpgradeDocumentLibrary extends UpgradeProcess {
046    
047            protected void deleteChecksumDirectory() throws Exception {
048                    Connection con = null;
049                    PreparedStatement ps = null;
050                    ResultSet rs = null;
051    
052                    try {
053                            con = DataAccess.getUpgradeOptimizedConnection();
054    
055                            ps = con.prepareStatement(
056                                    "select distinct companyId from DLFileEntry");
057    
058                            rs = ps.executeQuery();
059    
060                            while (rs.next()) {
061                                    long companyId = rs.getLong("companyId");
062    
063                                    try {
064                                            DLStoreUtil.deleteDirectory(companyId, 0, "checksum");
065                                    }
066                                    catch (Exception e) {
067                                    }
068                            }
069                    }
070                    finally {
071                            DataAccess.cleanUp(con, ps, rs);
072                    }
073            }
074    
075            protected void deleteTempDirectory() {
076                    try {
077                            DLStoreUtil.deleteDirectory(0, 0, "liferay_temp/");
078                    }
079                    catch (Exception e) {
080                    }
081            }
082    
083            @Override
084            protected void doUpgrade() throws Exception {
085    
086                    // DLFileEntryType
087    
088                    try {
089                            runSQL("alter table DLFileEntryType add fileEntryTypeKey STRING");
090    
091                            runSQL("alter_column_type DLFileEntryType name STRING null");
092                    }
093                    catch (SQLException sqle) {
094                            upgradeTable(
095                                    DLFileEntryTypeTable.TABLE_NAME,
096                                    DLFileEntryTypeTable.TABLE_COLUMNS,
097                                    DLFileEntryTypeTable.TABLE_SQL_CREATE,
098                                    DLFileEntryTypeTable.TABLE_SQL_ADD_INDEXES);
099                    }
100    
101                    updateFileEntryTypes();
102    
103                    // Checksum directory
104    
105                    deleteChecksumDirectory();
106    
107                    // Temp directory
108    
109                    deleteTempDirectory();
110            }
111    
112            protected String getUserName(long userId) throws Exception {
113                    Connection con = null;
114                    PreparedStatement ps = null;
115                    ResultSet rs = null;
116    
117                    try {
118                            con = DataAccess.getUpgradeOptimizedConnection();
119    
120                            ps = con.prepareStatement(
121                                    "select firstName, middleName, lastName from User_ where " +
122                                            "userId = ?");
123    
124                            ps.setLong(1, userId);
125    
126                            rs = ps.executeQuery();
127    
128                            if (rs.next()) {
129                                    String firstName = rs.getString("firstName");
130                                    String middleName = rs.getString("middleName");
131                                    String lastName = rs.getString("lastName");
132    
133                                    FullNameGenerator fullNameGenerator =
134                                            FullNameGeneratorFactory.getInstance();
135    
136                                    return fullNameGenerator.getFullName(
137                                            firstName, middleName, lastName);
138                            }
139    
140                            return StringPool.BLANK;
141                    }
142                    finally {
143                            DataAccess.cleanUp(con, ps, rs);
144                    }
145            }
146    
147            protected String localize(String content, String key) {
148                    Locale locale = LocaleUtil.getDefault();
149    
150                    Map<Locale, String> localizationMap = new HashMap<Locale, String>();
151    
152                    localizationMap.put(locale, content);
153    
154                    return LocalizationUtil.updateLocalization(
155                            localizationMap, StringPool.BLANK, key,
156                            LocaleUtil.toLanguageId(locale));
157            }
158    
159            protected void updateFileEntryType(
160                            long fileEntryTypeId, String fileEntryTypeKey, String name,
161                            String description)
162                    throws Exception {
163    
164                    Connection con = null;
165                    PreparedStatement ps = null;
166    
167                    try {
168                            con = DataAccess.getUpgradeOptimizedConnection();
169    
170                            ps = con.prepareStatement(
171                                    "update DLFileEntryType set fileEntryTypeKey = ?, name = ?, " +
172                                            "description = ? where fileEntryTypeId = ?");
173    
174                            ps.setString(1, fileEntryTypeKey);
175                            ps.setString(2, localize(name, "Name"));
176                            ps.setString(3, localize(description, "Description"));
177                            ps.setLong(4, fileEntryTypeId);
178    
179                            ps.executeUpdate();
180                    }
181                    finally {
182                            DataAccess.cleanUp(con, ps);
183                    }
184            }
185    
186            protected void updateFileEntryTypes() throws Exception {
187                    Connection con = null;
188                    PreparedStatement ps = null;
189                    ResultSet rs = null;
190    
191                    try {
192                            con = DataAccess.getUpgradeOptimizedConnection();
193    
194                            ps = con.prepareStatement(
195                                    "select fileEntryTypeId, name, description from " +
196                                            "DLFileEntryType");
197    
198                            rs = ps.executeQuery();
199    
200                            while (rs.next()) {
201                                    long fileEntryTypeId = rs.getLong("fileEntryTypeId");
202                                    String name = GetterUtil.getString(rs.getString("name"));
203                                    String description = rs.getString("description");
204    
205                                    if (fileEntryTypeId ==
206                                                    DLFileEntryTypeConstants.
207                                                            FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT) {
208    
209                                            name = DLFileEntryTypeConstants.NAME_BASIC_DOCUMENT;
210                                    }
211    
212                                    updateFileEntryType(
213                                            fileEntryTypeId, StringUtil.toUpperCase(name), name,
214                                            description);
215                            }
216                    }
217                    finally {
218                            DataAccess.cleanUp(con, ps, rs);
219                    }
220            }
221    
222    }