package com.oztime.exchange.sap;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.SQLException;
import java.util.*;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import com.oztime.poi.*;
import com.oztime.page.gdb.*;
import com.oztime.util.*;
public class DefaultChangeData implements ChangeData{
public static String filename="";
public static String afilename="";
public final static boolean DEBUG = true;//调试用
private static int BUFFER_SIZE = 8096;//缓冲区大小
public Call getRemoteCall(String URL) throws IOException,SQLException,FuncSqlException,ServiceException{
Service service = new Service();
Call call = (Call) service.createCall();//创建对象
call.setTargetEndpointAddress(URL);
call.setOperationName("getExpData");
call.addParameter( "op1", XMLType.XSD_STRING, ParameterMode.IN );
call.setReturnType( XMLType.SOAP_STRING);
return call;
}
public void execute() {
downLoadByList();
}
public void downLoadByList() {
SqlResult rs=null;
try{
GeneralDB gdb = GeneralDB.getGDB("ozrd");
Hashtable ht = new Hashtable();
ht.put("where","");
rs = gdb.accessDatabase("GroupLevel_sql.xml","GroupLevel_SelectAllGroup",ht);
}catch(Exception ex){
System.err.println("获取数据库链接发生错误,具体原因:"+ex.getMessage());
}
if(rs.getRowCount()>0){
for (int i=0;i<rs.getRowCount();i++) {
String id = rs.getField(i,"id").toString();
String theIP = rs.getField(i,"theIP").toString();
String port = rs.getField(i,"port").toString();
String tgroupName = rs.getField(i,"TgroupName").toString();
String tgroupid = rs.getField(i,"Tgroupid").toString();
Call call;
String remoteclassUrl="http://"+theIP+":"+port+"/www/OztimeFace.jws";
String filepath = com.oztime.page.system.Configuration.getPathProperty("deploy.home");
String NativeUrl=filepath+File.separator+ "rcpx" + File.separator+tgroupid+".xls";
String RemoteUrl = "http://"+theIP+":"+port+"/www/rcpx/"+tgroupid+".xls";
ImpData impdata = new ImpData(id);
try{
System.out.println(">>>>>>>>>>>>>>>>>>触发"+tgroupName+"定时导出功能操作开始>>>>>>>>>>>>>>>>>");
call = getRemoteCall(remoteclassUrl);
filename = (String)call.invoke( new Object[] {tgroupid} ) ;
System.out.println(">>>>>>>>>>>>>>>>>>触发"+tgroupName+"定时导出功能操作结束>>>>>>>>>>>>>>>>>");
afilename=tgroupid+".xls";
System.out.println("filename>>>"+filename);
System.out.println("afilename>>>"+afilename);
if(filename.equals(afilename)){
System.out.println("导出"+tgroupName+"增量数据的Excel成功!");
System.out.println(tgroupName+"的Excel数据开始下载到一级服务器上");
try{
saveFileToNative(RemoteUrl,NativeUrl);
}catch (IOException err) {
if (DEBUG) {
System.out.println("资源[" + RemoteUrl + "]下载失败!!!");
}
}
System.out.println(tgroupName+"的Excel数据下载到一级服务器上完毕");
try{
System.out.println(tgroupName+"的Excel数据导入一级数据库开始");
impdata.imp(NativeUrl);
System.out.println(tgroupName+"的Excel数据导入一级数据库结束");
}catch(WebException ex) {
System.err.print("导入出现异常,具体原因:"+ex.getMessage());
}
}
else{
System.out.println(tgroupName+"的Excel数据无法得到");
}
}catch(Exception ex){
ex.printStackTrace();
System.out.println(tgroupName+"定时导出功能操作异常");
}
}
}
}
public void saveFileToNative(String RemoteUrl, String NativeUrl) throws IOException {
FileOutputStream fos = null;
BufferedInputStream bis = null;
HttpURLConnection httpUrl = null;
URL url = null;
byte[] buf = new byte[BUFFER_SIZE];
int size = 0;
//建立链接
url = new URL(RemoteUrl);
httpUrl = (HttpURLConnection) url.openConnection();
//连接指定的资源
httpUrl.connect();
//获取网络输入流
bis = new BufferedInputStream(httpUrl.getInputStream());
//建立文件
fos = new FileOutputStream(NativeUrl);
if (DEBUG)
System.out.println("正在获取链接[" + RemoteUrl + "]的内容.../n将其保存为文件[" + NativeUrl + "]");
//保存文件
while ( (size = bis.read(buf)) != -1)
fos.write(buf, 0, size);
fos.close();
bis.close();
httpUrl.disconnect();
}
}