package com.itwheel.edigate.pricat.processor;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;

import javax.sql.DataSource;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.component.file.GenericFile;
import org.milyn.edi.unedifact.d96a.D96AInterchangeFactory;
import org.milyn.smooks.edi.unedifact.model.UNEdifactInterchange;

import com.itwheel.edigate.utils.SpringLocator;



public class ParsePricatProc implements Processor {
	
	public void process(Exchange exchange) throws Exception {
		
		D96AInterchangeFactory d96aFactory = D96AInterchangeFactory.getInstance();
		GenericFile<File> fileObj = (GenericFile<File>)exchange.getIn().getBody();
		InputStream pricatis = new FileInputStream(fileObj.getFile());
		UNEdifactInterchange interchange = d96aFactory.fromUNEdifact(pricatis);
		
		ParseEdi edi = new ParseEdi();
		EdiPricatHeadBean head = edi.parse(interchange);
		
		DataSource ds = (DataSource)SpringLocator.getBean("edi_ds");
		Connection conn = ds.getConnection();
		
		try {
			conn.setAutoCommit(false);
			EdiPricatDao dao = new EdiPricatDao();
			dao.handler(conn, head);
			conn.commit();
		} catch (Exception e) {
			conn.rollback();
		}
		conn.close();
		
		exchange.getOut().setHeaders(exchange.getIn().getHeaders());
		exchange.getOut().setBody(interchange);
	}

}

@@ -560,70 +560,329 @@
 %0D%0A%09p
-ublic void process(Exchange exchange) throws Exception %7B%0D%0A%09%09%0D%0A
+rivate DataSource ediDs = null;%0D%0A%09public void setEdiDs(DataSource ediDs) %7B%0D%0A%09%09this.ediDs = ediDs;%0D%0A%09%7D%0D%0A%09%0D%0A%09public void process(Exchange exchange) throws Exception %7B%0D%0A%09%09%0D%0A%09%09GenericFile%3CFile%3E fileObj = (GenericFile%3CFile%3E)exchange.getIn().getBody();%0D%0A%09%09if(fileObj != null) %7B%0D%0A%09%09%09System.out.println(fileObj.getFileName());%0D%0A%09
 %09%09D9
@@ -960,85 +960,8 @@
 ;%0D%0A%09
-%09GenericFile%3CFile%3E fileObj = (GenericFile%3CFile%3E)exchange.getIn().getBody();%0D%0A
 %09%09In
@@ -1001,16 +1001,22 @@
 tStream(
+(File)
 fileObj.
@@ -1022,18 +1022,24 @@
 .get
-File
+Body
 ());%0D%0A
+%09%09%09%0D%0A%09
 %09%09UN
@@ -1111,18 +1111,20 @@
 is);%0D%0A%09%09
+%09
 %0D%0A
+%09
 %09%09ParseE
@@ -1151,16 +1151,17 @@
 i();%0D%0A%09%09
+%09
 EdiPrica
@@ -1204,20 +1204,24 @@
 ge);%0D%0A%09%09
+%09
 %0D%0A
+//
 %09%09
+%09
 DataSour
@@ -1276,16 +1276,17 @@
 s%22);%0D%0A%09%09
+%09
 Connecti
@@ -1295,17 +1295,25 @@
  conn = 
-d
+this.ediD
 s.getCon
@@ -1326,18 +1326,20 @@
 n();%0D%0A%09%09
+%09
 %0D%0A
+%09
 %09%09try %7B%0D
@@ -1335,24 +1335,25 @@
 %0D%0A%09%09%09try %7B%0D%0A
+%09
 %09%09%09conn.setA
@@ -1370,24 +1370,25 @@
 false);%0D%0A%09%09%09
+%09
 EdiPricatDao
@@ -1418,16 +1418,17 @@
 ();%0D%0A%09%09%09
+%09
 dao.hand
@@ -1445,16 +1445,17 @@
 head);%0D%0A
+%09
 %09%09%09conn.
@@ -1467,16 +1467,17 @@
 t();%0D%0A%09%09
+%09
 %7D catch 
@@ -1496,16 +1496,17 @@
 ) %7B%0D%0A%09%09%09
+%09
 conn.rol
@@ -1517,19 +1517,32 @@
 k();%0D%0A%09%09
-%7D%0D%0A
+%09%7D finally %7B%0D%0A%09%09
 %09%09conn.c
@@ -1552,18 +1552,21 @@
 e();%0D%0A%09%09
+%09%7D
 %0D%0A
+%09
 %09%09exchan
@@ -1621,16 +1621,17 @@
 rs());%0D%0A
+%09
 %09%09exchan
@@ -1666,16 +1666,28 @@
 nge);%0D%0A%09
-%7D
+%09%7D%0D%0A%09%09%0D%0A%09%7D%0D%0A%09
 %0D%0A%0D%0A%7D%0D%0A

修复fileobj空指针问题，修复远程文件无法cast问题