001
014
015 package com.liferay.portal.upgrade.v6_1_0;
016
017 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018 import com.liferay.portal.kernel.language.LanguageUtil;
019 import com.liferay.portal.kernel.upgrade.UpgradeProcess;
020 import com.liferay.portal.kernel.util.LocaleUtil;
021 import com.liferay.portal.kernel.util.LocalizationUtil;
022 import com.liferay.portal.kernel.util.StringBundler;
023 import com.liferay.portal.kernel.util.StringUtil;
024 import com.liferay.portal.kernel.util.UnicodeProperties;
025 import com.liferay.portal.kernel.util.Validator;
026
027 import java.sql.Connection;
028 import java.sql.PreparedStatement;
029 import java.sql.ResultSet;
030
031 import java.util.Locale;
032
033
037 public class UpgradeLayout extends UpgradeProcess {
038
039 @Override
040 protected void doUpgrade() throws Exception {
041 Connection con = null;
042 PreparedStatement ps = null;
043 ResultSet rs = null;
044
045 try {
046 con = DataAccess.getUpgradeOptimizedConnection();
047
048 ps = con.prepareStatement(
049 "select plid, name, title, typeSettings from Layout");
050
051 rs = ps.executeQuery();
052
053 while (rs.next()) {
054 long plid = rs.getLong("plid");
055 String name = rs.getString("name");
056 String title = rs.getString("title");
057 String typeSettings = rs.getString("typeSettings");
058
059 updateLayout(plid, name, title, typeSettings);
060 }
061 }
062 finally {
063 DataAccess.cleanUp(con, ps, rs);
064 }
065 }
066
067 protected void updateJavaScript(
068 UnicodeProperties typeSettingsProperties, String javaScript1,
069 String javaScript2, String javaScript3)
070 throws Exception {
071
072 StringBundler sb = new StringBundler(6);
073
074 if (Validator.isNotNull(javaScript1)) {
075 sb.append("
076 sb.append(javaScript1);
077
078 typeSettingsProperties.remove("javascript-1");
079 }
080
081 if (Validator.isNotNull(javaScript2)) {
082 sb.append("\n\n\n
083 sb.append(javaScript2);
084
085 typeSettingsProperties.remove("javascript-2");
086 }
087
088 if (Validator.isNotNull(javaScript3)) {
089 sb.append("\n\n\n
090 sb.append(javaScript3);
091
092 typeSettingsProperties.remove("javascript-3");
093 }
094
095 String javascript = sb.toString();
096
097 if (Validator.isNotNull(javascript)) {
098 typeSettingsProperties.put("javascript", javascript);
099 }
100 }
101
102 protected void updateLayout(
103 long plid, String name, String title, String typeSettings)
104 throws Exception {
105
106 if (Validator.isNotNull(name)) {
107 name = StringUtil.replace(
108 name, new String[] {"<name", "</name>"},
109 new String[] {"<Name", "</Name>"});
110
111 updateName(plid, name);
112 }
113
114 if (Validator.isNotNull(title)) {
115 title = StringUtil.replace(
116 title, new String[] {"<title", "</title>"},
117 new String[] {"<Title", "</Title>"});
118
119 updateTitle(plid, title);
120 }
121
122 if (Validator.isNotNull(typeSettings)) {
123 Locale defaultLocale = LocaleUtil.getDefault();
124 String defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);
125
126 UnicodeProperties typeSettingsProperties = new UnicodeProperties(
127 true);
128
129 typeSettingsProperties.load(typeSettings);
130
131 String defaultDescription = typeSettingsProperties.getProperty(
132 "meta-description_" + defaultLanguageId);
133
134 if (Validator.isNotNull(defaultDescription)) {
135 typeSettingsProperties = updateMetaField(
136 plid, typeSettingsProperties, "meta-description_",
137 "Description", "description");
138 }
139
140 String defaultKeywords = typeSettingsProperties.getProperty(
141 "meta-keywords_" + defaultLanguageId);
142
143 if (Validator.isNotNull(defaultKeywords)) {
144 typeSettingsProperties = updateMetaField(
145 plid, typeSettingsProperties, "meta-keywords_", "Keywords",
146 "keywords");
147 }
148
149 String defaultRobots = typeSettingsProperties.getProperty(
150 "meta-robots_" + defaultLanguageId);
151
152 if (Validator.isNotNull(defaultRobots)) {
153 typeSettingsProperties = updateMetaField(
154 plid, typeSettingsProperties, "meta-robots_", "Robots",
155 "robots");
156 }
157
158 String javaScript1 = typeSettingsProperties.getProperty(
159 "javascript-1");
160 String javaScript2 = typeSettingsProperties.getProperty(
161 "javascript-2");
162 String javaScript3 = typeSettingsProperties.getProperty(
163 "javascript-3");
164
165 if ((javaScript1 != null) || (javaScript2 != null) ||
166 (javaScript3 != null)) {
167
168 updateJavaScript(
169 typeSettingsProperties, javaScript1, javaScript2,
170 javaScript3);
171 }
172
173 updateTypeSettings(plid, typeSettingsProperties.toString());
174 }
175 }
176
177 protected UnicodeProperties updateMetaField(
178 long plid, UnicodeProperties typeSettingsProperties,
179 String propertyName, String xmlName, String columName)
180 throws Exception {
181
182 String xml = null;
183
184 Locale[] locales = LanguageUtil.getAvailableLocales();
185
186 for (Locale locale : locales) {
187 String languageId = LocaleUtil.toLanguageId(locale);
188
189 String value = typeSettingsProperties.getProperty(
190 propertyName + languageId);
191
192 if (Validator.isNotNull(value)) {
193 xml = LocalizationUtil.updateLocalization(
194 xml, xmlName, value, languageId);
195
196 typeSettingsProperties.remove(propertyName + languageId);
197 }
198 }
199
200 Connection con = null;
201 PreparedStatement ps = null;
202
203 try {
204 con = DataAccess.getUpgradeOptimizedConnection();
205
206 ps = con.prepareStatement(
207 "update Layout set " + columName + " = ? where plid = " + plid);
208
209 ps.setString(1, xml);
210
211 ps.executeUpdate();
212 }
213 finally {
214 DataAccess.cleanUp(con, ps);
215 }
216
217 return typeSettingsProperties;
218 }
219
220 protected void updateName(long plid, String name) throws Exception {
221 Connection con = null;
222 PreparedStatement ps = null;
223
224 try {
225 con = DataAccess.getUpgradeOptimizedConnection();
226
227 ps = con.prepareStatement(
228 "update Layout set name = ? where plid = " + plid);
229
230 ps.setString(1, name);
231
232 ps.executeUpdate();
233 }
234 finally {
235 DataAccess.cleanUp(con, ps);
236 }
237 }
238
239 protected void updateTitle(long plid, String title) throws Exception {
240 Connection con = null;
241 PreparedStatement ps = null;
242
243 try {
244 con = DataAccess.getUpgradeOptimizedConnection();
245
246 ps = con.prepareStatement(
247 "update Layout set title = ? where plid = " + plid);
248
249 ps.setString(1, title);
250
251 ps.executeUpdate();
252 }
253 finally {
254 DataAccess.cleanUp(con, ps);
255 }
256 }
257
258 protected void updateTypeSettings(long plid, String typeSettings)
259 throws Exception {
260
261 Connection con = null;
262 PreparedStatement ps = null;
263
264 try {
265 con = DataAccess.getUpgradeOptimizedConnection();
266
267 ps = con.prepareStatement(
268 "update Layout set typeSettings = ? where plid = " + plid);
269
270 ps.setString(1, typeSettings);
271
272 ps.executeUpdate();
273 }
274 finally {
275 DataAccess.cleanUp(con, ps);
276 }
277 }
278
279 }