001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.upgrade;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.util.PortletKeys;
021    
022    import java.sql.Connection;
023    import java.sql.PreparedStatement;
024    import java.sql.ResultSet;
025    
026    /**
027     * @author Jorge Ferrer
028     * @author Brian Wing Shun Chan
029     */
030    public abstract class BaseUpgradePortletPreferences extends UpgradeProcess {
031    
032            protected void deletePortletPreferences(long portletPreferencesId)
033                    throws Exception {
034    
035                    runSQL(
036                            "delete from PortletPreferences where portletPreferencesId = " +
037                                    portletPreferencesId);
038            }
039    
040            @Override
041            protected void doUpgrade() throws Exception {
042                    updatePortletPreferences();
043            }
044    
045            protected Object[] getGroup(long groupId) throws Exception {
046                    Object[] group = null;
047    
048                    Connection con = null;
049                    PreparedStatement ps = null;
050                    ResultSet rs = null;
051    
052                    try {
053                            con = DataAccess.getConnection();
054    
055                            ps = con.prepareStatement(_GET_GROUP);
056    
057                            ps.setLong(1, groupId);
058    
059                            rs = ps.executeQuery();
060    
061                            while (rs.next()) {
062                                    long companyId = rs.getLong("companyId");
063    
064                                    group = new Object[] {groupId, companyId};
065                            }
066                    }
067                    finally {
068                            DataAccess.cleanUp(con, ps, rs);
069                    }
070    
071                    return group;
072            }
073    
074            protected Object[] getLayout(long plid) throws Exception {
075                    Object[] layout = null;
076    
077                    Connection con = null;
078                    PreparedStatement ps = null;
079                    ResultSet rs = null;
080    
081                    try {
082                            con = DataAccess.getConnection();
083    
084                            ps = con.prepareStatement(_GET_LAYOUT);
085    
086                            ps.setLong(1, plid);
087    
088                            rs = ps.executeQuery();
089    
090                            while (rs.next()) {
091                                    long groupId = rs.getLong("groupId");
092                                    long companyId = rs.getLong("companyId");
093                                    boolean privateLayout = rs.getBoolean("privateLayout");
094                                    long layoutId = rs.getLong("layoutId");
095    
096                                    layout = new Object[] {
097                                            groupId, companyId, privateLayout, layoutId};
098                            }
099                    }
100                    finally {
101                            DataAccess.cleanUp(con, ps, rs);
102                    }
103    
104                    return layout;
105            }
106    
107            protected String getLayoutUuid(long plid, long layoutId) throws Exception {
108                    Object[] layout = getLayout(plid);
109    
110                    if (layout == null) {
111                            return null;
112                    }
113    
114                    String uuid = null;
115    
116                    Connection con = null;
117                    PreparedStatement ps = null;
118                    ResultSet rs = null;
119    
120                    try {
121                            con = DataAccess.getConnection();
122    
123                            ps = con.prepareStatement(_GET_LAYOUT_UUID);
124    
125                            long groupId = (Long)layout[0];
126                            boolean privateLayout = (Boolean)layout[2];
127    
128                            ps.setLong(1, groupId);
129                            ps.setBoolean(2, privateLayout);
130                            ps.setLong(3, layoutId);
131    
132                            rs = ps.executeQuery();
133    
134                            if (rs.next()) {
135                                    uuid = rs.getString("uuid_");
136                            }
137                    }
138                    finally {
139                            DataAccess.cleanUp(con, ps, rs);
140                    }
141    
142                    return uuid;
143            }
144    
145            protected String[] getPortletIds() {
146                    return new String[0];
147            }
148    
149            protected String getUpdatePortletPreferencesWhereClause() {
150                    String[] portletIds = getPortletIds();
151    
152                    if (portletIds.length == 0) {
153                            throw new IllegalArgumentException(
154                                    "Subclasses must override getPortletIds or " +
155                                            "getUpdatePortletPreferencesWhereClause");
156                    }
157    
158                    StringBundler sb = new StringBundler(portletIds.length * 5 - 1);
159    
160                    for (int i = 0; i < portletIds.length; i++) {
161                            String portletId = portletIds[i];
162    
163                            sb.append("portletId ");
164    
165                            if (portletId.contains(StringPool.PERCENT)) {
166                                    sb.append(" like '");
167                                    sb.append(portletId);
168                                    sb.append("'");
169                            }
170                            else {
171                                    sb.append(" = '");
172                                    sb.append(portletId);
173                                    sb.append("'");
174                            }
175    
176                            if ((i + 1) < portletIds.length) {
177                                    sb.append(" or ");
178                            }
179                    }
180    
181                    return sb.toString();
182            }
183    
184            protected void updatePortletPreferences() throws Exception {
185                    Connection con = null;
186                    PreparedStatement ps = null;
187                    ResultSet rs = null;
188    
189                    try {
190                            con = DataAccess.getConnection();
191    
192                            ps = con.prepareStatement(
193                                    "select portletPreferencesId, ownerId, ownerType, plid, " +
194                                            "portletId, preferences from PortletPreferences where " +
195                                                    getUpdatePortletPreferencesWhereClause());
196    
197                            rs = ps.executeQuery();
198    
199                            while (rs.next()) {
200                                    long portletPreferencesId = rs.getLong("portletPreferencesId");
201                                    long ownerId = rs.getLong("ownerId");
202                                    int ownerType = rs.getInt("ownerType");
203                                    long plid = rs.getLong("plid");
204                                    String portletId = rs.getString("portletId");
205                                    String preferences = rs.getString("preferences");
206    
207                                    long companyId = 0;
208    
209                                    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) {
210                                            Object[] group = getGroup(ownerId);
211    
212                                            if (group != null) {
213                                                    companyId = (Long)group[1];
214                                            }
215                                    }
216                                    else {
217                                            Object[] layout = getLayout(plid);
218    
219                                            if (layout != null) {
220                                                    companyId = (Long)layout[1];
221                                            }
222                                    }
223    
224                                    if (companyId > 0) {
225                                            String newPreferences = upgradePreferences(
226                                                    companyId, ownerId, ownerType, plid, portletId,
227                                                    preferences);
228    
229                                            updatePortletPreferences(
230                                                    portletPreferencesId, newPreferences);
231                                    }
232                                    else {
233                                            deletePortletPreferences(portletPreferencesId);
234                                    }
235                            }
236                    }
237                    finally {
238                            DataAccess.cleanUp(con, ps, rs);
239                    }
240            }
241    
242            protected void updatePortletPreferences(
243                            long portletPreferencesId, String preferences)
244                    throws Exception {
245    
246                    Connection con = null;
247                    PreparedStatement ps = null;
248    
249                    try {
250                            con = DataAccess.getConnection();
251    
252                            ps = con.prepareStatement(
253                                    "update PortletPreferences set preferences = ? where " +
254                                            "portletPreferencesId = " + portletPreferencesId);
255    
256                            ps.setString(1, preferences);
257    
258                            ps.executeUpdate();
259                    }
260                    finally {
261                            DataAccess.cleanUp(con, ps);
262                    }
263            }
264    
265            protected abstract String upgradePreferences(
266                            long companyId, long ownerId, int ownerType, long plid,
267                            String portletId, String xml)
268                    throws Exception;
269    
270            private static final String _GET_GROUP =
271                    "select * from Group_ where groupId = ?";
272    
273            private static final String _GET_LAYOUT =
274                    "select * from Layout where plid = ?";
275    
276            private static final String _GET_LAYOUT_UUID =
277                    "select uuid_ from Layout where groupId = ? and privateLayout = ? " +
278                            "and layoutId = ?";
279    
280    }