001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.impl;
022:
023: import com.liferay.portal.MembershipRequestCommentsException;
024: import com.liferay.portal.PortalException;
025: import com.liferay.portal.SystemException;
026: import com.liferay.portal.kernel.language.LanguageUtil;
027: import com.liferay.portal.kernel.mail.MailMessage;
028: import com.liferay.portal.kernel.util.StringUtil;
029: import com.liferay.portal.kernel.util.Validator;
030: import com.liferay.portal.model.Company;
031: import com.liferay.portal.model.Group;
032: import com.liferay.portal.model.MembershipRequest;
033: import com.liferay.portal.model.Role;
034: import com.liferay.portal.model.User;
035: import com.liferay.portal.model.UserGroupRole;
036: import com.liferay.portal.model.impl.MembershipRequestImpl;
037: import com.liferay.portal.model.impl.RoleImpl;
038: import com.liferay.portal.service.base.MembershipRequestLocalServiceBaseImpl;
039: import com.liferay.portal.util.PrefsPropsUtil;
040: import com.liferay.portal.util.PropsUtil;
041: import com.liferay.util.UniqueList;
042:
043: import java.io.IOException;
044:
045: import java.util.Date;
046: import java.util.Iterator;
047: import java.util.List;
048:
049: import javax.mail.internet.InternetAddress;
050:
051: /**
052: * <a href="MembershipRequestLocalServiceImpl.java.html"><b><i>View Source</i>
053: * </b></a>
054: *
055: * @author Jorge Ferrer
056: *
057: */
058: public class MembershipRequestLocalServiceImpl extends
059: MembershipRequestLocalServiceBaseImpl {
060:
061: public MembershipRequest addMembershipRequest(long userId,
062: long groupId, String comments) throws PortalException,
063: SystemException {
064:
065: User user = userPersistence.findByPrimaryKey(userId);
066: Date now = new Date();
067:
068: validate(comments);
069:
070: long membershipRequestId = counterLocalService.increment();
071:
072: MembershipRequest membershipRequest = membershipRequestPersistence
073: .create(membershipRequestId);
074:
075: membershipRequest.setCompanyId(user.getCompanyId());
076: membershipRequest.setUserId(userId);
077: membershipRequest.setCreateDate(now);
078: membershipRequest.setGroupId(groupId);
079: membershipRequest.setComments(comments);
080: membershipRequest
081: .setStatusId(MembershipRequestImpl.STATUS_PENDING);
082:
083: membershipRequestPersistence.update(membershipRequest);
084:
085: notifyCommunityAdministrators(membershipRequest);
086:
087: return membershipRequest;
088: }
089:
090: public MembershipRequest getMembershipRequest(
091: long membershipRequestId) throws PortalException,
092: SystemException {
093:
094: return membershipRequestPersistence
095: .findByPrimaryKey(membershipRequestId);
096: }
097:
098: public void deleteMembershipRequests(long groupId)
099: throws SystemException {
100: membershipRequestPersistence.removeByGroupId(groupId);
101: }
102:
103: public void deleteMembershipRequests(long groupId, int statusId)
104: throws SystemException {
105:
106: membershipRequestPersistence.removeByG_S(groupId, statusId);
107: }
108:
109: public List search(long groupId, int status, int begin, int end)
110: throws SystemException {
111:
112: return membershipRequestPersistence.findByG_S(groupId, status,
113: begin, end);
114: }
115:
116: public int searchCount(long groupId, int status)
117: throws SystemException {
118: return membershipRequestPersistence.countByG_S(groupId, status);
119: }
120:
121: public void updateStatus(long replierUserId,
122: long membershipRequestId, String replyComments, int statusId)
123: throws PortalException, SystemException {
124:
125: validate(replyComments);
126:
127: MembershipRequest membershipRequest = membershipRequestPersistence
128: .findByPrimaryKey(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
139: .getUserId() };
140:
141: userLocalService.addGroupUsers(membershipRequest
142: .getGroupId(), addUserIds);
143: }
144:
145: notify(membershipRequest.getUserId(), membershipRequest,
146: PropsUtil.COMMUNITIES_EMAIL_MEMBERSHIP_REPLY_SUBJECT,
147: PropsUtil.COMMUNITIES_EMAIL_MEMBERSHIP_REPLY_BODY);
148: }
149:
150: protected void notify(long userId,
151: MembershipRequest membershipRequest,
152: String subjectProperty, String bodyProperty)
153: throws PortalException, SystemException {
154:
155: try {
156: Company company = companyPersistence
157: .findByPrimaryKey(membershipRequest.getCompanyId());
158:
159: Group group = groupPersistence
160: .findByPrimaryKey(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(membershipRequest
179: .getCompanyId(), bodyProperty);
180:
181: String statusKey = null;
182:
183: if (membershipRequest.getStatusId() == MembershipRequestImpl.STATUS_APPROVED) {
184:
185: statusKey = "approved";
186: } else if (membershipRequest.getStatusId() == MembershipRequestImpl.STATUS_DENIED) {
187:
188: statusKey = "denied";
189: } else {
190: statusKey = "pending";
191: }
192:
193: subject = StringUtil
194: .replace(subject, new String[] {
195: "[$COMMUNITY_NAME$]", "[$COMPANY_ID$]",
196: "[$COMPANY_MX$]", "[$COMPANY_NAME$]",
197: "[$FROM_ADDRESS$]", "[$FROM_NAME$]",
198: "[$PORTAL_URL$]", "[$STATUS$]",
199: "[$TO_NAME$]", "[$USER_ADDRESS$]",
200: "[$USER_NAME$]", },
201: new String[] {
202: group.getName(),
203: String.valueOf(company
204: .getCompanyId()),
205: company.getMx(),
206: company.getName(),
207: fromAddress,
208: fromName,
209: company.getVirtualHost(),
210: LanguageUtil.get(user.getLocale(),
211: statusKey), toName,
212: user.getEmailAddress(),
213: user.getFullName() });
214:
215: body = StringUtil
216: .replace(body, new String[] { "[$COMMENTS$]",
217: "[$COMMUNITY_NAME$]", "[$COMPANY_ID$]",
218: "[$COMPANY_MX$]", "[$COMPANY_NAME$]",
219: "[$FROM_ADDRESS$]", "[$FROM_NAME$]",
220: "[$PORTAL_URL$]", "[$REPLY_COMMENTS$]",
221: "[$STATUS$]", "[$TO_NAME$]",
222: "[$USER_ADDRESS$]", "[$USER_NAME$]", },
223: new String[] {
224: membershipRequest.getComments(),
225: group.getName(),
226: String.valueOf(company
227: .getCompanyId()),
228: company.getMx(),
229: company.getName(),
230: fromAddress,
231: fromName,
232: company.getVirtualHost(),
233: membershipRequest
234: .getReplyComments(),
235: LanguageUtil.get(user.getLocale(),
236: statusKey), toName,
237: user.getEmailAddress(),
238: user.getFullName() });
239:
240: InternetAddress from = new InternetAddress(fromAddress,
241: fromName);
242:
243: InternetAddress to = new InternetAddress(toAddress, toName);
244:
245: MailMessage message = new MailMessage(from, to, subject,
246: body, true);
247:
248: mailService.sendEmail(message);
249: } catch (IOException ioe) {
250: throw new SystemException(ioe);
251: }
252: }
253:
254: protected void notifyCommunityAdministrators(
255: MembershipRequest membershipRequest)
256: throws PortalException, SystemException {
257:
258: List admins = new UniqueList();
259:
260: Role communityAdminRole = roleLocalService.getRole(
261: membershipRequest.getCompanyId(),
262: RoleImpl.COMMUNITY_ADMINISTRATOR);
263:
264: List communityAdmins = userGroupRoleLocalService
265: .getUserGroupRolesByGroupAndRole(membershipRequest
266: .getGroupId(), communityAdminRole.getRoleId());
267:
268: admins.addAll(communityAdmins);
269:
270: Role communityOwnerRole = rolePersistence.findByC_N(
271: membershipRequest.getCompanyId(),
272: RoleImpl.COMMUNITY_OWNER);
273:
274: List communityOwners = userGroupRoleLocalService
275: .getUserGroupRolesByGroupAndRole(membershipRequest
276: .getGroupId(), communityOwnerRole.getRoleId());
277:
278: admins.addAll(communityOwners);
279:
280: Iterator itr = admins.iterator();
281:
282: while (itr.hasNext()) {
283: UserGroupRole userGroupRole = (UserGroupRole) itr.next();
284:
285: notify(
286: userGroupRole.getUserId(),
287: membershipRequest,
288: PropsUtil.COMMUNITIES_EMAIL_MEMBERSHIP_REQUEST_SUBJECT,
289: PropsUtil.COMMUNITIES_EMAIL_MEMBERSHIP_REQUEST_BODY);
290: }
291: }
292:
293: protected void validate(String comments) throws PortalException,
294: SystemException {
295:
296: if ((Validator.isNull(comments))
297: || (Validator.isNumber(comments))) {
298: throw new MembershipRequestCommentsException();
299: }
300: }
301:
302: }
|