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