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