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_0_12_to_6_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.util.PortalUtil;
020    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
021    
022    import java.sql.Connection;
023    import java.sql.PreparedStatement;
024    import java.sql.ResultSet;
025    
026    /**
027     * @author Juan Fernández
028     * @author Sergio González
029     */
030    public class UpgradeAsset extends UpgradeProcess {
031    
032            @Override
033            protected void doUpgrade() throws Exception {
034                    updateAssetClassTypeId();
035                    updateIGImageClassName();
036            }
037    
038            protected long getJournalStructureId(String structureId) throws Exception {
039                    Connection con = null;
040                    PreparedStatement ps = null;
041                    ResultSet rs = null;
042    
043                    long journalStructureId = 0;
044    
045                    try {
046                            con = DataAccess.getConnection();
047    
048                            ps = con.prepareStatement(
049                                    "select id_ from JournalStructure where structureId = ?");
050    
051                            ps.setString(1, structureId);
052    
053                            rs = ps.executeQuery();
054    
055                            if (rs.next()) {
056                                    journalStructureId = rs.getLong("id_");
057                            }
058                    }
059                    finally {
060                            DataAccess.cleanUp(con, ps, rs);
061                    }
062    
063                    return journalStructureId;
064            }
065    
066            protected void updateAssetClassTypeId() throws Exception {
067                    Connection con = null;
068                    PreparedStatement ps = null;
069                    ResultSet rs = null;
070    
071                    try {
072                            con = DataAccess.getConnection();
073    
074                            ps = con.prepareStatement(
075                                    "select resourcePrimKey, structureId from JournalArticle " +
076                                            "where structureId != ''");
077    
078                            rs = ps.executeQuery();
079    
080                            while (rs.next()) {
081                                    long resourcePrimKey = rs.getLong("resourcePrimKey");
082                                    String structureId = rs.getString("structureId");
083    
084                                    long journalStructureId = getJournalStructureId(structureId);
085    
086                                    runSQL(
087                                            "update AssetEntry set classTypeId = " +
088                                                    journalStructureId + " where classPK = " +
089                                                            resourcePrimKey);
090                            }
091                    }
092                    finally {
093                            DataAccess.cleanUp(con, ps, rs);
094                    }
095            }
096    
097            protected void updateIGImageClassName() throws Exception {
098                    long dlFileEntryClassNameId = PortalUtil.getClassNameId(
099                            DLFileEntry.class.getName());
100                    long igImageClassNameId = PortalUtil.getClassNameId(
101                            "com.liferay.portlet.imagegallery.model.IGImage");
102    
103                    runSQL(
104                            "update AssetEntry set classNameId = " + dlFileEntryClassNameId +
105                                    " where classNameId = " + igImageClassNameId);
106            }
107    
108    }