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