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.util.GetterUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Company;
025    import com.liferay.portal.model.PortletConstants;
026    import com.liferay.portal.upgrade.util.UpgradePortletId;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.PortletKeys;
029    import com.liferay.portlet.PortletPreferencesFactoryUtil;
030    import com.liferay.portlet.PortletPreferencesImpl;
031    import com.liferay.portlet.journal.model.JournalArticle;
032    
033    import java.sql.Connection;
034    import java.sql.PreparedStatement;
035    import java.sql.ResultSet;
036    import java.sql.SQLException;
037    
038    import javax.portlet.PortletPreferences;
039    
040    /**
041     * @author Eudaldo Alonso
042     */
043    public class UpgradeJournalArticles extends UpgradePortletId {
044    
045            protected long getCategoryId(long companyId, String type) throws Exception {
046                    if (Validator.isNull(type)) {
047                            return 0;
048                    }
049    
050                    long groupId = getCompanyGroupId(companyId);
051    
052                    Connection con = null;
053                    PreparedStatement ps = null;
054                    ResultSet rs = null;
055    
056                    try {
057                            con = DataAccess.getUpgradeOptimizedConnection();
058    
059                            ps = con.prepareStatement(
060                                    "select categoryId from AssetCategory where groupId = " +
061                                            groupId + " and name = ?");
062    
063                            ps.setString(1, type);
064    
065                            rs = ps.executeQuery();
066    
067                            while (rs.next()) {
068                                    return rs.getLong("categoryId");
069                            }
070    
071                            return 0;
072                    }
073                    finally {
074                            DataAccess.cleanUp(con, ps, rs);
075                    }
076            }
077    
078            protected long getCompanyGroupId(long companyId) throws Exception {
079                    Connection con = null;
080                    PreparedStatement ps = null;
081                    ResultSet rs = null;
082    
083                    try {
084                            con = DataAccess.getUpgradeOptimizedConnection();
085    
086                            ps = con.prepareStatement(
087                                    "select groupId from Group_ where classNameId = ? and " +
088                                            "classPK = ?");
089    
090                            ps.setLong(1, PortalUtil.getClassNameId(Company.class.getName()));
091                            ps.setLong(2, companyId);
092    
093                            rs = ps.executeQuery();
094    
095                            if (rs.next()) {
096                                    return rs.getLong("groupId");
097                            }
098    
099                            return 0;
100                    }
101                    finally {
102                            DataAccess.cleanUp(con, ps, rs);
103                    }
104            }
105    
106            protected long getCompanyId(long plid) throws Exception {
107                    Connection con = null;
108                    PreparedStatement ps = null;
109                    ResultSet rs = null;
110    
111                    try {
112                            con = DataAccess.getUpgradeOptimizedConnection();
113    
114                            ps = con.prepareStatement(
115                                    "select companyId from Layout where plid = " + plid);
116    
117                            rs = ps.executeQuery();
118    
119                            while (rs.next()) {
120                                    return rs.getLong("companyId");
121                            }
122    
123                            return 0;
124                    }
125                    finally {
126                            DataAccess.cleanUp(con, ps, rs);
127                    }
128            }
129    
130            protected long getGroupId(long plid) throws Exception {
131                    Connection con = null;
132                    PreparedStatement ps = null;
133                    ResultSet rs = null;
134    
135                    try {
136                            con = DataAccess.getUpgradeOptimizedConnection();
137    
138                            ps = con.prepareStatement(
139                                    "select groupId from Layout where plid = " + plid);
140    
141                            rs = ps.executeQuery();
142    
143                            while (rs.next()) {
144                                    return rs.getLong("groupId");
145                            }
146    
147                            return 0;
148                    }
149                    finally {
150                            DataAccess.cleanUp(con, ps, rs);
151                    }
152            }
153    
154            protected String getNewPreferences(long plid, String preferences)
155                    throws Exception {
156    
157                    PortletPreferences oldPortletPreferences =
158                            PortletPreferencesFactoryUtil.fromDefaultXML(preferences);
159    
160                    String ddmStructureKey = oldPortletPreferences.getValue(
161                            "ddmStructureKey", StringPool.BLANK);
162                    long groupId = GetterUtil.getLong(
163                            oldPortletPreferences.getValue("groupId", StringPool.BLANK));
164                    String orderByCol = oldPortletPreferences.getValue(
165                            "orderByCol", StringPool.BLANK);
166                    String orderByType = oldPortletPreferences.getValue(
167                            "orderByType", StringPool.BLANK);
168                    int pageDelta = GetterUtil.getInteger(
169                            oldPortletPreferences.getValue("pageDelta", StringPool.BLANK));
170                    String pageUrl = oldPortletPreferences.getValue(
171                            "pageUrl", StringPool.BLANK);
172                    String type = oldPortletPreferences.getValue("type", StringPool.BLANK);
173    
174                    PortletPreferences newPortletPreferences = new PortletPreferencesImpl();
175    
176                    newPortletPreferences.setValue(
177                            "anyAssetType",
178                            String.valueOf(PortalUtil.getClassNameId(JournalArticle.class)));
179    
180                    long companyId = getCompanyId(plid);
181    
182                    long structureId = getStructureId(companyId, plid, ddmStructureKey);
183    
184                    if (structureId > 0) {
185                            newPortletPreferences.setValue(
186                                    "anyClassTypeJournalArticleAssetRendererFactory",
187                                    String.valueOf(structureId));
188                    }
189    
190                    String assetLinkBehavior = "showFullContent";
191    
192                    if (pageUrl.equals("viewInContext")) {
193                            assetLinkBehavior = "viewInPortlet";
194                    }
195    
196                    newPortletPreferences.setValue("assetLinkBehavior", assetLinkBehavior);
197    
198                    if (structureId > 0) {
199                            newPortletPreferences.setValue(
200                                    "classTypeIds", String.valueOf(structureId));
201                    }
202    
203                    newPortletPreferences.setValue("delta", String.valueOf(pageDelta));
204                    newPortletPreferences.setValue("displayStyle", "table");
205                    newPortletPreferences.setValue("metadataFields", "publish-date,author");
206                    newPortletPreferences.setValue("orderByColumn1", orderByCol);
207                    newPortletPreferences.setValue("orderByType1", orderByType);
208                    newPortletPreferences.setValue("paginationType", "none");
209    
210                    long categoryId = getCategoryId(companyId, type);
211    
212                    if (categoryId > 0) {
213                            newPortletPreferences.setValue(
214                                    "queryAndOperator0", Boolean.TRUE.toString());
215                            newPortletPreferences.setValue(
216                                    "queryContains0", Boolean.TRUE.toString());
217                            newPortletPreferences.setValue("queryName0", "assetCategories");
218                            newPortletPreferences.setValue(
219                                    "queryValues0", String.valueOf(categoryId));
220                    }
221    
222                    newPortletPreferences.setValue(
223                            "showAddContentButton", Boolean.FALSE.toString());
224    
225                    String groupName = String.valueOf(groupId);
226    
227                    if (groupId == getGroupId(plid)) {
228                            groupName = "default";
229                    }
230    
231                    newPortletPreferences.setValue("scopeIds", "Group_" + groupName);
232    
233                    return PortletPreferencesFactoryUtil.toXML(newPortletPreferences);
234            }
235    
236            @Override
237            protected String[][] getRenamePortletIdsArray() {
238                    return new String[][] {
239                            new String[] {
240                                    _PORTLET_ID_JOURNAL_CONTENT_LIST, PortletKeys.ASSET_PUBLISHER
241                            }
242                    };
243            }
244    
245            protected long getStructureId(
246                            long companyId, long plid, String ddmStructureKey)
247                    throws Exception {
248    
249                    if (Validator.isNull(ddmStructureKey)) {
250                            return 0;
251                    }
252    
253                    long groupId = getGroupId(plid);
254                    long companyGroupId = getCompanyGroupId(companyId);
255    
256                    Connection con = null;
257                    PreparedStatement ps = null;
258                    ResultSet rs = null;
259    
260                    try {
261                            con = DataAccess.getUpgradeOptimizedConnection();
262    
263                            ps = con.prepareStatement(
264                                    "select structureId from DDMStructure where (groupId = " +
265                                            groupId + " or groupId = " + companyGroupId + ") and " +
266                                                    "structureKey = ?");
267    
268                            ps.setString(1, ddmStructureKey);
269    
270                            rs = ps.executeQuery();
271    
272                            while (rs.next()) {
273                                    return rs.getLong("structureId");
274                            }
275    
276                            return 0;
277                    }
278                    finally {
279                            DataAccess.cleanUp(con, ps, rs);
280                    }
281            }
282    
283            @Override
284            protected void updateInstanceablePortletPreferences(
285                            String oldRootPortletId, String newRootPortletId)
286                    throws Exception {
287    
288                    Connection con = null;
289                    PreparedStatement ps = null;
290                    ResultSet rs = null;
291    
292                    try {
293                            con = DataAccess.getUpgradeOptimizedConnection();
294    
295                            StringBundler sb = new StringBundler(9);
296    
297                            sb.append("select portletPreferencesId, plid, portletId, ");
298                            sb.append("preferences from PortletPreferences where portletId ");
299                            sb.append("= '");
300                            sb.append(oldRootPortletId);
301                            sb.append("' OR portletId like '");
302                            sb.append(oldRootPortletId);
303                            sb.append("_INSTANCE_%' OR portletId like '");
304                            sb.append(oldRootPortletId);
305                            sb.append("_USER_%_INSTANCE_%'");
306    
307                            ps = con.prepareStatement(sb.toString());
308    
309                            rs = ps.executeQuery();
310    
311                            while (rs.next()) {
312                                    long portletPreferencesId = rs.getLong("portletPreferencesId");
313                                    long plid = rs.getLong("plid");
314                                    String portletId = rs.getString("portletId");
315                                    String preferences = rs.getString("preferences");
316    
317                                    if (preferences.equals("<portlet-preferences />")) {
318                                            continue;
319                                    }
320    
321                                    String newPreferences = getNewPreferences(plid, preferences);
322    
323                                    long userId = PortletConstants.getUserId(portletId);
324                                    String instanceId = PortletConstants.getInstanceId(portletId);
325    
326                                    String newPortletId = PortletConstants.assemblePortletId(
327                                            PortletKeys.ASSET_PUBLISHER, userId, instanceId);
328    
329                                    updatePortletPreference(
330                                            portletPreferencesId, newPortletId, newPreferences);
331                            }
332                    }
333                    finally {
334                            DataAccess.cleanUp(con, ps, rs);
335                    }
336            }
337    
338            @Override
339            protected void updatePortlet(
340                            String oldRootPortletId, String newRootPortletId)
341                    throws Exception {
342    
343                    try {
344                            updateResourcePermission(oldRootPortletId, newRootPortletId, true);
345    
346                            updateInstanceablePortletPreferences(
347                                    oldRootPortletId, newRootPortletId);
348                    }
349                    catch (Exception e) {
350                            if (_log.isWarnEnabled()) {
351                                    _log.warn(e, e);
352                            }
353                    }
354            }
355    
356            protected void updatePortletPreference(
357                            long portletPreferencesId, String newPortletId,
358                            String newPreferences)
359                    throws Exception {
360    
361                    Connection con = null;
362                    PreparedStatement ps = null;
363    
364                    try {
365                            con = DataAccess.getUpgradeOptimizedConnection();
366    
367                            ps = con.prepareStatement(
368                                    "update PortletPreferences set preferences = ?, " +
369                                            "portletId = ? where portletPreferencesId = " +
370                                                    portletPreferencesId);
371    
372                            ps.setString(1, newPreferences);
373                            ps.setString(2, newPortletId);
374    
375                            ps.executeUpdate();
376                    }
377                    catch (SQLException sqle) {
378                            if (_log.isWarnEnabled()) {
379                                    _log.warn(sqle, sqle);
380                            }
381                    }
382                    finally {
383                            DataAccess.cleanUp(con, ps);
384                    }
385            }
386    
387            private static final String _PORTLET_ID_JOURNAL_CONTENT_LIST = "62";
388    
389            private static final Log _log = LogFactoryUtil.getLog(
390                    UpgradeJournalArticles.class);
391    
392    }