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