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