001    /**
002     * Copyright (c) 2000-2012 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.Date;
025    import java.sql.PreparedStatement;
026    import java.sql.ResultSet;
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                            Date createDate, Date modifiedDate, long classNameId, long classPK,
036                            String frequency)
037                    throws Exception {
038    
039                    Connection con = null;
040                    PreparedStatement ps = null;
041    
042                    try {
043                            con = DataAccess.getConnection();
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.setDate(5, createDate);
061                            ps.setDate(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 void updateMBMessages(long companyId) throws Exception {
087                    Connection con = null;
088                    PreparedStatement ps = null;
089                    ResultSet rs = null;
090    
091                    try {
092                            con = DataAccess.getConnection();
093    
094                            StringBundler sb = new StringBundler(5);
095    
096                            sb.append("select userId, MIN(userName) as userName, ");
097                            sb.append("classNameId, classPK, MIN(createDate) as createDate, ");
098                            sb.append("MIN(modifiedDate) as modifiedDate from MBMessage ");
099                            sb.append("where (companyId = " + companyId + ") and ");
100                            sb.append("(classNameId != 0) and (parentMessageId != 0) ");
101                            sb.append("group by userId, userName, classNameId, classPK");
102    
103                            String sql = sb.toString();
104    
105                            ps = con.prepareStatement(sql);
106    
107                            rs = ps.executeQuery();
108    
109                            while (rs.next()) {
110                                    long userId = rs.getLong("userId");
111                                    String userName = rs.getString("userName");
112                                    Date createDate = rs.getDate("createDate");
113                                    Date modifiedDate = rs.getDate("modifiedDate");
114                                    long classNameId = rs.getLong("classNameId");
115                                    long classPK = rs.getLong("classPK");
116    
117                                    long subscriptionId = increment();
118                                    String frequency = "instant";
119    
120                                    addSubscription(
121                                            subscriptionId, companyId, userId, userName, createDate,
122                                            modifiedDate, classNameId, classPK, frequency);
123                            }
124                    }
125                    finally {
126                            DataAccess.cleanUp(con, ps, rs);
127                    }
128            }
129    
130    }