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