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