1
22
23 package com.liferay.portal.upgrade.v5_1_0;
24
25 import com.liferay.portal.NoSuchLayoutException;
26 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.model.Layout;
30 import com.liferay.portal.service.LayoutLocalServiceUtil;
31 import com.liferay.portal.upgrade.UpgradeProcess;
32 import com.liferay.portlet.PortletPreferencesImpl;
33 import com.liferay.portlet.PortletPreferencesSerializer;
34
35 import java.sql.Connection;
36 import java.sql.PreparedStatement;
37 import java.sql.ResultSet;
38
39
45 public class UpgradeSitemap extends UpgradeProcess {
46
47 protected void deletePortletPreferences(long portletPreferencesId)
48 throws Exception {
49
50 runSQL(
51 "delete from PortletPreferences where portletPreferencesId = " +
52 portletPreferencesId);
53 }
54
55 protected void doUpgrade() throws Exception {
56 Connection con = null;
57 PreparedStatement ps = null;
58 ResultSet rs = null;
59
60 try {
61 con = DataAccess.getConnection();
62
63 ps = con.prepareStatement(
64 "select portletPreferencesId, ownerId, ownerType, plid, " +
65 "portletId, preferences from PortletPreferences where " +
66 "portletId like '85_%'");
67
68 rs = ps.executeQuery();
69
70 while (rs.next()) {
71 long portletPreferencesId = rs.getLong("portletPreferencesId");
72 long ownerId = rs.getLong("ownerId");
73 int ownerType = rs.getInt("ownerType");
74 long plid = rs.getLong("plid");
75 String portletId = rs.getString("portletId");
76 String preferences = rs.getString("preferences");
77
78 try {
79 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
80
81 String newPreferences = upgradePreferences(
82 layout.getCompanyId(), ownerId, ownerType, plid,
83 portletId, preferences);
84
85 updatePortletPreferences(
86 portletPreferencesId, newPreferences);
87 }
88 catch (NoSuchLayoutException nsle) {
89 deletePortletPreferences(portletPreferencesId);
90 }
91 }
92 }
93 finally {
94 DataAccess.cleanUp(con, ps, rs);
95 }
96 }
97
98 protected void updatePortletPreferences(
99 long portletPreferencesId, String preferences)
100 throws Exception {
101
102 Connection con = null;
103 PreparedStatement ps = null;
104
105 try {
106 con = DataAccess.getConnection();
107
108 ps = con.prepareStatement(
109 "update PortletPreferences set preferences = ? where " +
110 "portletPreferencesId = " + portletPreferencesId);
111
112 ps.setString(1, preferences);
113
114 ps.executeUpdate();
115 }
116 finally {
117 DataAccess.cleanUp(con, ps);
118 }
119 }
120
121 protected String upgradePreferences(
122 long companyId, long ownerId, int ownerType, long plid,
123 String portletId, String preferences)
124 throws Exception {
125
126 PortletPreferencesImpl prefs = PortletPreferencesSerializer.fromXML(
127 companyId, ownerId, ownerType, plid, portletId, preferences);
128
129 long rootPlid = GetterUtil.getLong(
130 prefs.getValue("root-plid", StringPool.BLANK));
131
132 if (rootPlid > 0) {
133 try {
134 Layout layout = LayoutLocalServiceUtil.getLayout(rootPlid);
135
136 prefs.setValue(
137 "root-layout-id", String.valueOf(layout.getLayoutId()));
138 }
139 catch (NoSuchLayoutException nsle) {
140 }
141 }
142
143 prefs.setValue("root-plid", null);
144
145 return PortletPreferencesSerializer.toXML(prefs);
146 }
147
148 }