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