001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.upgrade.v4_4_0;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.UnicodeProperties;
021    
022    import java.sql.Connection;
023    import java.sql.PreparedStatement;
024    import java.sql.ResultSet;
025    
026    /**
027     * @author Jorge Ferrer
028     */
029    public class UpgradeLayout extends UpgradeProcess {
030    
031            protected void doUpgrade() throws Exception {
032                    Connection con = null;
033                    PreparedStatement ps = null;
034                    ResultSet rs = null;
035    
036                    try {
037                            con = DataAccess.getConnection();
038    
039                            ps = con.prepareStatement(
040                                    "select plid, typeSettings from Layout where type_ = " +
041                                            "'link_to_layout'");
042    
043                            rs = ps.executeQuery();
044    
045                            while (rs.next()) {
046                                    long plid = rs.getLong("plid");
047                                    String typeSettings = rs.getString("typeSettings");
048    
049                                    UnicodeProperties typeSettingsProperties =
050                                            new UnicodeProperties(true);
051    
052                                    typeSettingsProperties.load(typeSettings);
053    
054                                    long linkToPlid = GetterUtil.getLong(
055                                            typeSettingsProperties.getProperty("linkToPlid"));
056    
057                                    if (linkToPlid > 0) {
058                                            Object[] layout = getLayout(linkToPlid);
059    
060                                            if (layout != null) {
061                                                    long groupId = (Long)layout[0];
062                                                    boolean privateLayout = (Boolean)layout[1];
063                                                    long layoutId = (Long)layout[2];
064    
065                                                    typeSettingsProperties.remove("linkToPlid");
066                                                    typeSettingsProperties.put(
067                                                            "groupId", String.valueOf(groupId));
068                                                    typeSettingsProperties.put(
069                                                            "privateLayout", String.valueOf(privateLayout));
070                                                    typeSettingsProperties.put(
071                                                            "linkToLayoutId", String.valueOf(layoutId));
072                                            }
073                                    }
074    
075                                    updateTypeSettings(plid, typeSettingsProperties.toString());
076                            }
077                    }
078                    finally {
079                            DataAccess.cleanUp(con, ps, rs);
080                    }
081            }
082    
083            protected Object[] getLayout(long plid) throws Exception {
084                    Object[] layout = null;
085    
086                    Connection con = null;
087                    PreparedStatement ps = null;
088                    ResultSet rs = null;
089    
090                    try {
091                            con = DataAccess.getConnection();
092    
093                            ps = con.prepareStatement(_GET_LAYOUT);
094    
095                            ps.setLong(1, plid);
096    
097                            rs = ps.executeQuery();
098    
099                            while (rs.next()) {
100                                    long groupId = rs.getLong("groupId");
101                                    boolean privateLayout = rs.getBoolean("privateLayout");
102                                    long layoutId = rs.getLong("layoutId");
103    
104                                    layout = new Object[] {groupId, privateLayout, layoutId};
105                            }
106                    }
107                    finally {
108                            DataAccess.cleanUp(con, ps, rs);
109                    }
110    
111                    return layout;
112            }
113    
114            protected void updateTypeSettings(long plid, String typeSettings)
115                    throws Exception {
116    
117                    Connection con = null;
118                    PreparedStatement ps = null;
119    
120                    try {
121                            con = DataAccess.getConnection();
122    
123                            ps = con.prepareStatement(
124                                    "update Layout set typeSettings = ? where plid = " + plid);
125    
126                            ps.setString(1, typeSettings);
127    
128                            ps.executeUpdate();
129                    }
130                    finally {
131                            DataAccess.cleanUp(con, ps);
132                    }
133            }
134    
135            private static final String _GET_LAYOUT =
136                    "select * from Layout where plid = ?";
137    
138    }