001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.util.GetterUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.upgrade.BaseUpgradePortletPreferences;
021    import com.liferay.portlet.PortletPreferencesImpl;
022    import com.liferay.portlet.PortletPreferencesSerializer;
023    
024    import java.sql.Connection;
025    import java.sql.PreparedStatement;
026    import java.sql.ResultSet;
027    
028    /**
029     * @author Raymond Augé
030     */
031    public class UpgradeRSS extends BaseUpgradePortletPreferences {
032    
033            protected String[] getArticleValues(long resourcePrimKey) {
034                    long groupId = 0;
035                    String articleId = StringPool.BLANK;
036    
037                    Connection con = null;
038                    PreparedStatement ps = null;
039                    ResultSet rs = null;
040    
041                    try {
042                            con = DataAccess.getConnection();
043    
044                            ps = con.prepareStatement(
045                                    "select groupId, articleId from JournalArticle where " +
046                                            "resourcePrimKey = ?");
047    
048                            ps.setLong(1, resourcePrimKey);
049    
050                            rs = ps.executeQuery();
051    
052                            rs.next();
053    
054                            groupId = rs.getLong("groupId");
055                            articleId = rs.getString("articleId");
056                    }
057                    catch (Exception e) {
058                    }
059                    finally {
060                            DataAccess.cleanUp(con, ps, rs);
061                    }
062    
063                    return new String[] {String.valueOf(groupId), articleId};
064            }
065    
066            protected String getUpdatePortletPreferencesWhereClause() {
067                    return "portletId like '39_INSTANCE_%'";
068            }
069    
070            protected void upgradeFooterValues(PortletPreferencesImpl preferences)
071                    throws Exception {
072    
073                    String[] footerArticleResouceValues = preferences.getValues(
074                            "footer-article-resource-values", new String[] {"0", ""});
075    
076                    long footerArticleResourcePrimKey = GetterUtil.getLong(
077                            footerArticleResouceValues[0]);
078    
079                    String[] values = getArticleValues(footerArticleResourcePrimKey);
080    
081                    preferences.setValues("footer-article-values", values);
082                    preferences.reset("footer-article-resource-values");
083            }
084    
085            protected void upgradeHeaderValues(PortletPreferencesImpl preferences)
086                    throws Exception {
087    
088                    String[] headerArticleResouceValues = preferences.getValues(
089                            "header-article-resource-values", new String[] {"0", ""});
090    
091                    long headerArticleResourcePrimKey = GetterUtil.getLong(
092                            headerArticleResouceValues[0]);
093    
094                    String[] values = getArticleValues(headerArticleResourcePrimKey);
095    
096                    preferences.setValues("header-article-values", values);
097                    preferences.reset("header-article-resource-values");
098            }
099    
100            protected String upgradePreferences(
101                            long companyId, long ownerId, int ownerType, long plid,
102                            String portletId, String xml)
103                    throws Exception {
104    
105                    PortletPreferencesImpl preferences =
106                            PortletPreferencesSerializer.fromXML(
107                                    companyId, ownerId, ownerType, plid, portletId, xml);
108    
109                    upgradeFooterValues(preferences);
110                    upgradeHeaderValues(preferences);
111    
112                    return PortletPreferencesSerializer.toXML(preferences);
113            }
114    
115    }