001
014
015 package com.liferay.portal.upgrade.v6_1_0;
016
017 import com.liferay.portal.kernel.language.LanguageUtil;
018 import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019 import com.liferay.portal.kernel.upgrade.util.UpgradeProcessUtil;
020 import com.liferay.portal.kernel.util.LocaleUtil;
021 import com.liferay.portal.kernel.util.LocalizationUtil;
022 import com.liferay.portal.kernel.util.LoggingTimer;
023 import com.liferay.portal.kernel.util.StringBundler;
024 import com.liferay.portal.kernel.util.StringUtil;
025 import com.liferay.portal.kernel.util.UnicodeProperties;
026 import com.liferay.portal.kernel.util.Validator;
027
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 updateLayouts();
042 }
043
044 protected void updateJavaScript(
045 UnicodeProperties typeSettingsProperties, String javaScript1,
046 String javaScript2, String javaScript3) {
047
048 StringBundler sb = new StringBundler(6);
049
050 if (Validator.isNotNull(javaScript1)) {
051 sb.append("
052 sb.append(javaScript1);
053
054 typeSettingsProperties.remove("javascript-1");
055 }
056
057 if (Validator.isNotNull(javaScript2)) {
058 sb.append("\n\n\n
059 sb.append(javaScript2);
060
061 typeSettingsProperties.remove("javascript-2");
062 }
063
064 if (Validator.isNotNull(javaScript3)) {
065 sb.append("\n\n\n
066 sb.append(javaScript3);
067
068 typeSettingsProperties.remove("javascript-3");
069 }
070
071 String javascript = sb.toString();
072
073 if (Validator.isNotNull(javascript)) {
074 typeSettingsProperties.put("javascript", javascript);
075 }
076 }
077
078 protected void updateLayout(
079 long plid, long companyId, String name, String title,
080 String typeSettings)
081 throws Exception {
082
083 if (Validator.isNotNull(name)) {
084 name = StringUtil.replace(
085 name, new String[] {"<name", "</name>"},
086 new String[] {"<Name", "</Name>"});
087
088 updateName(plid, name);
089 }
090
091 if (Validator.isNotNull(title)) {
092 title = StringUtil.replace(
093 title, new String[] {"<title", "</title>"},
094 new String[] {"<Title", "</Title>"});
095
096 updateTitle(plid, title);
097 }
098
099 if (Validator.isNull(typeSettings)) {
100 return;
101 }
102
103 String defaultLanguageId = UpgradeProcessUtil.getDefaultLanguageId(
104 companyId);
105
106 UnicodeProperties typeSettingsProperties = new UnicodeProperties(true);
107
108 typeSettingsProperties.load(typeSettings);
109
110 String defaultDescription = typeSettingsProperties.getProperty(
111 "meta-description_" + defaultLanguageId);
112
113 if (Validator.isNotNull(defaultDescription)) {
114 typeSettingsProperties = updateMetaField(
115 plid, typeSettingsProperties, "meta-description_",
116 "Description", "description");
117 }
118
119 String defaultKeywords = typeSettingsProperties.getProperty(
120 "meta-keywords_" + defaultLanguageId);
121
122 if (Validator.isNotNull(defaultKeywords)) {
123 typeSettingsProperties = updateMetaField(
124 plid, typeSettingsProperties, "meta-keywords_", "Keywords",
125 "keywords");
126 }
127
128 String defaultRobots = typeSettingsProperties.getProperty(
129 "meta-robots_" + defaultLanguageId);
130
131 if (Validator.isNotNull(defaultRobots)) {
132 typeSettingsProperties = updateMetaField(
133 plid, typeSettingsProperties, "meta-robots_", "Robots",
134 "robots");
135 }
136
137 String javaScript1 = typeSettingsProperties.getProperty("javascript-1");
138 String javaScript2 = typeSettingsProperties.getProperty("javascript-2");
139 String javaScript3 = typeSettingsProperties.getProperty("javascript-3");
140
141 if ((javaScript1 != null) || (javaScript2 != null) ||
142 (javaScript3 != null)) {
143
144 updateJavaScript(
145 typeSettingsProperties, javaScript1, javaScript2, javaScript3);
146 }
147
148 updateTypeSettings(plid, typeSettingsProperties.toString());
149 }
150
151 protected void updateLayouts() throws Exception {
152 try (LoggingTimer loggingTimer = new LoggingTimer();
153 PreparedStatement ps = connection.prepareStatement(
154 "select plid, companyId, name, title, typeSettings from " +
155 "Layout");
156 ResultSet rs = ps.executeQuery()) {
157
158 while (rs.next()) {
159 long plid = rs.getLong("plid");
160 long companyId = rs.getLong("companyId");
161 String name = rs.getString("name");
162 String title = rs.getString("title");
163 String typeSettings = rs.getString("typeSettings");
164
165 updateLayout(plid, companyId, name, title, typeSettings);
166 }
167 }
168 }
169
170 protected UnicodeProperties updateMetaField(
171 long plid, UnicodeProperties typeSettingsProperties,
172 String propertyName, String xmlName, String columName)
173 throws Exception {
174
175 String xml = null;
176
177 for (Locale locale : LanguageUtil.getAvailableLocales()) {
178 String languageId = LocaleUtil.toLanguageId(locale);
179
180 String value = typeSettingsProperties.getProperty(
181 propertyName + languageId);
182
183 if (Validator.isNotNull(value)) {
184 xml = LocalizationUtil.updateLocalization(
185 xml, xmlName, value, languageId);
186
187 typeSettingsProperties.remove(propertyName + languageId);
188 }
189 }
190
191 try (PreparedStatement ps = connection.prepareStatement(
192 "update Layout set " + columName + " = ? where plid = " +
193 plid)) {
194
195 ps.setString(1, xml);
196
197 ps.executeUpdate();
198 }
199
200 return typeSettingsProperties;
201 }
202
203 protected void updateName(long plid, String name) throws Exception {
204 try (PreparedStatement ps = connection.prepareStatement(
205 "update Layout set name = ? where plid = " + plid)) {
206
207 ps.setString(1, name);
208
209 ps.executeUpdate();
210 }
211 }
212
213 protected void updateTitle(long plid, String title) throws Exception {
214 try (PreparedStatement ps = connection.prepareStatement(
215 "update Layout set title = ? where plid = " + plid)) {
216
217 ps.setString(1, title);
218
219 ps.executeUpdate();
220 }
221 }
222
223 protected void updateTypeSettings(long plid, String typeSettings)
224 throws Exception {
225
226 try (PreparedStatement ps = connection.prepareStatement(
227 "update Layout set typeSettings = ? where plid = " + plid)) {
228
229 ps.setString(1, typeSettings);
230
231 ps.executeUpdate();
232 }
233 }
234
235 }