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.upgrade.UpgradeProcess;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.UnicodeProperties;
29  
30  import java.sql.Connection;
31  import java.sql.PreparedStatement;
32  import java.sql.ResultSet;
33  
34  /**
35   * <a href="UpgradeLayout.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Jorge Ferrer
38   */
39  public class UpgradeLayout extends UpgradeProcess {
40  
41      protected void doUpgrade() throws Exception {
42          Connection con = null;
43          PreparedStatement ps = null;
44          ResultSet rs = null;
45  
46          try {
47              con = DataAccess.getConnection();
48  
49              ps = con.prepareStatement(
50                  "select plid, typeSettings from Layout where type_ = " +
51                      "'link_to_layout'");
52  
53              rs = ps.executeQuery();
54  
55              while (rs.next()) {
56                  long plid = rs.getLong("plid");
57                  String typeSettings = rs.getString("typeSettings");
58  
59                  UnicodeProperties typeSettingsProperties =
60                      new UnicodeProperties(true);
61  
62                  typeSettingsProperties.load(typeSettings);
63  
64                  long linkToPlid = GetterUtil.getLong(
65                      typeSettingsProperties.getProperty("linkToPlid"));
66  
67                  if (linkToPlid > 0) {
68                      Object[] layout = getLayout(linkToPlid);
69  
70                      if (layout != null) {
71                          long groupId = (Long)layout[0];
72                          boolean privateLayout = (Boolean)layout[1];
73                          long layoutId = (Long)layout[2];
74  
75                          typeSettingsProperties.remove("linkToPlid");
76                          typeSettingsProperties.put(
77                              "groupId", String.valueOf(groupId));
78                          typeSettingsProperties.put(
79                              "privateLayout", String.valueOf(privateLayout));
80                          typeSettingsProperties.put(
81                              "linkToLayoutId", String.valueOf(layoutId));
82                      }
83                  }
84  
85                  updateTypeSettings(plid, typeSettingsProperties.toString());
86              }
87          }
88          finally {
89              DataAccess.cleanUp(con, ps, rs);
90          }
91      }
92  
93      protected Object[] getLayout(long plid) throws Exception {
94          Object[] layout = null;
95  
96          Connection con = null;
97          PreparedStatement ps = null;
98          ResultSet rs = null;
99  
100         try {
101             con = DataAccess.getConnection();
102 
103             ps = con.prepareStatement(_GET_LAYOUT);
104 
105             ps.setLong(1, plid);
106 
107             rs = ps.executeQuery();
108 
109             while (rs.next()) {
110                 long groupId = rs.getLong("groupId");
111                 boolean privateLayout = rs.getBoolean("privateLayout");
112                 long layoutId = rs.getLong("layoutId");
113 
114                 layout = new Object[] {groupId, privateLayout, layoutId};
115             }
116         }
117         finally {
118             DataAccess.cleanUp(con, ps, rs);
119         }
120 
121         return layout;
122     }
123 
124     protected void updateTypeSettings(long plid, String typeSettings)
125         throws Exception {
126 
127         Connection con = null;
128         PreparedStatement ps = null;
129 
130         try {
131             con = DataAccess.getConnection();
132 
133             ps = con.prepareStatement(
134                 "update Layout set typeSettings = ? where plid = " + plid);
135 
136             ps.setString(1, typeSettings);
137 
138             ps.executeUpdate();
139         }
140         finally {
141             DataAccess.cleanUp(con, ps);
142         }
143     }
144 
145     private static final String _GET_LAYOUT =
146         "select * from Layout where plid = ?";
147 
148 }