Thanks Andrey good document, a real eye opener. I did some more research and managed to get the code right, at least it seems like that. But now i’m getting the following error when executing the scheduled task:
java.lang.RuntimeException: com.haulmont.cuba.core.global.RemoteException:
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.3.6-cuba): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERROR: value too long for type character varying(50)
Error Code: 0
Call: INSERT INTO SYS_SENDING_MESSAGE (ID, ADDRESS_TO, ATTACHMENTS_NAME, ATTEMPTS_COUNT, ATTEMPTS_MADE, ADDRESS_BCC, BODY_CONTENT_TYPE, CAPTION, ADDRESS_CC, CONTENT_TEXT, CREATE_TS, CREATED_BY, DATE_SENT, DEADLINE, DELETE_TS, DELETED_BY, ADDRESS_FROM, EMAIL_HEADERS, STATUS, SYS_TENANT_ID, UPDATE_TS, UPDATED_BY, VERSION, CONTENT_TEXT_FILE_ID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
bind => [fd7f1f2c-4cb6-a7c2-ab54-16dc4d1b991a, l43sedoc@gmail.com, null, null, 0, null, Hello Employee,
This is a reminder that the following passport will be valid until:
2020-08-21
Last Name: Riley Emma
Fist Names: Hippolyte
Phone Number:
Mobile Number: 8862250
Expiration Date: 2020-08-21
This is a no-reply mail
Have a nice day., Pasport Expiration Reminder: 2020-08-21, null, null, 2020-07-21 10:52:12.919, admin, null, null, null, null, l43sedoc@gmail.com, null, 0, null, 2020-07-21 10:52:12.919, null, 1, null]
Query: InsertObjectQuery(com.haulmont.cuba.core.entity.SendingMessage-fd7f1f2c-4cb6-a7c2-ab54-16dc4d1b991a [new,managed])
org.eclipse.persistence.exceptions.DatabaseException:
Internal Exception: org.postgresql.util.PSQLException: ERROR: value too long for type character varying(50)
Error Code: 0
Call: INSERT INTO SYS_SENDING_MESSAGE (ID, ADDRESS_TO, ATTACHMENTS_NAME, ATTEMPTS_COUNT, ATTEMPTS_MADE, ADDRESS_BCC, BODY_CONTENT_TYPE, CAPTION, ADDRESS_CC, CONTENT_TEXT, CREATE_TS, CREATED_BY, DATE_SENT, DEADLINE, DELETE_TS, DELETED_BY, ADDRESS_FROM, EMAIL_HEADERS, STATUS, SYS_TENANT_ID, UPDATE_TS, UPDATED_BY, VERSION, CONTENT_TEXT_FILE_ID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
bind => [fd7f1f2c-4cb6-a7c2-ab54-16dc4d1b991a, l43sedoc@gmail.com, null, null, 0, null, Hello Employee,
This is a reminder that the following passport will be valid until:
2020-08-21
Last Name: Riley Emma
Fist Names: Hippolyte
Phone Number:
Mobile Number: 8862250
Expiration Date: 2020-08-21
This is a no-reply mail
Have a nice day., Pasport Expiration Reminder: 2020-08-21, null, null, 2020-07-21 10:52:12.919, admin, null, null, null, null, l43sedoc@gmail.com, null, 0, null, 2020-07-21 10:52:12.919, null, 1, null]
Query: InsertObjectQuery(com.haulmont.cuba.core.entity.SendingMessage-fd7f1f2c-4cb6-a7c2-ab54-16dc4d1b991a [new])
org.postgresql.util.PSQLException: ERROR: value too long for type character varying(50)
{Executed manually}
The code looks like this:
package com.company.krs.core;
import com.haulmont.cuba.core.Persistence;
import com.haulmont.cuba.core.Query;
import com.haulmont.cuba.core.app.EmailService;
import com.haulmont.cuba.core.global.EmailInfo;
import com.haulmont.cuba.security.app.Authenticated;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.inject.Inject;
import java.util.Iterator;
import java.util.List;
@Component(ReminderMailServiceBean.NAME)
public class ReminderMailServiceBean implements ReminderMailService {
public static final String NAME = "krs_ReminderMailServiceBean";
@Inject
protected EmailService emailService;
@Inject
private Persistence persistence;
@Authenticated
@Transactional
@Override
public void checkReminders(){
Query query = persistence.getEntityManager().createNativeQuery(
"select s.first_names,s.last_name,s.phone_number,s.mobile_number,s.expiration_date\n" +
"from KRS_REGISTRATIE s\n" +
"where s.expiration_date = current_date + INTERVAL '1' month");
List list = query.getResultList();
for (Iterator it = list.iterator(); it.hasNext(); ) {
Object[] row = (Object[]) it.next();
String first_names = (String) row[0];
String last_name = (String) row[1];
String phone_number = (String) row[2];
String mobile_number = (String) row[3];
String expiration_date = row[4].toString();
sendByEmail(first_names,last_name,phone_number,mobile_number,expiration_date);
}
}
// MAIL FUNCTIONALITY
private void sendByEmail(String last_name, String first_names, String phone_number, String mobile_number, String expiration_date) {
EmailInfo emailInfo = new EmailInfo(
"l43sedoc@gmail.com", // recipients
"Pasport Expiration Reminder: "+ expiration_date, // subject
null, // the "from" address will be taken from the "cuba.email.fromAddress" app property
"Hello Employee,\n\nThis is a reminder that the following passport will be valid until: \n "+expiration_date+"\n" +
"First Names: " +first_names+ "\n" +
"Last Name: " +last_name+ "\n" +
"Phone Number: " +phone_number+ "\n" +
"Mobile Number: " +mobile_number+ "\n" +
"Expiration Date: " +expiration_date+ "\n\nThis is a no-reply mail\nHave a nice day."// body template
);
emailService.sendEmailAsync(emailInfo);
}
}