001    /**
002     * Copyright (c) 2000-2012 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.getConnection();
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("// Custom JavaScript 1\n\n");
076                            sb.append(javaScript1);
077    
078                            typeSettingsProperties.remove("javascript-1");
079                    }
080    
081                    if (Validator.isNotNull(javaScript2)) {
082                            sb.append("\n\n\n // Custom JavaScript 2\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 // Custom JavaScript 3\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.getConnection();
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)
221                    throws Exception {
222    
223                    Connection con = null;
224                    PreparedStatement ps = null;
225    
226                    try {
227                            con = DataAccess.getConnection();
228    
229                            ps = con.prepareStatement(
230                                    "update Layout set name = ? where plid = " + plid);
231    
232                            ps.setString(1, name);
233    
234                            ps.executeUpdate();
235                    }
236                    finally {
237                            DataAccess.cleanUp(con, ps);
238                    }
239            }
240    
241            protected void updateTitle(long plid, String title) throws Exception {
242                    Connection con = null;
243                    PreparedStatement ps = null;
244    
245                    try {
246                            con = DataAccess.getConnection();
247    
248                            ps = con.prepareStatement(
249                                    "update Layout set title = ? where plid = " + plid);
250    
251                            ps.setString(1, title);
252    
253                            ps.executeUpdate();
254                    }
255                    finally {
256                            DataAccess.cleanUp(con, ps);
257                    }
258            }
259    
260            protected void updateTypeSettings(long plid, String typeSettings)
261                    throws Exception {
262    
263                    Connection con = null;
264                    PreparedStatement ps = null;
265    
266                    try {
267                            con = DataAccess.getConnection();
268    
269                            ps = con.prepareStatement(
270                                    "update Layout set typeSettings = ? where plid = " + plid);
271    
272                            ps.setString(1, typeSettings);
273    
274                            ps.executeUpdate();
275                    }
276                    finally {
277                            DataAccess.cleanUp(con, ps);
278                    }
279            }
280    
281    }