1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.v4_4_0;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.PropertiesUtil;
27  import com.liferay.portal.kernel.util.SafeProperties;
28  import com.liferay.portal.model.Layout;
29  import com.liferay.portal.service.LayoutLocalServiceUtil;
30  import com.liferay.portal.spring.hibernate.HibernateUtil;
31  import com.liferay.portal.upgrade.UpgradeException;
32  import com.liferay.portal.upgrade.UpgradeProcess;
33  import com.liferay.util.dao.DataAccess;
34  
35  import java.sql.Connection;
36  import java.sql.PreparedStatement;
37  import java.sql.ResultSet;
38  
39  import java.util.Properties;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  /**
45   * <a href="UpgradeLayout.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Jorge Ferrer
48   *
49   */
50  public class UpgradeLayout extends UpgradeProcess {
51  
52      public void upgrade() throws UpgradeException {
53          _log.info("Upgrading");
54  
55          try {
56              doUpgrade();
57          }
58          catch (Exception e) {
59              throw new UpgradeException(e);
60          }
61      }
62  
63      protected void doUpgrade() throws Exception {
64          Connection con = null;
65          PreparedStatement ps = null;
66          ResultSet rs = null;
67  
68          try {
69              con = HibernateUtil.getConnection();
70  
71              ps = con.prepareStatement(
72                  "select plid, typeSettings from Layout where type_ = " +
73                      "'link_to_layout'");
74  
75              rs = ps.executeQuery();
76  
77              while (rs.next()) {
78                  long plid = rs.getLong("plid");
79                  String typeSettings = rs.getString("typeSettings");
80  
81                  String newTypeSettings = upgradeTypeSettings(typeSettings);
82  
83                  ps = con.prepareStatement(
84                      "update Layout set typeSettings = ? where plid = " +
85                          plid);
86  
87                  ps.setString(1, newTypeSettings);
88  
89                  ps.executeUpdate();
90  
91                  ps.close();
92              }
93          }
94          finally {
95              DataAccess.cleanUp(con, ps, rs);
96          }
97      }
98  
99      protected String upgradeTypeSettings(String typeSettings) throws Exception {
100         Properties props = new SafeProperties();
101 
102         PropertiesUtil.load(props, typeSettings);
103 
104         long linkToPlid = GetterUtil.getLong(props.getProperty("linkToPlid"));
105 
106         if (linkToPlid > 0) {
107             Layout layout = LayoutLocalServiceUtil.getLayout(linkToPlid);
108 
109             props.remove("linkToPlid");
110             props.put("groupId", String.valueOf(layout.getGroupId()));
111             props.put(
112                 "privateLayout", String.valueOf(layout.isPrivateLayout()));
113             props.put("linkToLayoutId", String.valueOf(layout.getLayoutId()));
114         }
115 
116         return PropertiesUtil.toString(props);
117     }
118 
119     private static Log _log = LogFactory.getLog(UpgradeLayout.class);
120 
121 }