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