1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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 void deleteMembershipRequestsByUserId(long userId)
104         throws SystemException {
105 
106         membershipRequestPersistence.removeByUserId(userId);
107     }
108 
109     public List<MembershipRequest> search(
110             long groupId, int status, int start, int end)
111         throws SystemException {
112 
113         return membershipRequestPersistence.findByG_S(
114             groupId, status, start, end);
115     }
116 
117     public int searchCount(long groupId, int status) throws SystemException {
118         return membershipRequestPersistence.countByG_S(groupId, status);
119     }
120 
121     public void updateStatus(
122             long replierUserId, long membershipRequestId, String replyComments,
123             int statusId)
124         throws PortalException, SystemException {
125 
126         validate(replyComments);
127 
128         MembershipRequest membershipRequest =
129             membershipRequestPersistence.findByPrimaryKey(
130                 membershipRequestId);
131 
132         membershipRequest.setReplyComments(replyComments);
133         membershipRequest.setReplyDate(new Date());
134         membershipRequest.setReplierUserId(replierUserId);
135         membershipRequest.setStatusId(statusId);
136 
137         membershipRequestPersistence.update(membershipRequest, false);
138 
139         if (statusId == MembershipRequestImpl.STATUS_APPROVED) {
140             long[] addUserIds = new long[] {membershipRequest.getUserId()};
141 
142             userLocalService.addGroupUsers(
143                 membershipRequest.getGroupId(), addUserIds);
144         }
145 
146         try {
147             notify(
148                 membershipRequest.getUserId(), membershipRequest,
149                 PropsKeys.COMMUNITIES_EMAIL_MEMBERSHIP_REPLY_SUBJECT,
150                 PropsKeys.COMMUNITIES_EMAIL_MEMBERSHIP_REPLY_BODY);
151         }
152         catch (IOException ioe) {
153             throw new SystemException(ioe);
154         }
155     }
156 
157     protected void notify(
158             long userId, MembershipRequest membershipRequest,
159             String subjectProperty, String bodyProperty)
160         throws IOException, PortalException, SystemException {
161 
162         Company company = companyPersistence.findByPrimaryKey(
163             membershipRequest.getCompanyId());
164 
165         Group group = groupPersistence.findByPrimaryKey(
166             membershipRequest.getGroupId());
167 
168         User user = userPersistence.findByPrimaryKey(userId);
169         User requestUser = userPersistence.findByPrimaryKey(
170             membershipRequest.getUserId());
171 
172         String fromName = PrefsPropsUtil.getString(
173             membershipRequest.getCompanyId(),
174             PropsKeys.COMMUNITIES_EMAIL_FROM_NAME);
175 
176         String fromAddress = PrefsPropsUtil.getString(
177             membershipRequest.getCompanyId(),
178             PropsKeys.COMMUNITIES_EMAIL_FROM_ADDRESS);
179 
180         String toName = user.getFullName();
181         String toAddress = user.getEmailAddress();
182 
183         String subject = PrefsPropsUtil.getContent(
184             membershipRequest.getCompanyId(), subjectProperty);
185 
186         String body = PrefsPropsUtil.getContent(
187             membershipRequest.getCompanyId(), bodyProperty);
188 
189         String statusKey = null;
190 
191         if (membershipRequest.getStatusId() ==
192                 MembershipRequestImpl.STATUS_APPROVED) {
193 
194             statusKey = "approved";
195         }
196         else if (membershipRequest.getStatusId() ==
197                     MembershipRequestImpl.STATUS_DENIED) {
198 
199             statusKey = "denied";
200         }
201         else {
202             statusKey = "pending";
203         }
204 
205         subject = StringUtil.replace(
206             subject,
207             new String[] {
208                 "[$COMMUNITY_NAME$]",
209                 "[$COMPANY_ID$]",
210                 "[$COMPANY_MX$]",
211                 "[$COMPANY_NAME$]",
212                 "[$FROM_ADDRESS$]",
213                 "[$FROM_NAME$]",
214                 "[$PORTAL_URL$]",
215                 "[$REQUEST_USER_ADDRESS$]",
216                 "[$REQUEST_USER_NAME$]",
217                 "[$STATUS$]",
218                 "[$TO_NAME$]",
219                 "[$USER_ADDRESS$]",
220                 "[$USER_NAME$]",
221             },
222             new String[] {
223                 group.getName(),
224                 String.valueOf(company.getCompanyId()),
225                 company.getMx(),
226                 company.getName(),
227                 fromAddress,
228                 fromName,
229                 company.getVirtualHost(),
230                 requestUser.getEmailAddress(),
231                 requestUser.getFullName(),
232                 LanguageUtil.get(user.getLocale(), statusKey),
233                 toName,
234                 user.getEmailAddress(),
235                 user.getFullName()
236             });
237 
238         body = StringUtil.replace(
239             body,
240             new String[] {
241                 "[$COMMENTS$]",
242                 "[$COMMUNITY_NAME$]",
243                 "[$COMPANY_ID$]",
244                 "[$COMPANY_MX$]",
245                 "[$COMPANY_NAME$]",
246                 "[$FROM_ADDRESS$]",
247                 "[$FROM_NAME$]",
248                 "[$PORTAL_URL$]",
249                 "[$REPLY_COMMENTS$]",
250                 "[$REQUEST_USER_NAME$]",
251                 "[$REQUEST_USER_ADDRESS$]",
252                 "[$STATUS$]",
253                 "[$TO_NAME$]",
254                 "[$USER_ADDRESS$]",
255                 "[$USER_NAME$]",
256             },
257             new String[] {
258                 membershipRequest.getComments(),
259                 group.getName(),
260                 String.valueOf(company.getCompanyId()),
261                 company.getMx(),
262                 company.getName(),
263                 fromAddress,
264                 fromName,
265                 company.getVirtualHost(),
266                 membershipRequest.getReplyComments(),
267                 requestUser.getFullName(),
268                 requestUser.getEmailAddress(),
269                 LanguageUtil.get(user.getLocale(), statusKey),
270                 toName,
271                 user.getEmailAddress(),
272                 user.getFullName()
273             });
274 
275         InternetAddress from = new InternetAddress(fromAddress, fromName);
276 
277         InternetAddress to = new InternetAddress(toAddress, toName);
278 
279         MailMessage message = new MailMessage(from, to, subject, body, true);
280 
281         mailService.sendEmail(message);
282     }
283 
284     protected void notifyCommunityAdministrators(
285             MembershipRequest membershipRequest)
286         throws IOException, PortalException, SystemException {
287 
288         List<UserGroupRole> admins = new UniqueList<UserGroupRole>();
289 
290         Role communityAdminRole = roleLocalService.getRole(
291             membershipRequest.getCompanyId(),
292             RoleConstants.COMMUNITY_ADMINISTRATOR);
293 
294         List<UserGroupRole> communityAdmins =
295             userGroupRoleLocalService.getUserGroupRolesByGroupAndRole(
296                 membershipRequest.getGroupId(), communityAdminRole.getRoleId());
297 
298         admins.addAll(communityAdmins);
299 
300         Role communityOwnerRole = rolePersistence.findByC_N(
301             membershipRequest.getCompanyId(), RoleConstants.COMMUNITY_OWNER);
302 
303         List<UserGroupRole> communityOwners =
304             userGroupRoleLocalService.getUserGroupRolesByGroupAndRole(
305                 membershipRequest.getGroupId(), communityOwnerRole.getRoleId());
306 
307         admins.addAll(communityOwners);
308 
309         for (UserGroupRole userGroupRole : admins) {
310             notify(
311                 userGroupRole.getUserId(), membershipRequest,
312                 PropsKeys.COMMUNITIES_EMAIL_MEMBERSHIP_REQUEST_SUBJECT,
313                 PropsKeys.COMMUNITIES_EMAIL_MEMBERSHIP_REQUEST_BODY);
314         }
315     }
316 
317     protected void validate(String comments)
318         throws PortalException {
319 
320         if ((Validator.isNull(comments)) || (Validator.isNumber(comments))) {
321             throw new MembershipRequestCommentsException();
322         }
323     }
324 
325 }