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