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