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.PropsUtil;
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 begin, int end)
114         throws SystemException {
115 
116         return membershipRequestPersistence.findByG_S(
117             groupId, status, begin, 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                 PropsUtil.COMMUNITIES_EMAIL_MEMBERSHIP_REPLY_SUBJECT,
153                 PropsUtil.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 
173         String fromName = PrefsPropsUtil.getString(
174             membershipRequest.getCompanyId(),
175             PropsUtil.COMMUNITIES_EMAIL_FROM_NAME);
176 
177         String fromAddress = PrefsPropsUtil.getString(
178             membershipRequest.getCompanyId(),
179             PropsUtil.COMMUNITIES_EMAIL_FROM_ADDRESS);
180 
181         String toName = user.getFullName();
182         String toAddress = user.getEmailAddress();
183 
184         String subject = PrefsPropsUtil.getContent(
185             membershipRequest.getCompanyId(), subjectProperty);
186 
187         String body = PrefsPropsUtil.getContent(
188             membershipRequest.getCompanyId(), bodyProperty);
189 
190         String statusKey = null;
191 
192         if (membershipRequest.getStatusId() ==
193                 MembershipRequestImpl.STATUS_APPROVED) {
194 
195             statusKey = "approved";
196         }
197         else if (membershipRequest.getStatusId() ==
198                     MembershipRequestImpl.STATUS_DENIED) {
199 
200             statusKey = "denied";
201         }
202         else {
203             statusKey = "pending";
204         }
205 
206         subject = StringUtil.replace(
207             subject,
208             new String[] {
209                 "[$COMMUNITY_NAME$]",
210                 "[$COMPANY_ID$]",
211                 "[$COMPANY_MX$]",
212                 "[$COMPANY_NAME$]",
213                 "[$FROM_ADDRESS$]",
214                 "[$FROM_NAME$]",
215                 "[$PORTAL_URL$]",
216                 "[$STATUS$]",
217                 "[$TO_NAME$]",
218                 "[$USER_ADDRESS$]",
219                 "[$USER_NAME$]",
220             },
221             new String[] {
222                 group.getName(),
223                 String.valueOf(company.getCompanyId()),
224                 company.getMx(),
225                 company.getName(),
226                 fromAddress,
227                 fromName,
228                 company.getVirtualHost(),
229                 LanguageUtil.get(user.getLocale(), statusKey),
230                 toName,
231                 user.getEmailAddress(),
232                 user.getFullName()
233             });
234 
235         body = StringUtil.replace(
236             body,
237             new String[] {
238                 "[$COMMENTS$]",
239                 "[$COMMUNITY_NAME$]",
240                 "[$COMPANY_ID$]",
241                 "[$COMPANY_MX$]",
242                 "[$COMPANY_NAME$]",
243                 "[$FROM_ADDRESS$]",
244                 "[$FROM_NAME$]",
245                 "[$PORTAL_URL$]",
246                 "[$REPLY_COMMENTS$]",
247                 "[$STATUS$]",
248                 "[$TO_NAME$]",
249                 "[$USER_ADDRESS$]",
250                 "[$USER_NAME$]",
251             },
252             new String[] {
253                 membershipRequest.getComments(),
254                 group.getName(),
255                 String.valueOf(company.getCompanyId()),
256                 company.getMx(),
257                 company.getName(),
258                 fromAddress,
259                 fromName,
260                 company.getVirtualHost(),
261                 membershipRequest.getReplyComments(),
262                 LanguageUtil.get(user.getLocale(), statusKey),
263                 toName,
264                 user.getEmailAddress(),
265                 user.getFullName()
266             });
267 
268         InternetAddress from = new InternetAddress(fromAddress, fromName);
269 
270         InternetAddress to = new InternetAddress(toAddress, toName);
271 
272         MailMessage message = new MailMessage(from, to, subject, body, true);
273 
274         mailService.sendEmail(message);
275     }
276 
277     protected void notifyCommunityAdministrators(
278             MembershipRequest membershipRequest)
279         throws IOException, PortalException, SystemException {
280 
281         List<UserGroupRole> admins = new UniqueList<UserGroupRole>();
282 
283         Role communityAdminRole = roleLocalService.getRole(
284             membershipRequest.getCompanyId(), RoleImpl.COMMUNITY_ADMINISTRATOR);
285 
286         List<UserGroupRole> communityAdmins =
287             userGroupRoleLocalService.getUserGroupRolesByGroupAndRole(
288                 membershipRequest.getGroupId(), communityAdminRole.getRoleId());
289 
290         admins.addAll(communityAdmins);
291 
292         Role communityOwnerRole = rolePersistence.findByC_N(
293             membershipRequest.getCompanyId(), RoleImpl.COMMUNITY_OWNER);
294 
295         List<UserGroupRole> communityOwners =
296             userGroupRoleLocalService.getUserGroupRolesByGroupAndRole(
297                 membershipRequest.getGroupId(), communityOwnerRole.getRoleId());
298 
299         admins.addAll(communityOwners);
300 
301         for (UserGroupRole userGroupRole : admins) {
302             notify(
303                 userGroupRole.getUserId(), membershipRequest,
304                 PropsUtil.COMMUNITIES_EMAIL_MEMBERSHIP_REQUEST_SUBJECT,
305                 PropsUtil.COMMUNITIES_EMAIL_MEMBERSHIP_REQUEST_BODY);
306         }
307     }
308 
309     protected void validate(String comments)
310         throws PortalException, SystemException {
311 
312         if ((Validator.isNull(comments)) || (Validator.isNumber(comments))) {
313             throw new MembershipRequestCommentsException();
314         }
315     }
316 
317 }