Advertisement
Guest User

Untitled

a guest
Jul 11th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.20 KB | None | 0 0
  1. package com.retailingwireless.clear.reports;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.File;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.text.ParseException;
  10. import java.text.SimpleDateFormat;
  11. import java.util.ArrayList;
  12. import java.util.Arrays;
  13. import java.util.Collections;
  14. import java.util.Iterator;
  15. import java.util.List;
  16. import java.util.Properties;
  17. import java.util.Date;
  18. import javax.mail.Flags;
  19. import javax.mail.Folder;
  20. import javax.mail.Message;
  21. import javax.mail.MessagingException;
  22. import javax.mail.Multipart;
  23. import javax.mail.Part;
  24. import javax.mail.Session;
  25. import org.apache.commons.csv.CSVParser;
  26. import org.openbravo.base.model.ModelProvider;
  27. import org.openbravo.base.provider.OBProvider;
  28. import org.openbravo.dal.core.OBContext;
  29. import org.openbravo.dal.service.OBDal;
  30. import org.openbravo.model.ad.access.User;
  31. import org.openbravo.model.ad.system.Client;
  32. import org.openbravo.scheduling.ProcessBundle;
  33. import org.openbravo.service.db.DalBaseProcess;
  34. import com.retailingwireless.clear.data.ClearSalesEntry;
  35. import com.sun.mail.pop3.POP3Store;
  36.  
  37. public class ClearReportFetcher extends DalBaseProcess {
  38.   static String POP3_SERVER = "pop3.server.com";
  39.   static int POP3_PORT = 110;
  40.   static String POP3_MAILBOX = "INBOX";
  41.   static String POP3_USERNAME = "email@address.com";
  42.   static String POP3_PASSWORD = "password";
  43.   static ArrayList<String> DATE_PROPERTIES = new ArrayList<String>(Arrays.asList("dataDate",
  44.       "dateEntered", "deactDate", "statusDate"));
  45.   static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  46.   static Properties props = new Properties();
  47.   static Session session = Session.getDefaultInstance(props, null);
  48.   static POP3Store store = new POP3Store(session, null);
  49.  
  50.   OBContext ctx = OBContext.getOBContext();
  51.   User user = ctx.getUser();
  52.   Client client = ctx.getCurrentClient();
  53.  
  54.   private CSVParser parser;
  55.   private String[] headerRow;
  56.  
  57.   protected CSVParser getParser() {
  58.     return this.parser;
  59.   } // getParser()
  60.  
  61.   protected String[] getHeaderRow() {
  62.     return this.headerRow;
  63.   } // getHeaderRow()
  64.  
  65.   protected static String getPropertyName(String colnameVal) {
  66.     String colname = colnameVal;
  67.  
  68.     if (colname.toUpperCase().indexOf("_ID") > 0) {
  69.       colname = colname.replace("_ID", "_num");
  70.       colname = colname.replace("_Id", "_num");
  71.       colname = colname.replace("_id", "_num");
  72.       colname = colname.replace("_iD", "_num");
  73.     }
  74.  
  75.     colname = colname.replace("#", "num");
  76.  
  77.     return ModelProvider.getInstance().getEntity(ClearSalesEntry.class)
  78.       .getPropertyByColumnName(colname).getName();
  79.   } // getPropertyName()
  80.  
  81.   protected static boolean isDateProperty(String propname) {
  82.     return DATE_PROPERTIES.contains(propname);
  83.   } // isDateProperty()
  84.  
  85.   protected static Date convertDate(String dateval) throws ParseException {
  86.     if (dateval == null || dateval.trim().length() == 0) {
  87.       return null;
  88.     }
  89.     return DATE_FORMAT.parse(dateval);
  90.   } // convertDate()
  91.  
  92.   protected static File saveFile(String filename, InputStream input) throws IOException {
  93.     if (filename == null) {
  94.       filename = File.createTempFile("xx", ".out").getName();
  95.     }
  96.     SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
  97.     File file = new File(System.getProperty("java.io.tmpdir")
  98.             + "/clear-" + format.format(new Date()) + "-"
  99.             + filename);
  100.     // Do no overwrite existing file
  101.     for (int i=0; file.exists(); i++) {
  102.       file = new File(filename+i);
  103.     }
  104.     FileOutputStream fos = new FileOutputStream(file);
  105.     BufferedOutputStream bos = new BufferedOutputStream(fos);
  106.  
  107.     BufferedInputStream bis = new BufferedInputStream(input);
  108.     int aByte;
  109.     while ((aByte = bis.read()) != -1) {
  110.       bos.write(aByte);
  111.     }
  112.     bos.flush();
  113.     bos.close();
  114.     bis.close();
  115.    
  116.     System.out.println("Saved file " + file.getAbsolutePath() + ".");
  117.     return file;
  118.   }
  119.  
  120.   protected int fetchAttachments() throws MessagingException, IOException, ParseException {
  121.     store.close();
  122.     store.connect(POP3_SERVER, POP3_PORT, POP3_USERNAME, POP3_PASSWORD);
  123.     Folder folder = store.getFolder("INBOX");
  124.     folder.open(Folder.READ_WRITE);
  125.     Message message[] = folder.getMessages();
  126.     int attachmentCount = 0;
  127.     int processedRecords = 0;
  128.    
  129.     for (int i=0, n=message.length; i<n; i++) {
  130.         Multipart multipart = (Multipart)message[i].getContent();
  131.         for (int i1=0, n1=multipart.getCount(); i1<n1; i1++) {
  132.           Part part = multipart.getBodyPart(i1);
  133.  
  134.           String disposition = part.getDisposition();
  135.  
  136.           if ((disposition != null) && (disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE)))) {
  137.             String partFileName = part.getFileName();
  138.             String fileExtension = partFileName.substring(partFileName.length() - 3).toUpperCase();
  139.             if (fileExtension.equals("CSV")) {
  140.                 this.initParser(part.getInputStream());
  141.                 processedRecords = this.processCSV();
  142.                 saveFile("sales.csv", part.getInputStream());
  143.                 attachmentCount++;
  144.             } // if is CSV file
  145.           } // if attachment
  146.         } // for Multipart
  147.         message[i].setFlag(Flags.Flag.DELETED, true);
  148.     } // for Message
  149.  
  150.     folder.close(true);
  151.     store.close();
  152.    
  153.     System.out.println("Retrieved " + attachmentCount + " attachments.");
  154.     return processedRecords;
  155.   }
  156.  
  157.   @SuppressWarnings("deprecation")
  158.     protected void initParser(InputStream input) throws IOException {
  159.     this.parser = new CSVParser(input);
  160.     this.headerRow = this.parser.getLine();
  161.   } // initParser
  162.  
  163.   protected List<String> readUuids() {
  164.     int showSwitch = 0;
  165.     List<String> uuids = new ArrayList<String>();
  166.     Iterator<ClearSalesEntry> entries = OBDal.getInstance().createCriteria(ClearSalesEntry.class).list().iterator();
  167.    
  168.     while (entries.hasNext()) {
  169.         ClearSalesEntry entry = entries.next();
  170.         String dateEnteredColumn = DATE_FORMAT.format(entry.getDateEntered()) + "_";
  171.         String accountColumn = entry.getAccountNum() + "_";
  172.         String offerColumn = entry.getOfferNum() + "_";
  173.         String pullbackColumn = entry.getPullbackFlag() + "_";
  174.         String cancelColumn = entry.getCancelFlag() + "_";
  175.         String cancelReasonColumn = entry.getCancelReason();
  176.         String recordUuid = dateEnteredColumn + accountColumn + offerColumn + pullbackColumn + cancelColumn + cancelReasonColumn;
  177.         showSwitch++;
  178.         uuids.add(recordUuid);
  179.     }
  180.    
  181.     Collections.sort(uuids);
  182.     return uuids;
  183.   }
  184.  
  185.   protected int processCSV() throws ParseException, IOException {
  186.     String[] line;
  187.     int countRecords = 0;
  188.     int countDuplicates = 0;
  189.    
  190.         while ((line = this.getParser().getLine()) != null) {
  191.             ClearSalesEntry entry = OBProvider.getInstance().get(ClearSalesEntry.class);
  192.             for (int i = 0; i < line.length; i++) {
  193.         String colname = getHeaderRow()[i];
  194.         String propname = getPropertyName(colname);
  195.         Object propval = line[i];
  196.        
  197.         if (isDateProperty(propname)) {
  198.           propval = convertDate((String) propval);
  199.         }
  200.        
  201.         entry.set(propname, propval);
  202.       }
  203.       entry.setCreated(new Date());
  204.       entry.setUpdated(new Date());
  205.       entry.setCreatedBy(user);
  206.       entry.setUpdatedBy(user);
  207.       entry.setClient(client);
  208.      
  209.       OBDal.getInstance().save(entry);
  210.       try {
  211.         OBDal.getInstance().commitAndClose();
  212.       } catch (Exception e) {
  213.         countRecords--;
  214.         countDuplicates++;
  215.       }
  216.       countRecords++;
  217.     }
  218.         System.out.println("Imported " + countRecords + " from file while avoiding " + countDuplicates + " duplicates.");
  219.         return countRecords;
  220.   }
  221.  
  222.   protected int importSalesReports() throws IOException, ParseException, MessagingException {
  223.     return fetchAttachments();
  224.   } // importSalesReports()
  225.  
  226.   protected void doExecute(ProcessBundle arg0) throws Exception {
  227.     try {
  228.       System.out.println("Starting ClearReportFetcher...");
  229.  
  230.       int count = importSalesReports();
  231.  
  232.       System.out.println("Imported " + count + " records!");
  233.  
  234.     } catch (Exception e) {
  235.       e.printStackTrace();
  236.       throw e;
  237.     }
  238.   } // doExecute()
  239.  
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement