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.asset.kernel.model.AssetCategoryConstants;
018    import com.liferay.portal.kernel.dao.jdbc.AutoBatchPreparedStatementUtil;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
022    import com.liferay.portal.kernel.util.ArrayUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.LoggingTimer;
025    import com.liferay.portal.kernel.util.PortalUtil;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.UnicodeProperties;
029    import com.liferay.portal.upgrade.v7_0_0.util.AssetEntryTable;
030    import com.liferay.portlet.asset.util.AssetVocabularySettingsHelper;
031    
032    import java.sql.PreparedStatement;
033    import java.sql.ResultSet;
034    
035    import java.util.Arrays;
036    
037    /**
038     * @author Gergely Mathe
039     */
040    public class UpgradeAsset extends UpgradeProcess {
041    
042            @Override
043            protected void doUpgrade() throws Exception {
044                    alter(
045                            AssetEntryTable.class,
046                            new AlterColumnType("description", "TEXT null"),
047                            new AlterColumnType("summary", "TEXT null"));
048    
049                    updateAssetEntries();
050                    updateAssetVocabularies();
051            }
052    
053            protected long getDDMStructureId(String structureKey) throws Exception {
054                    try (PreparedStatement ps = connection.prepareStatement(
055                                    "select structureId from DDMStructure where structureKey = " +
056                                            "?")) {
057    
058                            ps.setString(1, structureKey);
059    
060                            try (ResultSet rs = ps.executeQuery()) {
061                                    if (rs.next()) {
062                                            return rs.getLong("structureId");
063                                    }
064    
065                                    return 0;
066                            }
067                    }
068            }
069    
070            protected void updateAssetEntries() throws Exception {
071                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
072                            long classNameId = PortalUtil.getClassNameId(
073                                    "com.liferay.journal.model.JournalArticle");
074    
075                            StringBundler sb = new StringBundler(9);
076    
077                            sb.append("select JournalArticle.resourcePrimKey from (select ");
078                            sb.append("JournalArticle.resourcePrimkey as primKey, ");
079                            sb.append("max(JournalArticle.version) as maxVersion from ");
080                            sb.append("JournalArticle group by ");
081                            sb.append("JournalArticle.resourcePrimkey) temp_table inner join ");
082                            sb.append("JournalArticle on (JournalArticle.indexable = ?) and ");
083                            sb.append("(JournalArticle.status = 0) and ");
084                            sb.append("(JournalArticle.resourcePrimkey = temp_table.primKey) ");
085                            sb.append("and (JournalArticle.version = temp_table.maxVersion)");
086    
087                            try (PreparedStatement ps1 = connection.prepareStatement(
088                                            sb.toString())) {
089    
090                                    ps1.setBoolean(1, false);
091    
092                                    try (PreparedStatement ps2 =
093                                                    AutoBatchPreparedStatementUtil.concurrentAutoBatch(
094                                                            connection,
095                                                            "update AssetEntry set listable = ? where " +
096                                                                    "classNameId = ? and classPK = ?");
097                                            ResultSet rs = ps1.executeQuery()) {
098    
099                                            while (rs.next()) {
100                                                    long classPK = rs.getLong("resourcePrimKey");
101    
102                                                    ps2.setBoolean(1, false);
103                                                    ps2.setLong(2, classNameId);
104                                                    ps2.setLong(3, classPK);
105    
106                                                    ps2.addBatch();
107                                            }
108    
109                                            ps2.executeBatch();
110                                    }
111                            }
112                    }
113            }
114    
115            protected void updateAssetVocabularies() throws Exception {
116                    try (LoggingTimer loggingTimer = new LoggingTimer();
117                            PreparedStatement ps1 = connection.prepareStatement(
118                                    "select vocabularyId, settings_ from AssetVocabulary");
119                            PreparedStatement ps2 =
120                                    AutoBatchPreparedStatementUtil.concurrentAutoBatch(
121                                            connection,
122                                            "update AssetVocabulary set settings_ = ? where " +
123                                                    "vocabularyId = ?");
124                            ResultSet rs = ps1.executeQuery()) {
125    
126                            while (rs.next()) {
127                                    long vocabularyId = rs.getLong("vocabularyId");
128                                    String settings = rs.getString("settings_");
129    
130                                    ps2.setString(1, upgradeVocabularySettings(settings));
131                                    ps2.setLong(2, vocabularyId);
132    
133                                    ps2.addBatch();
134                            }
135    
136                            ps2.executeBatch();
137                    }
138            }
139    
140            /**
141             * @deprecated As of 7.0.0, with no direct replacement
142             */
143            @Deprecated
144            protected void updateAssetVocabulary(long vocabularyId, String settings)
145                    throws Exception {
146    
147                    try (PreparedStatement ps = connection.prepareStatement(
148                                    "update AssetVocabulary set settings_ = ? where vocabularyId " +
149                                            "= ?")) {
150    
151                            ps.setString(1, settings);
152                            ps.setLong(2, vocabularyId);
153    
154                            ps.executeUpdate();
155                    }
156                    catch (Exception e) {
157                            if (_log.isWarnEnabled()) {
158                                    _log.warn("Unable to update vocabulary " + vocabularyId, e);
159                            }
160                    }
161            }
162    
163            protected String upgradeVocabularySettings(String settings) {
164                    UnicodeProperties properties = new UnicodeProperties(true);
165    
166                    properties.fastLoad(settings);
167    
168                    AssetVocabularySettingsHelper vocabularySettingsHelper =
169                            new AssetVocabularySettingsHelper();
170    
171                    vocabularySettingsHelper.setMultiValued(
172                            GetterUtil.getBoolean(properties.getProperty("multiValued"), true));
173    
174                    long[] classNameIds = StringUtil.split(
175                            properties.getProperty("selectedClassNameIds"), 0L);
176    
177                    long[] classTypePKs = new long[classNameIds.length];
178    
179                    Arrays.fill(classTypePKs, AssetCategoryConstants.ALL_CLASS_TYPE_PK);
180    
181                    long[] requiredClassNameIds = StringUtil.split(
182                            properties.getProperty("requiredClassNameIds"), 0L);
183    
184                    boolean[] requireds = new boolean[classNameIds.length];
185    
186                    for (int i = 0; i < classNameIds.length; i++) {
187                            requireds[i] = ArrayUtil.contains(
188                                    requiredClassNameIds, classNameIds[i]);
189                    }
190    
191                    vocabularySettingsHelper.setClassNameIdsAndClassTypePKs(
192                            classNameIds, classTypePKs, requireds);
193    
194                    return vocabularySettingsHelper.toString();
195            }
196    
197            private static final Log _log = LogFactoryUtil.getLog(UpgradeAsset.class);
198    
199    }