1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.verify;
16  
17  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
18  import com.liferay.portal.kernel.util.StringBundler;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portlet.social.service.SocialActivityLocalServiceUtil;
21  import com.liferay.portlet.social.service.SocialRequestLocalServiceUtil;
22  
23  import java.sql.Connection;
24  import java.sql.PreparedStatement;
25  import java.sql.ResultSet;
26  
27  /**
28   * <a href="VerifySocial.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class VerifySocial extends VerifyProcess {
33  
34      protected void deleteDuplicateActivities() throws Exception {
35          StringBundler sb = new StringBundler(14);
36  
37          sb.append("select distinct sa1.* from SocialActivity sa1 ");
38          sb.append("inner join SocialActivity sa2 on ");
39          sb.append("sa1.activityId != sa2.activityId and ");
40          sb.append("sa1.groupId = sa2.groupId and ");
41          sb.append("sa1.userId = sa2.userId and ");
42          sb.append("sa1.classNameId = sa2.classNameId and ");
43          sb.append("sa1.classPK = sa2.classPK and ");
44          sb.append("sa1.type_ = sa2.type_ and ");
45          sb.append("sa1.extraData = sa2.extraData and ");
46          sb.append("sa1.receiverUserId = sa2.receiverUserId ");
47          sb.append("where sa1.mirrorActivityId = 0 ");
48          sb.append("order by sa1.groupId, sa1.userId, sa1.classNameId, ");
49          sb.append("sa1.classPK, sa1.type_, sa1.extraData, ");
50          sb.append("sa1.receiverUserId, sa1.createDate desc");
51  
52          String sql = sb.toString();
53  
54          Connection con = null;
55          PreparedStatement ps = null;
56          ResultSet rs = null;
57  
58          try {
59              con = DataAccess.getConnection();
60  
61              ps = con.prepareStatement(sql);
62  
63              rs = ps.executeQuery();
64  
65              long groupId = 0;
66              long userId = 0;
67              long classNameId = 0;
68              long classPK = 0;
69              long type = 0;
70              String extraData = StringPool.BLANK;
71              long receiverUserId = 0;
72  
73              while (rs.next()) {
74                  long curActivityId = rs.getLong("activityId");
75                  long curGroupId = rs.getLong("groupId");
76                  long curUserId = rs.getLong("userId");
77                  long curClassNameId = rs.getLong("classNameId");
78                  long curClassPK = rs.getLong("classPK");
79                  long curType = rs.getLong("type_");
80                  String curExtraData = rs.getString("extraData");
81                  long curReceiverUserId = rs.getLong("receiverUserId");
82  
83                  if ((curGroupId == groupId) && (curUserId == userId) &&
84                      (curClassNameId == classNameId) &&
85                      (curClassPK == classPK) && (curType == type) &&
86                      (curExtraData.equals(extraData)) &&
87                      (curReceiverUserId == receiverUserId)) {
88  
89                      SocialActivityLocalServiceUtil.deleteActivity(
90                          curActivityId);
91                  }
92                  else {
93                      groupId = curGroupId;
94                      userId = curUserId;
95                      classNameId = curClassNameId;
96                      classPK = curClassPK;
97                      type = curType;
98                      extraData = curExtraData;
99                      receiverUserId = curReceiverUserId;
100                 }
101             }
102         }
103         finally {
104             DataAccess.cleanUp(con, ps, rs);
105         }
106     }
107 
108     protected void deleteDuplicateRequests() throws Exception {
109         StringBundler sb = new StringBundler(13);
110 
111         sb.append("select distinct sr1.* from SocialRequest sr1 ");
112         sb.append("inner join SocialRequest sr2 on ");
113         sb.append("sr1.requestId != sr2.requestId and ");
114         sb.append("sr1.groupId = sr2.groupId and ");
115         sb.append("sr1.userId = sr2.userId and ");
116         sb.append("sr1.classNameId = sr2.classNameId and ");
117         sb.append("sr1.classPK = sr2.classPK and ");
118         sb.append("sr1.type_ = sr2.type_ and ");
119         sb.append("sr1.extraData = sr2.extraData and ");
120         sb.append("sr1.receiverUserId = sr2.receiverUserId ");
121         sb.append("order by sr1.groupId, sr1.userId, sr1.classNameId, ");
122         sb.append("sr1.classPK, sr1.type_, sr1.extraData, ");
123         sb.append("sr1.receiverUserId, sr1.createDate desc");
124 
125         String sql = sb.toString();
126 
127         Connection con = null;
128         PreparedStatement ps = null;
129         ResultSet rs = null;
130 
131         try {
132             con = DataAccess.getConnection();
133 
134             ps = con.prepareStatement(sql);
135 
136             rs = ps.executeQuery();
137 
138             long groupId = 0;
139             long userId = 0;
140             long classNameId = 0;
141             long classPK = 0;
142             long type = 0;
143             String extraData = StringPool.BLANK;
144             long receiverUserId = 0;
145 
146             while (rs.next()) {
147                 long curRequestId = rs.getLong("requestId");
148                 long curGroupId = rs.getLong("groupId");
149                 long curUserId = rs.getLong("userId");
150                 long curClassNameId = rs.getLong("classNameId");
151                 long curClassPK = rs.getLong("classPK");
152                 long curType = rs.getLong("type_");
153                 String curExtraData = rs.getString("extraData");
154                 long curReceiverUserId = rs.getLong("receiverUserId");
155 
156                 if ((curGroupId == groupId) && (curUserId == userId) &&
157                     (curClassNameId == classNameId) &&
158                     (curClassPK == classPK) && (curType == type) &&
159                     (curExtraData.equals(extraData)) &&
160                     (curReceiverUserId == receiverUserId)) {
161 
162                     SocialRequestLocalServiceUtil.deleteRequest(curRequestId);
163                 }
164                 else {
165                     groupId = curGroupId;
166                     userId = curUserId;
167                     classNameId = curClassNameId;
168                     classPK = curClassPK;
169                     type = curType;
170                     extraData = curExtraData;
171                     receiverUserId = curReceiverUserId;
172                 }
173             }
174         }
175         finally {
176             DataAccess.cleanUp(con, ps, rs);
177         }
178     }
179 
180     protected void doVerify() throws Exception {
181 
182         // Temporarily comment these out because of performance issues. This
183         // verification is not needed because activities can be added while
184         // ensuring a duplicate does not exist. See LEP-6593.
185 
186         //deleteDuplicateActivities();
187         //deleteDuplicateRequests();
188     }
189 
190 }