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