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