001    /**
002     * Copyright (c) 2000-2013 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.v6_2_0;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.model.CustomizedPages;
024    import com.liferay.portal.model.LayoutTypePortletConstants;
025    import com.liferay.portal.model.PortletConstants;
026    import com.liferay.portal.model.PortletPreferences;
027    import com.liferay.portal.model.impl.PortletPreferencesImpl;
028    import com.liferay.portal.util.PortletKeys;
029    import com.liferay.portlet.PortalPreferencesImpl;
030    import com.liferay.portlet.PortalPreferencesWrapper;
031    import com.liferay.portlet.PortletPreferencesFactoryUtil;
032    
033    import java.sql.Connection;
034    import java.sql.PreparedStatement;
035    import java.sql.ResultSet;
036    
037    import java.util.ArrayList;
038    import java.util.List;
039    
040    /**
041     * @author Raymond Aug??
042     */
043    public class UpgradeCustomizablePortlets extends UpgradeProcess {
044    
045            @Override
046            protected void doUpgrade() throws Exception {
047                    Connection con = null;
048                    PreparedStatement ps = null;
049                    ResultSet rs = null;
050    
051                    try {
052                            con = DataAccess.getUpgradeOptimizedConnection();
053    
054                            ps = con.prepareStatement(
055                                    "select ownerId, ownerType, preferences from " +
056                                            "PortalPreferences where preferences like " +
057                                                    "'%com.liferay.portal.model.CustomizedPages%'");
058    
059                            rs = ps.executeQuery();
060    
061                            while (rs.next()) {
062                                    long ownerId = rs.getLong("ownerId");
063                                    int ownerType = rs.getInt("ownerType");
064                                    String preferences = rs.getString("preferences");
065    
066                                    PortalPreferencesWrapper portalPreferencesWrapper =
067                                            getPortalPreferencesInstance(
068                                                    ownerId, ownerType, preferences);
069    
070                                    upgradeCustomizablePreferences(
071                                            portalPreferencesWrapper, ownerId, ownerType, preferences);
072    
073                                    portalPreferencesWrapper.store();
074                            }
075                    }
076                    finally {
077                            DataAccess.cleanUp(con, ps, rs);
078                    }
079            }
080    
081            protected PortalPreferencesWrapper getPortalPreferencesInstance(
082                            long ownerId, int ownerType, String xml)
083                    throws SystemException {
084    
085                    PortalPreferencesImpl portalPreferencesImpl =
086                            (PortalPreferencesImpl)PortletPreferencesFactoryUtil.fromXML(
087                                    ownerId, ownerType, xml);
088    
089                    return new PortalPreferencesWrapper(portalPreferencesImpl);
090            }
091    
092            protected PortletPreferences getPortletPreferences(
093                            long ownerId, int ownerType, long plid, String portletId)
094                    throws Exception {
095    
096                    Connection con = null;
097                    PreparedStatement ps = null;
098                    ResultSet rs = null;
099    
100                    try {
101                            con = DataAccess.getUpgradeOptimizedConnection();
102    
103                            ps = con.prepareStatement(
104                                    "select portletPreferencesId, ownerId, ownerType, plid, " +
105                                            "portletId, preferences from PortletPreferences where " +
106                                                    "ownerId = ?, ownerType = ?, plid = ?, portletId = ?");
107    
108                            ps.setLong(1, ownerId);
109                            ps.setInt(2, ownerType);
110                            ps.setLong(3, plid);
111                            ps.setString(4, portletId);
112    
113                            rs = ps.executeQuery();
114    
115                            if (!rs.next()) {
116                                    return null;
117                            }
118    
119                            PortletPreferences portletPreferences =
120                                    new PortletPreferencesImpl();
121    
122                            portletPreferences.setPortletPreferencesId(
123                                    rs.getLong("portletPreferencesId"));
124                            portletPreferences.setOwnerId(rs.getLong("ownerId"));
125                            portletPreferences.setOwnerType(rs.getInt("ownerType"));
126                            portletPreferences.setPlid(rs.getLong("plid"));
127                            portletPreferences.setPortletId(rs.getString("portletId"));
128                            portletPreferences.setPreferences(rs.getString("preferences"));
129    
130                            return portletPreferences;
131                    }
132                    finally {
133                            DataAccess.cleanUp(con, ps, rs);
134                    }
135            }
136    
137            protected String migratePortletPreferencesToUserPreferences(
138                            long userId, long plid, String portletId)
139                    throws Exception {
140    
141                    if (!PortletConstants.hasInstanceId(portletId)) {
142                            return portletId;
143                    }
144    
145                    String instanceId = PortletConstants.getInstanceId(portletId);
146    
147                    String newPortletId = PortletConstants.assemblePortletId(
148                            portletId, userId, instanceId);
149    
150                    updatePortletPreferences(userId, plid, portletId, newPortletId);
151    
152                    return newPortletId;
153            }
154    
155            protected void updatePortletPreferences(
156                            long userId, long plid, String portletId, String newPortletId)
157                    throws Exception {
158    
159                    Connection con = null;
160                    PreparedStatement ps = null;
161                    ResultSet rs = null;
162    
163                    try {
164                            con = DataAccess.getUpgradeOptimizedConnection();
165    
166                            ps = con.prepareStatement(
167                                    "update PortletPreferences set ownerId = ?, ownerType = ?, " +
168                                            "plid = ?, portletId = ? where ownerId = ? and " +
169                                                    "ownerType = ? and plid = ? and portletId = ?");
170    
171                            ps.setLong(1, userId);
172                            ps.setInt(2, PortletKeys.PREFS_OWNER_TYPE_USER);
173                            ps.setLong(3, plid);
174                            ps.setString(4, newPortletId);
175                            ps.setLong(5, 0L);
176                            ps.setInt(6, PortletKeys.PREFS_OWNER_TYPE_LAYOUT);
177                            ps.setLong(7, plid);
178                            ps.setString(8, portletId);
179    
180                            ps.executeUpdate();
181                    }
182                    finally {
183                            DataAccess.cleanUp(con, ps, rs);
184                    }
185            }
186    
187            protected void upgradeCustomizablePreferences(
188                            PortalPreferencesWrapper portalPreferencesWrapper, long ownerId,
189                            int ownerType, String preferences)
190                    throws Exception {
191    
192                    PortalPreferencesImpl portalPreferencesImpl =
193                            portalPreferencesWrapper.getPortalPreferencesImpl();
194    
195                    int x = preferences.indexOf(_PREFIX);
196                    int y = -1;
197    
198                    if (x != -1) {
199                            x += _PREFIX.length();
200                            y = preferences.indexOf(_SUFFIX, x);
201                    }
202                    else {
203                            return;
204                    }
205    
206                    while (x != -1) {
207    
208                            // <name>com.liferay.portal.model.CustomizedPages10415#column-1
209                            // </name>
210    
211                            String[] parts = StringUtil.split(
212                                    preferences.substring(x, y), StringPool.POUND);
213    
214                            long plid = GetterUtil.getLong(parts[0]);
215                            String key = GetterUtil.getString(parts[1]);
216    
217                            if (key.startsWith(LayoutTypePortletConstants.COLUMN_PREFIX)) {
218                                    String value = portalPreferencesImpl.getValue(
219                                            CustomizedPages.namespacePlid(plid), key);
220    
221                                    List<String> newPortletIds = new ArrayList<String>();
222    
223                                    for (String customPortletId : StringUtil.split(value)) {
224                                            String newPortletId =
225                                                    migratePortletPreferencesToUserPreferences(
226                                                            ownerId, plid, customPortletId);
227    
228                                            newPortletIds.add(newPortletId);
229                                    }
230    
231                                    value = StringUtil.merge(newPortletIds);
232    
233                                    portalPreferencesImpl.setValue(
234                                            CustomizedPages.namespacePlid(plid), key, value);
235                            }
236    
237                            x = preferences.indexOf(_PREFIX, y);
238                            y = -1;
239    
240                            if (x != -1) {
241                                    x += _PREFIX.length();
242                                    y = preferences.indexOf(_SUFFIX, x);
243                            }
244                    }
245            }
246    
247            private static final String _PREFIX =
248                    "<name>com.liferay.portal.model.CustomizedPages";
249    
250            private static final String _SUFFIX = "</name>";
251    
252    }