001    /**
002     * Copyright (c) 2000-2012 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.v6_2_0;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.upgrade.v6_2_0.util.DDMTemplateTable;
020    import com.liferay.portal.util.PortalUtil;
021    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
022    
023    import java.sql.Connection;
024    import java.sql.PreparedStatement;
025    import java.sql.ResultSet;
026    import java.sql.SQLException;
027    
028    /**
029     * @author Juan Fernández
030     */
031    public class UpgradeDynamicDataMapping extends UpgradeProcess {
032    
033            @Override
034            protected void doUpgrade() throws Exception {
035                    try {
036                            runSQL("alter table DDMTemplate add classNameId LONG");
037    
038                            runSQL("alter table DDMTemplate add templateKey STRING");
039    
040                            runSQL("alter_column_name DDMTemplate structureId classPK LONG");
041                    }
042                    catch (SQLException sqle) {
043                            upgradeTable(
044                                    DDMTemplateTable.TABLE_NAME, DDMTemplateTable.TABLE_COLUMNS,
045                                    DDMTemplateTable.TABLE_SQL_CREATE,
046                                    DDMTemplateTable.TABLE_SQL_ADD_INDEXES);
047                    }
048    
049                    long classNameId = PortalUtil.getClassNameId(DDMTemplate.class);
050    
051                    runSQL("update DDMTemplate set classNameId = " + classNameId);
052    
053                    updateDDMStructureStructureKeys();
054            }
055    
056            private void updateDDMStructureStructureKeys() throws Exception {
057                    Connection con = null;
058                    PreparedStatement ps = null;
059                    ResultSet rs = null;
060    
061                    try {
062                            con = DataAccess.getUpgradeOptimizedConnection();
063    
064                            ps = con.prepareStatement("select * from DDMStructure");
065    
066                            rs = ps.executeQuery();
067    
068                            while (rs.next()) {
069                                    long structureId = rs.getLong("structureId");
070                                    String structureKey = rs.getString("structureKey");
071    
072                                    structureKey = structureKey.trim().toUpperCase();
073    
074                                    runSQL(
075                                            "update DDMStructure set structureKey = '" + structureKey +
076                                                    "' where structureId = " + structureId);
077                            }
078                    }
079                    finally {
080                            DataAccess.cleanUp(con, ps, rs);
081                    }
082            }
083    
084    }