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.v6_1_0;
016    
017    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
018    import com.liferay.portal.kernel.util.LoggingTimer;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.util.PortalInstances;
021    import com.liferay.portal.util.PropsValues;
022    
023    import java.sql.PreparedStatement;
024    import java.sql.ResultSet;
025    import java.sql.Timestamp;
026    
027    /**
028     * @author Juan Fern??ndez
029     */
030    public class UpgradeSubscription extends UpgradeProcess {
031    
032            protected void addSubscription(
033                            long subscriptionId, long companyId, long userId, String userName,
034                            Timestamp createDate, Timestamp modifiedDate, long classNameId,
035                            long classPK, String frequency)
036                    throws Exception {
037    
038                    StringBundler sb = new StringBundler(3);
039    
040                    sb.append("insert into Subscription (subscriptionId, companyId, ");
041                    sb.append("userId, userName, createDate, modifiedDate, classNameId, ");
042                    sb.append("classPK, frequency) values (?, ?, ?, ?, ?, ?, ?, ?, ?)");
043    
044                    try (PreparedStatement ps = connection.prepareStatement(
045                                    sb.toString())) {
046    
047                            ps.setLong(1, subscriptionId);
048                            ps.setLong(2, companyId);
049                            ps.setLong(3, userId);
050                            ps.setString(4, userName);
051                            ps.setTimestamp(5, createDate);
052                            ps.setTimestamp(6, modifiedDate);
053                            ps.setLong(7, classNameId);
054                            ps.setLong(8, classPK);
055                            ps.setString(9, frequency);
056    
057                            ps.executeUpdate();
058                    }
059            }
060    
061            @Override
062            protected void doUpgrade() throws Exception {
063                    if (!PropsValues.DISCUSSION_SUBSCRIBE_BY_DEFAULT) {
064                            return;
065                    }
066    
067                    long[] companyIds = PortalInstances.getCompanyIdsBySQL();
068    
069                    for (long companyId : companyIds) {
070                            updateMBMessages(companyId);
071                    }
072            }
073    
074            protected boolean hasSubscription(
075                            long companyId, long userId, long classNameId, long classPK)
076                    throws Exception {
077    
078                    try (PreparedStatement ps = connection.prepareStatement(
079                                    "select count(*) from Subscription where companyId = ? and " +
080                                            "userId = ? and classNameId = ? and classPK = ?")) {
081    
082                            ps.setLong(1, companyId);
083                            ps.setLong(2, userId);
084                            ps.setLong(3, classNameId);
085                            ps.setLong(4, classPK);
086    
087                            try (ResultSet rs = ps.executeQuery()) {
088                                    while (rs.next()) {
089                                            int count = rs.getInt(1);
090    
091                                            if (count > 0) {
092                                                    return true;
093                                            }
094                                    }
095    
096                                    return false;
097                            }
098                    }
099            }
100    
101            protected void updateMBMessages(long companyId) throws Exception {
102                    try (LoggingTimer loggingTimer = new LoggingTimer(
103                                    String.valueOf(companyId))) {
104    
105                            StringBundler sb = new StringBundler(7);
106    
107                            sb.append("select userId, MIN(userName) as userName, ");
108                            sb.append("classNameId, classPK, MIN(createDate) as createDate, ");
109                            sb.append("MIN(modifiedDate) as modifiedDate from MBMessage ");
110                            sb.append("where (companyId = ");
111                            sb.append(companyId);
112                            sb.append(") and (classNameId != 0) and (parentMessageId != 0) ");
113                            sb.append("group by userId, classNameId, classPK");
114    
115                            try (PreparedStatement ps = connection.prepareStatement(
116                                            sb.toString());
117                                    ResultSet rs = ps.executeQuery()) {
118    
119                                    while (rs.next()) {
120                                            long userId = rs.getLong("userId");
121                                            String userName = rs.getString("userName");
122                                            Timestamp createDate = rs.getTimestamp("createDate");
123                                            Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
124                                            long classNameId = rs.getLong("classNameId");
125                                            long classPK = rs.getLong("classPK");
126    
127                                            if (hasSubscription(
128                                                            companyId, userId, classNameId, classPK)) {
129    
130                                                    continue;
131                                            }
132    
133                                            long subscriptionId = increment();
134                                            String frequency = "instant";
135    
136                                            addSubscription(
137                                                    subscriptionId, companyId, userId, userName, createDate,
138                                                    modifiedDate, classNameId, classPK, frequency);
139                                    }
140                            }
141                    }
142            }
143    
144    }