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
040 public class SubscriptionLocalServiceImpl
041 extends SubscriptionLocalServiceBaseImpl {
042
043 public Subscription addSubscription(
044 long userId, long groupId, String className, long classPK)
045 throws PortalException, SystemException {
046
047 return addSubscription(
048 userId, groupId, className, classPK,
049 SubscriptionConstants.FREQUENCY_INSTANT);
050 }
051
052 public Subscription addSubscription(
053 long userId, long groupId, String className, long classPK,
054 String frequency)
055 throws PortalException, SystemException {
056
057
058
059 User user = userPersistence.findByPrimaryKey(userId);
060 long classNameId = PortalUtil.getClassNameId(className);
061 Date now = new Date();
062
063 long subscriptionId = counterLocalService.increment();
064
065 Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
066 user.getCompanyId(), userId, classNameId, classPK);
067
068 if (subscription == null) {
069 subscription = subscriptionPersistence.create(subscriptionId);
070
071 subscription.setCompanyId(user.getCompanyId());
072 subscription.setUserId(user.getUserId());
073 subscription.setUserName(user.getFullName());
074 subscription.setCreateDate(now);
075 subscription.setModifiedDate(now);
076 subscription.setClassNameId(classNameId);
077 subscription.setClassPK(classPK);
078 subscription.setFrequency(frequency);
079
080 subscriptionPersistence.update(subscription, false);
081 }
082
083 if (groupId > 0) {
084
085
086
087 try {
088 assetEntryLocalService.getEntry(className, classPK);
089 }
090 catch (Exception e) {
091 assetEntryLocalService.updateEntry(
092 userId, groupId, className, classPK, null, 0, null, null,
093 false, null, null, null, null, null,
094 String.valueOf(groupId), null, null, null, null, 0, 0, null,
095 false);
096 }
097
098
099
100 if (className.equals(MBThread.class.getName())) {
101 MBThread mbThread = mbThreadLocalService.getMBThread(classPK);
102
103 JSONObject extraDataJSONObject =
104 JSONFactoryUtil.createJSONObject();
105
106 extraDataJSONObject.put("threadId", classPK);
107
108 socialActivityLocalService.addActivity(
109 userId, groupId, MBMessage.class.getName(),
110 mbThread.getRootMessageId(),
111 SocialActivityConstants.TYPE_SUBSCRIBE,
112 extraDataJSONObject.toString(), 0);
113 }
114 else {
115 socialActivityLocalService.addActivity(
116 userId, groupId, className, classPK,
117 SocialActivityConstants.TYPE_SUBSCRIBE, StringPool.BLANK,
118 0);
119 }
120 }
121
122 return subscription;
123 }
124
125 @Override
126 public Subscription deleteSubscription(long subscriptionId)
127 throws PortalException, SystemException {
128
129 Subscription subscription = subscriptionPersistence.fetchByPrimaryKey(
130 subscriptionId);
131
132 return deleteSubscription(subscription);
133 }
134
135 public void deleteSubscription(long userId, String className, long classPK)
136 throws PortalException, SystemException {
137
138 User user = userPersistence.findByPrimaryKey(userId);
139 long classNameId = PortalUtil.getClassNameId(className);
140
141 Subscription subscription = subscriptionPersistence.findByC_U_C_C(
142 user.getCompanyId(), userId, classNameId, classPK);
143
144 deleteSubscription(subscription);
145 }
146
147 @Override
148 public Subscription deleteSubscription(Subscription subscription)
149 throws PortalException, SystemException {
150
151
152
153 subscriptionPersistence.remove(subscription);
154
155
156
157 AssetEntry assetEntry = assetEntryPersistence.fetchByC_C(
158 subscription.getClassNameId(), subscription.getClassPK());
159
160 if (assetEntry != null) {
161 String className = PortalUtil.getClassName(
162 subscription.getClassNameId());
163
164 socialActivityLocalService.addActivity(
165 subscription.getUserId(), assetEntry.getGroupId(), className,
166 subscription.getClassPK(),
167 SocialActivityConstants.TYPE_UNSUBSCRIBE, StringPool.BLANK, 0);
168 }
169
170 return subscription;
171 }
172
173 public void deleteSubscriptions(long userId)
174 throws PortalException, SystemException {
175
176 List<Subscription> subscriptions = subscriptionPersistence.findByUserId(
177 userId);
178
179 for (Subscription subscription : subscriptions) {
180 deleteSubscription(subscription);
181 }
182 }
183
184 public void deleteSubscriptions(
185 long companyId, String className, long classPK)
186 throws PortalException, SystemException {
187
188 long classNameId = PortalUtil.getClassNameId(className);
189
190 List<Subscription> subscriptions = subscriptionPersistence.findByC_C_C(
191 companyId, classNameId, classPK);
192
193 for (Subscription subscription : subscriptions) {
194 deleteSubscription(subscription);
195 }
196 }
197
198 public Subscription getSubscription(
199 long companyId, long userId, String className, long classPK)
200 throws PortalException, SystemException {
201
202 long classNameId = PortalUtil.getClassNameId(className);
203
204 return subscriptionPersistence.findByC_U_C_C(
205 companyId, userId, classNameId, classPK);
206 }
207
208 public List<Subscription> getSubscriptions(
209 long companyId, String className, long classPK)
210 throws SystemException {
211
212 long classNameId = PortalUtil.getClassNameId(className);
213
214 return subscriptionPersistence.findByC_C_C(
215 companyId, classNameId, classPK);
216 }
217
218 public List<Subscription> getUserSubscriptions(
219 long userId, int start, int end,
220 OrderByComparator orderByComparator)
221 throws SystemException {
222
223 return subscriptionPersistence.findByUserId(
224 userId, start, end, orderByComparator);
225 }
226
227 public List<Subscription> getUserSubscriptions(
228 long userId, String className)
229 throws SystemException {
230
231 long classNameId = PortalUtil.getClassNameId(className);
232
233 return subscriptionPersistence.findByU_C(userId, classNameId);
234 }
235
236 public int getUserSubscriptionsCount(long userId) throws SystemException {
237 return subscriptionPersistence.countByUserId(userId);
238 }
239
240 public boolean isSubscribed(
241 long companyId, long userId, String className, long classPK)
242 throws SystemException {
243
244 long classNameId = PortalUtil.getClassNameId(className);
245
246 Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
247 companyId, userId, classNameId, classPK);
248
249 if (subscription != null) {
250 return true;
251 }
252 else {
253 return false;
254 }
255 }
256
257 }