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.v7_0_0;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.repository.model.Folder;
021    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
022    import com.liferay.portal.kernel.util.ArrayUtil;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.workflow.WorkflowInstance;
026    import com.liferay.portal.model.Layout;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portlet.blogs.model.BlogsEntry;
029    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
030    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
031    import com.liferay.portlet.documentlibrary.model.DLFolder;
032    import com.liferay.portlet.messageboards.model.MBCategory;
033    import com.liferay.portlet.messageboards.model.MBThread;
034    import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
035    
036    import java.sql.Connection;
037    import java.sql.PreparedStatement;
038    import java.sql.ResultSet;
039    
040    import java.util.HashMap;
041    import java.util.Map;
042    
043    /**
044     * @author Eduardo Garcia
045     * @author Roberto D??az
046     * @author Iv??n Zaera
047     */
048    public class UpgradeSubscription extends UpgradeProcess {
049    
050            @Override
051            protected void doUpgrade() throws Exception {
052                    updateSubscriptionClassNames(
053                            Folder.class.getName(), DLFolder.class.getName());
054                    updateSubscriptionClassNames(
055                            "com.liferay.portlet.journal.model.JournalArticle",
056                            "com.liferay.portlet.journal.model.JournalFolder");
057    
058                    updateSubscriptionGroupIds();
059            }
060    
061            protected long getGroupId(long classNameId, long classPK) throws Exception {
062                    Connection con = null;
063                    PreparedStatement ps = null;
064                    ResultSet rs = null;
065    
066                    try {
067                            con = DataAccess.getUpgradeOptimizedConnection();
068    
069                            String className = PortalUtil.getClassName(classNameId);
070    
071                            String[] groupIdSQLParts = StringUtil.split(
072                                    _getGroupIdSQLPartsMap.get(className));
073    
074                            if (ArrayUtil.isEmpty(groupIdSQLParts)) {
075                                    if (_log.isWarnEnabled()) {
076                                            _log.warn(
077                                                    "Unable to determine the group ID for the class name " +
078                                                            className);
079                                    }
080    
081                                    return 0;
082                            }
083    
084                            String sql =
085                                    "select " + groupIdSQLParts[1] + " from " + groupIdSQLParts[0] +
086                                            " where " + groupIdSQLParts[2] + " = ?";
087    
088                            ps = con.prepareStatement(sql);
089    
090                            ps.setLong(1, classPK);
091    
092                            rs = ps.executeQuery();
093    
094                            if (rs.next()) {
095                                    return rs.getLong("groupId");
096                            }
097                    }
098                    finally {
099                            DataAccess.cleanUp(con, ps, rs);
100                    }
101    
102                    return 0;
103            }
104    
105            protected boolean hasGroup(long groupId) throws Exception {
106                    Connection con = null;
107                    PreparedStatement ps = null;
108                    ResultSet rs = null;
109    
110                    try {
111                            con = DataAccess.getUpgradeOptimizedConnection();
112    
113                            ps = con.prepareStatement(
114                                    "select count(*) from Group_ where groupId = ?");
115    
116                            ps.setLong(1, groupId);
117    
118                            rs = ps.executeQuery();
119    
120                            if (rs.next()) {
121                                    int count = rs.getInt(1);
122    
123                                    if (count > 0) {
124                                            return true;
125                                    }
126                            }
127    
128                            return false;
129                    }
130                    finally {
131                            DataAccess.cleanUp(con, ps, rs);
132                    }
133            }
134    
135            protected void updateSubscriptionClassNames(
136                            String oldClassName, String newClassName)
137                    throws Exception {
138    
139                    StringBundler sb = new StringBundler(4);
140    
141                    sb.append("update Subscription set classNameId = ");
142                    sb.append(PortalUtil.getClassNameId(newClassName));
143                    sb.append(" where classNameId = ");
144                    sb.append(PortalUtil.getClassNameId(oldClassName));
145    
146                    runSQL(sb.toString());
147            }
148    
149            protected void updateSubscriptionGroupId(
150                            long subscriptionId, long classNameId, long classPK)
151                    throws Exception {
152    
153                    long groupId = getGroupId(classNameId, classPK);
154    
155                    if ((groupId == 0) && hasGroup(classPK)) {
156                            groupId = classPK;
157                    }
158    
159                    if (groupId != 0) {
160                            runSQL(
161                                    "update Subscription set groupId = " + groupId + " where " +
162                                            "subscriptionId = " + subscriptionId);
163                    }
164            }
165    
166            protected void updateSubscriptionGroupIds() throws Exception {
167                    Connection con = null;
168                    PreparedStatement ps = null;
169                    ResultSet rs = null;
170    
171                    try {
172                            con = DataAccess.getUpgradeOptimizedConnection();
173    
174                            ps = con.prepareStatement(
175                                    "select subscriptionId, classNameId, classPK from " +
176                                            "Subscription");
177    
178                            rs = ps.executeQuery();
179    
180                            while (rs.next()) {
181                                    long subscriptionId = rs.getLong("subscriptionId");
182                                    long classNameId = rs.getLong("classNameId");
183                                    long classPK = rs.getLong("classPK");
184    
185                                    updateSubscriptionGroupId(subscriptionId, classNameId, classPK);
186                            }
187                    }
188                    finally {
189                            DataAccess.cleanUp(con, ps, rs);
190                    }
191            }
192    
193            private static final Log _log = LogFactoryUtil.getLog(
194                    UpgradeSubscription.class);
195    
196            private static final Map<String, String> _getGroupIdSQLPartsMap =
197                    new HashMap<>();
198    
199            static {
200                    _getGroupIdSQLPartsMap.put(
201                            BlogsEntry.class.getName(), "BlogsEntry,groupId,entryId");
202                    _getGroupIdSQLPartsMap.put(
203                            DLFileEntry.class.getName(), "DLFileEntry,groupId,fileEntryId");
204                    _getGroupIdSQLPartsMap.put(
205                            DLFileEntryType.class.getName(),
206                            "DLFileEntryType,groupId,fileEntryTypeId");
207                    _getGroupIdSQLPartsMap.put(
208                            DLFolder.class.getName(), "DLFolder,groupId,folderId");
209                    _getGroupIdSQLPartsMap.put(
210                            Layout.class.getName(), "Layout,groupId,plid");
211                    _getGroupIdSQLPartsMap.put(
212                            MBCategory.class.getName(), "MBCategory,groupId,categoryId");
213                    _getGroupIdSQLPartsMap.put(
214                            MBThread.class.getName(), "MBThread,groupId,threadId");
215                    _getGroupIdSQLPartsMap.put(
216                            SCProductEntry.class.getName(),
217                            "SCProductEntry,groupId,productEntryId");
218                    _getGroupIdSQLPartsMap.put(
219                            WorkflowInstance.class.getName(),
220                            "WorkflowInstance,groupId,workflowInstanceId");
221                    _getGroupIdSQLPartsMap.put(
222                            "com.liferay.bookmarks.model.BookmarksEntry",
223                            "BookmarksEntry,groupId,entryId");
224                    _getGroupIdSQLPartsMap.put(
225                            "com.liferay.bookmarks.model.BookmarksFolder",
226                            "BookmarksFolder,groupId,folderId");
227                    _getGroupIdSQLPartsMap.put(
228                            "com.liferay.portlet.dynamicdatamapping.DDMStructure",
229                            "DDMStructure,groupId,structureId");
230                    _getGroupIdSQLPartsMap.put(
231                            "com.liferay.portlet.journal.model.JournalFolder",
232                            "JournalFolder,groupId,folderId");
233                    _getGroupIdSQLPartsMap.put(
234                            "com.liferay.portlet.wiki.model.WikiNode",
235                            "WikiNode,groupId,nodeId");
236                    _getGroupIdSQLPartsMap.put(
237                            "com.liferay.portlet.wiki.model.WikiPage",
238                            "WikiPage,groupId,resourcePrimKey");
239            }
240    
241    }