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.isNotNull(typeSettings)) {
122                            Locale defaultLocale = LocaleUtil.getDefault();
123                            String defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);
124    
125                            UnicodeProperties typeSettingsProperties = new UnicodeProperties(
126                                    true);
127    
128                            typeSettingsProperties.load(typeSettings);
129    
130                            String defaultDescription = typeSettingsProperties.getProperty(
131                                    "meta-description_" + defaultLanguageId);
132    
133                            if (Validator.isNotNull(defaultDescription)) {
134                                    typeSettingsProperties = updateMetaField(
135                                            plid, typeSettingsProperties, "meta-description_",
136                                            "Description", "description");
137                            }
138    
139                            String defaultKeywords = typeSettingsProperties.getProperty(
140                                    "meta-keywords_" + defaultLanguageId);
141    
142                            if (Validator.isNotNull(defaultKeywords)) {
143                                    typeSettingsProperties = updateMetaField(
144                                            plid, typeSettingsProperties, "meta-keywords_", "Keywords",
145                                            "keywords");
146                            }
147    
148                            String defaultRobots = typeSettingsProperties.getProperty(
149                                    "meta-robots_" + defaultLanguageId);
150    
151                            if (Validator.isNotNull(defaultRobots)) {
152                                    typeSettingsProperties = updateMetaField(
153                                            plid, typeSettingsProperties, "meta-robots_", "Robots",
154                                            "robots");
155                            }
156    
157                            String javaScript1 = typeSettingsProperties.getProperty(
158                                    "javascript-1");
159                            String javaScript2 = typeSettingsProperties.getProperty(
160                                    "javascript-2");
161                            String javaScript3 = typeSettingsProperties.getProperty(
162                                    "javascript-3");
163    
164                            if ((javaScript1 != null) || (javaScript2 != null) ||
165                                    (javaScript3 != null)) {
166    
167                                    updateJavaScript(
168                                            typeSettingsProperties, javaScript1, javaScript2,
169                                            javaScript3);
170                            }
171    
172                            updateTypeSettings(plid, typeSettingsProperties.toString());
173                    }
174            }
175    
176            protected UnicodeProperties updateMetaField(
177                            long plid, UnicodeProperties typeSettingsProperties,
178                            String propertyName, String xmlName, String columName)
179                    throws Exception {
180    
181                    String xml = null;
182    
183                    Locale[] locales = LanguageUtil.getAvailableLocales();
184    
185                    for (Locale locale : locales) {
186                            String languageId = LocaleUtil.toLanguageId(locale);
187    
188                            String value = typeSettingsProperties.getProperty(
189                                    propertyName + languageId);
190    
191                            if (Validator.isNotNull(value)) {
192                                    xml = LocalizationUtil.updateLocalization(
193                                            xml, xmlName, value, languageId);
194    
195                                    typeSettingsProperties.remove(propertyName + languageId);
196                            }
197                    }
198    
199                    Connection con = null;
200                    PreparedStatement ps = null;
201    
202                    try {
203                            con = DataAccess.getUpgradeOptimizedConnection();
204    
205                            ps = con.prepareStatement(
206                                    "update Layout set " + columName + " = ? where plid = " + plid);
207    
208                            ps.setString(1, xml);
209    
210                            ps.executeUpdate();
211                    }
212                    finally {
213                            DataAccess.cleanUp(con, ps);
214                    }
215    
216                    return typeSettingsProperties;
217            }
218    
219            protected void updateName(long plid, String name) throws Exception {
220                    Connection con = null;
221                    PreparedStatement ps = null;
222    
223                    try {
224                            con = DataAccess.getUpgradeOptimizedConnection();
225    
226                            ps = con.prepareStatement(
227                                    "update Layout set name = ? where plid = " + plid);
228    
229                            ps.setString(1, name);
230    
231                            ps.executeUpdate();
232                    }
233                    finally {
234                            DataAccess.cleanUp(con, ps);
235                    }
236            }
237    
238            protected void updateTitle(long plid, String title) throws Exception {
239                    Connection con = null;
240                    PreparedStatement ps = null;
241    
242                    try {
243                            con = DataAccess.getUpgradeOptimizedConnection();
244    
245                            ps = con.prepareStatement(
246                                    "update Layout set title = ? where plid = " + plid);
247    
248                            ps.setString(1, title);
249    
250                            ps.executeUpdate();
251                    }
252                    finally {
253                            DataAccess.cleanUp(con, ps);
254                    }
255            }
256    
257            protected void updateTypeSettings(long plid, String typeSettings)
258                    throws Exception {
259    
260                    Connection con = null;
261                    PreparedStatement ps = null;
262    
263                    try {
264                            con = DataAccess.getUpgradeOptimizedConnection();
265    
266                            ps = con.prepareStatement(
267                                    "update Layout set typeSettings = ? where plid = " + plid);
268    
269                            ps.setString(1, typeSettings);
270    
271                            ps.executeUpdate();
272                    }
273                    finally {
274                            DataAccess.cleanUp(con, ps);
275                    }
276            }
277    
278    }