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