001    /**
002     * Copyright (c) 2000-present 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.upgrade.UpgradeProcess;
018    import com.liferay.portal.kernel.util.LoggingTimer;
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.PreparedStatement;
025    import java.sql.ResultSet;
026    
027    /**
028     * @author Juan Fern??ndez
029     * @author Sergio Gonz??lez
030     */
031    public class UpgradeJournal extends UpgradeProcess {
032    
033            @Override
034            protected void doUpgrade() throws Exception {
035                    alter(
036                            JournalArticleTable.class,
037                            new AlterColumnType("title", "STRING null"));
038    
039                    alter(
040                            JournalStructureTable.class,
041                            new AlterColumnType("name", "STRING null"),
042                            new AlterColumnType("description", "STRING null"));
043    
044                    alter(
045                            JournalTemplateTable.class,
046                            new AlterColumnType("name", "STRING null"),
047                            new AlterColumnType("description", "STRING null"));
048    
049                    updateStructureXsd();
050            }
051    
052            protected void updateStructureXsd() throws Exception {
053                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
054                            runSQL(
055                                    "update JournalStructure set xsd = replace(xsd, " +
056                                            "'image_gallery', 'document_library') where xsd like " +
057                                                    "'%image_gallery%'");
058                    }
059                    catch (Exception e) {
060                            try (PreparedStatement ps = connection.prepareStatement(
061                                            "select id_, xsd from JournalStructure where xsd like " +
062                                                    "'%image_gallery%'");
063                                    ResultSet rs = ps.executeQuery()) {
064    
065                                    while (rs.next()) {
066                                            long id = rs.getLong("id_");
067                                            String xsd = rs.getString("xsd");
068    
069                                            xsd = StringUtil.replace(
070                                                    xsd, "image_gallery", "document_library");
071    
072                                            updateStructureXsd(id, xsd);
073                                    }
074                            }
075                    }
076            }
077    
078            protected void updateStructureXsd(long id, String xsd) throws Exception {
079                    try (PreparedStatement ps = connection.prepareStatement(
080                                    "update JournalStructure set xsd = ? where id_ = ?")) {
081    
082                            ps.setString(1, xsd);
083                            ps.setLong(2, id);
084    
085                            ps.executeUpdate();
086                    }
087            }
088    
089    }