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 void deleteSubscription(long subscriptionId)
127 throws PortalException, SystemException {
128
129 Subscription subscription = subscriptionPersistence.fetchByPrimaryKey(
130 subscriptionId);
131
132 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 void 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
171 public void deleteSubscriptions(long userId)
172 throws PortalException, SystemException {
173
174 List<Subscription> subscriptions = subscriptionPersistence.findByUserId(
175 userId);
176
177 for (Subscription subscription : subscriptions) {
178 deleteSubscription(subscription);
179 }
180 }
181
182 public void deleteSubscriptions(
183 long companyId, String className, long classPK)
184 throws PortalException, SystemException {
185
186 long classNameId = PortalUtil.getClassNameId(className);
187
188 List<Subscription> subscriptions = subscriptionPersistence.findByC_C_C(
189 companyId, classNameId, classPK);
190
191 for (Subscription subscription : subscriptions) {
192 deleteSubscription(subscription);
193 }
194 }
195
196 public Subscription getSubscription(
197 long companyId, long userId, String className, long classPK)
198 throws PortalException, SystemException {
199
200 long classNameId = PortalUtil.getClassNameId(className);
201
202 return subscriptionPersistence.findByC_U_C_C(
203 companyId, userId, classNameId, classPK);
204 }
205
206 public List<Subscription> getSubscriptions(
207 long companyId, String className, long classPK)
208 throws SystemException {
209
210 long classNameId = PortalUtil.getClassNameId(className);
211
212 return subscriptionPersistence.findByC_C_C(
213 companyId, classNameId, classPK);
214 }
215
216 public List<Subscription> getUserSubscriptions(
217 long userId, int start, int end,
218 OrderByComparator orderByComparator)
219 throws SystemException {
220
221 return subscriptionPersistence.findByUserId(
222 userId, start, end, orderByComparator);
223 }
224
225 public List<Subscription> getUserSubscriptions(
226 long userId, String className)
227 throws SystemException {
228
229 long classNameId = PortalUtil.getClassNameId(className);
230
231 return subscriptionPersistence.findByU_C(userId, classNameId);
232 }
233
234 public int getUserSubscriptionsCount(long userId) throws SystemException {
235 return subscriptionPersistence.countByUserId(userId);
236 }
237
238 public boolean isSubscribed(
239 long companyId, long userId, String className, long classPK)
240 throws SystemException {
241
242 long classNameId = PortalUtil.getClassNameId(className);
243
244 Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
245 companyId, userId, classNameId, classPK);
246
247 if (subscription != null) {
248 return true;
249 }
250 else {
251 return false;
252 }
253 }
254
255 }