001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
085                                            structureId);
086    
087                                    runSQL(
088                                            "update AssetEntry set classTypeId = " +
089                                                    journalStructureId + " where classPK = " +
090                                                            resourcePrimKey);
091                            }
092                    }
093                    finally {
094                            DataAccess.cleanUp(con, ps, rs);
095                    }
096            }
097    
098            protected void updateIGImageClassName() throws Exception {
099                    long dlFileEntryClassNameId = PortalUtil.getClassNameId(
100                            DLFileEntry.class.getName());
101                    long igImageClassNameId = PortalUtil.getClassNameId(
102                            "com.liferay.portlet.imagegallery.model.IGImage");
103    
104                    runSQL(
105                            "update AssetEntry set classNameId = " + dlFileEntryClassNameId +
106                                    " where classNameId = " + igImageClassNameId);
107            }
108    
109    }