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