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