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.v6_1_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.StringUtil;
020    import com.liferay.portal.upgrade.v6_1_0.util.JournalArticleTable;
021    import com.liferay.portal.upgrade.v6_1_0.util.JournalStructureTable;
022    import com.liferay.portal.upgrade.v6_1_0.util.JournalTemplateTable;
023    
024    import java.sql.Connection;
025    import java.sql.PreparedStatement;
026    import java.sql.ResultSet;
027    import java.sql.SQLException;
028    
029    /**
030     * @author Juan Fern??ndez
031     * @author Sergio Gonz??lez
032     */
033    public class UpgradeJournal extends UpgradeProcess {
034    
035            @Override
036            protected void doUpgrade() throws Exception {
037                    try {
038                            runSQL("alter_column_type JournalArticle title STRING null");
039    
040                            runSQL("alter_column_type JournalStructure name STRING null");
041                            runSQL(
042                                    "alter_column_type JournalStructure description STRING null");
043    
044                            runSQL("alter_column_type JournalTemplate name STRING null");
045                            runSQL("alter_column_type JournalTemplate description STRING null");
046                    }
047                    catch (SQLException sqle) {
048                            upgradeTable(
049                                    JournalArticleTable.TABLE_NAME,
050                                    JournalArticleTable.TABLE_COLUMNS,
051                                    JournalArticleTable.TABLE_SQL_CREATE,
052                                    JournalArticleTable.TABLE_SQL_ADD_INDEXES);
053    
054                            upgradeTable(
055                                    JournalStructureTable.TABLE_NAME,
056                                    JournalStructureTable.TABLE_COLUMNS,
057                                    JournalStructureTable.TABLE_SQL_CREATE,
058                                    JournalStructureTable.TABLE_SQL_ADD_INDEXES);
059    
060                            upgradeTable(
061                                    JournalTemplateTable.TABLE_NAME,
062                                    JournalTemplateTable.TABLE_COLUMNS,
063                                    JournalTemplateTable.TABLE_SQL_CREATE,
064                                    JournalTemplateTable.TABLE_SQL_ADD_INDEXES);
065                    }
066    
067                    updateStructureXsd();
068            }
069    
070            protected void updateStructureXsd() throws Exception {
071                    Connection con = null;
072                    PreparedStatement ps = null;
073                    ResultSet rs = null;
074    
075                    try {
076                            runSQL(
077                                    "update JournalStructure set xsd = replace(xsd, " +
078                                            "'image_gallery', 'document_library') where xsd like " +
079                                                    "'%image_gallery%'");
080                    }
081                    catch (Exception e) {
082                            con = DataAccess.getUpgradeOptimizedConnection();
083    
084                            ps = con.prepareStatement(
085                                    "select id_, xsd from JournalStructure where xsd like " +
086                                            "'%image_gallery%'");
087    
088                            rs = ps.executeQuery();
089    
090                            while (rs.next()) {
091                                    long id = rs.getLong("id_");
092                                    String xsd = rs.getString("xsd");
093    
094                                    xsd = StringUtil.replace(
095                                            xsd, "image_gallery", "document_library");
096    
097                                    updateStructureXsd(id, xsd);
098                            }
099                    }
100                    finally {
101                            DataAccess.cleanUp(con, ps, rs);
102                    }
103            }
104    
105            protected void updateStructureXsd(long id, String xsd) throws Exception {
106                    Connection con = null;
107                    PreparedStatement ps = null;
108                    ResultSet rs = null;
109    
110                    try {
111                            con = DataAccess.getUpgradeOptimizedConnection();
112    
113                            ps = con.prepareStatement(
114                                    "update JournalStructure set xsd = ? where id_ = ?");
115    
116                            ps.setString(1, xsd);
117                            ps.setLong(2, id);
118    
119                            ps.executeUpdate();
120                    }
121                    finally {
122                            DataAccess.cleanUp(con, ps, rs);
123                    }
124            }
125    
126    }