001    /**
002     * Copyright (c) 2000-2013 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.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    /**
034     * @author Jorge Ferrer
035     * @author Julio Camarero
036     */
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    
071                    StringBundler sb = new StringBundler(6);
072    
073                    if (Validator.isNotNull(javaScript1)) {
074                            sb.append("// Custom JavaScript 1\n\n");
075                            sb.append(javaScript1);
076    
077                            typeSettingsProperties.remove("javascript-1");
078                    }
079    
080                    if (Validator.isNotNull(javaScript2)) {
081                            sb.append("\n\n\n // Custom JavaScript 2\n\n");
082                            sb.append(javaScript2);
083    
084                            typeSettingsProperties.remove("javascript-2");
085                    }
086    
087                    if (Validator.isNotNull(javaScript3)) {
088                            sb.append("\n\n\n // Custom JavaScript 3\n\n");
089                            sb.append(javaScript3);
090    
091                            typeSettingsProperties.remove("javascript-3");
092                    }
093    
094                    String javascript = sb.toString();
095    
096                    if (Validator.isNotNull(javascript)) {
097                            typeSettingsProperties.put("javascript", javascript);
098                    }
099            }
100    
101            protected void updateLayout(
102                            long plid, String name, String title, String typeSettings)
103                    throws Exception {
104    
105                    if (Validator.isNotNull(name)) {
106                            name = StringUtil.replace(
107                                    name, new String[] {"<name", "</name>"},
108                                    new String[] {"<Name", "</Name>"});
109    
110                            updateName(plid, name);
111                    }
112    
113                    if (Validator.isNotNull(title)) {
114                            title = StringUtil.replace(
115                                    title, new String[] {"<title", "</title>"},
116                                    new String[] {"<Title", "</Title>"});
117    
118                            updateTitle(plid, title);
119                    }
120    
121                    if (Validator.isNull(typeSettings)) {
122                            return;
123                    }
124    
125                    Locale defaultLocale = LocaleUtil.getDefault();
126                    String defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);
127    
128                    UnicodeProperties typeSettingsProperties = new UnicodeProperties(true);
129    
130                    typeSettingsProperties.load(typeSettings);
131    
132                    String defaultDescription = typeSettingsProperties.getProperty(
133                            "meta-description_" + defaultLanguageId);
134    
135                    if (Validator.isNotNull(defaultDescription)) {
136                            typeSettingsProperties = updateMetaField(
137                                    plid, typeSettingsProperties, "meta-description_",
138                                    "Description", "description");
139                    }
140    
141                    String defaultKeywords = typeSettingsProperties.getProperty(
142                            "meta-keywords_" + defaultLanguageId);
143    
144                    if (Validator.isNotNull(defaultKeywords)) {
145                            typeSettingsProperties = updateMetaField(
146                                    plid, typeSettingsProperties, "meta-keywords_", "Keywords",
147                                    "keywords");
148                    }
149    
150                    String defaultRobots = typeSettingsProperties.getProperty(
151                            "meta-robots_" + defaultLanguageId);
152    
153                    if (Validator.isNotNull(defaultRobots)) {
154                            typeSettingsProperties = updateMetaField(
155                                    plid, typeSettingsProperties, "meta-robots_", "Robots",
156                                    "robots");
157                    }
158    
159                    String javaScript1 = typeSettingsProperties.getProperty("javascript-1");
160                    String javaScript2 = typeSettingsProperties.getProperty("javascript-2");
161                    String javaScript3 = typeSettingsProperties.getProperty("javascript-3");
162    
163                    if ((javaScript1 != null) || (javaScript2 != null) ||
164                            (javaScript3 != null)) {
165    
166                            updateJavaScript(
167                                    typeSettingsProperties, javaScript1, javaScript2, javaScript3);
168                    }
169    
170                    updateTypeSettings(plid, typeSettingsProperties.toString());
171            }
172    
173            protected UnicodeProperties updateMetaField(
174                            long plid, UnicodeProperties typeSettingsProperties,
175                            String propertyName, String xmlName, String columName)
176                    throws Exception {
177    
178                    String xml = null;
179    
180                    Locale[] locales = LanguageUtil.getAvailableLocales();
181    
182                    for (Locale locale : locales) {
183                            String languageId = LocaleUtil.toLanguageId(locale);
184    
185                            String value = typeSettingsProperties.getProperty(
186                                    propertyName + languageId);
187    
188                            if (Validator.isNotNull(value)) {
189                                    xml = LocalizationUtil.updateLocalization(
190                                            xml, xmlName, value, languageId);
191    
192                                    typeSettingsProperties.remove(propertyName + languageId);
193                            }
194                    }
195    
196                    Connection con = null;
197                    PreparedStatement ps = null;
198    
199                    try {
200                            con = DataAccess.getUpgradeOptimizedConnection();
201    
202                            ps = con.prepareStatement(
203                                    "update Layout set " + columName + " = ? where plid = " + plid);
204    
205                            ps.setString(1, xml);
206    
207                            ps.executeUpdate();
208                    }
209                    finally {
210                            DataAccess.cleanUp(con, ps);
211                    }
212    
213                    return typeSettingsProperties;
214            }
215    
216            protected void updateName(long plid, String name) throws Exception {
217                    Connection con = null;
218                    PreparedStatement ps = null;
219    
220                    try {
221                            con = DataAccess.getUpgradeOptimizedConnection();
222    
223                            ps = con.prepareStatement(
224                                    "update Layout set name = ? where plid = " + plid);
225    
226                            ps.setString(1, name);
227    
228                            ps.executeUpdate();
229                    }
230                    finally {
231                            DataAccess.cleanUp(con, ps);
232                    }
233            }
234    
235            protected void updateTitle(long plid, String title) throws Exception {
236                    Connection con = null;
237                    PreparedStatement ps = null;
238    
239                    try {
240                            con = DataAccess.getUpgradeOptimizedConnection();
241    
242                            ps = con.prepareStatement(
243                                    "update Layout set title = ? where plid = " + plid);
244    
245                            ps.setString(1, title);
246    
247                            ps.executeUpdate();
248                    }
249                    finally {
250                            DataAccess.cleanUp(con, ps);
251                    }
252            }
253    
254            protected void updateTypeSettings(long plid, String typeSettings)
255                    throws Exception {
256    
257                    Connection con = null;
258                    PreparedStatement ps = null;
259    
260                    try {
261                            con = DataAccess.getUpgradeOptimizedConnection();
262    
263                            ps = con.prepareStatement(
264                                    "update Layout set typeSettings = ? where plid = " + plid);
265    
266                            ps.setString(1, typeSettings);
267    
268                            ps.executeUpdate();
269                    }
270                    finally {
271                            DataAccess.cleanUp(con, ps);
272                    }
273            }
274    
275    }