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