Allow predefined SMS messages and Emails to be sent via existing SMS/Email implementations

Allow SMS messages without SMS campaign to be saved
This commit is contained in:
Alex Ivanov
2017-06-19 20:42:27 +01:00
parent 17ff4b54c5
commit dfc168d3d7
9 changed files with 105 additions and 41 deletions
@@ -20,24 +20,24 @@ package org.apache.fineract.infrastructure.core.domain;
public class EmailDetail {
private final String organisationName;
private final String username;
private final String contactName;
private final String subject;
private final String body;
private final String address;
private final String contactName;
public EmailDetail(final String organisationName, final String contactName, final String address, final String username) {
this.organisationName = organisationName;
this.contactName = contactName;
public EmailDetail(final String subject, final String body, final String address, final String contactName) {
this.subject = subject;
this.body = body;
this.address = address;
this.username = username;
this.contactName = contactName;
}
public String getOrganisationName() {
return this.organisationName;
public String getSubject() {
return subject;
}
public String getUsername() {
return this.username;
public String getBody() {
return body;
}
public String getContactName() {
@@ -39,7 +39,25 @@ public class GmailBackedPlatformEmailService implements PlatformEmailService {
}
@Override
public void sendToUserAccount(final EmailDetail emailDetail, final String unencodedPassword) {
public void sendToUserAccount(String organisationName, String contactName,
String address, String username, String unencodedPassword) {
final String subject = "Welcome " + contactName + " to " + organisationName;
final String body = "You are receiving this email as your email account: " +
address + " has being used to create a user account for an organisation named [" +
organisationName + "] on Mifos.\n" +
"You can login using the following credentials:\nusername: " + username + "\n" +
"password: " + unencodedPassword + "\n" +
"You must change this password upon first log in using Uppercase, Lowercase, number and character.\n" +
"Thank you and welcome to the organisation.";
final EmailDetail emailDetail = new EmailDetail(subject, body, address, contactName);
sendDefinedEmail(emailDetail);
}
@Override
public void sendDefinedEmail(EmailDetail emailDetails) {
final Email email = new SimpleEmail();
final SMTPCredentialsData smtpCredentialsData = this.externalServicesReadPlatformService.getSMTPCredentials();
final String authuserName = smtpCredentialsData.getUsername();
@@ -51,32 +69,19 @@ public class GmailBackedPlatformEmailService implements PlatformEmailService {
email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
email.setDebug(false); // true if you want to debug
email.setHostName(smtpCredentialsData.getHost());
try {
if(smtpCredentialsData.isUseTLS()){
email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
}
email.setFrom(authuser, authuserName);
if(smtpCredentialsData.isUseTLS()){
email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
}
email.setFrom(authuser, authuserName);
final StringBuilder subjectBuilder = new StringBuilder().append("Welcome ").append(emailDetail.getContactName())
.append(" to ").append(emailDetail.getOrganisationName());
email.setSubject(emailDetails.getSubject());
email.setMsg(emailDetails.getBody());
email.setSubject(subjectBuilder.toString());
final String sendToEmail = emailDetail.getAddress();
final StringBuilder messageBuilder = new StringBuilder().append("You are receiving this email as your email account: ")
.append(sendToEmail).append(" has being used to create a user account for an organisation named [")
.append(emailDetail.getOrganisationName()).append("] on Mifos.\n")
.append("You can login using the following credentials:\nusername: ").append(emailDetail.getUsername()).append("\n")
.append("password: ").append(unencodedPassword).append("\n")
.append("You must change this password upon first log in using Uppercase, Lowercase, number and character.\n")
.append("Thank you and welcome to the organisation.");
email.setMsg(messageBuilder.toString());
email.addTo(sendToEmail, emailDetail.getContactName());
email.addTo(emailDetails.getAddress(), emailDetails.getContactName());
email.send();
} catch (final EmailException e) {
} catch (EmailException e) {
throw new PlatformEmailSendException(e);
}
}
@@ -22,6 +22,9 @@ import org.apache.fineract.infrastructure.core.domain.EmailDetail;
public interface PlatformEmailService {
void sendToUserAccount(EmailDetail emailDetail, String unencodedPassword);
void sendToUserAccount(String organisationName,String contactName,
String address, String username, String unencodedPassword);
void sendDefinedEmail(EmailDetail emailDetails);
}
@@ -114,6 +114,15 @@ public class SmsMessageApiQueueResourceData {
return providerId;
}
/**
* Returns the JSOPN representation of the current object.
* @return the JSON representation of the current object
*/
public String toJsonString() {
Gson gson = new Gson();
return gson.toJson(this);
}
/**
* @return JSON representation of the object
**/
@@ -60,7 +60,7 @@ public class SmsMessage extends AbstractPersistableCustom<Long> {
private Staff staff;
@ManyToOne
@JoinColumn(name = "campaign_id", nullable = false)
@JoinColumn(name = "campaign_id", nullable = true)
private SmsCampaign smsCampaign;
@Column(name = "status_enum", nullable = false)
@@ -40,6 +40,13 @@ public interface SmsMessageScheduledJobService {
*/
public void sendTriggeredMessages(Map<SmsCampaign, Collection<SmsMessage>> smsDataMap);
/**
* Sends a triggered batch of SMS messages to SMS gateway using specified provider.
* @param smsMessage the SMS messages to queue for sending.
* @param providerId the provider ID of the SMS gateway to be used
*/
void sendTriggeredMessage(Collection<SmsMessage> smsMessage, long providerId);
/**
* get delivery report from the SMS gateway
**/
@@ -204,6 +204,29 @@ public class SmsMessageScheduledJobServiceImpl implements SmsMessageScheduledJob
}
}
@Override
public void sendTriggeredMessage(Collection<SmsMessage> smsMessages, long providerId) {
try {
Collection<SmsMessageApiQueueResourceData> apiQueueResourceDatas = new ArrayList<>();
StringBuilder request = new StringBuilder();
for(SmsMessage smsMessage : smsMessages) {
SmsMessageApiQueueResourceData apiQueueResourceData =
SmsMessageApiQueueResourceData.instance(smsMessage.getId(), null,
null, null, smsMessage.getMobileNo(),
smsMessage.getMessage(), providerId);
apiQueueResourceDatas.add(apiQueueResourceData);
smsMessage.setStatusType(SmsMessageStatusType.WAITING_FOR_DELIVERY_REPORT.getValue());
}
this.smsMessageRepository.save(smsMessages);
request.append(SmsMessageApiQueueResourceData.toJsonString(apiQueueResourceDatas));
logger.info("Sending triggered SMS to specific provider with request - " + request.toString());
this.triggeredExecutorService.execute(new SmsTask(ThreadLocalContextUtil.getTenant(),
apiQueueResourceDatas));
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
/**
* get SMS message delivery reports from the SMS gateway (or intermediate
* gateway)
@@ -18,7 +18,6 @@
*/
package org.apache.fineract.useradministration.domain;
import org.apache.fineract.infrastructure.core.domain.EmailDetail;
import org.apache.fineract.infrastructure.core.service.PlatformEmailService;
import org.apache.fineract.infrastructure.security.service.PlatformPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
@@ -54,10 +53,8 @@ public class JpaUserDomainService implements UserDomainService {
this.userRepository.saveAndFlush(appUser);
if (sendPasswordToEmail.booleanValue()) {
final EmailDetail emailDetail = new EmailDetail(appUser.getOffice().getName(), appUser.getFirstname(), appUser.getEmail(),
appUser.getUsername());
this.emailService.sendToUserAccount(emailDetail, unencodedPassword);
this.emailService.sendToUserAccount(appUser.getOffice().getName(),
appUser.getFirstname(), appUser.getEmail(), appUser.getUsername(), unencodedPassword);
}
}
@@ -0,0 +1,20 @@
--
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.
--
ALTER TABLE `sms_messages_outbound` CHANGE `campaign_id` `campaign_id` BIGINT(20) NULL DEFAULT NULL;