1
22
23 package com.liferay.portal.upgrade.v4_3_5;
24
25 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
26 import com.liferay.portal.kernel.log.Log;
27 import com.liferay.portal.kernel.log.LogFactoryUtil;
28 import com.liferay.portal.kernel.util.GetterUtil;
29 import com.liferay.portal.kernel.util.StringUtil;
30 import com.liferay.portal.model.PortletConstants;
31 import com.liferay.portal.upgrade.UpgradeException;
32 import com.liferay.portal.upgrade.UpgradeProcess;
33
34 import java.sql.Connection;
35 import java.sql.PreparedStatement;
36 import java.sql.ResultSet;
37
38
44 public class UpgradePortletId extends UpgradeProcess {
45
46 public void upgrade() throws UpgradeException {
47 Log log = getLog();
48
49 log.info("Upgrading");
50
51 try {
52 doUpgrade();
53 }
54 catch (Exception e) {
55 throw new UpgradeException(e);
56 }
57 }
58
59 protected void doUpgrade() throws Exception {
60
61
63 String[][] portletIdsArray = getPortletIdsArray();
64
65 for (int i = 0; i < portletIdsArray.length; i++) {
66 String[] portletIds = portletIdsArray[i];
67
68 String oldRootPortletId = portletIds[0];
69 String newRootPortletId = portletIds[1];
70
71 upgradePortlet(oldRootPortletId, newRootPortletId);
72 upgradeResource(oldRootPortletId, newRootPortletId);
73 upgradeResourceCode(oldRootPortletId, newRootPortletId);
74 }
75 }
76
77 protected Log getLog() {
78 return _log;
79 }
80
81 protected String[][] getPortletIdsArray() {
82 return new String[][] {
83 new String[] {
84 "94",
85 "1_WAR_googleadsenseportlet"
86 },
87 new String[] {
88 "95",
89 "1_WAR_googlegadgetportlet"
90 },
91 new String[] {
92 "96",
93 "1_WAR_googlemapsportlet"
94 }
95 };
96 }
97
98 protected void upgradeLayout(
99 long plid, String oldPortletId, String newPortletId)
100 throws Exception {
101
102 Connection con = null;
103 PreparedStatement ps = null;
104 ResultSet rs = null;
105
106 try {
107 con = DataAccess.getConnection();
108
109 ps = con.prepareStatement(
110 "select typeSettings from Layout where plid = " + plid);
111
112 rs = ps.executeQuery();
113
114 while (rs.next()) {
115 String typeSettings = rs.getString("typeSettings");
116
117 String newTypeSettings = upgradeTypeSettings(
118 typeSettings, oldPortletId, newPortletId);
119
120 ps = con.prepareStatement(
121 "update Layout set typeSettings = ? where plid = " +
122 plid);
123
124 ps.setString(1, newTypeSettings);
125
126 ps.executeUpdate();
127
128 ps.close();
129 }
130 }
131 finally {
132 DataAccess.cleanUp(con, ps, rs);
133 }
134 }
135
136 protected void upgradePortlet(
137 String oldRootPortletId, String newRootPortletId)
138 throws Exception {
139
140 runSQL(
141 "update Portlet set portletId = '" + newRootPortletId +
142 "' where portletId = '" + oldRootPortletId + "'");
143 }
144
145 protected void upgradePortletPreferences(
146 String oldPortletId, String newPortletId)
147 throws Exception {
148
149 runSQL(
150 "update PortletPreferences set portletId = '" + newPortletId +
151 "' where portletId = '" + oldPortletId + "'");
152 }
153
154 protected void upgradeResource(
155 String oldRootPortletId, String newRootPortletId)
156 throws Exception {
157
158 Connection con = null;
159 PreparedStatement ps = null;
160 ResultSet rs = null;
161
162 try {
163 con = DataAccess.getConnection();
164
165 ps = con.prepareStatement(
166 "select primKey from Resource_ where primKey like ?");
167
168 String primKeyLike =
169 "%" + PortletConstants.LAYOUT_SEPARATOR + oldRootPortletId +
170 PortletConstants.INSTANCE_SEPARATOR + "%";
171
172 ps.setString(1, primKeyLike);
173
174 rs = ps.executeQuery();
175
176 while (rs.next()) {
177 String oldPrimKey = rs.getString("primKey");
178
179 int pos = oldPrimKey.indexOf(PortletConstants.LAYOUT_SEPARATOR);
180
181 long plid = GetterUtil.getLong(
182 oldPrimKey.substring(0, pos));
183
184 pos = oldPrimKey.indexOf(PortletConstants.INSTANCE_SEPARATOR);
185
186 String instanceId = oldPrimKey.substring(
187 pos + PortletConstants.INSTANCE_SEPARATOR.length());
188
189 String newPrimKey =
190 plid + PortletConstants.LAYOUT_SEPARATOR +
191 newRootPortletId + PortletConstants.INSTANCE_SEPARATOR +
192 instanceId;
193
194 runSQL(
195 "update Resource_ set primKey = '" + newPrimKey +
196 "' where primKey = '" + oldPrimKey + "'");
197
198 String oldPortletId =
199 oldRootPortletId + PortletConstants.INSTANCE_SEPARATOR +
200 instanceId;
201 String newPortletId =
202 newRootPortletId + PortletConstants.INSTANCE_SEPARATOR +
203 instanceId;
204
205 upgradeLayout(plid, oldPortletId, newPortletId);
206 upgradePortletPreferences(oldPortletId, newPortletId);
207 }
208 }
209 finally {
210 DataAccess.cleanUp(con, ps, rs);
211 }
212 }
213
214 protected void upgradeResourceCode(
215 String oldRootPortletId, String newRootPortletId)
216 throws Exception {
217
218 runSQL(
219 "update ResourceCode set name = '" + newRootPortletId +
220 "' where name = '" + oldRootPortletId + "'");
221 }
222
223 protected String upgradeTypeSettings(
224 String typeSettings, String oldPortletId, String newPortletId)
225 throws Exception {
226
227 return StringUtil.replace(typeSettings, oldPortletId, newPortletId);
228 }
229
230 private static Log _log = LogFactoryUtil.getLog(UpgradePortletId.class);
231
232 }