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