1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.verify;
24  
25  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portlet.social.service.SocialActivityLocalServiceUtil;
30  import com.liferay.portlet.social.service.SocialRequestLocalServiceUtil;
31  
32  import java.sql.Connection;
33  import java.sql.PreparedStatement;
34  import java.sql.ResultSet;
35  
36  /**
37   * <a href="VerifySocial.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class VerifySocial extends VerifyProcess {
43  
44      public void verify() throws VerifyException {
45          _log.info("Verifying");
46  
47          try {
48              verifySocial();
49          }
50          catch (Exception e) {
51              throw new VerifyException(e);
52          }
53      }
54  
55      protected void deleteDuplicateActivities() throws Exception {
56          StringBuilder sb = new StringBuilder();
57  
58          sb.append("select distinct sa1.* from SocialActivity sa1 ");
59          sb.append("inner join SocialActivity sa2 on ");
60          sb.append("sa1.activityId != sa2.activityId and ");
61          sb.append("sa1.groupId = sa2.groupId and ");
62          sb.append("sa1.userId = sa2.userId and ");
63          sb.append("sa1.classNameId = sa2.classNameId and ");
64          sb.append("sa1.classPK = sa2.classPK and ");
65          sb.append("sa1.type_ = sa2.type_ and ");
66          sb.append("sa1.extraData = sa2.extraData and ");
67          sb.append("sa1.receiverUserId = sa2.receiverUserId ");
68          sb.append("where sa1.mirrorActivityId = 0 ");
69          sb.append("order by sa1.groupId, sa1.userId, sa1.classNameId, ");
70          sb.append("sa1.classPK, sa1.type_, sa1.extraData, ");
71          sb.append("sa1.receiverUserId, sa1.createDate desc");
72  
73          String sql = sb.toString();
74  
75          Connection con = null;
76          PreparedStatement ps = null;
77          ResultSet rs = null;
78  
79          try {
80              con = DataAccess.getConnection();
81  
82              ps = con.prepareStatement(sql);
83  
84              rs = ps.executeQuery();
85  
86              long groupId = 0;
87              long userId = 0;
88              long classNameId = 0;
89              long classPK = 0;
90              long type = 0;
91              String extraData = StringPool.BLANK;
92              long receiverUserId = 0;
93  
94              while (rs.next()) {
95                  long curActivityId = rs.getLong("activityId");
96                  long curGroupId = rs.getLong("groupId");
97                  long curUserId = rs.getLong("userId");
98                  long curClassNameId = rs.getLong("classNameId");
99                  long curClassPK = rs.getLong("classPK");
100                 long curType = rs.getLong("type_");
101                 String curExtraData = rs.getString("extraData");
102                 long curReceiverUserId = rs.getLong("receiverUserId");
103 
104                 if ((curGroupId == groupId) && (curUserId == userId) &&
105                     (curClassNameId == classNameId) &&
106                     (curClassPK == classPK) && (curType == type) &&
107                     (curExtraData.equals(extraData)) &&
108                     (curReceiverUserId == receiverUserId)) {
109 
110                     SocialActivityLocalServiceUtil.deleteActivity(
111                         curActivityId);
112                 }
113                 else {
114                     groupId = curGroupId;
115                     userId = curUserId;
116                     classNameId = curClassNameId;
117                     classPK = curClassPK;
118                     type = curType;
119                     extraData = curExtraData;
120                     receiverUserId = curReceiverUserId;
121                 }
122             }
123         }
124         finally {
125             DataAccess.cleanUp(con, ps, rs);
126         }
127     }
128 
129     protected void deleteDuplicateRequests() throws Exception {
130         StringBuilder sb = new StringBuilder();
131 
132         sb.append("select distinct sr1.* from SocialRequest sr1 ");
133         sb.append("inner join SocialRequest sr2 on ");
134         sb.append("sr1.requestId != sr2.requestId and ");
135         sb.append("sr1.groupId = sr2.groupId and ");
136         sb.append("sr1.userId = sr2.userId and ");
137         sb.append("sr1.classNameId = sr2.classNameId and ");
138         sb.append("sr1.classPK = sr2.classPK and ");
139         sb.append("sr1.type_ = sr2.type_ and ");
140         sb.append("sr1.extraData = sr2.extraData and ");
141         sb.append("sr1.receiverUserId = sr2.receiverUserId ");
142         sb.append("order by sr1.groupId, sr1.userId, sr1.classNameId, ");
143         sb.append("sr1.classPK, sr1.type_, sr1.extraData, ");
144         sb.append("sr1.receiverUserId, sr1.createDate desc");
145 
146         String sql = sb.toString();
147 
148         Connection con = null;
149         PreparedStatement ps = null;
150         ResultSet rs = null;
151 
152         try {
153             con = DataAccess.getConnection();
154 
155             ps = con.prepareStatement(sql);
156 
157             rs = ps.executeQuery();
158 
159             long groupId = 0;
160             long userId = 0;
161             long classNameId = 0;
162             long classPK = 0;
163             long type = 0;
164             String extraData = StringPool.BLANK;
165             long receiverUserId = 0;
166 
167             while (rs.next()) {
168                 long curRequestId = rs.getLong("requestId");
169                 long curGroupId = rs.getLong("groupId");
170                 long curUserId = rs.getLong("userId");
171                 long curClassNameId = rs.getLong("classNameId");
172                 long curClassPK = rs.getLong("classPK");
173                 long curType = rs.getLong("type_");
174                 String curExtraData = rs.getString("extraData");
175                 long curReceiverUserId = rs.getLong("receiverUserId");
176 
177                 if ((curGroupId == groupId) && (curUserId == userId) &&
178                     (curClassNameId == classNameId) &&
179                     (curClassPK == classPK) && (curType == type) &&
180                     (curExtraData.equals(extraData)) &&
181                     (curReceiverUserId == receiverUserId)) {
182 
183                     SocialRequestLocalServiceUtil.deleteRequest(curRequestId);
184                 }
185                 else {
186                     groupId = curGroupId;
187                     userId = curUserId;
188                     classNameId = curClassNameId;
189                     classPK = curClassPK;
190                     type = curType;
191                     extraData = curExtraData;
192                     receiverUserId = curReceiverUserId;
193                 }
194             }
195         }
196         finally {
197             DataAccess.cleanUp(con, ps, rs);
198         }
199     }
200 
201     protected void verifySocial() throws Exception {
202 
203         // Temporarily comment these out because of performance issues. This
204         // verification is not needed because activities can be added while
205         // ensuring a duplicate does not exist. See LEP-6593.
206 
207         //deleteDuplicateActivities();
208         //deleteDuplicateRequests();
209     }
210 
211     private static Log _log = LogFactoryUtil.getLog(VerifySocial.class);
212 
213 }