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.v5_2_3;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.UnicodeProperties;
021    import com.liferay.portal.kernel.util.Validator;
022    
023    import java.sql.Connection;
024    import java.sql.PreparedStatement;
025    import java.sql.ResultSet;
026    
027    /**
028     * @author Samuel Kong
029     */
030    public class UpgradeLayout extends UpgradeProcess {
031    
032            @Override
033            protected void doUpgrade() throws Exception {
034                    String languageId = LocaleUtil.toLanguageId(LocaleUtil.getDefault());
035    
036                    Connection con = null;
037                    PreparedStatement ps = null;
038                    ResultSet rs = null;
039    
040                    try {
041                            con = DataAccess.getUpgradeOptimizedConnection();
042    
043                            ps = con.prepareStatement(
044                                    "select plid, typeSettings from Layout where typeSettings " +
045                                            "like '%meta-description=%'");
046    
047                            rs = ps.executeQuery();
048    
049                            while (rs.next()) {
050                                    long plid = rs.getLong("plid");
051                                    String typeSettings = rs.getString("typeSettings");
052    
053                                    UnicodeProperties typeSettingsProperties =
054                                            new UnicodeProperties(true);
055    
056                                    typeSettingsProperties.load(typeSettings);
057    
058                                    String oldMetaDescription = typeSettingsProperties.getProperty(
059                                            "meta-description");
060                                    String newMetaDescription = typeSettingsProperties.getProperty(
061                                            "meta-description_" + languageId);
062    
063                                    if (Validator.isNotNull(oldMetaDescription) &&
064                                            Validator.isNull(newMetaDescription)) {
065    
066                                            typeSettingsProperties.setProperty(
067                                                    "meta-description_" + languageId, oldMetaDescription);
068                                    }
069    
070                                    typeSettingsProperties.remove("meta-description");
071    
072                                    String oldMetaKeywords = typeSettingsProperties.getProperty(
073                                            "meta-keywords");
074                                    String newMetaKeywords = typeSettingsProperties.getProperty(
075                                            "meta-keywords_" + languageId);
076    
077                                    if (Validator.isNotNull(oldMetaKeywords) &&
078                                            Validator.isNull(newMetaKeywords)) {
079    
080                                            typeSettingsProperties.setProperty(
081                                                    "meta-keywords_" + languageId, oldMetaKeywords);
082                                    }
083    
084                                    typeSettingsProperties.remove("meta-keywords");
085    
086                                    String oldMetaRobots = typeSettingsProperties.getProperty(
087                                            "meta-robots");
088                                    String newMetaRobots = typeSettingsProperties.getProperty(
089                                            "meta-robots_" + languageId);
090    
091                                    if (Validator.isNotNull(oldMetaRobots) &&
092                                            Validator.isNull(newMetaRobots)) {
093    
094                                            typeSettingsProperties.setProperty(
095                                                    "meta-robots_" + languageId, oldMetaRobots);
096                                    }
097    
098                                    typeSettingsProperties.remove("meta-robots");
099    
100                                    updateTypeSettings(plid, typeSettingsProperties.toString());
101                            }
102                    }
103                    finally {
104                            DataAccess.cleanUp(con, ps, rs);
105                    }
106            }
107    
108            protected void updateTypeSettings(long plid, String typeSettings)
109                    throws Exception {
110    
111                    Connection con = null;
112                    PreparedStatement ps = null;
113    
114                    try {
115                            con = DataAccess.getUpgradeOptimizedConnection();
116    
117                            ps = con.prepareStatement(
118                                    "update Layout set typeSettings = ? where plid = " + plid);
119    
120                            ps.setString(1, typeSettings);
121    
122                            ps.executeUpdate();
123                    }
124                    finally {
125                            DataAccess.cleanUp(con, ps);
126                    }
127            }
128    
129    }