001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.verify;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portlet.social.service.SocialActivityLocalServiceUtil;
021    import com.liferay.portlet.social.service.SocialRequestLocalServiceUtil;
022    
023    import java.sql.Connection;
024    import java.sql.PreparedStatement;
025    import java.sql.ResultSet;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class VerifySocial extends VerifyProcess {
031    
032            protected void deleteDuplicateActivities() throws Exception {
033                    StringBundler sb = new StringBundler(14);
034    
035                    sb.append("select distinct sa1.* from SocialActivity sa1 ");
036                    sb.append("inner join SocialActivity sa2 on ");
037                    sb.append("sa1.activityId != sa2.activityId and ");
038                    sb.append("sa1.groupId = sa2.groupId and ");
039                    sb.append("sa1.userId = sa2.userId and ");
040                    sb.append("sa1.classNameId = sa2.classNameId and ");
041                    sb.append("sa1.classPK = sa2.classPK and ");
042                    sb.append("sa1.type_ = sa2.type_ and ");
043                    sb.append("sa1.extraData = sa2.extraData and ");
044                    sb.append("sa1.receiverUserId = sa2.receiverUserId ");
045                    sb.append("where sa1.mirrorActivityId = 0 ");
046                    sb.append("order by sa1.groupId, sa1.userId, sa1.classNameId, ");
047                    sb.append("sa1.classPK, sa1.type_, sa1.extraData, ");
048                    sb.append("sa1.receiverUserId, sa1.createDate desc");
049    
050                    String sql = sb.toString();
051    
052                    Connection con = null;
053                    PreparedStatement ps = null;
054                    ResultSet rs = null;
055    
056                    try {
057                            con = DataAccess.getConnection();
058    
059                            ps = con.prepareStatement(sql);
060    
061                            rs = ps.executeQuery();
062    
063                            long groupId = 0;
064                            long userId = 0;
065                            long classNameId = 0;
066                            long classPK = 0;
067                            long type = 0;
068                            String extraData = StringPool.BLANK;
069                            long receiverUserId = 0;
070    
071                            while (rs.next()) {
072                                    long curActivityId = rs.getLong("activityId");
073                                    long curGroupId = rs.getLong("groupId");
074                                    long curUserId = rs.getLong("userId");
075                                    long curClassNameId = rs.getLong("classNameId");
076                                    long curClassPK = rs.getLong("classPK");
077                                    long curType = rs.getLong("type_");
078                                    String curExtraData = rs.getString("extraData");
079                                    long curReceiverUserId = rs.getLong("receiverUserId");
080    
081                                    if ((curGroupId == groupId) && (curUserId == userId) &&
082                                            (curClassNameId == classNameId) &&
083                                            (curClassPK == classPK) && (curType == type) &&
084                                            (curExtraData.equals(extraData)) &&
085                                            (curReceiverUserId == receiverUserId)) {
086    
087                                            SocialActivityLocalServiceUtil.deleteActivity(
088                                                    curActivityId);
089                                    }
090                                    else {
091                                            groupId = curGroupId;
092                                            userId = curUserId;
093                                            classNameId = curClassNameId;
094                                            classPK = curClassPK;
095                                            type = curType;
096                                            extraData = curExtraData;
097                                            receiverUserId = curReceiverUserId;
098                                    }
099                            }
100                    }
101                    finally {
102                            DataAccess.cleanUp(con, ps, rs);
103                    }
104            }
105    
106            protected void deleteDuplicateRequests() throws Exception {
107                    StringBundler sb = new StringBundler(13);
108    
109                    sb.append("select distinct sr1.* from SocialRequest sr1 ");
110                    sb.append("inner join SocialRequest sr2 on ");
111                    sb.append("sr1.requestId != sr2.requestId and ");
112                    sb.append("sr1.groupId = sr2.groupId and ");
113                    sb.append("sr1.userId = sr2.userId and ");
114                    sb.append("sr1.classNameId = sr2.classNameId and ");
115                    sb.append("sr1.classPK = sr2.classPK and ");
116                    sb.append("sr1.type_ = sr2.type_ and ");
117                    sb.append("sr1.extraData = sr2.extraData and ");
118                    sb.append("sr1.receiverUserId = sr2.receiverUserId ");
119                    sb.append("order by sr1.groupId, sr1.userId, sr1.classNameId, ");
120                    sb.append("sr1.classPK, sr1.type_, sr1.extraData, ");
121                    sb.append("sr1.receiverUserId, sr1.createDate desc");
122    
123                    String sql = sb.toString();
124    
125                    Connection con = null;
126                    PreparedStatement ps = null;
127                    ResultSet rs = null;
128    
129                    try {
130                            con = DataAccess.getConnection();
131    
132                            ps = con.prepareStatement(sql);
133    
134                            rs = ps.executeQuery();
135    
136                            long groupId = 0;
137                            long userId = 0;
138                            long classNameId = 0;
139                            long classPK = 0;
140                            long type = 0;
141                            String extraData = StringPool.BLANK;
142                            long receiverUserId = 0;
143    
144                            while (rs.next()) {
145                                    long curRequestId = rs.getLong("requestId");
146                                    long curGroupId = rs.getLong("groupId");
147                                    long curUserId = rs.getLong("userId");
148                                    long curClassNameId = rs.getLong("classNameId");
149                                    long curClassPK = rs.getLong("classPK");
150                                    long curType = rs.getLong("type_");
151                                    String curExtraData = rs.getString("extraData");
152                                    long curReceiverUserId = rs.getLong("receiverUserId");
153    
154                                    if ((curGroupId == groupId) && (curUserId == userId) &&
155                                            (curClassNameId == classNameId) &&
156                                            (curClassPK == classPK) && (curType == type) &&
157                                            (curExtraData.equals(extraData)) &&
158                                            (curReceiverUserId == receiverUserId)) {
159    
160                                            SocialRequestLocalServiceUtil.deleteRequest(curRequestId);
161                                    }
162                                    else {
163                                            groupId = curGroupId;
164                                            userId = curUserId;
165                                            classNameId = curClassNameId;
166                                            classPK = curClassPK;
167                                            type = curType;
168                                            extraData = curExtraData;
169                                            receiverUserId = curReceiverUserId;
170                                    }
171                            }
172                    }
173                    finally {
174                            DataAccess.cleanUp(con, ps, rs);
175                    }
176            }
177    
178            protected void doVerify() throws Exception {
179    
180                    // Temporarily comment these out because of performance issues. This
181                    // verification is not needed because activities can be added while
182                    // ensuring a duplicate does not exist. See LEP-6593.
183    
184                    //deleteDuplicateActivities();
185                    //deleteDuplicateRequests();
186            }
187    
188    }