Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package jobs;
- import com.simbat.iem.cli.Contact;
- import com.simbat.iem.cli.io.ContactInput;
- import com.simbat.iem.cli.io.JSonContactStream;
- import com.simbat.iem.cli.io.XmlContactStream;
- import java.io.EOFException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.List;
- import models.Project;
- import models.Subscriber;
- import models.SubscriberProject;
- import play.Logger;
- import play.Play;
- import play.jobs.Job;
- import play.jobs.On;
- import play.libs.WS;
- /**
- * A job to request emails from projects
- * Imports emails yesterday
- * Warning: it uses same username/password to request emails for every project!
- * @author lauri
- */
- @On("0 0 5 * * ?")
- public class SubscribersImportJob extends Job {
- public static final String CONF_USERNAME = "office.iem.import.username";
- public static final String CONF_PASSWORD = "office.iem.import.password";
- @Override
- public void doJob() throws Exception {
- List<Project> projects = Project.findActive();
- for (Project project : projects) {
- importEmails(project);
- }
- (new SubscribersExportJob()).in(20);
- }
- private void importEmails(Project project) {
- if (project.emailExportUrl == null) {
- Logger.debug("Project {0} is not configured for subscriber collection", project.name);
- } else {
- //Date project.emailExportedDate;
- Date date = dateOrYesterday(project.emailExportedDate);
- WS.HttpResponse response = WS.url(project.emailExportUrl, datestamp(date)).
- authenticate(
- Play.configuration.getProperty(CONF_USERNAME),
- Play.configuration.getProperty(CONF_PASSWORD)).
- get();
- InputStream is = response.getStream();
- ContactInput in = response.getContentType().contains("xml") ? new XmlContactStream(is) : new JSonContactStream(is);
- try {
- for (Contact contact = in.readContact(); contact != null; contact = in.readContact()) {
- Subscriber subscriber = Subscriber.findByEmail(contact.getEmail());
- if (subscriber == null) {
- subscriber = Subscriber.create(contact, project);
- subscriber.save();
- } else {
- if (contact.getName() != null) {
- subscriber.name = contact.getName();
- }
- if (contact.getCountry() != null) {
- subscriber.country = contact.getCountry();
- }
- if (contact.getCity() != null) {
- subscriber.city = contact.getCity();
- }
- //@todo: do we need a check here?
- subscriber.projects.add(SubscriberProject.create(subscriber, project));
- subscriber.needSync = true;
- subscriber.save();
- }
- }
- } catch (EOFException ex) {
- // ok, finished
- } catch (IOException ex) {
- Logger.warn(ex, "A I/O error during email import");
- } finally {
- project.emailExportedDate = date;
- project.save();
- }
- }
- }
- private Date dateOrYesterday(Date date) {
- if (date == null) {
- return null;
- }
- Calendar cal = Calendar.getInstance();
- cal.setTime(new Date());
- cal.add(Calendar.DATE, -1);
- Date yesterday = cal.getTime();
- return (date.compareTo(yesterday) > 0) ? yesterday : date;
- }
- private String datestamp(Date date) {
- DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
- if (date == null) {
- return "2011-01-01";
- }
- return df.format(date);
- }
- }
- /*
- Execution exception (In /app/jobs/SubscribersImportJob.java around line 92)
- PersistenceException occured : insert into "Subscriber" ("city", "country", "email", "name", "needSync", "id") values (?, ?, ?, ?, ?, ?)
- play.exceptions.JavaExecutionException: insert into "Subscriber" ("city", "country", "email", "name", "needSync", "id") values (?, ?, ?, ?, ?, ?)
- at play.jobs.Job.call(Job.java:155)
- at play.jobs.Job$2.call(Job.java:94)
- at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
- at java.util.concurrent.FutureTask.run(FutureTask.java:138)
- at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
- at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)
- at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
- at java.lang.Thread.run(Thread.java:662)
- Caused by: javax.persistence.PersistenceException: insert into "Subscriber" ("city", "country", "email", "name", "needSync", "id") values (?, ?, ?, ?, ?, ?)
- at play.db.jpa.JPABase._save(JPABase.java:50)
- at play.db.jpa.GenericModel.save(GenericModel.java:184)
- at jobs.SubscribersImportJob.importEmails(SubscribersImportJob.java:92)
- at jobs.SubscribersImportJob.doJob(SubscribersImportJob.java:44)
- at play.jobs.Job.doJobWithResult(Job.java:50)
- at play.jobs.Job.call(Job.java:146)
- ... 8 more
- */
Add Comment
Please, Sign In to add comment