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.DataAccess;
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.PortalUtil;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.UnicodeProperties;
028    import com.liferay.portal.upgrade.v7_0_0.util.AssetEntryTable;
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.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("?) 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                            ps.setBoolean(1, false);
130    
131                            rs = ps.executeQuery();
132    
133                            while (rs.next()) {
134                                    long classPK = rs.getLong("resourcePrimKey");
135    
136                                    runSQL(
137                                            "update AssetEntry set listable = [$FALSE$] where " +
138                                                    "classNameId = " + classNameId + " and classPK = " +
139                                                            classPK);
140                            }
141                    }
142                    finally {
143                            DataAccess.cleanUp(ps, rs);
144                    }
145            }
146    
147            protected void updateAssetVocabularies() throws Exception {
148                    PreparedStatement statement = null;
149                    ResultSet result = null;
150    
151                    try {
152                            statement = connection.prepareStatement(
153                                    "select vocabularyId, settings_ from AssetVocabulary");
154    
155                            result = statement.executeQuery();
156    
157                            while (result.next()) {
158                                    long vocabularyId = result.getLong("vocabularyId");
159                                    String settings = result.getString("settings_");
160    
161                                    updateAssetVocabulary(
162                                            vocabularyId, upgradeVocabularySettings(settings));
163                            }
164                    }
165                    finally {
166                            DataAccess.cleanUp(statement, result);
167                    }
168            }
169    
170            protected void updateAssetVocabulary(long vocabularyId, String settings)
171                    throws Exception {
172    
173                    PreparedStatement ps = null;
174                    ResultSet rs = null;
175    
176                    try {
177                            ps = connection.prepareStatement(
178                                    "update AssetVocabulary set settings_ = ? where vocabularyId " +
179                                            "= ?");
180    
181                            ps.setString(1, settings);
182                            ps.setLong(2, vocabularyId);
183    
184                            ps.executeUpdate();
185                    }
186                    catch (Exception e) {
187                            if (_log.isWarnEnabled()) {
188                                    _log.warn("Unable to update vocabulary " + vocabularyId, e);
189                            }
190                    }
191                    finally {
192                            DataAccess.cleanUp(ps, rs);
193                    }
194            }
195    
196            protected String upgradeVocabularySettings(String settings) {
197                    UnicodeProperties properties = new UnicodeProperties(true);
198    
199                    properties.fastLoad(settings);
200    
201                    AssetVocabularySettingsHelper vocabularySettingsHelper =
202                            new AssetVocabularySettingsHelper();
203    
204                    vocabularySettingsHelper.setMultiValued(
205                            GetterUtil.getBoolean(properties.getProperty("multiValued"), true));
206    
207                    long[] classNameIds = StringUtil.split(
208                            properties.getProperty("selectedClassNameIds"), 0L);
209    
210                    long[] classTypePKs = new long[classNameIds.length];
211    
212                    Arrays.fill(classTypePKs, AssetCategoryConstants.ALL_CLASS_TYPE_PK);
213    
214                    long[] requiredClassNameIds = StringUtil.split(
215                            properties.getProperty("requiredClassNameIds"), 0L);
216    
217                    boolean[] requireds = new boolean[classNameIds.length];
218    
219                    for (int i = 0; i < classNameIds.length; i++) {
220                            requireds[i] = ArrayUtil.contains(
221                                    requiredClassNameIds, classNameIds[i]);
222                    }
223    
224                    vocabularySettingsHelper.setClassNameIdsAndClassTypePKs(
225                            classNameIds, classTypePKs, requireds);
226    
227                    return vocabularySettingsHelper.toString();
228            }
229    
230            private static final Log _log = LogFactoryUtil.getLog(UpgradeAsset.class);
231    
232    }