1   /**
2    * Copyright (c) 2000-2008 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.User;
37  import com.liferay.portal.model.UserGroupRole;
38  import com.liferay.portal.model.impl.MembershipRequestImpl;
39  import com.liferay.portal.model.impl.RoleImpl;
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_NAME$]",
219                 "[$REQUEST_USER_ADDRESS$]",
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.getFullName(),
234                 requestUser.getEmailAddress(),
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                 "[$STATUS$]",
254                 "[$TO_NAME$]",
255                 "[$USER_ADDRESS$]",
256                 "[$USER_NAME$]",
257             },
258             new String[] {
259                 membershipRequest.getComments(),
260                 group.getName(),
261                 String.valueOf(company.getCompanyId()),
262                 company.getMx(),
263                 company.getName(),
264                 fromAddress,
265                 fromName,
266                 company.getVirtualHost(),
267                 membershipRequest.getReplyComments(),
268                 LanguageUtil.get(user.getLocale(), statusKey),
269                 toName,
270                 user.getEmailAddress(),
271                 user.getFullName()
272             });
273 
274         InternetAddress from = new InternetAddress(fromAddress, fromName);
275 
276         InternetAddress to = new InternetAddress(toAddress, toName);
277 
278         MailMessage message = new MailMessage(from, to, subject, body, true);
279 
280         mailService.sendEmail(message);
281     }
282 
283     protected void notifyCommunityAdministrators(
284             MembershipRequest membershipRequest)
285         throws IOException, PortalException, SystemException {
286 
287         List<UserGroupRole> admins = new UniqueList<UserGroupRole>();
288 
289         Role communityAdminRole = roleLocalService.getRole(
290             membershipRequest.getCompanyId(), RoleImpl.COMMUNITY_ADMINISTRATOR);
291 
292         List<UserGroupRole> communityAdmins =
293             userGroupRoleLocalService.getUserGroupRolesByGroupAndRole(
294                 membershipRequest.getGroupId(), communityAdminRole.getRoleId());
295 
296         admins.addAll(communityAdmins);
297 
298         Role communityOwnerRole = rolePersistence.findByC_N(
299             membershipRequest.getCompanyId(), RoleImpl.COMMUNITY_OWNER);
300 
301         List<UserGroupRole> communityOwners =
302             userGroupRoleLocalService.getUserGroupRolesByGroupAndRole(
303                 membershipRequest.getGroupId(), communityOwnerRole.getRoleId());
304 
305         admins.addAll(communityOwners);
306 
307         for (UserGroupRole userGroupRole : admins) {
308             notify(
309                 userGroupRole.getUserId(), membershipRequest,
310                 PropsKeys.COMMUNITIES_EMAIL_MEMBERSHIP_REQUEST_SUBJECT,
311                 PropsKeys.COMMUNITIES_EMAIL_MEMBERSHIP_REQUEST_BODY);
312         }
313     }
314 
315     protected void validate(String comments)
316         throws PortalException {
317 
318         if ((Validator.isNull(comments)) || (Validator.isNumber(comments))) {
319             throw new MembershipRequestCommentsException();
320         }
321     }
322 
323 }