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