Guest User

Untitled

a guest
Oct 27th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.68 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package jobs;
  6.  
  7. import com.simbat.iem.cli.Contact;
  8. import com.simbat.iem.cli.io.ContactInput;
  9. import com.simbat.iem.cli.io.JSonContactStream;
  10. import com.simbat.iem.cli.io.XmlContactStream;
  11. import java.io.EOFException;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.text.DateFormat;
  15. import java.text.SimpleDateFormat;
  16. import java.util.Calendar;
  17. import java.util.Date;
  18. import java.util.List;
  19. import models.Project;
  20. import models.Subscriber;
  21. import models.SubscriberProject;
  22. import play.Logger;
  23. import play.Play;
  24. import play.jobs.Job;
  25. import play.jobs.On;
  26. import play.libs.WS;
  27.  
  28. /**
  29.  * A job to request emails from projects
  30.  * Imports emails yesterday
  31.  * Warning: it uses same username/password to request emails for every project!
  32.  * @author lauri
  33.  */
  34. @On("0 0 5 * * ?")
  35. public class SubscribersImportJob extends Job {
  36.  
  37.     public static final String CONF_USERNAME = "office.iem.import.username";
  38.     public static final String CONF_PASSWORD = "office.iem.import.password";
  39.  
  40.     @Override
  41.     public void doJob() throws Exception {
  42.         List<Project> projects = Project.findActive();
  43.         for (Project project : projects) {
  44.             importEmails(project);
  45.         }
  46.         (new SubscribersExportJob()).in(20);
  47.     }
  48.  
  49.     private void importEmails(Project project) {
  50.         if (project.emailExportUrl == null) {
  51.             Logger.debug("Project {0} is not configured for subscriber collection", project.name);
  52.         } else {
  53.             //Date project.emailExportedDate;
  54.             Date date = dateOrYesterday(project.emailExportedDate);
  55.             WS.HttpResponse response = WS.url(project.emailExportUrl, datestamp(date)).
  56.                     authenticate(
  57.                     Play.configuration.getProperty(CONF_USERNAME),
  58.                     Play.configuration.getProperty(CONF_PASSWORD)).
  59.                     get();
  60.  
  61.             InputStream is = response.getStream();
  62.             ContactInput in = response.getContentType().contains("xml") ? new XmlContactStream(is) : new JSonContactStream(is);
  63.  
  64.             try {
  65.                 for (Contact contact = in.readContact(); contact != null; contact = in.readContact()) {
  66.                     Subscriber subscriber = Subscriber.findByEmail(contact.getEmail());
  67.                     if (subscriber == null) {
  68.                         subscriber = Subscriber.create(contact, project);
  69.                         subscriber.save();
  70.                     } else {
  71.                         if (contact.getName() != null) {
  72.                             subscriber.name = contact.getName();
  73.                         }
  74.                         if (contact.getCountry() != null) {
  75.                             subscriber.country = contact.getCountry();
  76.                         }
  77.                         if (contact.getCity() != null) {
  78.                             subscriber.city = contact.getCity();
  79.                         }
  80.                         //@todo: do we need a check here?
  81.                         subscriber.projects.add(SubscriberProject.create(subscriber, project));
  82.                         subscriber.needSync = true;
  83.                         subscriber.save();
  84.                     }
  85.                 }
  86.             } catch (EOFException ex) {
  87.                 // ok, finished
  88.             } catch (IOException ex) {
  89.                 Logger.warn(ex, "A I/O error during email import");
  90.             } finally {
  91.                 project.emailExportedDate = date;
  92.                 project.save();
  93.             }
  94.         }
  95.     }
  96.  
  97.     private Date dateOrYesterday(Date date) {
  98.         if (date == null) {
  99.             return null;
  100.         }
  101.         Calendar cal = Calendar.getInstance();
  102.         cal.setTime(new Date());
  103.         cal.add(Calendar.DATE, -1);
  104.  
  105.         Date yesterday = cal.getTime();
  106.         return (date.compareTo(yesterday) > 0) ? yesterday : date;
  107.     }
  108.  
  109.     private String datestamp(Date date) {
  110.         DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  111.         if (date == null) {
  112.             return "2011-01-01";
  113.         }
  114.         return df.format(date);
  115.     }
  116. }
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. /*
  124.  
  125. Execution exception (In /app/jobs/SubscribersImportJob.java around line 92)
  126. PersistenceException occured : insert into "Subscriber" ("city", "country", "email", "name", "needSync", "id") values (?, ?, ?, ?, ?, ?)
  127.  
  128. play.exceptions.JavaExecutionException: insert into "Subscriber" ("city", "country", "email", "name", "needSync", "id") values (?, ?, ?, ?, ?, ?)
  129.     at play.jobs.Job.call(Job.java:155)
  130.     at play.jobs.Job$2.call(Job.java:94)
  131.     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
  132.     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
  133.     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
  134.     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)
  135.     at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
  136.     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
  137.     at java.lang.Thread.run(Thread.java:662)
  138. Caused by: javax.persistence.PersistenceException: insert into "Subscriber" ("city", "country", "email", "name", "needSync", "id") values (?, ?, ?, ?, ?, ?)
  139.     at play.db.jpa.JPABase._save(JPABase.java:50)
  140.     at play.db.jpa.GenericModel.save(GenericModel.java:184)
  141.     at jobs.SubscribersImportJob.importEmails(SubscribersImportJob.java:92)
  142.     at jobs.SubscribersImportJob.doJob(SubscribersImportJob.java:44)
  143.     at play.jobs.Job.doJobWithResult(Job.java:50)
  144.     at play.jobs.Job.call(Job.java:146)
  145.     ... 8 more
  146.  
  147.  
  148. */
Add Comment
Please, Sign In to add comment