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.document.library.kernel.model.DLFileEntryTypeConstants;
018    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
019    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
020    import com.liferay.portal.kernel.upgrade.util.UpgradeProcessUtil;
021    import com.liferay.portal.kernel.util.PortalUtil;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.upgrade.v6_1_0.util.AssetEntryTable;
024    import com.liferay.portal.util.PropsValues;
025    
026    import java.sql.Connection;
027    import java.sql.PreparedStatement;
028    import java.sql.ResultSet;
029    import java.sql.SQLException;
030    
031    /**
032     * @author Juan Fernández
033     * @author Sergio González
034     * @author Brian Wing Shun Chan
035     */
036    public class UpgradeAsset extends UpgradeProcess {
037    
038            @Override
039            protected void doUpgrade() throws Exception {
040                    try {
041                            runSQL("alter_column_type AssetEntry title STRING null");
042                    }
043                    catch (SQLException sqle) {
044                            upgradeTable(
045                                    AssetEntryTable.TABLE_NAME, AssetEntryTable.TABLE_COLUMNS,
046                                    AssetEntryTable.TABLE_SQL_CREATE,
047                                    AssetEntryTable.TABLE_SQL_ADD_INDEXES);
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                    try {
060                            con = DataAccess.getUpgradeOptimizedConnection();
061    
062                            ps = con.prepareStatement(
063                                    "select id_ from JournalStructure where structureId = ?");
064    
065                            ps.setString(1, structureId);
066    
067                            rs = ps.executeQuery();
068    
069                            if (rs.next()) {
070                                    return rs.getLong("id_");
071                            }
072    
073                            return 0;
074                    }
075                    finally {
076                            DataAccess.cleanUp(con, ps, rs);
077                    }
078            }
079    
080            protected void updateAssetClassTypeId() throws Exception {
081                    long classNameId = PortalUtil.getClassNameId(
082                            "com.liferay.portlet.journal.model.JournalArticle");
083    
084                    Connection con = null;
085                    PreparedStatement ps = null;
086                    ResultSet rs = null;
087    
088                    try {
089                            con = DataAccess.getUpgradeOptimizedConnection();
090    
091                            ps = con.prepareStatement(
092                                    "select resourcePrimKey, structureId from JournalArticle " +
093                                            "where structureId != ''");
094    
095                            rs = ps.executeQuery();
096    
097                            while (rs.next()) {
098                                    long resourcePrimKey = rs.getLong("resourcePrimKey");
099                                    String structureId = rs.getString("structureId");
100    
101                                    long journalStructureId = getJournalStructureId(structureId);
102    
103                                    runSQL(
104                                            "update AssetEntry set classTypeId = " +
105                                                    journalStructureId + " where classNameId = " +
106                                                            classNameId + " and classPK = " + 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                            "com.liferay.portlet.documentlibrary.model.DLFileEntry");
117                    long igImageClassNameId = PortalUtil.getClassNameId(
118                            "com.liferay.portlet.imagegallery.model.IGImage");
119    
120                    if (PropsValues.DL_FILE_ENTRY_TYPE_IG_IMAGE_AUTO_CREATE_ON_UPGRADE) {
121                            UpgradeProcessUtil.setCreateIGImageDocumentType(true);
122    
123                            updateIGImageClassNameWithClassTypeId(
124                                    dlFileEntryClassNameId, igImageClassNameId);
125                    }
126                    else {
127                            updateIGImageClassNameWithoutClassTypeId(
128                                    dlFileEntryClassNameId, igImageClassNameId);
129                    }
130            }
131    
132            protected void updateIGImageClassNameWithClassTypeId(
133                            long dlFileEntryClassNameId, long igImageClassNameId)
134                    throws Exception {
135    
136                    Connection con = null;
137                    PreparedStatement ps = null;
138                    ResultSet rs = null;
139    
140                    try {
141                            con = DataAccess.getUpgradeOptimizedConnection();
142    
143                            ps = con.prepareStatement(
144                                    "select fileEntryTypeId, companyId from DLFileEntryType " +
145                                            "where name = ?");
146    
147                            ps.setString(1, DLFileEntryTypeConstants.NAME_IG_IMAGE);
148    
149                            rs = ps.executeQuery();
150    
151                            while (rs.next()) {
152                                    long fileEntryTypeId = rs.getLong("fileEntryTypeId");
153                                    long companyId = rs.getLong("companyId");
154    
155                                    StringBundler sb = new StringBundler(8);
156    
157                                    sb.append("update AssetEntry set classNameId = ");
158                                    sb.append(dlFileEntryClassNameId);
159                                    sb.append(", classTypeId = ");
160                                    sb.append(fileEntryTypeId);
161                                    sb.append(" where classNameId = ");
162                                    sb.append(igImageClassNameId);
163                                    sb.append(" AND companyId = ");
164                                    sb.append(companyId);
165    
166                                    runSQL(sb.toString());
167                            }
168                    }
169                    finally {
170                            DataAccess.cleanUp(con, ps, rs);
171                    }
172            }
173    
174            protected void updateIGImageClassNameWithoutClassTypeId(
175                            long dlFileEntryClassNameId, long igImageClassNameId)
176                    throws Exception {
177    
178                    runSQL(
179                            "update AssetEntry set classNameId = " + dlFileEntryClassNameId +
180                                    " where classNameId = " + igImageClassNameId);
181            }
182    
183    }