Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.retailingwireless.clear.reports;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Properties;
- import java.util.Date;
- import javax.mail.Flags;
- import javax.mail.Folder;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Multipart;
- import javax.mail.Part;
- import javax.mail.Session;
- import org.apache.commons.csv.CSVParser;
- import org.openbravo.base.model.ModelProvider;
- import org.openbravo.base.provider.OBProvider;
- import org.openbravo.dal.core.OBContext;
- import org.openbravo.dal.service.OBDal;
- import org.openbravo.model.ad.access.User;
- import org.openbravo.model.ad.system.Client;
- import org.openbravo.scheduling.ProcessBundle;
- import org.openbravo.service.db.DalBaseProcess;
- import com.retailingwireless.clear.data.ClearSalesEntry;
- import com.sun.mail.pop3.POP3Store;
- public class ClearReportFetcher extends DalBaseProcess {
- static String POP3_SERVER = "pop3.server.com";
- static int POP3_PORT = 110;
- static String POP3_MAILBOX = "INBOX";
- static String POP3_USERNAME = "email@address.com";
- static String POP3_PASSWORD = "password";
- static ArrayList<String> DATE_PROPERTIES = new ArrayList<String>(Arrays.asList("dataDate",
- "dateEntered", "deactDate", "statusDate"));
- static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- static Properties props = new Properties();
- static Session session = Session.getDefaultInstance(props, null);
- static POP3Store store = new POP3Store(session, null);
- OBContext ctx = OBContext.getOBContext();
- User user = ctx.getUser();
- Client client = ctx.getCurrentClient();
- private CSVParser parser;
- private String[] headerRow;
- protected CSVParser getParser() {
- return this.parser;
- } // getParser()
- protected String[] getHeaderRow() {
- return this.headerRow;
- } // getHeaderRow()
- protected static String getPropertyName(String colnameVal) {
- String colname = colnameVal;
- if (colname.toUpperCase().indexOf("_ID") > 0) {
- colname = colname.replace("_ID", "_num");
- colname = colname.replace("_Id", "_num");
- colname = colname.replace("_id", "_num");
- colname = colname.replace("_iD", "_num");
- }
- colname = colname.replace("#", "num");
- return ModelProvider.getInstance().getEntity(ClearSalesEntry.class)
- .getPropertyByColumnName(colname).getName();
- } // getPropertyName()
- protected static boolean isDateProperty(String propname) {
- return DATE_PROPERTIES.contains(propname);
- } // isDateProperty()
- protected static Date convertDate(String dateval) throws ParseException {
- if (dateval == null || dateval.trim().length() == 0) {
- return null;
- }
- return DATE_FORMAT.parse(dateval);
- } // convertDate()
- protected static File saveFile(String filename, InputStream input) throws IOException {
- if (filename == null) {
- filename = File.createTempFile("xx", ".out").getName();
- }
- SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
- File file = new File(System.getProperty("java.io.tmpdir")
- + "/clear-" + format.format(new Date()) + "-"
- + filename);
- // Do no overwrite existing file
- for (int i=0; file.exists(); i++) {
- file = new File(filename+i);
- }
- FileOutputStream fos = new FileOutputStream(file);
- BufferedOutputStream bos = new BufferedOutputStream(fos);
- BufferedInputStream bis = new BufferedInputStream(input);
- int aByte;
- while ((aByte = bis.read()) != -1) {
- bos.write(aByte);
- }
- bos.flush();
- bos.close();
- bis.close();
- System.out.println("Saved file " + file.getAbsolutePath() + ".");
- return file;
- }
- protected int fetchAttachments() throws MessagingException, IOException, ParseException {
- store.close();
- store.connect(POP3_SERVER, POP3_PORT, POP3_USERNAME, POP3_PASSWORD);
- Folder folder = store.getFolder("INBOX");
- folder.open(Folder.READ_WRITE);
- Message message[] = folder.getMessages();
- int attachmentCount = 0;
- int processedRecords = 0;
- for (int i=0, n=message.length; i<n; i++) {
- Multipart multipart = (Multipart)message[i].getContent();
- for (int i1=0, n1=multipart.getCount(); i1<n1; i1++) {
- Part part = multipart.getBodyPart(i1);
- String disposition = part.getDisposition();
- if ((disposition != null) && (disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE)))) {
- String partFileName = part.getFileName();
- String fileExtension = partFileName.substring(partFileName.length() - 3).toUpperCase();
- if (fileExtension.equals("CSV")) {
- this.initParser(part.getInputStream());
- processedRecords = this.processCSV();
- saveFile("sales.csv", part.getInputStream());
- attachmentCount++;
- } // if is CSV file
- } // if attachment
- } // for Multipart
- message[i].setFlag(Flags.Flag.DELETED, true);
- } // for Message
- folder.close(true);
- store.close();
- System.out.println("Retrieved " + attachmentCount + " attachments.");
- return processedRecords;
- }
- @SuppressWarnings("deprecation")
- protected void initParser(InputStream input) throws IOException {
- this.parser = new CSVParser(input);
- this.headerRow = this.parser.getLine();
- } // initParser
- protected List<String> readUuids() {
- int showSwitch = 0;
- List<String> uuids = new ArrayList<String>();
- Iterator<ClearSalesEntry> entries = OBDal.getInstance().createCriteria(ClearSalesEntry.class).list().iterator();
- while (entries.hasNext()) {
- ClearSalesEntry entry = entries.next();
- String dateEnteredColumn = DATE_FORMAT.format(entry.getDateEntered()) + "_";
- String accountColumn = entry.getAccountNum() + "_";
- String offerColumn = entry.getOfferNum() + "_";
- String pullbackColumn = entry.getPullbackFlag() + "_";
- String cancelColumn = entry.getCancelFlag() + "_";
- String cancelReasonColumn = entry.getCancelReason();
- String recordUuid = dateEnteredColumn + accountColumn + offerColumn + pullbackColumn + cancelColumn + cancelReasonColumn;
- showSwitch++;
- uuids.add(recordUuid);
- }
- Collections.sort(uuids);
- return uuids;
- }
- protected int processCSV() throws ParseException, IOException {
- String[] line;
- int countRecords = 0;
- int countDuplicates = 0;
- while ((line = this.getParser().getLine()) != null) {
- ClearSalesEntry entry = OBProvider.getInstance().get(ClearSalesEntry.class);
- for (int i = 0; i < line.length; i++) {
- String colname = getHeaderRow()[i];
- String propname = getPropertyName(colname);
- Object propval = line[i];
- if (isDateProperty(propname)) {
- propval = convertDate((String) propval);
- }
- entry.set(propname, propval);
- }
- entry.setCreated(new Date());
- entry.setUpdated(new Date());
- entry.setCreatedBy(user);
- entry.setUpdatedBy(user);
- entry.setClient(client);
- OBDal.getInstance().save(entry);
- try {
- OBDal.getInstance().commitAndClose();
- } catch (Exception e) {
- countRecords--;
- countDuplicates++;
- }
- countRecords++;
- }
- System.out.println("Imported " + countRecords + " from file while avoiding " + countDuplicates + " duplicates.");
- return countRecords;
- }
- protected int importSalesReports() throws IOException, ParseException, MessagingException {
- return fetchAttachments();
- } // importSalesReports()
- protected void doExecute(ProcessBundle arg0) throws Exception {
- try {
- System.out.println("Starting ClearReportFetcher...");
- int count = importSalesReports();
- System.out.println("Imported " + count + " records!");
- } catch (Exception e) {
- e.printStackTrace();
- throw e;
- }
- } // doExecute()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement