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.v6_0_6;
016    
017    import com.liferay.portal.kernel.portlet.PortletPreferencesFactoryUtil;
018    import com.liferay.portal.kernel.upgrade.BaseUpgradePortletPreferences;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    
022    import java.sql.PreparedStatement;
023    import java.sql.ResultSet;
024    
025    import javax.portlet.PortletPreferences;
026    
027    /**
028     * @author Raymond Aug??
029     */
030    public class UpgradeRSS extends BaseUpgradePortletPreferences {
031    
032            protected String[] getArticleValues(long resourcePrimKey) {
033                    long groupId = 0;
034                    String articleId = StringPool.BLANK;
035    
036                    try (PreparedStatement ps = connection.prepareStatement(
037                                    "select groupId, articleId from JournalArticle where " +
038                                            "resourcePrimKey = ?")) {
039    
040                            ps.setLong(1, resourcePrimKey);
041    
042                            try (ResultSet rs = ps.executeQuery()) {
043                                    rs.next();
044    
045                                    groupId = rs.getLong("groupId");
046                                    articleId = rs.getString("articleId");
047                            }
048                    }
049                    catch (Exception e) {
050                    }
051    
052                    return new String[] {String.valueOf(groupId), articleId};
053            }
054    
055            @Override
056            protected String[] getPortletIds() {
057                    return new String[] {"39_INSTANCE_%"};
058            }
059    
060            protected void updateFooterValues(PortletPreferences portletPreferences)
061                    throws Exception {
062    
063                    String[] footerArticleResouceValues = portletPreferences.getValues(
064                            "footer-article-resource-values", new String[] {"0", ""});
065    
066                    long footerArticleResourcePrimKey = GetterUtil.getLong(
067                            footerArticleResouceValues[0]);
068    
069                    String[] values = getArticleValues(footerArticleResourcePrimKey);
070    
071                    portletPreferences.setValues("footer-article-values", values);
072                    portletPreferences.reset("footer-article-resource-values");
073            }
074    
075            protected void updateHeaderValues(PortletPreferences portletPreferences)
076                    throws Exception {
077    
078                    String[] headerArticleResouceValues = portletPreferences.getValues(
079                            "header-article-resource-values", new String[] {"0", ""});
080    
081                    long headerArticleResourcePrimKey = GetterUtil.getLong(
082                            headerArticleResouceValues[0]);
083    
084                    String[] values = getArticleValues(headerArticleResourcePrimKey);
085    
086                    portletPreferences.setValues("header-article-values", values);
087                    portletPreferences.reset("header-article-resource-values");
088            }
089    
090            @Override
091            protected String upgradePreferences(
092                            long companyId, long ownerId, int ownerType, long plid,
093                            String portletId, String xml)
094                    throws Exception {
095    
096                    PortletPreferences portletPreferences =
097                            PortletPreferencesFactoryUtil.fromXML(
098                                    companyId, ownerId, ownerType, plid, portletId, xml);
099    
100                    updateFooterValues(portletPreferences);
101                    updateHeaderValues(portletPreferences);
102    
103                    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
104            }
105    
106    }