001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.upgrade.util.UpgradeProcessUtil;
021    import com.liferay.portal.kernel.util.LocaleUtil;
022    import com.liferay.portal.kernel.util.LocalizationUtil;
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.Connection;
029    import java.sql.PreparedStatement;
030    import java.sql.ResultSet;
031    
032    import java.util.Locale;
033    
034    /**
035     * @author Jorge Ferrer
036     * @author Julio Camarero
037     */
038    public class UpgradeLayout extends UpgradeProcess {
039    
040            @Override
041            protected void doUpgrade() throws Exception {
042                    Connection con = null;
043                    PreparedStatement ps = null;
044                    ResultSet rs = null;
045    
046                    try {
047                            con = DataAccess.getUpgradeOptimizedConnection();
048    
049                            ps = con.prepareStatement(
050                                    "select plid, companyId, name, title, typeSettings from " +
051                                            "Layout");
052    
053                            rs = ps.executeQuery();
054    
055                            while (rs.next()) {
056                                    long plid = rs.getLong("plid");
057                                    long companyId = rs.getLong("companyId");
058                                    String name = rs.getString("name");
059                                    String title = rs.getString("title");
060                                    String typeSettings = rs.getString("typeSettings");
061    
062                                    updateLayout(plid, companyId, name, title, typeSettings);
063                            }
064                    }
065                    finally {
066                            DataAccess.cleanUp(con, ps, rs);
067                    }
068            }
069    
070            protected void updateJavaScript(
071                    UnicodeProperties typeSettingsProperties, String javaScript1,
072                    String javaScript2, String javaScript3) {
073    
074                    StringBundler sb = new StringBundler(6);
075    
076                    if (Validator.isNotNull(javaScript1)) {
077                            sb.append("// Custom JavaScript 1\n\n");
078                            sb.append(javaScript1);
079    
080                            typeSettingsProperties.remove("javascript-1");
081                    }
082    
083                    if (Validator.isNotNull(javaScript2)) {
084                            sb.append("\n\n\n // Custom JavaScript 2\n\n");
085                            sb.append(javaScript2);
086    
087                            typeSettingsProperties.remove("javascript-2");
088                    }
089    
090                    if (Validator.isNotNull(javaScript3)) {
091                            sb.append("\n\n\n // Custom JavaScript 3\n\n");
092                            sb.append(javaScript3);
093    
094                            typeSettingsProperties.remove("javascript-3");
095                    }
096    
097                    String javascript = sb.toString();
098    
099                    if (Validator.isNotNull(javascript)) {
100                            typeSettingsProperties.put("javascript", javascript);
101                    }
102            }
103    
104            protected void updateLayout(
105                            long plid, long companyId, String name, String title,
106                            String typeSettings)
107                    throws Exception {
108    
109                    if (Validator.isNotNull(name)) {
110                            name = StringUtil.replace(
111                                    name, new String[] {"<name", "</name>"},
112                                    new String[] {"<Name", "</Name>"});
113    
114                            updateName(plid, name);
115                    }
116    
117                    if (Validator.isNotNull(title)) {
118                            title = StringUtil.replace(
119                                    title, new String[] {"<title", "</title>"},
120                                    new String[] {"<Title", "</Title>"});
121    
122                            updateTitle(plid, title);
123                    }
124    
125                    if (Validator.isNull(typeSettings)) {
126                            return;
127                    }
128    
129                    String defaultLanguageId = UpgradeProcessUtil.getDefaultLanguageId(
130                            companyId);
131    
132                    UnicodeProperties typeSettingsProperties = new UnicodeProperties(true);
133    
134                    typeSettingsProperties.load(typeSettings);
135    
136                    String defaultDescription = typeSettingsProperties.getProperty(
137                            "meta-description_" + defaultLanguageId);
138    
139                    if (Validator.isNotNull(defaultDescription)) {
140                            typeSettingsProperties = updateMetaField(
141                                    plid, typeSettingsProperties, "meta-description_",
142                                    "Description", "description");
143                    }
144    
145                    String defaultKeywords = typeSettingsProperties.getProperty(
146                            "meta-keywords_" + defaultLanguageId);
147    
148                    if (Validator.isNotNull(defaultKeywords)) {
149                            typeSettingsProperties = updateMetaField(
150                                    plid, typeSettingsProperties, "meta-keywords_", "Keywords",
151                                    "keywords");
152                    }
153    
154                    String defaultRobots = typeSettingsProperties.getProperty(
155                            "meta-robots_" + defaultLanguageId);
156    
157                    if (Validator.isNotNull(defaultRobots)) {
158                            typeSettingsProperties = updateMetaField(
159                                    plid, typeSettingsProperties, "meta-robots_", "Robots",
160                                    "robots");
161                    }
162    
163                    String javaScript1 = typeSettingsProperties.getProperty("javascript-1");
164                    String javaScript2 = typeSettingsProperties.getProperty("javascript-2");
165                    String javaScript3 = typeSettingsProperties.getProperty("javascript-3");
166    
167                    if ((javaScript1 != null) || (javaScript2 != null) ||
168                            (javaScript3 != null)) {
169    
170                            updateJavaScript(
171                                    typeSettingsProperties, javaScript1, javaScript2, javaScript3);
172                    }
173    
174                    updateTypeSettings(plid, typeSettingsProperties.toString());
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                    for (Locale locale : LanguageUtil.getAvailableLocales()) {
185                            String languageId = LocaleUtil.toLanguageId(locale);
186    
187                            String value = typeSettingsProperties.getProperty(
188                                    propertyName + languageId);
189    
190                            if (Validator.isNotNull(value)) {
191                                    xml = LocalizationUtil.updateLocalization(
192                                            xml, xmlName, value, languageId);
193    
194                                    typeSettingsProperties.remove(propertyName + languageId);
195                            }
196                    }
197    
198                    Connection con = null;
199                    PreparedStatement ps = null;
200    
201                    try {
202                            con = DataAccess.getUpgradeOptimizedConnection();
203    
204                            ps = con.prepareStatement(
205                                    "update Layout set " + columName + " = ? where plid = " + plid);
206    
207                            ps.setString(1, xml);
208    
209                            ps.executeUpdate();
210                    }
211                    finally {
212                            DataAccess.cleanUp(con, ps);
213                    }
214    
215                    return typeSettingsProperties;
216            }
217    
218            protected void updateName(long plid, String name) throws Exception {
219                    Connection con = null;
220                    PreparedStatement ps = null;
221    
222                    try {
223                            con = DataAccess.getUpgradeOptimizedConnection();
224    
225                            ps = con.prepareStatement(
226                                    "update Layout set name = ? where plid = " + plid);
227    
228                            ps.setString(1, name);
229    
230                            ps.executeUpdate();
231                    }
232                    finally {
233                            DataAccess.cleanUp(con, ps);
234                    }
235            }
236    
237            protected void updateTitle(long plid, String title) throws Exception {
238                    Connection con = null;
239                    PreparedStatement ps = null;
240    
241                    try {
242                            con = DataAccess.getUpgradeOptimizedConnection();
243    
244                            ps = con.prepareStatement(
245                                    "update Layout set title = ? where plid = " + plid);
246    
247                            ps.setString(1, title);
248    
249                            ps.executeUpdate();
250                    }
251                    finally {
252                            DataAccess.cleanUp(con, ps);
253                    }
254            }
255    
256            protected void updateTypeSettings(long plid, String typeSettings)
257                    throws Exception {
258    
259                    Connection con = null;
260                    PreparedStatement ps = null;
261    
262                    try {
263                            con = DataAccess.getUpgradeOptimizedConnection();
264    
265                            ps = con.prepareStatement(
266                                    "update Layout set typeSettings = ? where plid = " + plid);
267    
268                            ps.setString(1, typeSettings);
269    
270                            ps.executeUpdate();
271                    }
272                    finally {
273                            DataAccess.cleanUp(con, ps);
274                    }
275            }
276    
277    }