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.document.library.kernel.model.DLFileEntryTypeConstants;
018    import com.liferay.document.library.kernel.store.DLStoreUtil;
019    import com.liferay.portal.kernel.security.auth.FullNameGenerator;
020    import com.liferay.portal.kernel.security.auth.FullNameGeneratorFactory;
021    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
022    import com.liferay.portal.kernel.upgrade.util.UpgradeProcessUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.LocaleUtil;
025    import com.liferay.portal.kernel.util.LocalizationUtil;
026    import com.liferay.portal.kernel.util.LoggingTimer;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.StringUtil;
029    import com.liferay.portal.upgrade.v6_2_0.util.DLFileEntryTypeTable;
030    
031    import java.sql.PreparedStatement;
032    import java.sql.ResultSet;
033    
034    import java.util.HashMap;
035    import java.util.Locale;
036    import java.util.Map;
037    
038    /**
039     * @author Dennis Ju
040     * @author Mate Thurzo
041     * @author Alexander Chow
042     * @author Roberto D??az
043     */
044    public class UpgradeDocumentLibrary extends UpgradeProcess {
045    
046            protected void deleteChecksumDirectory() throws Exception {
047                    try (LoggingTimer loggingTimer = new LoggingTimer();
048                            PreparedStatement ps = connection.prepareStatement(
049                                    "select distinct companyId from DLFileEntry");
050                            ResultSet rs = ps.executeQuery()) {
051    
052                            while (rs.next()) {
053                                    long companyId = rs.getLong("companyId");
054    
055                                    DLStoreUtil.deleteDirectory(companyId, 0, "checksum");
056                            }
057                    }
058            }
059    
060            protected void deleteTempDirectory() {
061                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
062                            DLStoreUtil.deleteDirectory(0, 0, "liferay_temp/");
063                    }
064            }
065    
066            @Override
067            protected void doUpgrade() throws Exception {
068    
069                    // DLFileEntryType
070    
071                    alter(
072                            DLFileEntryTypeTable.class,
073                            new AlterTableAddColumn("fileEntryTypeKey STRING"),
074                            new AlterColumnType("name", "STRING null"));
075    
076                    updateFileEntryTypes();
077    
078                    // Checksum directory
079    
080                    deleteChecksumDirectory();
081    
082                    // Temp directory
083    
084                    deleteTempDirectory();
085            }
086    
087            protected String getUserName(long userId) throws Exception {
088                    try (PreparedStatement ps = connection.prepareStatement(
089                                    "select firstName, middleName, lastName from User_ where " +
090                                            "userId = ?")) {
091    
092                            ps.setLong(1, userId);
093    
094                            try (ResultSet rs = ps.executeQuery()) {
095                                    if (rs.next()) {
096                                            String firstName = rs.getString("firstName");
097                                            String middleName = rs.getString("middleName");
098                                            String lastName = rs.getString("lastName");
099    
100                                            FullNameGenerator fullNameGenerator =
101                                                    FullNameGeneratorFactory.getInstance();
102    
103                                            return fullNameGenerator.getFullName(
104                                                    firstName, middleName, lastName);
105                                    }
106    
107                                    return StringPool.BLANK;
108                            }
109                    }
110            }
111    
112            protected String localize(long companyId, String content, String key)
113                    throws Exception {
114    
115                    String languageId = UpgradeProcessUtil.getDefaultLanguageId(companyId);
116    
117                    Locale locale = LocaleUtil.fromLanguageId(languageId);
118    
119                    Map<Locale, String> localizationMap = new HashMap<>();
120    
121                    localizationMap.put(locale, content);
122    
123                    return LocalizationUtil.updateLocalization(
124                            localizationMap, StringPool.BLANK, key, languageId);
125            }
126    
127            protected void updateFileEntryType(
128                            long fileEntryTypeId, long companyId, String fileEntryTypeKey,
129                            String name, String description)
130                    throws Exception {
131    
132                    try (PreparedStatement ps = connection.prepareStatement(
133                                    "update DLFileEntryType set fileEntryTypeKey = ?, name = ?, " +
134                                            "description = ? where fileEntryTypeId = ?")) {
135    
136                            ps.setString(1, fileEntryTypeKey);
137                            ps.setString(2, localize(companyId, name, "Name"));
138                            ps.setString(3, localize(companyId, description, "Description"));
139                            ps.setLong(4, fileEntryTypeId);
140    
141                            ps.executeUpdate();
142                    }
143            }
144    
145            protected void updateFileEntryTypes() throws Exception {
146                    try (LoggingTimer loggingTimer = new LoggingTimer();
147                            PreparedStatement ps = connection.prepareStatement(
148                                    "select fileEntryTypeId, companyId, name, description from " +
149                                            "DLFileEntryType");
150                            ResultSet rs = ps.executeQuery()) {
151    
152                            while (rs.next()) {
153                                    long fileEntryTypeId = rs.getLong("fileEntryTypeId");
154                                    long companyId = rs.getLong("companyId");
155                                    String name = GetterUtil.getString(rs.getString("name"));
156                                    String description = rs.getString("description");
157    
158                                    if (fileEntryTypeId ==
159                                                    DLFileEntryTypeConstants.
160                                                            FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT) {
161    
162                                            name = DLFileEntryTypeConstants.NAME_BASIC_DOCUMENT;
163                                    }
164    
165                                    updateFileEntryType(
166                                            fileEntryTypeId, companyId, StringUtil.toUpperCase(name),
167                                            name, description);
168                            }
169                    }
170            }
171    
172    }