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.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.util.PropsValues;
024    
025    import java.sql.Connection;
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                    updateAssetClassTypeId();
039                    updateIGImageClassName();
040            }
041    
042            protected long getJournalStructureId(String structureId) throws Exception {
043                    Connection con = null;
044                    PreparedStatement ps = null;
045                    ResultSet rs = null;
046    
047                    try {
048                            con = DataAccess.getUpgradeOptimizedConnection();
049    
050                            ps = con.prepareStatement(
051                                    "select id_ from JournalStructure where structureId = ?");
052    
053                            ps.setString(1, structureId);
054    
055                            rs = ps.executeQuery();
056    
057                            if (rs.next()) {
058                                    return rs.getLong("id_");
059                            }
060    
061                            return 0;
062                    }
063                    finally {
064                            DataAccess.cleanUp(con, ps, rs);
065                    }
066            }
067    
068            protected void updateAssetClassTypeId() throws Exception {
069                    long classNameId = PortalUtil.getClassNameId(
070                            "com.liferay.portlet.journal.model.JournalArticle");
071    
072                    Connection con = null;
073                    PreparedStatement ps = null;
074                    ResultSet rs = null;
075    
076                    try {
077                            con = DataAccess.getUpgradeOptimizedConnection();
078    
079                            ps = con.prepareStatement(
080                                    "select resourcePrimKey, structureId from JournalArticle " +
081                                            "where structureId != ''");
082    
083                            rs = ps.executeQuery();
084    
085                            while (rs.next()) {
086                                    long resourcePrimKey = rs.getLong("resourcePrimKey");
087                                    String structureId = rs.getString("structureId");
088    
089                                    long journalStructureId = getJournalStructureId(structureId);
090    
091                                    runSQL(
092                                            "update AssetEntry set classTypeId = " +
093                                                    journalStructureId + " where classNameId = " +
094                                                            classNameId + " and classPK = " + resourcePrimKey);
095                            }
096                    }
097                    finally {
098                            DataAccess.cleanUp(con, ps, rs);
099                    }
100            }
101    
102            protected void updateIGImageClassName() throws Exception {
103                    long dlFileEntryClassNameId = PortalUtil.getClassNameId(
104                            "com.liferay.document.library.kernel.model.DLFileEntry");
105                    long igImageClassNameId = PortalUtil.getClassNameId(
106                            "com.liferay.portlet.imagegallery.model.IGImage");
107    
108                    if (PropsValues.DL_FILE_ENTRY_TYPE_IG_IMAGE_AUTO_CREATE_ON_UPGRADE) {
109                            UpgradeProcessUtil.setCreateIGImageDocumentType(true);
110    
111                            updateIGImageClassNameWithClassTypeId(
112                                    dlFileEntryClassNameId, igImageClassNameId);
113                    }
114                    else {
115                            updateIGImageClassNameWithoutClassTypeId(
116                                    dlFileEntryClassNameId, igImageClassNameId);
117                    }
118            }
119    
120            protected void updateIGImageClassNameWithClassTypeId(
121                            long dlFileEntryClassNameId, long igImageClassNameId)
122                    throws Exception {
123    
124                    Connection con = null;
125                    PreparedStatement ps = null;
126                    ResultSet rs = null;
127    
128                    try {
129                            con = DataAccess.getUpgradeOptimizedConnection();
130    
131                            ps = con.prepareStatement(
132                                    "select fileEntryTypeId, companyId from DLFileEntryType " +
133                                            "where name = ?");
134    
135                            ps.setString(1, DLFileEntryTypeConstants.NAME_IG_IMAGE);
136    
137                            rs = ps.executeQuery();
138    
139                            while (rs.next()) {
140                                    long fileEntryTypeId = rs.getLong("fileEntryTypeId");
141                                    long companyId = rs.getLong("companyId");
142    
143                                    StringBundler sb = new StringBundler(8);
144    
145                                    sb.append("update AssetEntry set classNameId = ");
146                                    sb.append(dlFileEntryClassNameId);
147                                    sb.append(", classTypeId = ");
148                                    sb.append(fileEntryTypeId);
149                                    sb.append(" where classNameId = ");
150                                    sb.append(igImageClassNameId);
151                                    sb.append(" AND companyId = ");
152                                    sb.append(companyId);
153    
154                                    runSQL(sb.toString());
155                            }
156                    }
157                    finally {
158                            DataAccess.cleanUp(con, ps, rs);
159                    }
160            }
161    
162            protected void updateIGImageClassNameWithoutClassTypeId(
163                            long dlFileEntryClassNameId, long igImageClassNameId)
164                    throws Exception {
165    
166                    runSQL(
167                            "update AssetEntry set classNameId = " + dlFileEntryClassNameId +
168                                    " where classNameId = " + igImageClassNameId);
169            }
170    
171    }