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