1
22
23 package com.liferay.portal.upgrade.v5_1_0;
24
25 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
26 import com.liferay.portal.kernel.upgrade.UpgradeProcess;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portlet.PortletPreferencesImpl;
30 import com.liferay.portlet.PortletPreferencesSerializer;
31
32 import java.sql.Connection;
33 import java.sql.PreparedStatement;
34 import java.sql.ResultSet;
35
36
42 public class UpgradeSitemap extends UpgradeProcess {
43
44 protected void deletePortletPreferences(long portletPreferencesId)
45 throws Exception {
46
47 runSQL(
48 "delete from PortletPreferences where portletPreferencesId = " +
49 portletPreferencesId);
50 }
51
52 protected void doUpgrade() throws Exception {
53 Connection con = null;
54 PreparedStatement ps = null;
55 ResultSet rs = null;
56
57 try {
58 con = DataAccess.getConnection();
59
60 ps = con.prepareStatement(
61 "select portletPreferencesId, ownerId, ownerType, plid, " +
62 "portletId, preferences from PortletPreferences where " +
63 "portletId like '85_%'");
64
65 rs = ps.executeQuery();
66
67 while (rs.next()) {
68 long portletPreferencesId = rs.getLong("portletPreferencesId");
69 long ownerId = rs.getLong("ownerId");
70 int ownerType = rs.getInt("ownerType");
71 long plid = rs.getLong("plid");
72 String portletId = rs.getString("portletId");
73 String preferences = rs.getString("preferences");
74
75 Object[] layout = getLayout(plid);
76
77 if (layout != null) {
78 long companyId = (Long)layout[0];
79
80 String newPreferences = upgradePreferences(
81 companyId, ownerId, ownerType, plid, portletId,
82 preferences);
83
84 updatePortletPreferences(
85 portletPreferencesId, newPreferences);
86 }
87 else {
88 deletePortletPreferences(portletPreferencesId);
89 }
90 }
91 }
92 finally {
93 DataAccess.cleanUp(con, ps, rs);
94 }
95 }
96
97 protected Object[] getLayout(long plid) throws Exception {
98 Object[] layout = null;
99
100 Connection con = null;
101 PreparedStatement ps = null;
102 ResultSet rs = null;
103
104 try {
105 con = DataAccess.getConnection();
106
107 ps = con.prepareStatement(_GET_LAYOUT);
108
109 ps.setLong(1, plid);
110
111 rs = ps.executeQuery();
112
113 while (rs.next()) {
114 long companyId = rs.getLong("companyId");
115 long layoutId = rs.getLong("layoutId");
116
117 layout = new Object[] {companyId, layoutId};
118 }
119 }
120 finally {
121 DataAccess.cleanUp(con, ps, rs);
122 }
123
124 return layout;
125 }
126
127 protected void updatePortletPreferences(
128 long portletPreferencesId, String preferences)
129 throws Exception {
130
131 Connection con = null;
132 PreparedStatement ps = null;
133
134 try {
135 con = DataAccess.getConnection();
136
137 ps = con.prepareStatement(
138 "update PortletPreferences set preferences = ? where " +
139 "portletPreferencesId = " + portletPreferencesId);
140
141 ps.setString(1, preferences);
142
143 ps.executeUpdate();
144 }
145 finally {
146 DataAccess.cleanUp(con, ps);
147 }
148 }
149
150 protected String upgradePreferences(
151 long companyId, long ownerId, int ownerType, long plid,
152 String portletId, String xml)
153 throws Exception {
154
155 PortletPreferencesImpl preferences =
156 PortletPreferencesSerializer.fromXML(
157 companyId, ownerId, ownerType, plid, portletId, xml);
158
159 long rootPlid = GetterUtil.getLong(
160 preferences.getValue("root-plid", StringPool.BLANK));
161
162 if (rootPlid > 0) {
163 Object[] layout = getLayout(rootPlid);
164
165 if (layout != null) {
166 long layoutId = (Long)layout[1];
167
168 preferences.setValue(
169 "root-layout-id", String.valueOf(layoutId));
170 }
171 }
172
173 preferences.setValue("root-plid", null);
174
175 return PortletPreferencesSerializer.toXML(preferences);
176 }
177
178 private static final String _GET_LAYOUT =
179 "select * from Layout where plid = ?";
180
181 }