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.v5_2_0;
24  
25  import com.liferay.portal.NoSuchLayoutException;
26  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.Layout;
33  import com.liferay.portal.service.LayoutLocalServiceUtil;
34  import com.liferay.portal.upgrade.UpgradeException;
35  import com.liferay.portal.upgrade.UpgradeProcess;
36  import com.liferay.portlet.PortletPreferencesImpl;
37  import com.liferay.portlet.PortletPreferencesSerializer;
38  
39  import java.sql.Connection;
40  import java.sql.PreparedStatement;
41  import java.sql.ResultSet;
42  
43  /**
44   * <a href="UpgradeDocumentLibrary.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Samuel Kong
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class UpgradeDocumentLibrary extends UpgradeProcess {
51  
52      public void upgrade() throws UpgradeException {
53          _log.info("Upgrading");
54  
55          try {
56              doUpgrade();
57          }
58          catch (Exception e) {
59              throw new UpgradeException(e);
60          }
61      }
62  
63      protected void deletePortletPreferences(long portletPreferencesId)
64          throws Exception {
65  
66          runSQL(
67              "delete from PortletPreferences where portletPreferencesId = " +
68                  portletPreferencesId);
69      }
70  
71      protected void doUpgrade() throws Exception {
72          Connection con = null;
73          PreparedStatement ps = null;
74          ResultSet rs = null;
75  
76          try {
77              con = DataAccess.getConnection();
78  
79              ps = con.prepareStatement(
80                  "select portletPreferencesId, ownerId, ownerType, plid, " +
81                      "portletId, preferences from PortletPreferences where " +
82                          "portletId = '20'");
83  
84              rs = ps.executeQuery();
85  
86              while (rs.next()) {
87                  long portletPreferencesId = rs.getLong("portletPreferencesId");
88                  long ownerId = rs.getLong("ownerId");
89                  int ownerType = rs.getInt("ownerType");
90                  long plid = rs.getLong("plid");
91                  String portletId = rs.getString("portletId");
92                  String preferences = rs.getString("preferences");
93  
94                  try {
95                      Layout layout = LayoutLocalServiceUtil.getLayout(plid);
96  
97                      String newPreferences = upgradePreferences(
98                          layout.getCompanyId(), ownerId, ownerType, plid,
99                          portletId, preferences);
100 
101                     updatePortletPreferences(
102                         portletPreferencesId, newPreferences);
103                 }
104                 catch (NoSuchLayoutException nsle) {
105                     deletePortletPreferences(portletPreferencesId);
106                 }
107             }
108         }
109         finally {
110             DataAccess.cleanUp(con, ps, rs);
111         }
112     }
113 
114     protected void updatePortletPreferences(
115             long portletPreferencesId, String preferences)
116         throws Exception {
117 
118         Connection con = null;
119         PreparedStatement ps = null;
120 
121         try {
122             con = DataAccess.getConnection();
123 
124             ps = con.prepareStatement(
125                 "update PortletPreferences set preferences = ? where " +
126                     "portletPreferencesId = " + portletPreferencesId);
127 
128             ps.setString(1, preferences);
129 
130             ps.executeUpdate();
131         }
132         finally {
133             DataAccess.cleanUp(con, ps);
134         }
135     }
136 
137     protected String upgradePreferences(
138             long companyId, long ownerId, int ownerType, long plid,
139             String portletId, String xml)
140         throws Exception {
141 
142         PortletPreferencesImpl preferences =
143             PortletPreferencesSerializer.fromXML(
144                 companyId, ownerId, ownerType, plid, portletId, xml);
145 
146         String fileEntryColumns = preferences.getValue(
147             "fileEntryColumns", StringPool.BLANK);
148 
149         if (Validator.isNotNull(fileEntryColumns)) {
150             fileEntryColumns = StringUtil.replace(
151                 fileEntryColumns, "document", "name");
152 
153             preferences.setValue("fileEntryColumns", fileEntryColumns);
154         }
155 
156         return PortletPreferencesSerializer.toXML(preferences);
157     }
158 
159     private static Log _log =
160         LogFactoryUtil.getLog(UpgradeDocumentLibrary.class);
161 
162 }