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