1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.MembershipRequestCommentsException;
18  import com.liferay.portal.kernel.exception.PortalException;
19  import com.liferay.portal.kernel.exception.SystemException;
20  import com.liferay.portal.kernel.language.LanguageUtil;
21  import com.liferay.portal.kernel.mail.MailMessage;
22  import com.liferay.portal.kernel.util.PropsKeys;
23  import com.liferay.portal.kernel.util.StringUtil;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.model.Company;
26  import com.liferay.portal.model.Group;
27  import com.liferay.portal.model.MembershipRequest;
28  import com.liferay.portal.model.MembershipRequestConstants;
29  import com.liferay.portal.model.Role;
30  import com.liferay.portal.model.RoleConstants;
31  import com.liferay.portal.model.User;
32  import com.liferay.portal.model.UserGroupRole;
33  import com.liferay.portal.service.base.MembershipRequestLocalServiceBaseImpl;
34  import com.liferay.portal.util.PrefsPropsUtil;
35  import com.liferay.util.UniqueList;
36  
37  import java.io.IOException;
38  
39  import java.util.Date;
40  import java.util.List;
41  
42  import javax.mail.internet.InternetAddress;
43  
44  /**
45   * <a href="MembershipRequestLocalServiceImpl.java.html"><b><i>View Source</i>
46   * </b></a>
47   *
48   * @author Jorge Ferrer
49   */
50  public class MembershipRequestLocalServiceImpl
51      extends MembershipRequestLocalServiceBaseImpl {
52  
53      public MembershipRequest addMembershipRequest(
54              long userId, long groupId, String comments)
55          throws PortalException, SystemException {
56  
57          User user = userPersistence.findByPrimaryKey(userId);
58          Date now = new Date();
59  
60          validate(comments);
61  
62          long membershipRequestId = counterLocalService.increment();
63  
64          MembershipRequest membershipRequest =
65              membershipRequestPersistence.create(membershipRequestId);
66  
67          membershipRequest.setCompanyId(user.getCompanyId());
68          membershipRequest.setUserId(userId);
69          membershipRequest.setCreateDate(now);
70          membershipRequest.setGroupId(groupId);
71          membershipRequest.setComments(comments);
72          membershipRequest.setStatusId(
73              MembershipRequestConstants.STATUS_PENDING);
74  
75          membershipRequestPersistence.update(membershipRequest, false);
76  
77          try {
78              notifyCommunityAdministrators(membershipRequest);
79          }
80          catch (IOException ioe) {
81              throw new SystemException(ioe);
82          }
83  
84          return membershipRequest;
85      }
86  
87      public MembershipRequest getMembershipRequest(long membershipRequestId)
88          throws PortalException, SystemException {
89  
90          return membershipRequestPersistence.findByPrimaryKey(
91              membershipRequestId);
92      }
93  
94      public void deleteMembershipRequests(long groupId) throws SystemException {
95          membershipRequestPersistence.removeByGroupId(groupId);
96      }
97  
98      public void deleteMembershipRequests(long groupId, int statusId)
99          throws SystemException {
100 
101         membershipRequestPersistence.removeByG_S(groupId, statusId);
102     }
103 
104     public List<MembershipRequest> search(
105             long groupId, int status, int start, int end)
106         throws SystemException {
107 
108         return membershipRequestPersistence.findByG_S(
109             groupId, status, start, end);
110     }
111 
112     public int searchCount(long groupId, int status) throws SystemException {
113         return membershipRequestPersistence.countByG_S(groupId, status);
114     }
115 
116     public void updateStatus(
117             long replierUserId, long membershipRequestId, String replyComments,
118             int statusId)
119         throws PortalException, SystemException {
120 
121         validate(replyComments);
122 
123         MembershipRequest membershipRequest =
124             membershipRequestPersistence.findByPrimaryKey(
125                 membershipRequestId);
126 
127         membershipRequest.setReplyComments(replyComments);
128         membershipRequest.setReplyDate(new Date());
129         membershipRequest.setReplierUserId(replierUserId);
130         membershipRequest.setStatusId(statusId);
131 
132         membershipRequestPersistence.update(membershipRequest, false);
133 
134         if (statusId == MembershipRequestConstants.STATUS_APPROVED) {
135             long[] addUserIds = new long[] {membershipRequest.getUserId()};
136 
137             userLocalService.addGroupUsers(
138                 membershipRequest.getGroupId(), addUserIds);
139         }
140 
141         try {
142             notify(
143                 membershipRequest.getUserId(), membershipRequest,
144                 PropsKeys.COMMUNITIES_EMAIL_MEMBERSHIP_REPLY_SUBJECT,
145                 PropsKeys.COMMUNITIES_EMAIL_MEMBERSHIP_REPLY_BODY);
146         }
147         catch (IOException ioe) {
148             throw new SystemException(ioe);
149         }
150     }
151 
152     protected void notify(
153             long userId, MembershipRequest membershipRequest,
154             String subjectProperty, String bodyProperty)
155         throws IOException, PortalException, SystemException {
156 
157         Company company = companyPersistence.findByPrimaryKey(
158             membershipRequest.getCompanyId());
159 
160         Group group = groupPersistence.findByPrimaryKey(
161             membershipRequest.getGroupId());
162 
163         User user = userPersistence.findByPrimaryKey(userId);
164         User requestUser = userPersistence.findByPrimaryKey(
165             membershipRequest.getUserId());
166 
167         String fromName = PrefsPropsUtil.getString(
168             membershipRequest.getCompanyId(),
169             PropsKeys.COMMUNITIES_EMAIL_FROM_NAME);
170 
171         String fromAddress = PrefsPropsUtil.getString(
172             membershipRequest.getCompanyId(),
173             PropsKeys.COMMUNITIES_EMAIL_FROM_ADDRESS);
174 
175         String toName = user.getFullName();
176         String toAddress = user.getEmailAddress();
177 
178         String subject = PrefsPropsUtil.getContent(
179             membershipRequest.getCompanyId(), subjectProperty);
180 
181         String body = PrefsPropsUtil.getContent(
182             membershipRequest.getCompanyId(), bodyProperty);
183 
184         String statusKey = null;
185 
186         if (membershipRequest.getStatusId() ==
187                 MembershipRequestConstants.STATUS_APPROVED) {
188 
189             statusKey = "approved";
190         }
191         else if (membershipRequest.getStatusId() ==
192                     MembershipRequestConstants.STATUS_DENIED) {
193 
194             statusKey = "denied";
195         }
196         else {
197             statusKey = "pending";
198         }
199 
200         subject = StringUtil.replace(
201             subject,
202             new String[] {
203                 "[$COMMUNITY_NAME$]",
204                 "[$COMPANY_ID$]",
205                 "[$COMPANY_MX$]",
206                 "[$COMPANY_NAME$]",
207                 "[$FROM_ADDRESS$]",
208                 "[$FROM_NAME$]",
209                 "[$PORTAL_URL$]",
210                 "[$REQUEST_USER_ADDRESS$]",
211                 "[$REQUEST_USER_NAME$]",
212                 "[$STATUS$]",
213                 "[$TO_NAME$]",
214                 "[$USER_ADDRESS$]",
215                 "[$USER_NAME$]",
216             },
217             new String[] {
218                 group.getName(),
219                 String.valueOf(company.getCompanyId()),
220                 company.getMx(),
221                 company.getName(),
222                 fromAddress,
223                 fromName,
224                 company.getVirtualHost(),
225                 requestUser.getEmailAddress(),
226                 requestUser.getFullName(),
227                 LanguageUtil.get(user.getLocale(), statusKey),
228                 toName,
229                 user.getEmailAddress(),
230                 user.getFullName()
231             });
232 
233         body = StringUtil.replace(
234             body,
235             new String[] {
236                 "[$COMMENTS$]",
237                 "[$COMMUNITY_NAME$]",
238                 "[$COMPANY_ID$]",
239                 "[$COMPANY_MX$]",
240                 "[$COMPANY_NAME$]",
241                 "[$FROM_ADDRESS$]",
242                 "[$FROM_NAME$]",
243                 "[$PORTAL_URL$]",
244                 "[$REPLY_COMMENTS$]",
245                 "[$REQUEST_USER_NAME$]",
246                 "[$REQUEST_USER_ADDRESS$]",
247                 "[$STATUS$]",
248                 "[$TO_NAME$]",
249                 "[$USER_ADDRESS$]",
250                 "[$USER_NAME$]",
251             },
252             new String[] {
253                 membershipRequest.getComments(),
254                 group.getName(),
255                 String.valueOf(company.getCompanyId()),
256                 company.getMx(),
257                 company.getName(),
258                 fromAddress,
259                 fromName,
260                 company.getVirtualHost(),
261                 membershipRequest.getReplyComments(),
262                 requestUser.getFullName(),
263                 requestUser.getEmailAddress(),
264                 LanguageUtil.get(user.getLocale(), statusKey),
265                 toName,
266                 user.getEmailAddress(),
267                 user.getFullName()
268             });
269 
270         InternetAddress from = new InternetAddress(fromAddress, fromName);
271 
272         InternetAddress to = new InternetAddress(toAddress, toName);
273 
274         MailMessage message = new MailMessage(from, to, subject, body, true);
275 
276         mailService.sendEmail(message);
277     }
278 
279     protected void notifyCommunityAdministrators(
280             MembershipRequest membershipRequest)
281         throws IOException, PortalException, SystemException {
282 
283         List<UserGroupRole> admins = new UniqueList<UserGroupRole>();
284 
285         Role communityAdminRole = roleLocalService.getRole(
286             membershipRequest.getCompanyId(),
287             RoleConstants.COMMUNITY_ADMINISTRATOR);
288 
289         List<UserGroupRole> communityAdmins =
290             userGroupRoleLocalService.getUserGroupRolesByGroupAndRole(
291                 membershipRequest.getGroupId(), communityAdminRole.getRoleId());
292 
293         admins.addAll(communityAdmins);
294 
295         Role communityOwnerRole = rolePersistence.findByC_N(
296             membershipRequest.getCompanyId(), RoleConstants.COMMUNITY_OWNER);
297 
298         List<UserGroupRole> communityOwners =
299             userGroupRoleLocalService.getUserGroupRolesByGroupAndRole(
300                 membershipRequest.getGroupId(), communityOwnerRole.getRoleId());
301 
302         admins.addAll(communityOwners);
303 
304         for (UserGroupRole userGroupRole : admins) {
305             notify(
306                 userGroupRole.getUserId(), membershipRequest,
307                 PropsKeys.COMMUNITIES_EMAIL_MEMBERSHIP_REQUEST_SUBJECT,
308                 PropsKeys.COMMUNITIES_EMAIL_MEMBERSHIP_REQUEST_BODY);
309         }
310     }
311 
312     protected void validate(String comments)
313         throws PortalException {
314 
315         if ((Validator.isNull(comments)) || (Validator.isNumber(comments))) {
316             throw new MembershipRequestCommentsException();
317         }
318     }
319 
320 }