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_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.upgrade.v6_1_0.util.AssetEntryTable;
022    import com.liferay.portal.util.PortalUtil;
023    import com.liferay.portal.util.PropsValues;
024    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
025    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
026    import com.liferay.portlet.journal.model.JournalArticle;
027    
028    import java.sql.Connection;
029    import java.sql.PreparedStatement;
030    import java.sql.ResultSet;
031    import java.sql.SQLException;
032    
033    /**
034     * @author Juan Fern??ndez
035     * @author Sergio Gonz??lez
036     * @author Brian Wing Shun Chan
037     */
038    public class UpgradeAsset extends UpgradeProcess {
039    
040            @Override
041            protected void doUpgrade() throws Exception {
042                    try {
043                            runSQL("alter_column_type AssetEntry title STRING null");
044                    }
045                    catch (SQLException sqle) {
046                            upgradeTable(
047                                    AssetEntryTable.TABLE_NAME, AssetEntryTable.TABLE_COLUMNS,
048                                    AssetEntryTable.TABLE_SQL_CREATE,
049                                    AssetEntryTable.TABLE_SQL_ADD_INDEXES);
050                    }
051    
052                    updateAssetClassTypeId();
053                    updateIGImageClassName();
054            }
055    
056            protected long getJournalStructureId(String structureId) throws Exception {
057                    Connection con = null;
058                    PreparedStatement ps = null;
059                    ResultSet rs = null;
060    
061                    try {
062                            con = DataAccess.getUpgradeOptimizedConnection();
063    
064                            ps = con.prepareStatement(
065                                    "select id_ from JournalStructure where structureId = ?");
066    
067                            ps.setString(1, structureId);
068    
069                            rs = ps.executeQuery();
070    
071                            if (rs.next()) {
072                                    return rs.getLong("id_");
073                            }
074    
075                            return 0;
076                    }
077                    finally {
078                            DataAccess.cleanUp(con, ps, rs);
079                    }
080            }
081    
082            protected void updateAssetClassTypeId() throws Exception {
083                    long classNameId = PortalUtil.getClassNameId(JournalArticle.class);
084    
085                    Connection con = null;
086                    PreparedStatement ps = null;
087                    ResultSet rs = null;
088    
089                    try {
090                            con = DataAccess.getUpgradeOptimizedConnection();
091    
092                            ps = con.prepareStatement(
093                                    "select resourcePrimKey, structureId from JournalArticle " +
094                                            "where structureId != ''");
095    
096                            rs = ps.executeQuery();
097    
098                            while (rs.next()) {
099                                    long resourcePrimKey = rs.getLong("resourcePrimKey");
100                                    String structureId = rs.getString("structureId");
101    
102                                    long journalStructureId = getJournalStructureId(structureId);
103    
104                                    runSQL(
105                                            "update AssetEntry set classTypeId = " +
106                                                    journalStructureId + " where classNameId = " +
107                                                            classNameId + " and classPK = " + resourcePrimKey);
108                            }
109                    }
110                    finally {
111                            DataAccess.cleanUp(con, ps, rs);
112                    }
113            }
114    
115            protected void updateIGImageClassName() throws Exception {
116                    long dlFileEntryClassNameId = PortalUtil.getClassNameId(
117                            DLFileEntry.class.getName());
118                    long igImageClassNameId = PortalUtil.getClassNameId(
119                            "com.liferay.portlet.imagegallery.model.IGImage");
120    
121                    if (PropsValues.DL_FILE_ENTRY_TYPE_IG_IMAGE_AUTO_CREATE_ON_UPGRADE) {
122                            UpgradeProcessUtil.setCreateIGImageDocumentType(true);
123    
124                            updateIGImageClassNameWithClassTypeId(
125                                    dlFileEntryClassNameId, igImageClassNameId);
126    
127                    }
128                    else {
129                            updateIGImageClassNameWithoutClassTypeId(
130                                    dlFileEntryClassNameId, igImageClassNameId);
131                    }
132            }
133    
134            protected void updateIGImageClassNameWithClassTypeId(
135                            long dlFileEntryClassNameId, long igImageClassNameId)
136                    throws Exception {
137    
138                    Connection con = null;
139                    PreparedStatement ps = null;
140                    ResultSet rs = null;
141    
142                    try {
143                            con = DataAccess.getUpgradeOptimizedConnection();
144    
145                            ps = con.prepareStatement(
146                                    "select fileEntryTypeId, companyId from DLFileEntryType " +
147                                            "where name = ?");
148    
149                            ps.setString(1, DLFileEntryTypeConstants.NAME_IG_IMAGE);
150    
151                            rs = ps.executeQuery();
152    
153                            while (rs.next()) {
154                                    long fileEntryTypeId = rs.getLong("fileEntryTypeId");
155                                    long companyId = rs.getLong("companyId");
156    
157                                    StringBundler sb = new StringBundler(8);
158    
159                                    sb.append("update AssetEntry set classNameId = ");
160                                    sb.append(dlFileEntryClassNameId);
161                                    sb.append(", classTypeId = ");
162                                    sb.append(fileEntryTypeId);
163                                    sb.append(" where classNameId = ");
164                                    sb.append(igImageClassNameId);
165                                    sb.append(" AND companyId = ");
166                                    sb.append(companyId);
167    
168                                    runSQL(sb.toString());
169                            }
170                    }
171                    finally {
172                            DataAccess.cleanUp(con, ps, rs);
173                    }
174            }
175    
176            protected void updateIGImageClassNameWithoutClassTypeId(
177                            long dlFileEntryClassNameId, long igImageClassNameId)
178                    throws Exception {
179    
180                    runSQL(
181                            "update AssetEntry set classNameId = " + dlFileEntryClassNameId +
182                                    " where classNameId = " + igImageClassNameId);
183            }
184    
185    }