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