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