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.v7_0_0;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.language.LanguageUtil;
019    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
020    import com.liferay.portal.kernel.upgrade.util.UpgradeProcessUtil;
021    import com.liferay.portal.kernel.util.FileUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.LocaleUtil;
024    import com.liferay.portal.kernel.util.LocalizationUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.model.Company;
028    import com.liferay.portal.repository.liferayrepository.LiferayRepository;
029    import com.liferay.portal.repository.portletrepository.PortletRepository;
030    import com.liferay.portal.upgrade.v7_0_0.util.DLFolderTable;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
033    import com.liferay.portlet.documentlibrary.util.DLUtil;
034    
035    import java.sql.Connection;
036    import java.sql.PreparedStatement;
037    import java.sql.ResultSet;
038    import java.sql.SQLException;
039    
040    import java.util.HashMap;
041    import java.util.Locale;
042    import java.util.Map;
043    
044    /**
045     * @author Michael Young
046     */
047    public class UpgradeDocumentLibrary extends UpgradeProcess {
048    
049            @Override
050            protected void doUpgrade() throws Exception {
051    
052                    // DLFileEntry
053    
054                    runSQL("alter table DLFileEntry add fileName VARCHAR(255) null");
055    
056                    updateFileEntryFileNames();
057    
058                    // DLFileEntryType
059    
060                    updateFileEntryTypeNamesAndDescriptions();
061    
062                    // DLFileVersion
063    
064                    runSQL("alter table DLFileVersion add fileName VARCHAR(255) null");
065    
066                    updateFileVersionFileNames();
067    
068                    // DLFolder
069    
070                    try {
071                            runSQL("alter_column_type DLFolder name VARCHAR(255) null");
072                    }
073                    catch (SQLException sqle) {
074                            upgradeTable(
075                                    DLFolderTable.TABLE_NAME, DLFolderTable.TABLE_COLUMNS,
076                                    DLFolderTable.TABLE_SQL_CREATE,
077                                    DLFolderTable.TABLE_SQL_ADD_INDEXES);
078                    }
079    
080                    updateRepositoryClassNameIds();
081            }
082    
083            protected boolean hasFileEntry(long groupId, long folderId, String fileName)
084                    throws Exception {
085    
086                    Connection con = null;
087                    PreparedStatement ps = null;
088                    ResultSet rs = null;
089    
090                    try {
091                            con = DataAccess.getUpgradeOptimizedConnection();
092    
093                            ps = con.prepareStatement(
094                                    "select count(*) from DLFileEntry where groupId = ? and " +
095                                            "folderId = ? and fileName = ?");
096    
097                            ps.setLong(1, groupId);
098                            ps.setLong(2, folderId);
099                            ps.setString(3, fileName);
100    
101                            rs = ps.executeQuery();
102    
103                            while (rs.next()) {
104                                    int count = rs.getInt(1);
105    
106                                    if (count > 0) {
107                                            return true;
108                                    }
109                            }
110    
111                            return false;
112                    }
113                    finally {
114                            DataAccess.cleanUp(con, ps, rs);
115                    }
116            }
117    
118            protected void updateFileEntryFileName(long fileEntryId, String fileName)
119                    throws Exception {
120    
121                    Connection con = null;
122                    PreparedStatement ps = null;
123    
124                    try {
125                            con = DataAccess.getUpgradeOptimizedConnection();
126    
127                            ps = con.prepareStatement(
128                                    "update DLFileEntry set fileName = ? where fileEntryId = ?");
129    
130                            ps.setString(1, fileName);
131                            ps.setLong(2, fileEntryId);
132    
133                            ps.executeUpdate();
134                    }
135                    finally {
136                            DataAccess.cleanUp(con, ps);
137                    }
138            }
139    
140            protected void updateFileEntryFileNames() throws Exception {
141                    Connection con = null;
142                    PreparedStatement ps = null;
143                    ResultSet rs = null;
144    
145                    try {
146                            con = DataAccess.getUpgradeOptimizedConnection();
147    
148                            ps = con.prepareStatement(
149                                    "select fileEntryId, groupId, folderId, extension, title, " +
150                                            "version from DLFileEntry");
151    
152                            rs = ps.executeQuery();
153    
154                            while (rs.next()) {
155                                    long fileEntryId = rs.getLong("fileEntryId");
156                                    long groupId = rs.getLong("groupId");
157                                    long folderId = rs.getLong("folderId");
158                                    String extension = GetterUtil.getString(
159                                            rs.getString("extension"));
160                                    String title = GetterUtil.getString(rs.getString("title"));
161                                    String version = rs.getString("version");
162    
163                                    String uniqueFileName = DLUtil.getSanitizedFileName(
164                                            title, extension);
165    
166                                    String titleExtension = StringPool.BLANK;
167                                    String titleWithoutExtension = title;
168    
169                                    if (title.endsWith(StringPool.PERIOD + extension)) {
170                                            titleExtension = extension;
171                                            titleWithoutExtension = FileUtil.stripExtension(title);
172                                    }
173    
174                                    String uniqueTitle = StringPool.BLANK;
175    
176                                    for (int i = 1;; i++) {
177                                            if (!hasFileEntry(groupId, folderId, uniqueFileName)) {
178                                                    break;
179                                            }
180    
181                                            uniqueTitle =
182                                                    titleWithoutExtension + StringPool.UNDERLINE +
183                                                            String.valueOf(i);
184    
185                                            if (Validator.isNotNull(titleExtension)) {
186                                                    uniqueTitle += StringPool.PERIOD.concat(titleExtension);
187                                            }
188    
189                                            uniqueFileName = DLUtil.getSanitizedFileName(
190                                                    uniqueTitle, extension);
191                                    }
192    
193                                    updateFileEntryFileName(fileEntryId, uniqueFileName);
194    
195                                    if (Validator.isNotNull(uniqueTitle)) {
196                                            updateFileEntryTitle(fileEntryId, uniqueTitle, version);
197                                    }
198                            }
199                    }
200                    finally {
201                            DataAccess.cleanUp(con, ps, rs);
202                    }
203            }
204    
205            protected void updateFileEntryTitle(
206                            long fileEntryId, String title, String version)
207                    throws Exception {
208    
209                    Connection con = null;
210                    PreparedStatement ps = null;
211    
212                    try {
213                            con = DataAccess.getUpgradeOptimizedConnection();
214    
215                            ps = con.prepareStatement(
216                                    "update DLFileEntry set title = ? where fileEntryId = ?");
217    
218                            ps.setString(1, title);
219                            ps.setLong(2, fileEntryId);
220    
221                            ps.executeUpdate();
222    
223                            ps = con.prepareStatement(
224                                    "update DLFileVersion set title = ? where fileEntryId = " +
225                                            "? and version = ?");
226    
227                            ps.setString(1, title);
228                            ps.setLong(2, fileEntryId);
229                            ps.setString(3, version);
230    
231                            ps.executeUpdate();
232                    }
233                    finally {
234                            DataAccess.cleanUp(con, ps);
235                    }
236            }
237    
238            protected void updateFileEntryTypeNamesAndDescriptions() throws Exception {
239                    Connection con = null;
240                    PreparedStatement ps = null;
241                    ResultSet rs = null;
242    
243                    try {
244                            con = DataAccess.getUpgradeOptimizedConnection();
245    
246                            ps = con.prepareStatement(
247                                    "select companyId, groupId from Group_ where classNameId = ?");
248    
249                            long classNameId = PortalUtil.getClassNameId(Company.class);
250    
251                            ps.setLong(1, classNameId);
252    
253                            rs = ps.executeQuery();
254    
255                            while (rs.next()) {
256                                    long companyId = rs.getLong(1);
257                                    long groupId = rs.getLong(2);
258    
259                                    updateFileEntryTypeNamesAndDescriptions(companyId, groupId);
260                            }
261                    }
262                    finally {
263                            DataAccess.cleanUp(con, ps, rs);
264                    }
265            }
266    
267            protected void updateFileEntryTypeNamesAndDescriptions(
268                            long companyId, long groupId)
269                    throws Exception {
270    
271                    Map<String, String> nameLanguageKeys = new HashMap<>();
272    
273                    nameLanguageKeys.put(
274                            DLFileEntryTypeConstants.NAME_CONTRACT,
275                            DLFileEntryTypeConstants.FILE_ENTRY_TYPE_KEY_CONTRACT);
276                    nameLanguageKeys.put(
277                            DLFileEntryTypeConstants.NAME_MARKETING_BANNER,
278                            DLFileEntryTypeConstants.FILE_ENTRY_TYPE_KEY_MARKETING_BANNER);
279                    nameLanguageKeys.put(
280                            DLFileEntryTypeConstants.NAME_ONLINE_TRAINING,
281                            DLFileEntryTypeConstants.FILE_ENTRY_TYPE_KEY_ONLINE_TRAINING);
282                    nameLanguageKeys.put(
283                            DLFileEntryTypeConstants.NAME_SALES_PRESENTATION,
284                            DLFileEntryTypeConstants.FILE_ENTRY_TYPE_KEY_SALES_PRESENTATION);
285    
286                    for (Map.Entry<String, String> nameAndKey :
287                                    nameLanguageKeys.entrySet()) {
288    
289                            String dlFileEntryTypeKey = nameAndKey.getValue();
290                            String nameLanguageKey = nameAndKey.getKey();
291    
292                            updateFileEntryTypeNamesAndDescriptions(
293                                    companyId, groupId, dlFileEntryTypeKey, nameLanguageKey);
294                    }
295            }
296    
297            protected void updateFileEntryTypeNamesAndDescriptions(
298                            long companyId, long groupId, String dlFileEntryTypeKey,
299                            String nameLanguageKey)
300                    throws Exception {
301    
302                    Connection con = null;
303                    PreparedStatement ps = null;
304                    ResultSet rs = null;
305    
306                    try {
307                            con = DataAccess.getUpgradeOptimizedConnection();
308    
309                            ps = con.prepareStatement(
310                                    "select fileEntryTypeId, name, description from " +
311                                            "DLFileEntryType where groupId = ? and fileEntryTypeKey " +
312                                                    "= ?");
313    
314                            ps.setLong(1, groupId);
315                            ps.setString(2, dlFileEntryTypeKey);
316    
317                            rs = ps.executeQuery();
318    
319                            if (!rs.next()) {
320                                    return;
321                            }
322    
323                            long fileEntryTypeId = rs.getLong(1);
324                            String name = rs.getString(2);
325                            String description = rs.getString(3);
326    
327                            if (rs.next()) {
328                                    throw new IllegalStateException(
329                                            String.format(
330                                                    "Found more than one row in table DLFileEntryType " +
331                                                            "with groupId %s and fileEntryTypeKey %s",
332                                                    groupId, dlFileEntryTypeKey));
333                            }
334    
335                            updateFileEntryTypeNamesAndDescriptions(
336                                    companyId, fileEntryTypeId, nameLanguageKey, name, description);
337                    }
338                    finally {
339                            DataAccess.cleanUp(con, ps, rs);
340                    }
341            }
342    
343            protected void updateFileEntryTypeNamesAndDescriptions(
344                            long companyId, long dlFileEntryTypeId, String nameLanguageKey,
345                            String nameXML, String descriptionXML)
346                    throws Exception {
347    
348                    boolean update = false;
349    
350                    Locale defaultLocale = LocaleUtil.fromLanguageId(
351                            UpgradeProcessUtil.getDefaultLanguageId(companyId));
352    
353                    String defaultValue = LanguageUtil.get(defaultLocale, nameLanguageKey);
354    
355                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
356                            nameXML);
357                    Map<Locale, String> descriptionMap =
358                            LocalizationUtil.getLocalizationMap(descriptionXML);
359    
360                    for (Locale locale : LanguageUtil.getSupportedLocales()) {
361                            String value = LanguageUtil.get(locale, nameLanguageKey);
362    
363                            if (!locale.equals(defaultLocale) && value.equals(defaultValue)) {
364                                    continue;
365                            }
366    
367                            String description = descriptionMap.get(locale);
368    
369                            if (description == null) {
370                                    descriptionMap.put(locale, value);
371    
372                                    update = true;
373                            }
374    
375                            String name = nameMap.get(locale);
376    
377                            if (name == null) {
378                                    nameMap.put(locale, value);
379    
380                                    update = true;
381                            }
382                    }
383    
384                    if (update) {
385                            updateFileEntryTypeNamesAndDescriptions(
386                                    dlFileEntryTypeId, nameXML, descriptionXML, nameMap,
387                                    descriptionMap, defaultLocale);
388                    }
389            }
390    
391            protected void updateFileEntryTypeNamesAndDescriptions(
392                            long fileEntryTypeId, String nameXML, String descriptionXML,
393                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
394                            Locale defaultLocale)
395                    throws Exception {
396    
397                    Connection con = null;
398                    PreparedStatement ps = null;
399    
400                    try {
401                            con = DataAccess.getUpgradeOptimizedConnection();
402    
403                            ps = con.prepareStatement(
404                                    "update DLFileEntryType set name = ?, description = ? where " +
405                                            "fileEntryTypeId = ?");
406    
407                            String languageId = LanguageUtil.getLanguageId(defaultLocale);
408    
409                            nameXML = LocalizationUtil.updateLocalization(
410                                    nameMap, nameXML, "Name", languageId);
411                            descriptionXML = LocalizationUtil.updateLocalization(
412                                    descriptionMap, descriptionXML, "Description", languageId);
413    
414                            ps.setString(1, nameXML);
415                            ps.setString(2, descriptionXML);
416                            ps.setLong(3, fileEntryTypeId);
417    
418                            int rowCount = ps.executeUpdate();
419    
420                            if (rowCount != 1) {
421                                    throw new IllegalStateException(
422                                            String.format(
423                                                    "Updated %s rows in table DLFileEntryType with " +
424                                                            "fileEntryTypeId %s",
425                                                    rowCount, fileEntryTypeId));
426                            }
427                    }
428                    finally {
429                            DataAccess.cleanUp(con, ps);
430                    }
431            }
432    
433            protected void updateFileVersionFileName(
434                            long fileVersionId, String fileName)
435                    throws Exception {
436    
437                    Connection con = null;
438                    PreparedStatement ps = null;
439    
440                    try {
441                            con = DataAccess.getUpgradeOptimizedConnection();
442    
443                            ps = con.prepareStatement(
444                                    "update DLFileVersion set fileName = ? where " +
445                                            "fileVersionId = ?");
446    
447                            ps.setString(1, fileName);
448                            ps.setLong(2, fileVersionId);
449    
450                            ps.executeUpdate();
451                    }
452                    finally {
453                            DataAccess.cleanUp(con, ps);
454                    }
455            }
456    
457            protected void updateFileVersionFileNames() throws Exception {
458                    Connection con = null;
459                    PreparedStatement ps = null;
460                    ResultSet rs = null;
461    
462                    try {
463                            con = DataAccess.getUpgradeOptimizedConnection();
464    
465                            ps = con.prepareStatement(
466                                    "select fileVersionId, extension, title from DLFileVersion");
467    
468                            rs = ps.executeQuery();
469    
470                            while (rs.next()) {
471                                    long fileVersionId = rs.getLong("fileVersionId");
472                                    String extension = GetterUtil.getString(
473                                            rs.getString("extension"));
474                                    String title = GetterUtil.getString(rs.getString("title"));
475    
476                                    String fileName = DLUtil.getSanitizedFileName(title, extension);
477    
478                                    updateFileVersionFileName(fileVersionId, fileName);
479                            }
480                    }
481                    finally {
482                            DataAccess.cleanUp(con, ps, rs);
483                    }
484            }
485    
486            protected void updateRepositoryClassNameIds() throws Exception {
487                    long liferayRepositoryClassNameId = PortalUtil.getClassNameId(
488                            LiferayRepository.class);
489                    long portletRepositoryClassNameId = PortalUtil.getClassNameId(
490                            PortletRepository.class);
491    
492                    Connection con = null;
493                    PreparedStatement ps = null;
494    
495                    try {
496                            con = DataAccess.getUpgradeOptimizedConnection();
497    
498                            ps = con.prepareStatement(
499                                    "update Repository set classNameId = ? where classNameId = ?");
500    
501                            ps.setLong(1, portletRepositoryClassNameId);
502                            ps.setLong(2, liferayRepositoryClassNameId);
503    
504                            ps.executeUpdate();
505                    }
506                    finally {
507                            DataAccess.cleanUp(con, ps);
508                    }
509            }
510    
511    }