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