001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.json.JSONFactoryUtil;
020 import com.liferay.portal.kernel.json.JSONObject;
021 import com.liferay.portal.kernel.util.OrderByComparator;
022 import com.liferay.portal.model.Subscription;
023 import com.liferay.portal.model.SubscriptionConstants;
024 import com.liferay.portal.model.User;
025 import com.liferay.portal.service.base.SubscriptionLocalServiceBaseImpl;
026 import com.liferay.portal.util.PortalUtil;
027 import com.liferay.portlet.asset.model.AssetEntry;
028 import com.liferay.portlet.messageboards.model.MBMessage;
029 import com.liferay.portlet.messageboards.model.MBThread;
030 import com.liferay.portlet.social.model.SocialActivityConstants;
031
032 import java.util.Date;
033 import java.util.List;
034
035
044 public class SubscriptionLocalServiceImpl
045 extends SubscriptionLocalServiceBaseImpl {
046
047
070 @Override
071 public Subscription addSubscription(
072 long userId, long groupId, String className, long classPK)
073 throws PortalException, SystemException {
074
075 return addSubscription(
076 userId, groupId, className, classPK,
077 SubscriptionConstants.FREQUENCY_INSTANT);
078 }
079
080
103 @Override
104 public Subscription addSubscription(
105 long userId, long groupId, String className, long classPK,
106 String frequency)
107 throws PortalException, SystemException {
108
109
110
111 User user = userPersistence.findByPrimaryKey(userId);
112 long classNameId = PortalUtil.getClassNameId(className);
113 Date now = new Date();
114
115 long subscriptionId = counterLocalService.increment();
116
117 Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
118 user.getCompanyId(), userId, classNameId, classPK);
119
120 if (subscription == null) {
121 subscription = subscriptionPersistence.create(subscriptionId);
122
123 subscription.setCompanyId(user.getCompanyId());
124 subscription.setUserId(user.getUserId());
125 subscription.setUserName(user.getFullName());
126 subscription.setCreateDate(now);
127 subscription.setModifiedDate(now);
128 subscription.setClassNameId(classNameId);
129 subscription.setClassPK(classPK);
130 subscription.setFrequency(frequency);
131
132 subscriptionPersistence.update(subscription);
133 }
134
135 if (groupId > 0) {
136
137
138
139 AssetEntry assetEntry = null;
140
141 try {
142 assetEntry = assetEntryLocalService.getEntry(
143 className, classPK);
144 }
145 catch (Exception e) {
146 assetEntry = assetEntryLocalService.updateEntry(
147 userId, groupId, subscription.getCreateDate(),
148 subscription.getModifiedDate(), className, classPK, null, 0,
149 null, null, false, null, null, null, null,
150 String.valueOf(groupId), null, null, null, null, 0, 0, null,
151 false);
152 }
153
154
155
156 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
157
158 extraDataJSONObject.put("title", assetEntry.getTitle());
159
160 if (className.equals(MBThread.class.getName())) {
161 MBThread mbThread = mbThreadLocalService.getMBThread(classPK);
162
163 extraDataJSONObject.put("threadId", classPK);
164
165 socialActivityLocalService.addActivity(
166 userId, groupId, MBMessage.class.getName(),
167 mbThread.getRootMessageId(),
168 SocialActivityConstants.TYPE_SUBSCRIBE,
169 extraDataJSONObject.toString(), 0);
170 }
171 else {
172 if (classPK != groupId) {
173 socialActivityLocalService.addActivity(
174 userId, groupId, className, classPK,
175 SocialActivityConstants.TYPE_SUBSCRIBE,
176 extraDataJSONObject.toString(), 0);
177 }
178 }
179 }
180
181 return subscription;
182 }
183
184
193 @Override
194 public Subscription deleteSubscription(long subscriptionId)
195 throws PortalException, SystemException {
196
197 Subscription subscription = subscriptionPersistence.fetchByPrimaryKey(
198 subscriptionId);
199
200 return deleteSubscription(subscription);
201 }
202
203
214 @Override
215 public void deleteSubscription(long userId, String className, long classPK)
216 throws PortalException, SystemException {
217
218 User user = userPersistence.findByPrimaryKey(userId);
219 long classNameId = PortalUtil.getClassNameId(className);
220
221 Subscription subscription = subscriptionPersistence.findByC_U_C_C(
222 user.getCompanyId(), userId, classNameId, classPK);
223
224 deleteSubscription(subscription);
225 }
226
227
236 @Override
237 public Subscription deleteSubscription(Subscription subscription)
238 throws PortalException, SystemException {
239
240
241
242 subscriptionPersistence.remove(subscription);
243
244
245
246 AssetEntry assetEntry = assetEntryPersistence.fetchByC_C(
247 subscription.getClassNameId(), subscription.getClassPK());
248
249 if (assetEntry != null) {
250 String className = PortalUtil.getClassName(
251 subscription.getClassNameId());
252
253 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
254
255 extraDataJSONObject.put("title", assetEntry.getTitle());
256
257 socialActivityLocalService.addActivity(
258 subscription.getUserId(), assetEntry.getGroupId(), className,
259 subscription.getClassPK(),
260 SocialActivityConstants.TYPE_UNSUBSCRIBE,
261 extraDataJSONObject.toString(), 0);
262 }
263
264 return subscription;
265 }
266
267
274 @Override
275 public void deleteSubscriptions(long userId)
276 throws PortalException, SystemException {
277
278 List<Subscription> subscriptions = subscriptionPersistence.findByUserId(
279 userId);
280
281 for (Subscription subscription : subscriptions) {
282 deleteSubscription(subscription);
283 }
284 }
285
286
295 @Override
296 public void deleteSubscriptions(
297 long companyId, String className, long classPK)
298 throws PortalException, SystemException {
299
300 long classNameId = PortalUtil.getClassNameId(className);
301
302 List<Subscription> subscriptions = subscriptionPersistence.findByC_C_C(
303 companyId, classNameId, classPK);
304
305 for (Subscription subscription : subscriptions) {
306 deleteSubscription(subscription);
307 }
308 }
309
310
321 @Override
322 public Subscription getSubscription(
323 long companyId, long userId, String className, long classPK)
324 throws PortalException, SystemException {
325
326 long classNameId = PortalUtil.getClassNameId(className);
327
328 return subscriptionPersistence.findByC_U_C_C(
329 companyId, userId, classNameId, classPK);
330 }
331
332
342 @Override
343 public List<Subscription> getSubscriptions(
344 long companyId, long userId, String className, long[] classPKs)
345 throws SystemException {
346
347 long classNameId = PortalUtil.getClassNameId(className);
348
349 return subscriptionPersistence.findByC_U_C_C(
350 companyId, userId, classNameId, classPKs);
351 }
352
353
362 @Override
363 public List<Subscription> getSubscriptions(
364 long companyId, String className, long classPK)
365 throws SystemException {
366
367 long classNameId = PortalUtil.getClassNameId(className);
368
369 return subscriptionPersistence.findByC_C_C(
370 companyId, classNameId, classPK);
371 }
372
373
383 @Override
384 public List<Subscription> getUserSubscriptions(
385 long userId, int start, int end,
386 OrderByComparator orderByComparator)
387 throws SystemException {
388
389 return subscriptionPersistence.findByUserId(
390 userId, start, end, orderByComparator);
391 }
392
393
402 @Override
403 public List<Subscription> getUserSubscriptions(
404 long userId, String className)
405 throws SystemException {
406
407 long classNameId = PortalUtil.getClassNameId(className);
408
409 return subscriptionPersistence.findByU_C(userId, classNameId);
410 }
411
412
419 @Override
420 public int getUserSubscriptionsCount(long userId) throws SystemException {
421 return subscriptionPersistence.countByUserId(userId);
422 }
423
424
435 @Override
436 public boolean isSubscribed(
437 long companyId, long userId, String className, long classPK)
438 throws SystemException {
439
440 long classNameId = PortalUtil.getClassNameId(className);
441
442 Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
443 companyId, userId, classNameId, classPK);
444
445 if (subscription != null) {
446 return true;
447 }
448 else {
449 return false;
450 }
451 }
452
453
465 @Override
466 public boolean isSubscribed(
467 long companyId, long userId, String className, long[] classPKs)
468 throws SystemException {
469
470 long classNameId = PortalUtil.getClassNameId(className);
471
472 int count = subscriptionPersistence.countByC_U_C_C(
473 companyId, userId, classNameId, classPKs);
474
475 if (count > 0) {
476 return true;
477 }
478 else {
479 return false;
480 }
481 }
482
483 }