001    /**
002     * Copyright (c) 2000-2013 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.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.getUpgradeOptimizedConnection();
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    }