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