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.upgrade.BaseUpgradePortletPreferences;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portlet.PortletPreferencesFactoryUtil;
023    import com.liferay.portlet.portletdisplaytemplate.util.PortletDisplayTemplate;
024    
025    import java.sql.Connection;
026    import java.sql.PreparedStatement;
027    import java.sql.ResultSet;
028    
029    import javax.portlet.PortletPreferences;
030    
031    /**
032     * @author Eduardo Garcia
033     */
034    public class UpgradePortletDisplayTemplatePreferences
035            extends BaseUpgradePortletPreferences {
036    
037            protected String getTemplateKey(
038                            long displayStyleGroupId, String displayStyle)
039                    throws Exception {
040    
041                    String uuid = displayStyle.substring(DISPLAY_STYLE_PREFIX_6_2.length());
042    
043                    Connection con = null;
044                    PreparedStatement ps = null;
045                    ResultSet rs = null;
046    
047                    try {
048                            con = DataAccess.getUpgradeOptimizedConnection();
049    
050                            ps = con.prepareStatement(
051                                    "select templateKey from DDMTemplate where groupId = ?" +
052                                            " and uuid_ = ?");
053    
054                            ps.setLong(1, displayStyleGroupId);
055                            ps.setString(2, uuid);
056    
057                            rs = ps.executeQuery();
058    
059                            while (rs.next()) {
060                                    return rs.getString("templateKey");
061                            }
062    
063                            return null;
064                    }
065                    finally {
066                            DataAccess.cleanUp(con, ps, rs);
067                    }
068            }
069    
070            @Override
071            protected String getUpdatePortletPreferencesWhereClause() {
072                    StringBundler sb = new StringBundler(5);
073    
074                    sb.append("(preferences like '%");
075                    sb.append(DISPLAY_STYLE_PREFIX_6_2);
076                    sb.append("%')");
077    
078                    return sb.toString();
079            }
080    
081            protected void upgradeDisplayStyle(PortletPreferences portletPreferences)
082                    throws Exception {
083    
084                    String displayStyle = GetterUtil.getString(
085                            portletPreferences.getValue("displayStyle", null));
086    
087                    if (Validator.isNull(displayStyle) ||
088                            !displayStyle.startsWith(DISPLAY_STYLE_PREFIX_6_2)) {
089    
090                            return;
091                    }
092    
093                    long displayStyleGroupId = GetterUtil.getLong(
094                            portletPreferences.getValue("displayStyleGroupId", null));
095    
096                    String templateKey = getTemplateKey(displayStyleGroupId, displayStyle);
097    
098                    if (templateKey != null) {
099                            portletPreferences.setValue(
100                                    "displayStyle",
101                                    PortletDisplayTemplate.DISPLAY_STYLE_PREFIX + templateKey);
102                    }
103            }
104    
105            @Override
106            protected String upgradePreferences(
107                            long companyId, long ownerId, int ownerType, long plid,
108                            String portletId, String xml)
109                    throws Exception {
110    
111                    PortletPreferences portletPreferences =
112                            PortletPreferencesFactoryUtil.fromXML(
113                                    companyId, ownerId, ownerType, plid, portletId, xml);
114    
115                    upgradeDisplayStyle(portletPreferences);
116    
117                    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
118            }
119    
120            protected static final String DISPLAY_STYLE_PREFIX_6_2 = "ddmTemplate_";
121    
122    }