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.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.upgrade.util.UpgradeProcessUtil;
020    import com.liferay.portal.kernel.util.LoggingTimer;
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.PreparedStatement;
027    import java.sql.ResultSet;
028    
029    /**
030     * @author Juan Fern??ndez
031     * @author Sergio Gonz??lez
032     * @author Brian Wing Shun Chan
033     */
034    public class UpgradeAsset extends UpgradeProcess {
035    
036            @Override
037            protected void doUpgrade() throws Exception {
038                    alter(
039                            AssetEntryTable.class, new AlterColumnType("title", "STRING null"));
040    
041                    updateAssetClassTypeId();
042                    updateIGImageClassName();
043            }
044    
045            protected long getJournalStructureId(String structureId) throws Exception {
046                    try (PreparedStatement ps = connection.prepareStatement(
047                                    "select id_ from JournalStructure where structureId = ?")) {
048    
049                            ps.setString(1, structureId);
050    
051                            try (ResultSet rs = ps.executeQuery()) {
052                                    if (rs.next()) {
053                                            return rs.getLong("id_");
054                                    }
055    
056                                    return 0;
057                            }
058                    }
059            }
060    
061            protected void updateAssetClassTypeId() throws Exception {
062                    try (LoggingTimer loggingTimer = new LoggingTimer();
063                            PreparedStatement ps = connection.prepareStatement(
064                                    "select resourcePrimKey, structureId from JournalArticle " +
065                                            "where structureId != ''");
066                            ResultSet rs = ps.executeQuery()) {
067    
068                            long classNameId = PortalUtil.getClassNameId(
069                                    "com.liferay.portlet.journal.model.JournalArticle");
070    
071                            while (rs.next()) {
072                                    long resourcePrimKey = rs.getLong("resourcePrimKey");
073                                    String structureId = rs.getString("structureId");
074    
075                                    long journalStructureId = getJournalStructureId(structureId);
076    
077                                    runSQL(
078                                            "update AssetEntry set classTypeId = " +
079                                                    journalStructureId + " where classNameId = " +
080                                                            classNameId + " and classPK = " + resourcePrimKey);
081                            }
082                    }
083            }
084    
085            protected void updateIGImageClassName() throws Exception {
086                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
087                            long dlFileEntryClassNameId = PortalUtil.getClassNameId(
088                                    "com.liferay.portlet.documentlibrary.model.DLFileEntry");
089                            long igImageClassNameId = PortalUtil.getClassNameId(
090                                    "com.liferay.portlet.imagegallery.model.IGImage");
091    
092                            if (PropsValues.
093                                            DL_FILE_ENTRY_TYPE_IG_IMAGE_AUTO_CREATE_ON_UPGRADE) {
094    
095                                    UpgradeProcessUtil.setCreateIGImageDocumentType(true);
096    
097                                    updateIGImageClassNameWithClassTypeId(
098                                            dlFileEntryClassNameId, igImageClassNameId);
099                            }
100                            else {
101                                    updateIGImageClassNameWithoutClassTypeId(
102                                            dlFileEntryClassNameId, igImageClassNameId);
103                            }
104                    }
105            }
106    
107            protected void updateIGImageClassNameWithClassTypeId(
108                            long dlFileEntryClassNameId, long igImageClassNameId)
109                    throws Exception {
110    
111                    try (PreparedStatement ps = connection.prepareStatement(
112                                    "select fileEntryTypeId, companyId from DLFileEntryType " +
113                                            "where name = ?")) {
114    
115                            ps.setString(1, DLFileEntryTypeConstants.NAME_IG_IMAGE);
116    
117                            try (ResultSet rs = ps.executeQuery()) {
118                                    while (rs.next()) {
119                                            long fileEntryTypeId = rs.getLong("fileEntryTypeId");
120                                            long companyId = rs.getLong("companyId");
121    
122                                            StringBundler sb = new StringBundler(8);
123    
124                                            sb.append("update AssetEntry set classNameId = ");
125                                            sb.append(dlFileEntryClassNameId);
126                                            sb.append(", classTypeId = ");
127                                            sb.append(fileEntryTypeId);
128                                            sb.append(" where classNameId = ");
129                                            sb.append(igImageClassNameId);
130                                            sb.append(" AND companyId = ");
131                                            sb.append(companyId);
132    
133                                            runSQL(sb.toString());
134                                    }
135                            }
136                    }
137            }
138    
139            protected void updateIGImageClassNameWithoutClassTypeId(
140                            long dlFileEntryClassNameId, long igImageClassNameId)
141                    throws Exception {
142    
143                    runSQL(
144                            "update AssetEntry set classNameId = " + dlFileEntryClassNameId +
145                                    " where classNameId = " + igImageClassNameId);
146            }
147    
148    }