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                                    0, 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 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.setString(7, portletId);
178    
179                            ps.executeUpdate();
180                    }
181                    finally {
182                            DataAccess.cleanUp(con, ps, rs);
183                    }
184            }
185    
186            protected void upgradeCustomizablePreferences(
187                            PortalPreferencesWrapper portalPreferencesWrapper, long ownerId,
188                            int ownerType, String preferences)
189                    throws Exception {
190    
191                    PortalPreferencesImpl portalPreferencesImpl =
192                            portalPreferencesWrapper.getPortalPreferencesImpl();
193    
194                    int x = preferences.indexOf(_PREFIX);
195                    int y = -1;
196    
197                    if (x != -1) {
198                            x += _PREFIX.length();
199                            y = preferences.indexOf(_SUFFIX, x);
200                    }
201                    else {
202                            return;
203                    }
204    
205                    while (x != -1) {
206    
207                            // <name>com.liferay.portal.model.CustomizedPages10415#column-1
208                            // </name>
209    
210                            String[] parts = StringUtil.split(
211                                    preferences.substring(x, y), StringPool.POUND);
212    
213                            long plid = GetterUtil.getLong(parts[0]);
214                            String key = GetterUtil.getString(parts[1]);
215    
216                            if (key.startsWith(LayoutTypePortletConstants.COLUMN_PREFIX)) {
217                                    String value = portalPreferencesImpl.getValue(
218                                            CustomizedPages.namespacePlid(plid), key);
219    
220                                    List<String> newPortletIds = new ArrayList<String>();
221    
222                                    for (String customPortletId : StringUtil.split(value)) {
223                                            String newPortletId =
224                                                    migratePortletPreferencesToUserPreferences(
225                                                            ownerId, plid, customPortletId);
226    
227                                            newPortletIds.add(newPortletId);
228                                    }
229    
230                                    value = StringUtil.merge(newPortletIds);
231    
232                                    portalPreferencesImpl.setValue(
233                                            CustomizedPages.namespacePlid(plid), key, value);
234                            }
235    
236                            x = preferences.indexOf(_PREFIX, y);
237                            y = -1;
238    
239                            if (x != -1) {
240                                    x += _PREFIX.length();
241                                    y = preferences.indexOf(_SUFFIX, x);
242                            }
243                    }
244            }
245    
246            private static final String _PREFIX =
247                    "<name>com.liferay.portal.model.CustomizedPages";
248    
249            private static final String _SUFFIX = "</name>";
250    
251    }