001    /**
002     * Copyright (c) 2000-2010 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.upgrade;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    
020    import java.sql.Connection;
021    import java.sql.PreparedStatement;
022    import java.sql.ResultSet;
023    
024    /**
025     * @author Jorge Ferrer
026     * @author Brian Wing Shun Chan
027     */
028    public abstract class BaseUpgradePortletPreferences extends UpgradeProcess {
029    
030            protected void deletePortletPreferences(long portletPreferencesId)
031                    throws Exception {
032    
033                    runSQL(
034                            "delete from PortletPreferences where portletPreferencesId = " +
035                                    portletPreferencesId);
036            }
037    
038            protected void doUpgrade() throws Exception {
039                    updatePortletPreferences();
040            }
041    
042            protected Object[] getLayout(long plid) throws Exception {
043                    Object[] layout = null;
044    
045                    Connection con = null;
046                    PreparedStatement ps = null;
047                    ResultSet rs = null;
048    
049                    try {
050                            con = DataAccess.getConnection();
051    
052                            ps = con.prepareStatement(_GET_LAYOUT);
053    
054                            ps.setLong(1, plid);
055    
056                            rs = ps.executeQuery();
057    
058                            while (rs.next()) {
059                                    long groupId = rs.getLong("groupId");
060                                    long companyId = rs.getLong("companyId");
061                                    boolean privateLayout = rs.getBoolean("privateLayout");
062                                    long layoutId = rs.getLong("layoutId");
063    
064                                    layout = new Object[] {
065                                            groupId, companyId, privateLayout, layoutId};
066                            }
067                    }
068                    finally {
069                            DataAccess.cleanUp(con, ps, rs);
070                    }
071    
072                    return layout;
073            }
074    
075            protected String getLayoutUuid(long plid, long layoutId) throws Exception {
076                    Object[] layout = getLayout(plid);
077    
078                    if (layout == null) {
079                            return null;
080                    }
081    
082                    String uuid = null;
083    
084                    Connection con = null;
085                    PreparedStatement ps = null;
086                    ResultSet rs = null;
087    
088                    try {
089                            con = DataAccess.getConnection();
090    
091                            ps = con.prepareStatement(_GET_LAYOUT_UUID);
092    
093                            long groupId = (Long)layout[0];
094                            boolean privateLayout = (Boolean)layout[2];
095    
096                            ps.setLong(1, groupId);
097                            ps.setBoolean(2, privateLayout);
098                            ps.setLong(3, layoutId);
099    
100                            rs = ps.executeQuery();
101    
102                            if (rs.next()) {
103                                    uuid = rs.getString("uuid_");
104                            }
105                    }
106                    finally {
107                            DataAccess.cleanUp(con, ps, rs);
108                    }
109    
110                    return uuid;
111            }
112    
113            protected abstract String getUpdatePortletPreferencesWhereClause();
114    
115            protected void updatePortletPreferences() throws Exception {
116                    Connection con = null;
117                    PreparedStatement ps = null;
118                    ResultSet rs = null;
119    
120                    try {
121                            con = DataAccess.getConnection();
122    
123                            ps = con.prepareStatement(
124                                    "select portletPreferencesId, ownerId, ownerType, plid, " +
125                                            "portletId, preferences from PortletPreferences where " +
126                                                    getUpdatePortletPreferencesWhereClause());
127    
128                            rs = ps.executeQuery();
129    
130                            while (rs.next()) {
131                                    long portletPreferencesId = rs.getLong("portletPreferencesId");
132                                    long ownerId = rs.getLong("ownerId");
133                                    int ownerType = rs.getInt("ownerType");
134                                    long plid = rs.getLong("plid");
135                                    String portletId = rs.getString("portletId");
136                                    String preferences = rs.getString("preferences");
137    
138                                    Object[] layout = getLayout(plid);
139    
140                                    if (layout != null) {
141                                            long companyId = (Long)layout[1];
142    
143                                            String newPreferences = upgradePreferences(
144                                                    companyId, ownerId, ownerType, plid, portletId,
145                                                    preferences);
146    
147                                            updatePortletPreferences(
148                                                    portletPreferencesId, newPreferences);
149                                    }
150                                    else {
151                                            deletePortletPreferences(portletPreferencesId);
152                                    }
153                            }
154                    }
155                    finally {
156                            DataAccess.cleanUp(con, ps, rs);
157                    }
158            }
159    
160            protected void updatePortletPreferences(
161                            long portletPreferencesId, String preferences)
162                    throws Exception {
163    
164                    Connection con = null;
165                    PreparedStatement ps = null;
166    
167                    try {
168                            con = DataAccess.getConnection();
169    
170                            ps = con.prepareStatement(
171                                    "update PortletPreferences set preferences = ? where " +
172                                            "portletPreferencesId = " + portletPreferencesId);
173    
174                            ps.setString(1, preferences);
175    
176                            ps.executeUpdate();
177                    }
178                    finally {
179                            DataAccess.cleanUp(con, ps);
180                    }
181            }
182    
183            protected abstract String upgradePreferences(
184                            long companyId, long ownerId, int ownerType, long plid,
185                            String portletId, String xml)
186                    throws Exception;
187    
188            private static final String _GET_LAYOUT =
189                    "select * from Layout where plid = ?";
190    
191            private static final String _GET_LAYOUT_UUID =
192                    "select uuid_ from Layout where groupId = ? and privateLayout = ? " +
193                            "and layoutId = ?";
194    
195    }