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.PortalException;
19  import com.liferay.portal.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.Role;
29  import com.liferay.portal.model.RoleConstants;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.model.UserGroupRole;
32  import com.liferay.portal.model.impl.MembershipRequestImpl;
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(MembershipRequestImpl.STATUS_PENDING);
73  
74          membershipRequestPersistence.update(membershipRequest, false);
75  
76          try {
77              notifyCommunityAdministrators(membershipRequest);
78          }
79          catch (IOException ioe) {
80              throw new SystemException(ioe);
81          }
82  
83          return membershipRequest;
84      }
85  
86      public MembershipRequest getMembershipRequest(long membershipRequestId)
87          throws PortalException, SystemException {
88  
89          return membershipRequestPersistence.findByPrimaryKey(
90              membershipRequestId);
91      }
92  
93      public void deleteMembershipRequests(long groupId) throws SystemException {
94          membershipRequestPersistence.removeByGroupId(groupId);
95      }
96  
97      public void deleteMembershipRequests(long groupId, int statusId)
98          throws SystemException {
99  
100         membershipRequestPersistence.removeByG_S(groupId, statusId);
101     }
102 
103     public List<MembershipRequest> search(
104             long groupId, int status, int start, int end)
105         throws SystemException {
106 
107         return membershipRequestPersistence.findByG_S(
108             groupId, status, start, end);
109     }
110 
111     public int searchCount(long groupId, int status) throws SystemException {
112         return membershipRequestPersistence.countByG_S(groupId, status);
113     }
114 
115     public void updateStatus(
116             long replierUserId, long membershipRequestId, String replyComments,
117             int statusId)
118         throws PortalException, SystemException {
119 
120         validate(replyComments);
121 
122         MembershipRequest membershipRequest =
123             membershipRequestPersistence.findByPrimaryKey(
124                 membershipRequestId);
125 
126         membershipRequest.setReplyComments(replyComments);
127         membershipRequest.setReplyDate(new Date());
128         membershipRequest.setReplierUserId(replierUserId);
129         membershipRequest.setStatusId(statusId);
130 
131         membershipRequestPersistence.update(membershipRequest, false);
132 
133         if (statusId == MembershipRequestImpl.STATUS_APPROVED) {
134             long[] addUserIds = new long[] {membershipRequest.getUserId()};
135 
136             userLocalService.addGroupUsers(
137                 membershipRequest.getGroupId(), addUserIds);
138         }
139 
140         try {
141             notify(
142                 membershipRequest.getUserId(), membershipRequest,
143                 PropsKeys.COMMUNITIES_EMAIL_MEMBERSHIP_REPLY_SUBJECT,
144                 PropsKeys.COMMUNITIES_EMAIL_MEMBERSHIP_REPLY_BODY);
145         }
146         catch (IOException ioe) {
147             throw new SystemException(ioe);
148         }
149     }
150 
151     protected void notify(
152             long userId, MembershipRequest membershipRequest,
153             String subjectProperty, String bodyProperty)
154         throws IOException, PortalException, SystemException {
155 
156         Company company = companyPersistence.findByPrimaryKey(
157             membershipRequest.getCompanyId());
158 
159         Group group = groupPersistence.findByPrimaryKey(
160             membershipRequest.getGroupId());
161 
162         User user = userPersistence.findByPrimaryKey(userId);
163         User requestUser = userPersistence.findByPrimaryKey(
164             membershipRequest.getUserId());
165 
166         String fromName = PrefsPropsUtil.getString(
167             membershipRequest.getCompanyId(),
168             PropsKeys.COMMUNITIES_EMAIL_FROM_NAME);
169 
170         String fromAddress = PrefsPropsUtil.getString(
171             membershipRequest.getCompanyId(),
172             PropsKeys.COMMUNITIES_EMAIL_FROM_ADDRESS);
173 
174         String toName = user.getFullName();
175         String toAddress = user.getEmailAddress();
176 
177         String subject = PrefsPropsUtil.getContent(
178             membershipRequest.getCompanyId(), subjectProperty);
179 
180         String body = PrefsPropsUtil.getContent(
181             membershipRequest.getCompanyId(), bodyProperty);
182 
183         String statusKey = null;
184 
185         if (membershipRequest.getStatusId() ==
186                 MembershipRequestImpl.STATUS_APPROVED) {
187 
188             statusKey = "approved";
189         }
190         else if (membershipRequest.getStatusId() ==
191                     MembershipRequestImpl.STATUS_DENIED) {
192 
193             statusKey = "denied";
194         }
195         else {
196             statusKey = "pending";
197         }
198 
199         subject = StringUtil.replace(
200             subject,
201             new String[] {
202                 "[$COMMUNITY_NAME$]",
203                 "[$COMPANY_ID$]",
204                 "[$COMPANY_MX$]",
205                 "[$COMPANY_NAME$]",
206                 "[$FROM_ADDRESS$]",
207                 "[$FROM_NAME$]",
208                 "[$PORTAL_URL$]",
209                 "[$REQUEST_USER_ADDRESS$]",
210                 "[$REQUEST_USER_NAME$]",
211                 "[$STATUS$]",
212                 "[$TO_NAME$]",
213                 "[$USER_ADDRESS$]",
214                 "[$USER_NAME$]",
215             },
216             new String[] {
217                 group.getName(),
218                 String.valueOf(company.getCompanyId()),
219                 company.getMx(),
220                 company.getName(),
221                 fromAddress,
222                 fromName,
223                 company.getVirtualHost(),
224                 requestUser.getEmailAddress(),
225                 requestUser.getFullName(),
226                 LanguageUtil.get(user.getLocale(), statusKey),
227                 toName,
228                 user.getEmailAddress(),
229                 user.getFullName()
230             });
231 
232         body = StringUtil.replace(
233             body,
234             new String[] {
235                 "[$COMMENTS$]",
236                 "[$COMMUNITY_NAME$]",
237                 "[$COMPANY_ID$]",
238                 "[$COMPANY_MX$]",
239                 "[$COMPANY_NAME$]",
240                 "[$FROM_ADDRESS$]",
241                 "[$FROM_NAME$]",
242                 "[$PORTAL_URL$]",
243                 "[$REPLY_COMMENTS$]",
244                 "[$REQUEST_USER_NAME$]",
245                 "[$REQUEST_USER_ADDRESS$]",
246                 "[$STATUS$]",
247                 "[$TO_NAME$]",
248                 "[$USER_ADDRESS$]",
249                 "[$USER_NAME$]",
250             },
251             new String[] {
252                 membershipRequest.getComments(),
253                 group.getName(),
254                 String.valueOf(company.getCompanyId()),
255                 company.getMx(),
256                 company.getName(),
257                 fromAddress,
258                 fromName,
259                 company.getVirtualHost(),
260                 membershipRequest.getReplyComments(),
261                 requestUser.getFullName(),
262                 requestUser.getEmailAddress(),
263                 LanguageUtil.get(user.getLocale(), statusKey),
264                 toName,
265                 user.getEmailAddress(),
266                 user.getFullName()
267             });
268 
269         InternetAddress from = new InternetAddress(fromAddress, fromName);
270 
271         InternetAddress to = new InternetAddress(toAddress, toName);
272 
273         MailMessage message = new MailMessage(from, to, subject, body, true);
274 
275         mailService.sendEmail(message);
276     }
277 
278     protected void notifyCommunityAdministrators(
279             MembershipRequest membershipRequest)
280         throws IOException, PortalException, SystemException {
281 
282         List<UserGroupRole> admins = new UniqueList<UserGroupRole>();
283 
284         Role communityAdminRole = roleLocalService.getRole(
285             membershipRequest.getCompanyId(),
286             RoleConstants.COMMUNITY_ADMINISTRATOR);
287 
288         List<UserGroupRole> communityAdmins =
289             userGroupRoleLocalService.getUserGroupRolesByGroupAndRole(
290                 membershipRequest.getGroupId(), communityAdminRole.getRoleId());
291 
292         admins.addAll(communityAdmins);
293 
294         Role communityOwnerRole = rolePersistence.findByC_N(
295             membershipRequest.getCompanyId(), RoleConstants.COMMUNITY_OWNER);
296 
297         List<UserGroupRole> communityOwners =
298             userGroupRoleLocalService.getUserGroupRolesByGroupAndRole(
299                 membershipRequest.getGroupId(), communityOwnerRole.getRoleId());
300 
301         admins.addAll(communityOwners);
302 
303         for (UserGroupRole userGroupRole : admins) {
304             notify(
305                 userGroupRole.getUserId(), membershipRequest,
306                 PropsKeys.COMMUNITIES_EMAIL_MEMBERSHIP_REQUEST_SUBJECT,
307                 PropsKeys.COMMUNITIES_EMAIL_MEMBERSHIP_REQUEST_BODY);
308         }
309     }
310 
311     protected void validate(String comments)
312         throws PortalException {
313 
314         if ((Validator.isNull(comments)) || (Validator.isNumber(comments))) {
315             throw new MembershipRequestCommentsException();
316         }
317     }
318 
319 }