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.counter.kernel.model.Counter;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.util.StringBundler;
020    
021    import java.sql.PreparedStatement;
022    import java.sql.ResultSet;
023    import java.sql.Statement;
024    
025    /**
026     * @author Adolfo P??rez
027     */
028    public class UpgradeSocial extends UpgradeProcess {
029    
030            protected void addSocialActivitySets(long delta) throws Exception {
031                    try (Statement s = connection.createStatement()) {
032                            StringBundler sb = new StringBundler(6);
033    
034                            sb.append("insert into SocialActivitySet select (activityId + ");
035                            sb.append(delta);
036                            sb.append(") as activitySetId, groupId, companyId, userId, ");
037                            sb.append("createDate, createDate AS modifiedDate, classNameId, ");
038                            sb.append("classPK, type_, extraData, 1 as activityCount from ");
039                            sb.append("SocialActivity where mirrorActivityId = 0");
040    
041                            s.execute(sb.toString());
042                    }
043            }
044    
045            @Override
046            protected void doUpgrade() throws Exception {
047                    if (getSocialActivitySetsCount() > 0) {
048                            return;
049                    }
050    
051                    long increment = increment();
052    
053                    long delta = getDelta(increment);
054    
055                    addSocialActivitySets(delta);
056    
057                    updateSocialActivities(delta);
058    
059                    increment(Counter.class.getName(), getSocialActivitySetsCount());
060            }
061    
062            protected long getDelta(long increment) throws Exception {
063                    try (Statement s = connection.createStatement()) {
064                            try (ResultSet rs = s.executeQuery(
065                                            "select min(activityId) from SocialActivity")) {
066    
067                                    if (rs.next()) {
068                                            long minActivityId = rs.getLong(1);
069    
070                                            return increment - minActivityId;
071                                    }
072    
073                                    return 0;
074                            }
075                    }
076            }
077    
078            protected int getSocialActivitySetsCount() throws Exception {
079                    try (Statement s = connection.createStatement()) {
080                            String query = "select count(activitySetId) from SocialActivitySet";
081    
082                            try (ResultSet rs = s.executeQuery(query)) {
083                                    if (rs.next()) {
084                                            return rs.getInt(1);
085                                    }
086    
087                                    return 0;
088                            }
089                    }
090            }
091    
092            protected void updateSocialActivities(long delta) throws Exception {
093                    try (PreparedStatement ps = connection.prepareStatement(
094                                    "update SocialActivity set activitySetId = (activityId + ?)")) {
095    
096                            ps.setLong(1, delta);
097    
098                            ps.execute();
099                    }
100            }
101    
102    }