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.kernel.util.StringPool;
023 import com.liferay.portal.model.Subscription;
024 import com.liferay.portal.model.SubscriptionConstants;
025 import com.liferay.portal.model.User;
026 import com.liferay.portal.service.base.SubscriptionLocalServiceBaseImpl;
027 import com.liferay.portal.util.PortalUtil;
028 import com.liferay.portlet.asset.model.AssetEntry;
029 import com.liferay.portlet.messageboards.model.MBMessage;
030 import com.liferay.portlet.messageboards.model.MBThread;
031 import com.liferay.portlet.social.model.SocialActivityConstants;
032
033 import java.util.Date;
034 import java.util.List;
035
036
045 public class SubscriptionLocalServiceImpl
046 extends SubscriptionLocalServiceBaseImpl {
047
048
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 public Subscription addSubscription(
104 long userId, long groupId, String className, long classPK,
105 String frequency)
106 throws PortalException, SystemException {
107
108
109
110 User user = userPersistence.findByPrimaryKey(userId);
111 long classNameId = PortalUtil.getClassNameId(className);
112 Date now = new Date();
113
114 long subscriptionId = counterLocalService.increment();
115
116 Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
117 user.getCompanyId(), userId, classNameId, classPK);
118
119 if (subscription == null) {
120 subscription = subscriptionPersistence.create(subscriptionId);
121
122 subscription.setCompanyId(user.getCompanyId());
123 subscription.setUserId(user.getUserId());
124 subscription.setUserName(user.getFullName());
125 subscription.setCreateDate(now);
126 subscription.setModifiedDate(now);
127 subscription.setClassNameId(classNameId);
128 subscription.setClassPK(classPK);
129 subscription.setFrequency(frequency);
130
131 subscriptionPersistence.update(subscription);
132 }
133
134 if (groupId > 0) {
135
136
137
138 try {
139 assetEntryLocalService.getEntry(className, classPK);
140 }
141 catch (Exception e) {
142 assetEntryLocalService.updateEntry(
143 userId, groupId, subscription.getCreateDate(),
144 subscription.getModifiedDate(), className, classPK, null, 0,
145 null, null, false, null, null, null, null,
146 String.valueOf(groupId), null, null, null, null, 0, 0, null,
147 false);
148 }
149
150
151
152 if (className.equals(MBThread.class.getName())) {
153 MBThread mbThread = mbThreadLocalService.getMBThread(classPK);
154
155 JSONObject extraDataJSONObject =
156 JSONFactoryUtil.createJSONObject();
157
158 extraDataJSONObject.put("threadId", classPK);
159
160 socialActivityLocalService.addActivity(
161 userId, groupId, MBMessage.class.getName(),
162 mbThread.getRootMessageId(),
163 SocialActivityConstants.TYPE_SUBSCRIBE,
164 extraDataJSONObject.toString(), 0);
165 }
166 else {
167 if (classPK != groupId) {
168 socialActivityLocalService.addActivity(
169 userId, groupId, className, classPK,
170 SocialActivityConstants.TYPE_SUBSCRIBE,
171 StringPool.BLANK, 0);
172 }
173 }
174 }
175
176 return subscription;
177 }
178
179
188 @Override
189 public Subscription deleteSubscription(long subscriptionId)
190 throws PortalException, SystemException {
191
192 Subscription subscription = subscriptionPersistence.fetchByPrimaryKey(
193 subscriptionId);
194
195 return deleteSubscription(subscription);
196 }
197
198
209 public void deleteSubscription(long userId, String className, long classPK)
210 throws PortalException, SystemException {
211
212 User user = userPersistence.findByPrimaryKey(userId);
213 long classNameId = PortalUtil.getClassNameId(className);
214
215 Subscription subscription = subscriptionPersistence.findByC_U_C_C(
216 user.getCompanyId(), userId, classNameId, classPK);
217
218 deleteSubscription(subscription);
219 }
220
221
230 @Override
231 public Subscription deleteSubscription(Subscription subscription)
232 throws PortalException, SystemException {
233
234
235
236 subscriptionPersistence.remove(subscription);
237
238
239
240 AssetEntry assetEntry = assetEntryPersistence.fetchByC_C(
241 subscription.getClassNameId(), subscription.getClassPK());
242
243 if (assetEntry != null) {
244 String className = PortalUtil.getClassName(
245 subscription.getClassNameId());
246
247 socialActivityLocalService.addActivity(
248 subscription.getUserId(), assetEntry.getGroupId(), className,
249 subscription.getClassPK(),
250 SocialActivityConstants.TYPE_UNSUBSCRIBE, StringPool.BLANK, 0);
251 }
252
253 return subscription;
254 }
255
256
263 public void deleteSubscriptions(long userId)
264 throws PortalException, SystemException {
265
266 List<Subscription> subscriptions = subscriptionPersistence.findByUserId(
267 userId);
268
269 for (Subscription subscription : subscriptions) {
270 deleteSubscription(subscription);
271 }
272 }
273
274
283 public void deleteSubscriptions(
284 long companyId, String className, long classPK)
285 throws PortalException, SystemException {
286
287 long classNameId = PortalUtil.getClassNameId(className);
288
289 List<Subscription> subscriptions = subscriptionPersistence.findByC_C_C(
290 companyId, classNameId, classPK);
291
292 for (Subscription subscription : subscriptions) {
293 deleteSubscription(subscription);
294 }
295 }
296
297
308 public Subscription getSubscription(
309 long companyId, long userId, String className, long classPK)
310 throws PortalException, SystemException {
311
312 long classNameId = PortalUtil.getClassNameId(className);
313
314 return subscriptionPersistence.findByC_U_C_C(
315 companyId, userId, classNameId, classPK);
316 }
317
318
328 public List<Subscription> getSubscriptions(
329 long companyId, long userId, String className, long[] classPKs)
330 throws SystemException {
331
332 long classNameId = PortalUtil.getClassNameId(className);
333
334 return subscriptionPersistence.findByC_U_C_C(
335 companyId, userId, classNameId, classPKs);
336 }
337
338
347 public List<Subscription> getSubscriptions(
348 long companyId, String className, long classPK)
349 throws SystemException {
350
351 long classNameId = PortalUtil.getClassNameId(className);
352
353 return subscriptionPersistence.findByC_C_C(
354 companyId, classNameId, classPK);
355 }
356
357
366 public List<Subscription> getUserSubscriptions(
367 long userId, int start, int end,
368 OrderByComparator orderByComparator)
369 throws SystemException {
370
371 return subscriptionPersistence.findByUserId(
372 userId, start, end, orderByComparator);
373 }
374
375
384 public List<Subscription> getUserSubscriptions(
385 long userId, String className)
386 throws SystemException {
387
388 long classNameId = PortalUtil.getClassNameId(className);
389
390 return subscriptionPersistence.findByU_C(userId, classNameId);
391 }
392
393
400 public int getUserSubscriptionsCount(long userId) throws SystemException {
401 return subscriptionPersistence.countByUserId(userId);
402 }
403
404
415 public boolean isSubscribed(
416 long companyId, long userId, String className, long classPK)
417 throws SystemException {
418
419 long classNameId = PortalUtil.getClassNameId(className);
420
421 Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
422 companyId, userId, classNameId, classPK);
423
424 if (subscription != null) {
425 return true;
426 }
427 else {
428 return false;
429 }
430 }
431
432
444 public boolean isSubscribed(
445 long companyId, long userId, String className, long[] classPKs)
446 throws SystemException {
447
448 long classNameId = PortalUtil.getClassNameId(className);
449
450 int count = subscriptionPersistence.countByC_U_C_C(
451 companyId, userId, classNameId, classPKs);
452
453 if (count > 0) {
454 return true;
455 }
456 else {
457 return false;
458 }
459 }
460
461 }