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