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                    else {
116                            updateIGImageClassNameWithoutClassTypeId(
117                                    dlFileEntryClassNameId, igImageClassNameId);
118                    }
119            }
120    
121            protected void updateIGImageClassNameWithClassTypeId(
122                            long dlFileEntryClassNameId, long igImageClassNameId)
123                    throws Exception {
124    
125                    Connection con = null;
126                    PreparedStatement ps = null;
127                    ResultSet rs = null;
128    
129                    try {
130                            con = DataAccess.getUpgradeOptimizedConnection();
131    
132                            ps = con.prepareStatement(
133                                    "select fileEntryTypeId, companyId from DLFileEntryType " +
134                                            "where name = ?");
135    
136                            ps.setString(1, DLFileEntryTypeConstants.NAME_IG_IMAGE);
137    
138                            rs = ps.executeQuery();
139    
140                            while (rs.next()) {
141                                    long fileEntryTypeId = rs.getLong("fileEntryTypeId");
142                                    long companyId = rs.getLong("companyId");
143    
144                                    StringBundler sb = new StringBundler(8);
145    
146                                    sb.append("update AssetEntry set classNameId = ");
147                                    sb.append(dlFileEntryClassNameId);
148                                    sb.append(", classTypeId = ");
149                                    sb.append(fileEntryTypeId);
150                                    sb.append(" where classNameId = ");
151                                    sb.append(igImageClassNameId);
152                                    sb.append(" AND companyId = ");
153                                    sb.append(companyId);
154    
155                                    runSQL(sb.toString());
156                            }
157                    }
158                    finally {
159                            DataAccess.cleanUp(con, ps, rs);
160                    }
161            }
162    
163            protected void updateIGImageClassNameWithoutClassTypeId(
164                            long dlFileEntryClassNameId, long igImageClassNameId)
165                    throws Exception {
166    
167                    runSQL(
168                            "update AssetEntry set classNameId = " + dlFileEntryClassNameId +
169                                    " where classNameId = " + igImageClassNameId);
170            }
171    
172    }