1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.upgrade.v5_1_5;
24  
25  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
26  import com.liferay.portal.kernel.util.LocaleUtil;
27  import com.liferay.portal.kernel.util.UnicodeProperties;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.upgrade.UpgradeProcess;
30  
31  import java.sql.Connection;
32  import java.sql.PreparedStatement;
33  import java.sql.ResultSet;
34  
35  /**
36   * <a href="UpgradeLayout.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Samuel Kong
39   */
40  public class UpgradeLayout extends UpgradeProcess {
41  
42      protected void doUpgrade() throws Exception {
43          String languageId = LocaleUtil.toLanguageId(LocaleUtil.getDefault());
44  
45          Connection con = null;
46          PreparedStatement ps = null;
47          ResultSet rs = null;
48  
49          try {
50              con = DataAccess.getConnection();
51  
52              ps = con.prepareStatement(
53                  "select plid, typeSettings from Layout where typeSettings " +
54                      "like '%meta-description=%'");
55  
56              rs = ps.executeQuery();
57  
58              while (rs.next()) {
59                  long plid = rs.getLong("plid");
60                  String typeSettings = rs.getString("typeSettings");
61  
62                  UnicodeProperties typeSettingsProperties =
63                      new UnicodeProperties();
64  
65                  typeSettingsProperties.load(typeSettings);
66  
67                  String oldMetaDescription = typeSettingsProperties.getProperty(
68                      "meta-description");
69                  String newMetaDescription = typeSettingsProperties.getProperty(
70                      "meta-description_" + languageId);
71  
72                  if (Validator.isNotNull(oldMetaDescription) &&
73                      Validator.isNull(newMetaDescription)) {
74  
75                      typeSettingsProperties.setProperty(
76                          "meta-description_" + languageId, oldMetaDescription);
77                  }
78  
79                  typeSettingsProperties.remove("meta-description");
80  
81                  String oldMetaKeywords = typeSettingsProperties.getProperty(
82                      "meta-keywords");
83                  String newMetaKeywords = typeSettingsProperties.getProperty(
84                      "meta-keywords_" + languageId);
85  
86                  if (Validator.isNotNull(oldMetaKeywords) &&
87                      Validator.isNull(newMetaKeywords)) {
88  
89                      typeSettingsProperties.setProperty(
90                          "meta-keywords_" + languageId, oldMetaKeywords);
91                  }
92  
93                  typeSettingsProperties.remove("meta-keywords");
94  
95                  String oldMetaRobots = typeSettingsProperties.getProperty(
96                      "meta-robots");
97                  String newMetaRobots = typeSettingsProperties.getProperty(
98                      "meta-robots_" + languageId);
99  
100                 if (Validator.isNotNull(oldMetaRobots) &&
101                     Validator.isNull(newMetaRobots)) {
102 
103                     typeSettingsProperties.setProperty(
104                         "meta-robots_" + languageId, oldMetaRobots);
105                 }
106 
107                 typeSettingsProperties.remove("meta-robots");
108 
109                 updateTypeSettings(plid, typeSettingsProperties.toString());
110             }
111         }
112         finally {
113             DataAccess.cleanUp(con, ps, rs);
114         }
115     }
116 
117     protected void updateTypeSettings(long plid, String typeSettings)
118         throws Exception {
119 
120         Connection con = null;
121         PreparedStatement ps = null;
122         ResultSet rs = null;
123 
124         try {
125             con = DataAccess.getConnection();
126 
127             ps = con.prepareStatement(
128                 "update Layout set typeSettings = ? where plid = ?");
129 
130             ps.setString(1, typeSettings);
131             ps.setLong(2, plid);
132 
133             ps.executeUpdate();
134         }
135         finally {
136             DataAccess.cleanUp(con, ps, rs);
137         }
138     }
139 
140 }