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    import com.liferay.portal.upgrade.UpgradeProcessUtil;
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                    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.getUpgradeOptimizedConnection();
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) throws Exception {
221                    Connection con = null;
222                    PreparedStatement ps = null;
223    
224                    try {
225                            con = DataAccess.getUpgradeOptimizedConnection();
226    
227                            ps = con.prepareStatement(
228                                    "update Layout set name = ? where plid = " + plid);
229    
230                            ps.setString(1, name);
231    
232                            ps.executeUpdate();
233                    }
234                    finally {
235                            DataAccess.cleanUp(con, ps);
236                    }
237            }
238    
239            protected void updateTitle(long plid, String title) throws Exception {
240                    Connection con = null;
241                    PreparedStatement ps = null;
242    
243                    try {
244                            con = DataAccess.getUpgradeOptimizedConnection();
245    
246                            ps = con.prepareStatement(
247                                    "update Layout set title = ? where plid = " + plid);
248    
249                            ps.setString(1, title);
250    
251                            ps.executeUpdate();
252                    }
253                    finally {
254                            DataAccess.cleanUp(con, ps);
255                    }
256            }
257    
258            protected void updateTypeSettings(long plid, String typeSettings)
259                    throws Exception {
260    
261                    Connection con = null;
262                    PreparedStatement ps = null;
263    
264                    try {
265                            con = DataAccess.getUpgradeOptimizedConnection();
266    
267                            ps = con.prepareStatement(
268                                    "update Layout set typeSettings = ? where plid = " + plid);
269    
270                            ps.setString(1, typeSettings);
271    
272                            ps.executeUpdate();
273                    }
274                    finally {
275                            DataAccess.cleanUp(con, ps);
276                    }
277            }
278    
279    }