1
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.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 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43
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 Connection con = null;
67 PreparedStatement ps = null;
68
69 try {
70 con = DataAccess.getConnection();
71
72 ps = con.prepareStatement(
73 "delete from PortletPreferences where portletPreferencesId = " +
74 portletPreferencesId);
75
76 ps.executeUpdate();
77 }
78 finally {
79 DataAccess.cleanUp(con, ps);
80 }
81 }
82
83 protected void doUpgrade() throws Exception {
84 Connection con = null;
85 PreparedStatement ps = null;
86 ResultSet rs = null;
87
88 try {
89 con = DataAccess.getConnection();
90
91 ps = con.prepareStatement(
92 "select portletPreferencesId, ownerId, ownerType, plid, " +
93 "portletId, preferences from PortletPreferences where " +
94 "portletId = 20");
95
96 rs = ps.executeQuery();
97
98 while (rs.next()) {
99 long portletPreferencesId = rs.getLong("portletPreferencesId");
100 long ownerId = rs.getLong("ownerId");
101 int ownerType = rs.getInt("ownerType");
102 long plid = rs.getLong("plid");
103 String portletId = rs.getString("portletId");
104 String preferences = rs.getString("preferences");
105
106 try {
107 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
108
109 String newPreferences = upgradePreferences(
110 layout.getCompanyId(), ownerId, ownerType, plid,
111 portletId, preferences);
112
113 updatePortletPreferences(
114 portletPreferencesId, newPreferences);
115 }
116 catch (NoSuchLayoutException nsle) {
117 deletePortletPreferences(portletPreferencesId);
118 }
119 }
120 }
121 finally {
122 DataAccess.cleanUp(con, ps, rs);
123 }
124 }
125
126 protected void updatePortletPreferences(
127 long portletPreferencesId, String preferences)
128 throws Exception {
129
130 Connection con = null;
131 PreparedStatement ps = null;
132
133 try {
134 con = DataAccess.getConnection();
135
136 ps = con.prepareStatement(
137 "update PortletPreferences set preferences = ? where " +
138 "portletPreferencesId = " + portletPreferencesId);
139
140 ps.setString(1, preferences);
141
142 ps.executeUpdate();
143 }
144 finally {
145 DataAccess.cleanUp(con, ps);
146 }
147 }
148
149 protected String upgradePreferences(
150 long companyId, long ownerId, int ownerType, long plid,
151 String portletId, String xml)
152 throws Exception {
153
154 PortletPreferencesImpl preferences =
155 PortletPreferencesSerializer.fromXML(
156 companyId, ownerId, ownerType, plid, portletId, xml);
157
158 String fileEntryColumns = preferences.getValue(
159 "fileEntryColumns", StringPool.BLANK);
160
161 fileEntryColumns = StringUtil.replace(
162 fileEntryColumns, "document", "name");
163
164 preferences.setValue("fileEntryColumns", fileEntryColumns);
165
166 return PortletPreferencesSerializer.toXML(preferences);
167 }
168
169 private static Log _log = LogFactory.getLog(UpgradeDocumentLibrary.class);
170
171 }