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