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.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
021    import com.liferay.portal.kernel.util.ArrayUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.UnicodeProperties;
026    import com.liferay.portal.upgrade.v7_0_0.util.AssetEntryTable;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portlet.asset.model.AssetCategoryConstants;
029    import com.liferay.portlet.asset.util.AssetVocabularySettingsHelper;
030    
031    import java.sql.PreparedStatement;
032    import java.sql.ResultSet;
033    import java.sql.SQLException;
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                    try {
045                            runSQL("alter_column_type AssetEntry description TEXT null");
046                            runSQL("alter_column_type AssetEntry summary TEXT null");
047                    }
048                    catch (SQLException sqle) {
049                            upgradeTable(
050                                    AssetEntryTable.TABLE_NAME, AssetEntryTable.TABLE_COLUMNS,
051                                    AssetEntryTable.TABLE_SQL_CREATE,
052                                    AssetEntryTable.TABLE_SQL_ADD_INDEXES);
053                    }
054    
055                    updateAssetEntries();
056                    updateAssetVocabularies();
057            }
058    
059            protected long getDDMStructureId(String structureKey) throws Exception {
060                    PreparedStatement ps = null;
061                    ResultSet rs = null;
062    
063                    try {
064                            ps = connection.prepareStatement(
065                                    "select structureId from DDMStructure where structureKey = ?");
066    
067                            ps.setString(1, structureKey);
068    
069                            rs = ps.executeQuery();
070    
071                            if (rs.next()) {
072                                    return rs.getLong("structureId");
073                            }
074    
075                            return 0;
076                    }
077                    finally {
078                            DataAccess.cleanUp(ps, rs);
079                    }
080            }
081    
082            protected void updateAssetEntries() throws Exception {
083                    long classNameId = PortalUtil.getClassNameId(
084                            "com.liferay.portlet.journal.model.JournalArticle");
085    
086                    PreparedStatement ps = null;
087                    ResultSet rs = null;
088    
089                    try {
090                            connection = DataAccess.getUpgradeOptimizedConnection();
091    
092                            ps = connection.prepareStatement(
093                                    "select resourcePrimKey, structureId from JournalArticle " +
094                                            "where structureId != ''");
095    
096                            rs = ps.executeQuery();
097    
098                            while (rs.next()) {
099                                    long resourcePrimKey = rs.getLong("resourcePrimKey");
100                                    String structureId = rs.getString("structureId");
101    
102                                    long ddmStructureId = getDDMStructureId(structureId);
103    
104                                    runSQL(
105                                            "update AssetEntry set classTypeId = " + ddmStructureId +
106                                                    " where classNameId = " + classNameId +
107                                                            " and classPK = " + resourcePrimKey);
108                            }
109                    }
110                    finally {
111                            DataAccess.cleanUp(ps, rs);
112                    }
113    
114                    try {
115                            StringBundler sb = new StringBundler(9);
116    
117                            sb.append("select JournalArticle.resourcePrimKey from (select ");
118                            sb.append("JournalArticle.resourcePrimkey as primKey, ");
119                            sb.append("max(JournalArticle.version) as maxVersion from ");
120                            sb.append("JournalArticle group by ");
121                            sb.append("JournalArticle.resourcePrimkey) temp_table inner join ");
122                            sb.append("JournalArticle on (JournalArticle.indexable = ");
123                            sb.append("[$FALSE$]) and (JournalArticle.status = 0) and ");
124                            sb.append("(JournalArticle.resourcePrimkey = temp_table.primKey) ");
125                            sb.append("and (JournalArticle.version = temp_table.maxVersion)");
126    
127                            ps = connection.prepareStatement(sb.toString());
128    
129                            rs = ps.executeQuery();
130    
131                            while (rs.next()) {
132                                    long classPK = rs.getLong("resourcePrimKey");
133    
134                                    runSQL(
135                                            "update AssetEntry set listable = [$FALSE$] where " +
136                                                    "classNameId = " + classNameId + " and classPK = " +
137                                                            classPK);
138                            }
139                    }
140                    finally {
141                            DataAccess.cleanUp(ps, rs);
142                    }
143            }
144    
145            protected void updateAssetVocabularies() throws Exception {
146                    PreparedStatement statement = null;
147                    ResultSet result = null;
148    
149                    try {
150                            statement = connection.prepareStatement(
151                                    "select vocabularyId, settings_ from AssetVocabulary");
152    
153                            result = statement.executeQuery();
154    
155                            while (result.next()) {
156                                    long vocabularyId = result.getLong("vocabularyId");
157                                    String settings = result.getString("settings_");
158    
159                                    updateAssetVocabulary(
160                                            vocabularyId, upgradeVocabularySettings(settings));
161                            }
162                    }
163                    finally {
164                            DataAccess.cleanUp(statement, result);
165                    }
166            }
167    
168            protected void updateAssetVocabulary(long vocabularyId, String settings)
169                    throws Exception {
170    
171                    PreparedStatement ps = null;
172                    ResultSet rs = null;
173    
174                    try {
175                            ps = connection.prepareStatement(
176                                    "update AssetVocabulary set settings_ = ? where vocabularyId " +
177                                            "= ?");
178    
179                            ps.setString(1, settings);
180                            ps.setLong(2, vocabularyId);
181    
182                            ps.executeUpdate();
183                    }
184                    catch (Exception e) {
185                            if (_log.isWarnEnabled()) {
186                                    _log.warn("Unable to update vocabulary " + vocabularyId, e);
187                            }
188                    }
189                    finally {
190                            DataAccess.cleanUp(ps, rs);
191                    }
192            }
193    
194            protected String upgradeVocabularySettings(String settings) {
195                    UnicodeProperties properties = new UnicodeProperties(true);
196    
197                    properties.fastLoad(settings);
198    
199                    AssetVocabularySettingsHelper vocabularySettingsHelper =
200                            new AssetVocabularySettingsHelper();
201    
202                    vocabularySettingsHelper.setMultiValued(
203                            GetterUtil.getBoolean(properties.getProperty("multiValued"), true));
204    
205                    long[] classNameIds = StringUtil.split(
206                            properties.getProperty("selectedClassNameIds"), 0L);
207    
208                    long[] classTypePKs = new long[classNameIds.length];
209    
210                    Arrays.fill(classTypePKs, AssetCategoryConstants.ALL_CLASS_TYPE_PK);
211    
212                    long[] requiredClassNameIds = StringUtil.split(
213                            properties.getProperty("requiredClassNameIds"), 0L);
214    
215                    boolean[] requireds = new boolean[classNameIds.length];
216    
217                    for (int i = 0; i < classNameIds.length; i++) {
218                            requireds[i] = ArrayUtil.contains(
219                                    requiredClassNameIds, classNameIds[i]);
220                    }
221    
222                    vocabularySettingsHelper.setClassNameIdsAndClassTypePKs(
223                            classNameIds, classTypePKs, requireds);
224    
225                    return vocabularySettingsHelper.toString();
226            }
227    
228            private static final Log _log = LogFactoryUtil.getLog(UpgradeAsset.class);
229    
230    }