1   /**
2    * Copyright (c) 2000-2008 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.util.StringPool;
27  import com.liferay.portlet.social.service.SocialActivityLocalServiceUtil;
28  import com.liferay.portlet.social.service.SocialRequestLocalServiceUtil;
29  
30  import java.sql.Connection;
31  import java.sql.PreparedStatement;
32  import java.sql.ResultSet;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  /**
38   * <a href="VerifySocial.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class VerifySocial extends VerifyProcess {
44  
45      public void verify() throws VerifyException {
46          _log.info("Verifying");
47  
48          try {
49              verifySocial();
50          }
51          catch (Exception e) {
52              throw new VerifyException(e);
53          }
54      }
55  
56      protected void deleteDuplicateActivities() throws Exception {
57          StringBuilder sb = new StringBuilder();
58  
59          sb.append("select distinct sa1.* from SocialActivity sa1 ");
60          sb.append("inner join SocialActivity sa2 on ");
61          sb.append("sa1.activityId != sa2.activityId and ");
62          sb.append("sa1.groupId = sa2.groupId and ");
63          sb.append("sa1.userId = sa2.userId and ");
64          sb.append("sa1.classNameId = sa2.classNameId and ");
65          sb.append("sa1.classPK = sa2.classPK and ");
66          sb.append("sa1.type_ = sa2.type_ and ");
67          sb.append("sa1.extraData = sa2.extraData and ");
68          sb.append("sa1.receiverUserId = sa2.receiverUserId ");
69          sb.append("where sa1.mirrorActivityId = 0 ");
70          sb.append("order by sa1.groupId, sa1.userId, sa1.classNameId, ");
71          sb.append("sa1.classPK, sa1.type_, sa1.extraData, ");
72          sb.append("sa1.receiverUserId, sa1.createDate desc");
73  
74          String sql = sb.toString();
75  
76          Connection con = null;
77          PreparedStatement ps = null;
78          ResultSet rs = null;
79  
80          try {
81              con = DataAccess.getConnection();
82  
83              ps = con.prepareStatement(sql);
84  
85              rs = ps.executeQuery();
86  
87              long groupId = 0;
88              long userId = 0;
89              long classNameId = 0;
90              long classPK = 0;
91              long type = 0;
92              String extraData = StringPool.BLANK;
93              long receiverUserId = 0;
94  
95              while (rs.next()) {
96                  long curActivityId = rs.getLong("activityId");
97                  long curGroupId = rs.getLong("groupId");
98                  long curUserId = rs.getLong("userId");
99                  long curClassNameId = rs.getLong("classNameId");
100                 long curClassPK = rs.getLong("classPK");
101                 long curType = rs.getLong("type_");
102                 String curExtraData = rs.getString("extraData");
103                 long curReceiverUserId = rs.getLong("receiverUserId");
104 
105                 if ((curGroupId == groupId) && (curUserId == userId) &&
106                     (curClassNameId == classNameId) &&
107                     (curClassPK == classPK) && (curType == type) &&
108                     (curExtraData.equals(extraData)) &&
109                     (curReceiverUserId == receiverUserId)) {
110 
111                     SocialActivityLocalServiceUtil.deleteActivity(
112                         curActivityId);
113                 }
114                 else {
115                     groupId = curGroupId;
116                     userId = curUserId;
117                     classNameId = curClassNameId;
118                     classPK = curClassPK;
119                     type = curType;
120                     extraData = curExtraData;
121                     receiverUserId = curReceiverUserId;
122                 }
123             }
124         }
125         finally {
126             DataAccess.cleanUp(con, ps, rs);
127         }
128     }
129 
130     protected void deleteDuplicateRequests() throws Exception {
131         StringBuilder sb = new StringBuilder();
132 
133         sb.append("select distinct sr1.* from SocialRequest sr1 ");
134         sb.append("inner join SocialRequest sr2 on ");
135         sb.append("sr1.requestId != sr2.requestId and ");
136         sb.append("sr1.groupId = sr2.groupId and ");
137         sb.append("sr1.userId = sr2.userId and ");
138         sb.append("sr1.classNameId = sr2.classNameId and ");
139         sb.append("sr1.classPK = sr2.classPK and ");
140         sb.append("sr1.type_ = sr2.type_ and ");
141         sb.append("sr1.extraData = sr2.extraData and ");
142         sb.append("sr1.receiverUserId = sr2.receiverUserId ");
143         sb.append("order by sr1.groupId, sr1.userId, sr1.classNameId, ");
144         sb.append("sr1.classPK, sr1.type_, sr1.extraData, ");
145         sb.append("sr1.receiverUserId, sr1.createDate desc");
146 
147         String sql = sb.toString();
148 
149         Connection con = null;
150         PreparedStatement ps = null;
151         ResultSet rs = null;
152 
153         try {
154             con = DataAccess.getConnection();
155 
156             ps = con.prepareStatement(sql);
157 
158             rs = ps.executeQuery();
159 
160             long groupId = 0;
161             long userId = 0;
162             long classNameId = 0;
163             long classPK = 0;
164             long type = 0;
165             String extraData = StringPool.BLANK;
166             long receiverUserId = 0;
167 
168             while (rs.next()) {
169                 long curRequestId = rs.getLong("requestId");
170                 long curGroupId = rs.getLong("groupId");
171                 long curUserId = rs.getLong("userId");
172                 long curClassNameId = rs.getLong("classNameId");
173                 long curClassPK = rs.getLong("classPK");
174                 long curType = rs.getLong("type_");
175                 String curExtraData = rs.getString("extraData");
176                 long curReceiverUserId = rs.getLong("receiverUserId");
177 
178                 if ((curGroupId == groupId) && (curUserId == userId) &&
179                     (curClassNameId == classNameId) &&
180                     (curClassPK == classPK) && (curType == type) &&
181                     (curExtraData.equals(extraData)) &&
182                     (curReceiverUserId == receiverUserId)) {
183 
184                     SocialRequestLocalServiceUtil.deleteRequest(curRequestId);
185                 }
186                 else {
187                     groupId = curGroupId;
188                     userId = curUserId;
189                     classNameId = curClassNameId;
190                     classPK = curClassPK;
191                     type = curType;
192                     extraData = curExtraData;
193                     receiverUserId = curReceiverUserId;
194                 }
195             }
196         }
197         finally {
198             DataAccess.cleanUp(con, ps, rs);
199         }
200     }
201 
202     protected void verifySocial() throws Exception {
203 
204         // Temporarily comment these out because of performance issues. This
205         // verification is not needed because activities can be added while
206         // ensuring a duplicate does not exist. See LEP-6593.
207 
208         //deleteDuplicateActivities();
209         //deleteDuplicateRequests();
210     }
211 
212     private static Log _log = LogFactory.getLog(VerifySocial.class);
213 
214 }