1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.upgrade.v5_1_3;
21  
22  import com.liferay.portal.NoSuchLayoutException;
23  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.service.LayoutLocalServiceUtil;
31  import com.liferay.portal.upgrade.UpgradeException;
32  import com.liferay.portal.upgrade.UpgradeProcess;
33  import com.liferay.portlet.PortletPreferencesImpl;
34  import com.liferay.portlet.PortletPreferencesSerializer;
35  
36  import java.sql.Connection;
37  import java.sql.PreparedStatement;
38  import java.sql.ResultSet;
39  
40  /**
41   * <a href="UpgradeDocumentLibrary.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Samuel Kong
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class UpgradeDocumentLibrary extends UpgradeProcess {
48  
49      public void upgrade() throws UpgradeException {
50          _log.info("Upgrading");
51  
52          try {
53              doUpgrade();
54          }
55          catch (Exception e) {
56              throw new UpgradeException(e);
57          }
58      }
59  
60      protected void deletePortletPreferences(long portletPreferencesId)
61          throws Exception {
62  
63          runSQL(
64              "delete from PortletPreferences where portletPreferencesId = " +
65                  portletPreferencesId);
66      }
67  
68      protected void doUpgrade() throws Exception {
69          Connection con = null;
70          PreparedStatement ps = null;
71          ResultSet rs = null;
72  
73          try {
74              con = DataAccess.getConnection();
75  
76              ps = con.prepareStatement(
77                  "select portletPreferencesId, ownerId, ownerType, plid, " +
78                      "portletId, preferences from PortletPreferences where " +
79                          "portletId = '20'");
80  
81              rs = ps.executeQuery();
82  
83              while (rs.next()) {
84                  long portletPreferencesId = rs.getLong("portletPreferencesId");
85                  long ownerId = rs.getLong("ownerId");
86                  int ownerType = rs.getInt("ownerType");
87                  long plid = rs.getLong("plid");
88                  String portletId = rs.getString("portletId");
89                  String preferences = rs.getString("preferences");
90  
91                  try {
92                      Layout layout = LayoutLocalServiceUtil.getLayout(plid);
93  
94                      String newPreferences = upgradePreferences(
95                          layout.getCompanyId(), ownerId, ownerType, plid,
96                          portletId, preferences);
97  
98                      updatePortletPreferences(
99                          portletPreferencesId, newPreferences);
100                 }
101                 catch (NoSuchLayoutException nsle) {
102                     deletePortletPreferences(portletPreferencesId);
103                 }
104             }
105         }
106         finally {
107             DataAccess.cleanUp(con, ps, rs);
108         }
109     }
110 
111     protected void updatePortletPreferences(
112             long portletPreferencesId, String preferences)
113         throws Exception {
114 
115         Connection con = null;
116         PreparedStatement ps = null;
117 
118         try {
119             con = DataAccess.getConnection();
120 
121             ps = con.prepareStatement(
122                 "update PortletPreferences set preferences = ? where " +
123                     "portletPreferencesId = " + portletPreferencesId);
124 
125             ps.setString(1, preferences);
126 
127             ps.executeUpdate();
128         }
129         finally {
130             DataAccess.cleanUp(con, ps);
131         }
132     }
133 
134     protected String upgradePreferences(
135             long companyId, long ownerId, int ownerType, long plid,
136             String portletId, String xml)
137         throws Exception {
138 
139         PortletPreferencesImpl preferences =
140             PortletPreferencesSerializer.fromXML(
141                 companyId, ownerId, ownerType, plid, portletId, xml);
142 
143         String fileEntryColumns = preferences.getValue(
144             "fileEntryColumns", StringPool.BLANK);
145 
146         if (Validator.isNotNull(fileEntryColumns)) {
147             fileEntryColumns = StringUtil.replace(
148                 fileEntryColumns, "document", "name");
149 
150             preferences.setValue("fileEntryColumns", fileEntryColumns);
151         }
152 
153         return PortletPreferencesSerializer.toXML(preferences);
154     }
155 
156     private static Log _log =
157          LogFactoryUtil.getLog(UpgradeDocumentLibrary.class);
158 
159 }