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 socialActivityLocalService.addActivity(
168 userId, groupId, className, classPK,
169 SocialActivityConstants.TYPE_SUBSCRIBE, StringPool.BLANK,
170 0);
171 }
172 }
173
174 return subscription;
175 }
176
177
186 @Override
187 public Subscription deleteSubscription(long subscriptionId)
188 throws PortalException, SystemException {
189
190 Subscription subscription = subscriptionPersistence.fetchByPrimaryKey(
191 subscriptionId);
192
193 return deleteSubscription(subscription);
194 }
195
196
207 public void deleteSubscription(long userId, String className, long classPK)
208 throws PortalException, SystemException {
209
210 User user = userPersistence.findByPrimaryKey(userId);
211 long classNameId = PortalUtil.getClassNameId(className);
212
213 Subscription subscription = subscriptionPersistence.findByC_U_C_C(
214 user.getCompanyId(), userId, classNameId, classPK);
215
216 deleteSubscription(subscription);
217 }
218
219
228 @Override
229 public Subscription deleteSubscription(Subscription subscription)
230 throws PortalException, SystemException {
231
232
233
234 subscriptionPersistence.remove(subscription);
235
236
237
238 AssetEntry assetEntry = assetEntryPersistence.fetchByC_C(
239 subscription.getClassNameId(), subscription.getClassPK());
240
241 if (assetEntry != null) {
242 String className = PortalUtil.getClassName(
243 subscription.getClassNameId());
244
245 socialActivityLocalService.addActivity(
246 subscription.getUserId(), assetEntry.getGroupId(), className,
247 subscription.getClassPK(),
248 SocialActivityConstants.TYPE_UNSUBSCRIBE, StringPool.BLANK, 0);
249 }
250
251 return subscription;
252 }
253
254
261 public void deleteSubscriptions(long userId)
262 throws PortalException, SystemException {
263
264 List<Subscription> subscriptions = subscriptionPersistence.findByUserId(
265 userId);
266
267 for (Subscription subscription : subscriptions) {
268 deleteSubscription(subscription);
269 }
270 }
271
272
281 public void deleteSubscriptions(
282 long companyId, String className, long classPK)
283 throws PortalException, SystemException {
284
285 long classNameId = PortalUtil.getClassNameId(className);
286
287 List<Subscription> subscriptions = subscriptionPersistence.findByC_C_C(
288 companyId, classNameId, classPK);
289
290 for (Subscription subscription : subscriptions) {
291 deleteSubscription(subscription);
292 }
293 }
294
295
306 public Subscription getSubscription(
307 long companyId, long userId, String className, long classPK)
308 throws PortalException, SystemException {
309
310 long classNameId = PortalUtil.getClassNameId(className);
311
312 return subscriptionPersistence.findByC_U_C_C(
313 companyId, userId, classNameId, classPK);
314 }
315
316
326 public List<Subscription> getSubscriptions(
327 long companyId, long userId, String className, long[] classPKs)
328 throws SystemException {
329
330 long classNameId = PortalUtil.getClassNameId(className);
331
332 return subscriptionPersistence.findByC_U_C_C(
333 companyId, userId, classNameId, classPKs);
334 }
335
336
345 public List<Subscription> getSubscriptions(
346 long companyId, String className, long classPK)
347 throws SystemException {
348
349 long classNameId = PortalUtil.getClassNameId(className);
350
351 return subscriptionPersistence.findByC_C_C(
352 companyId, classNameId, classPK);
353 }
354
355
364 public List<Subscription> getUserSubscriptions(
365 long userId, int start, int end,
366 OrderByComparator orderByComparator)
367 throws SystemException {
368
369 return subscriptionPersistence.findByUserId(
370 userId, start, end, orderByComparator);
371 }
372
373
382 public List<Subscription> getUserSubscriptions(
383 long userId, String className)
384 throws SystemException {
385
386 long classNameId = PortalUtil.getClassNameId(className);
387
388 return subscriptionPersistence.findByU_C(userId, classNameId);
389 }
390
391
398 public int getUserSubscriptionsCount(long userId) throws SystemException {
399 return subscriptionPersistence.countByUserId(userId);
400 }
401
402
413 public boolean isSubscribed(
414 long companyId, long userId, String className, long classPK)
415 throws SystemException {
416
417 long classNameId = PortalUtil.getClassNameId(className);
418
419 Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
420 companyId, userId, classNameId, classPK);
421
422 if (subscription != null) {
423 return true;
424 }
425 else {
426 return false;
427 }
428 }
429
430
442 public boolean isSubscribed(
443 long companyId, long userId, String className, long[] classPKs)
444 throws SystemException {
445
446 long classNameId = PortalUtil.getClassNameId(className);
447
448 int count = subscriptionPersistence.countByC_U_C_C(
449 companyId, userId, classNameId, classPKs);
450
451 if (count > 0) {
452 return true;
453 }
454 else {
455 return false;
456 }
457 }
458
459 }