1
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
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 }