1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.v5_1_3;
24  
25  import com.liferay.portal.NoSuchLayoutException;
26  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.upgrade.UpgradeProcess;
31  import com.liferay.portlet.PortletPreferencesImpl;
32  import com.liferay.portlet.PortletPreferencesSerializer;
33  
34  import java.sql.Connection;
35  import java.sql.PreparedStatement;
36  import java.sql.ResultSet;
37  
38  /**
39   * <a href="UpgradeDocumentLibrary.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Samuel Kong
42   * @author Brian Wing Shun Chan
43   * @author Edward Shin
44   */
45  public class UpgradeDocumentLibrary extends UpgradeProcess {
46  
47      protected void deletePortletPreferences(long portletPreferencesId)
48          throws Exception {
49  
50          runSQL(
51              "delete from PortletPreferences where portletPreferencesId = " +
52                  portletPreferencesId);
53      }
54  
55      protected void doUpgrade() throws Exception {
56          Connection con = null;
57          PreparedStatement ps = null;
58          ResultSet rs = null;
59  
60          try {
61              con = DataAccess.getConnection();
62  
63              ps = con.prepareStatement(
64                  "select portletPreferencesId, ownerId, ownerType, plid, " +
65                      "portletId, preferences from PortletPreferences where " +
66                          "portletId = '20'");
67  
68              rs = ps.executeQuery();
69  
70              while (rs.next()) {
71                  long portletPreferencesId = rs.getLong("portletPreferencesId");
72                  long ownerId = rs.getLong("ownerId");
73                  int ownerType = rs.getInt("ownerType");
74                  long plid = rs.getLong("plid");
75                  String portletId = rs.getString("portletId");
76                  String preferences = rs.getString("preferences");
77  
78                  try {
79                      String newPreferences = upgradePreferences(
80                          ownerId, ownerType, plid, portletId, preferences);
81  
82                      updatePortletPreferences(
83                          portletPreferencesId, newPreferences);
84                  }
85                  catch (NoSuchLayoutException nsle) {
86                      deletePortletPreferences(portletPreferencesId);
87                  }
88              }
89          }
90          finally {
91              DataAccess.cleanUp(con, ps, rs);
92          }
93      }
94  
95      protected void updatePortletPreferences(
96              long portletPreferencesId, String preferences)
97          throws Exception {
98  
99          Connection con = null;
100         PreparedStatement ps = null;
101 
102         try {
103             con = DataAccess.getConnection();
104 
105             ps = con.prepareStatement(
106                 "update PortletPreferences set preferences = ? where " +
107                     "portletPreferencesId = " + portletPreferencesId);
108 
109             ps.setString(1, preferences);
110 
111             ps.executeUpdate();
112         }
113         finally {
114             DataAccess.cleanUp(con, ps);
115         }
116     }
117 
118     protected String upgradePreferences(
119             long ownerId, int ownerType, long plid, String portletId,
120             String xml)
121         throws Exception {
122 
123         long companyId = 0;
124 
125         Connection con = null;
126         PreparedStatement ps = null;
127         ResultSet rs = null;
128 
129         try {
130             con = DataAccess.getConnection();
131 
132             ps = con.prepareStatement(
133                 "select companyId from Layout where plid = ?");
134 
135             ps.setLong(1, plid);
136 
137             rs = ps.executeQuery();
138 
139             if (rs.next()) {
140                 companyId = rs.getLong("companyId");
141             }
142         }
143         finally {
144             DataAccess.cleanUp(con, ps, rs);
145         }
146 
147         if (companyId <= 0) {
148             throw new NoSuchLayoutException();
149         }
150 
151         PortletPreferencesImpl preferences =
152             PortletPreferencesSerializer.fromXML(
153                 companyId, ownerId, ownerType, plid, portletId, xml);
154 
155         String fileEntryColumns = preferences.getValue(
156             "fileEntryColumns", StringPool.BLANK);
157 
158         if (Validator.isNotNull(fileEntryColumns)) {
159             fileEntryColumns = StringUtil.replace(
160                 fileEntryColumns, "document", "name");
161 
162             preferences.setValue("fileEntryColumns", fileEntryColumns);
163         }
164 
165         return PortletPreferencesSerializer.toXML(preferences);
166     }
167 
168 }