1   /**
2    * Copyright (c) 2000-2009 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.dao.jdbc.DataAccess;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.UnicodeProperties;
28  import com.liferay.portal.model.Layout;
29  import com.liferay.portal.service.LayoutLocalServiceUtil;
30  import com.liferay.portal.upgrade.UpgradeProcess;
31  
32  import java.sql.Connection;
33  import java.sql.PreparedStatement;
34  import java.sql.ResultSet;
35  
36  /**
37   * <a href="UpgradeLayout.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Jorge Ferrer
40   */
41  public class UpgradeLayout extends UpgradeProcess {
42  
43      protected void doUpgrade() throws Exception {
44          Connection con = null;
45          PreparedStatement ps = null;
46          ResultSet rs = null;
47  
48          try {
49              con = DataAccess.getConnection();
50  
51              ps = con.prepareStatement(
52                  "select plid, typeSettings from Layout where type_ = " +
53                      "'link_to_layout'");
54  
55              rs = ps.executeQuery();
56  
57              while (rs.next()) {
58                  long plid = rs.getLong("plid");
59                  String typeSettings = rs.getString("typeSettings");
60  
61                  String newTypeSettings = upgradeTypeSettings(typeSettings);
62  
63                  ps = con.prepareStatement(
64                      "update Layout set typeSettings = ? where plid = " +
65                          plid);
66  
67                  ps.setString(1, newTypeSettings);
68  
69                  ps.executeUpdate();
70  
71                  ps.close();
72              }
73          }
74          finally {
75              DataAccess.cleanUp(con, ps, rs);
76          }
77      }
78  
79      protected String upgradeTypeSettings(String typeSettings) throws Exception {
80          UnicodeProperties props = new UnicodeProperties(true);
81  
82          props.load(typeSettings);
83  
84          long linkToPlid = GetterUtil.getLong(props.getProperty("linkToPlid"));
85  
86          if (linkToPlid > 0) {
87              Layout layout = LayoutLocalServiceUtil.getLayout(linkToPlid);
88  
89              props.remove("linkToPlid");
90              props.put("groupId", String.valueOf(layout.getGroupId()));
91              props.put(
92                  "privateLayout", String.valueOf(layout.isPrivateLayout()));
93              props.put("linkToLayoutId", String.valueOf(layout.getLayoutId()));
94          }
95  
96          return props.toString();
97      }
98  
99  }