1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.upgrade.v4_4_0;
16  
17  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
18  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.UnicodeProperties;
21  
22  import java.sql.Connection;
23  import java.sql.PreparedStatement;
24  import java.sql.ResultSet;
25  
26  /**
27   * <a href="UpgradeLayout.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Jorge Ferrer
30   */
31  public class UpgradeLayout extends UpgradeProcess {
32  
33      protected void doUpgrade() throws Exception {
34          Connection con = null;
35          PreparedStatement ps = null;
36          ResultSet rs = null;
37  
38          try {
39              con = DataAccess.getConnection();
40  
41              ps = con.prepareStatement(
42                  "select plid, typeSettings from Layout where type_ = " +
43                      "'link_to_layout'");
44  
45              rs = ps.executeQuery();
46  
47              while (rs.next()) {
48                  long plid = rs.getLong("plid");
49                  String typeSettings = rs.getString("typeSettings");
50  
51                  UnicodeProperties typeSettingsProperties =
52                      new UnicodeProperties(true);
53  
54                  typeSettingsProperties.load(typeSettings);
55  
56                  long linkToPlid = GetterUtil.getLong(
57                      typeSettingsProperties.getProperty("linkToPlid"));
58  
59                  if (linkToPlid > 0) {
60                      Object[] layout = getLayout(linkToPlid);
61  
62                      if (layout != null) {
63                          long groupId = (Long)layout[0];
64                          boolean privateLayout = (Boolean)layout[1];
65                          long layoutId = (Long)layout[2];
66  
67                          typeSettingsProperties.remove("linkToPlid");
68                          typeSettingsProperties.put(
69                              "groupId", String.valueOf(groupId));
70                          typeSettingsProperties.put(
71                              "privateLayout", String.valueOf(privateLayout));
72                          typeSettingsProperties.put(
73                              "linkToLayoutId", String.valueOf(layoutId));
74                      }
75                  }
76  
77                  updateTypeSettings(plid, typeSettingsProperties.toString());
78              }
79          }
80          finally {
81              DataAccess.cleanUp(con, ps, rs);
82          }
83      }
84  
85      protected Object[] getLayout(long plid) throws Exception {
86          Object[] layout = null;
87  
88          Connection con = null;
89          PreparedStatement ps = null;
90          ResultSet rs = null;
91  
92          try {
93              con = DataAccess.getConnection();
94  
95              ps = con.prepareStatement(_GET_LAYOUT);
96  
97              ps.setLong(1, plid);
98  
99              rs = ps.executeQuery();
100 
101             while (rs.next()) {
102                 long groupId = rs.getLong("groupId");
103                 boolean privateLayout = rs.getBoolean("privateLayout");
104                 long layoutId = rs.getLong("layoutId");
105 
106                 layout = new Object[] {groupId, privateLayout, layoutId};
107             }
108         }
109         finally {
110             DataAccess.cleanUp(con, ps, rs);
111         }
112 
113         return layout;
114     }
115 
116     protected void updateTypeSettings(long plid, String typeSettings)
117         throws Exception {
118 
119         Connection con = null;
120         PreparedStatement ps = null;
121 
122         try {
123             con = DataAccess.getConnection();
124 
125             ps = con.prepareStatement(
126                 "update Layout set typeSettings = ? where plid = " + plid);
127 
128             ps.setString(1, typeSettings);
129 
130             ps.executeUpdate();
131         }
132         finally {
133             DataAccess.cleanUp(con, ps);
134         }
135     }
136 
137     private static final String _GET_LAYOUT =
138         "select * from Layout where plid = ?";
139 
140 }