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