Hello, I am a student working on a project in Cuba platform and was wondering how to schedule an email (reminder) depending on a training(entity) start-date attribute. So an employee in our system is reminded by mail a few days before his training starts.
I have the email part down and am able to send emails targeted to the manager of a certain training when an employee presses a request button to join the training.
Hi.
Create a Managed Bean and add a method with the @Authenticated and @Transactional annotations to it. In this method create a query to select employees from the database with a necessary date and send emails to them using the EmailerAPI bean: Sending Methods - CUBA Platform. Developer’s Manual
Create a scheduled task from the Scheduled Tasks browser (Administration → Scheduled Tasks).
Fill in the following fields:
Bean name. Set the name of the Managed Bean that you have created previously.
Method name. Set the name of the method that you have created previously.
Period, sec. Set the task execution interval in seconds.
Save the scheduled task and click the Activate button.
Note that cuba.schedulingActive application property should be set to true to enable tasks processing.
Remember, that this task sends emails every time it is run and you should prevent repeated email sending.
If you still have any questions, feel free to ask them here.
I think I understand what you are telling me although I am still a little puzzeled as to where and how I make this managed bean do I make my current Training entity a managed bean as described on the explanation page by adding a particular import and some other tags? Or is it a new entity that I first need to add to the data model making this a managed bean? This is not really explained in the Making a managed bean dev manual page.
Hi!
You have to create a Java class, that placed in the com.company.{project_name}.core package using IDE (Eclipse, IntelliJ Idea). To make this class a managed bean, add the @org.springframework.stereotype.Component annotation to the Java class. Add a method with the @Authenticated and @Transactional annotations to the class.
I found the correct query for my problem but am struggling to use the data the query returns.
My query returns the table of emails of users that have a training the next day with the “start date” of their training and the “description” of their training. I know how the email sending works. but how do I talk to that query data to place it in the email?
For instance if I get 3 rows back with each row containing an “EMAIL” (of the user who is attending a particular training), a “DESCRIPTION” (title of this particular training) & a “START_DATE” (start date of that particular training this employee is attending)
How do I make my emailer send seperate mails to each email address in the table with the mail containing info from that email addresses particular row.
My code:
package com.company.hrtrainingplatform.core;
import com.company.hrtrainingplatform.entity.Employee;
import com.haulmont.cuba.core.app.EmailService;
import com.haulmont.cuba.core.global.DataManager;
import com.haulmont.cuba.core.global.EmailInfo;
import com.haulmont.cuba.core.global.LoadContext;
import com.haulmont.cuba.gui.components.Table;
import com.haulmont.cuba.security.app.Authenticated;
import com.haulmont.cuba.security.entity.User;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSetImpl;
import com.mysql.jdbc.Statement;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.inject.Inject;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
@Component(ReminderMailCheck.NAME)
public class ReminderMailCheck {
public static final String NAME = "hrtrainingplatform_ReminderMailCheck";
@Inject
private DataManager dataManager;
@Inject
protected EmailService emailService;
@Authenticated
@Transactional
public void checkReminders(Connection con){
// LoadContext<> loadContext = LoadContext.createQuery("select s.email,e.user_id,l.employee_id,t.description,t.START_DATE\n" +
// "from HRTRAININGPLATFORM_TRAINING t,HRTRAININGPLATFORM_TRAINING_EMPLOYEE_LINK l,HRTRAININGPLATFORM_EMPLOYEE e,SEC_USER s\n" +
// "where l.TRAINING_ID=t.ID and l.EMPLOYEE_ID=e.ID and e.USER_ID=s.ID and CURRENT_DATE=DATE_SUB(CAST(t.START_DATE AS DATE), INTERVAL 1 DAY)");
// ^^^^^^^^^^^^^^^^^^^^^^^^^^ QUERRY THAT TAKES THE NECESSARY DATA FROM OUR DATABASE. DETAILS ARE IN THE POSTED QUESTION ABOVE.^^^^^^^^^^^^^^^^^
//
// User user = dataManager.load(loadContext);
//
// --------MAIL FUNCTIONALITY BELOW------
//
// private void sendByEmail() {
//
//
// String trainingDesc = ;--------------problem here (data from query needed)
// UUID trainingUUID = ;--------------problem here (same as last)
//
// EmailInfo emailInfo = new EmailInfo(
//
// , // email address --------------problem here (same as last)
// "Training Request: "+ trainingDescription, // subject --------------problem here (same as last)
// null, // the "from" address will be taken from the "cuba.email.fromAddress" app property
//
// "Reminder Mail boddy" // body template
// );
//
// sendByEmail();
// emailService.sendEmailAsync(emailInfo);
}
}
Not sure if I am on the right track but hope you can help me.
I also don’t really get how LoadContext works here but it was used in another Bean that I have so I thought it might somehow work in this file as wel but I don’t really know how it works.
You can find the code example in the following documentation Running SQL Queries. Use еhe first example.
Add create EmailInfo() and call sendEmailAsync() in a loop. And it is better to carry out creation and sending in separate methods and cause them in a cycle consistently.