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.upgrade.v5_2_2;
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 Julio Camarero
026     */
027    public class UpgradeWebForm extends UpgradeProcess {
028    
029            @Override
030            protected void doUpgrade() throws Exception {
031                    long oldClassNameId = getClassNameId(_OLD_WEBFORM_CLASS_NAME);
032    
033                    if (oldClassNameId == 0) {
034                            return;
035                    }
036    
037                    long newClassNameId = getClassNameId(_NEW_WEBFORM_CLASS_NAME);
038    
039                    if (newClassNameId == 0) {
040                            runSQL(
041                                    "update ClassName_ set value = '" +
042                                            _NEW_WEBFORM_CLASS_NAME + "' where value = '" +
043                                                    _OLD_WEBFORM_CLASS_NAME + "'");
044                    }
045                    else {
046                            runSQL(
047                                    "update ExpandoTable set classNameId = '" + newClassNameId +
048                                            "' where classNameId = '" + oldClassNameId + "'");
049    
050                            runSQL(
051                                    "update ExpandoValue set classNameId = '" + newClassNameId +
052                                            "' where classNameId = '" + oldClassNameId + "'");
053    
054                            runSQL(
055                                    "delete from ClassName_ where value = '" +
056                                            _OLD_WEBFORM_CLASS_NAME + "'");
057                    }
058            }
059    
060            protected long getClassNameId(String className) throws Exception {
061                    Connection con = null;
062                    PreparedStatement ps = null;
063                    ResultSet rs = null;
064    
065                    try {
066                            con = DataAccess.getConnection();
067    
068                            ps = con.prepareStatement(
069                                    "select classNameId from ClassName_ where value = ?");
070    
071                            ps.setString(1, className);
072    
073                            rs = ps.executeQuery();
074    
075                            if (rs.next()) {
076                                    return rs.getLong("classNameId");
077                            }
078    
079                            return 0;
080                    }
081                    finally {
082                            DataAccess.cleanUp(con, ps, rs);
083                    }
084            }
085    
086            private static final String _NEW_WEBFORM_CLASS_NAME =
087                    "com.liferay.webform.util.WebFormUtil";
088    
089            private static final String _OLD_WEBFORM_CLASS_NAME =
090                    "com.liferay.portlet.webform.util.WebFormUtil";
091    
092    }