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