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