|
@@ -0,0 +1,238 @@
|
|
|
+package com.uas.main;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.FileWriter;
|
|
|
+import java.io.OutputStreamWriter;
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.sql.Statement;
|
|
|
+import java.text.DateFormat;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+import org.apache.commons.net.ftp.FTPClient;
|
|
|
+import org.codehaus.jackson.map.ObjectMapper;
|
|
|
+
|
|
|
+import com.uas.util.BaseUtil;
|
|
|
+import com.uas.util.FtpUtil;
|
|
|
+import com.uas.util.JdbcUtil;
|
|
|
+
|
|
|
+public class Upload {
|
|
|
+ public static Pattern pat = Pattern.compile("\\{(.*?)\\}");
|
|
|
+
|
|
|
+ public static String getHeader(String xmltemp){
|
|
|
+ return xmltemp.substring(xmltemp.indexOf("<?xml"),xmltemp.indexOf("<Item>"));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getDetailItem(String xmltemp){
|
|
|
+ return xmltemp.substring(xmltemp.indexOf("<Item>"),xmltemp.indexOf("</Detail"));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String replaceFieldByData(String str,Map<String,Object> data){
|
|
|
+ Matcher mat = pat.matcher(str);
|
|
|
+ String res = str;
|
|
|
+ while(mat.find()){
|
|
|
+ if(data.get(mat.group(1).toUpperCase())!=null){
|
|
|
+ res = res.replace("{" + mat.group(1).toUpperCase() + "}", replaceXmlSpecialSymbol(data.get(mat.group(1).toUpperCase()).toString()));
|
|
|
+ }else{
|
|
|
+ res = res.replace("{" + mat.group(1).toUpperCase() + "}", "");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String replaceXmlSpecialSymbol(String data){
|
|
|
+ return data.replace("&", "&")
|
|
|
+ .replace("<", "<")
|
|
|
+ .replace(">", ">")
|
|
|
+ .replace("'", "'")
|
|
|
+ .replace("\"", """);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getCurrentdate(String format){
|
|
|
+ DateFormat df = new SimpleDateFormat(format);
|
|
|
+ Date today = Calendar.getInstance().getTime();
|
|
|
+ return df.format(today);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static File createXmlFile(String header,String items,String template,String id,String type){
|
|
|
+ File xmlFile = null;
|
|
|
+ try{
|
|
|
+ String xml = header + items + "</Detail>" + template.toString().substring(template.toString().indexOf("</Detail>")+9);
|
|
|
+ xmlFile = new File(System.getProperty("java.io.tmpdir")+File.separator + type + "-" + getCurrentdate("yyyyMMdd") + id+".xml");
|
|
|
+ if(!xmlFile.exists()){
|
|
|
+ xmlFile.createNewFile();
|
|
|
+ }
|
|
|
+ FileWriter fw = new FileWriter(xmlFile.getAbsoluteFile());
|
|
|
+ OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(xmlFile.getAbsoluteFile()),"UTF-8");
|
|
|
+ out.write(xml);
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+
|
|
|
+ fw.close();
|
|
|
+ }catch(Exception e){
|
|
|
+ BaseUtil.getLogger().error(e.toString());
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return xmlFile;
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public static File decodeAndGenerateXml(String template,String datastr,String xlid,String type) throws Exception{
|
|
|
+ String header = getHeader(template.toString());
|
|
|
+ String detailItem = getDetailItem(template.toString());
|
|
|
+ String currentDate = getCurrentdate("yyyyMMddHHmm");
|
|
|
+ Map<String,Object> data = new ObjectMapper().readValue(datastr, HashMap.class);
|
|
|
+ data.put("FILENAME", type + "-" + data.get("FILENAME"));
|
|
|
+ data.put("CURRENTDATE", currentDate);
|
|
|
+ header = replaceFieldByData(header,data);
|
|
|
+
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ List<Map<String,Object>> detailData = (List<Map<String,Object>>)data.get("DETAIL");
|
|
|
+ for(Map<String,Object> detail:detailData){
|
|
|
+ sb.append(replaceFieldByData(detailItem,detail));
|
|
|
+ }
|
|
|
+
|
|
|
+ File file = createXmlFile(header,sb.toString(),template.toString(),xlid,type);
|
|
|
+ return file;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void turnToFormal(Connection connection,String id,File file,String sob){
|
|
|
+ Statement statement = null;
|
|
|
+ try{
|
|
|
+ connection.setAutoCommit(false);
|
|
|
+ statement = connection.createStatement();
|
|
|
+ statement.execute("insert into "+sob + "."+"xmldatalog(xl_id,xl_data,xl_date,xl_depot,xl_caller,xl_code,xl_from,xl_status,xl_sourceid,xl_filename) "
|
|
|
+ + "select "+sob + "."+"XMLDATALOG_SEQ.NEXTVAL,xl_data,sysdate,xl_depot,xl_caller,xl_code,'upload','success',xl_sourceid,'"+file.getName()+"' from xmldatalogtemp where xl_id=" + id);
|
|
|
+ statement.execute("delete from "+sob + "."+"xmldatalogtemp where xl_id=" + id);
|
|
|
+ connection.commit();
|
|
|
+ BaseUtil.getLogger().info("trun to formal success!");
|
|
|
+ }catch(Exception e){
|
|
|
+ try {
|
|
|
+ connection.rollback();
|
|
|
+ } catch (SQLException e1) {
|
|
|
+ BaseUtil.getLogger().error(e1.toString());
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ BaseUtil.getLogger().error(e.toString());
|
|
|
+ e.printStackTrace();
|
|
|
+ }finally{
|
|
|
+ try{
|
|
|
+ if(statement!=null){
|
|
|
+ statement.close();
|
|
|
+ }
|
|
|
+ }catch(Exception e){
|
|
|
+ BaseUtil.getLogger().error(e.toString());
|
|
|
+ }
|
|
|
+ statement = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean upload(FTPClient client,String ip,String folder,File file){
|
|
|
+ boolean flag = false;
|
|
|
+ if(client!=null){
|
|
|
+ flag = FtpUtil.uploadFile(client, folder,file);
|
|
|
+ }
|
|
|
+ System.out.println("upload " + file.getName() + " to " + ip + ":" + folder + " " + (flag?"success":"fail")+"");
|
|
|
+ BaseUtil.getLogger().info("upload " + file.getName() + " to " + ip + ":" + folder + " " + (flag?"success":"fail")+"");
|
|
|
+ return flag;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void run() {
|
|
|
+ uploadBySob("YHND_SZ");
|
|
|
+ uploadBySob("YHND_HK");
|
|
|
+ uploadBySob("YITOA_ZX");
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public static void uploadBySob(String sob) {
|
|
|
+ Statement statement = null;
|
|
|
+ FTPClient client = null;
|
|
|
+
|
|
|
+ Connection con = null;
|
|
|
+ con = JdbcUtil.getConnectBySob(sob);
|
|
|
+
|
|
|
+ try{
|
|
|
+ statement = con.createStatement();
|
|
|
+ ResultSet rs = statement.executeQuery("select * from "+sob+"."+"xmldatalogtemp where xl_depot is not null order by xl_depot desc");
|
|
|
+ String depot = "";
|
|
|
+ while(rs.next()){
|
|
|
+ try{
|
|
|
+ String xldepot = rs.getString("xl_depot");
|
|
|
+ String type = rs.getString("xl_type");
|
|
|
+ if(!"null".equals(xldepot)&&xldepot!=null&&!"null".equals(type)&&type!=null){
|
|
|
+ Object template = JdbcUtil.getXmlTemplate(con,sob).get(rs.getString("xl_caller")+"_" + xldepot);
|
|
|
+ if(template!=null){
|
|
|
+ Map<String,Object> ftpConfig = JdbcUtil.getFtpConfigs();
|
|
|
+ Map<String,Object> config = (Map<String,Object>)ftpConfig.get(xldepot);
|
|
|
+ String folder = config.get(type).toString();
|
|
|
+
|
|
|
+ if(!depot.equals(xldepot)){
|
|
|
+ if(client!=null){
|
|
|
+ FtpUtil.closeFtpClient(client);
|
|
|
+ }
|
|
|
+ depot = xldepot;
|
|
|
+ client = FtpUtil.connect(config,config.get(type).toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ File file = decodeAndGenerateXml(template.toString(),rs.getString("xl_data"),rs.getString("xl_id"),folder);
|
|
|
+ boolean uploadSuccess = upload(client,config.get("ip").toString(),folder,file);
|
|
|
+ if(uploadSuccess){
|
|
|
+ turnToFormal(con,rs.getString("xl_id"),file,sob);
|
|
|
+ }
|
|
|
+ file.delete();
|
|
|
+ }else{
|
|
|
+ BaseUtil.getLogger().info("caller:"+rs.getString("xl_caller")+" depot:"+xldepot+" template is null");
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ BaseUtil.getLogger().info("warehouse ftpdepot is null");
|
|
|
+ }
|
|
|
+ JdbcUtil.clearXmlTemplate();
|
|
|
+ }catch(Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ BaseUtil.getLogger().error(e.toString());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(client!=null){
|
|
|
+ FtpUtil.closeFtpClient(client);
|
|
|
+ client = null;
|
|
|
+ }
|
|
|
+ try{
|
|
|
+ con.close();
|
|
|
+ }catch(SQLException e){
|
|
|
+ e.printStackTrace();
|
|
|
+ BaseUtil.getLogger().error(e.toString());
|
|
|
+ }finally{
|
|
|
+ con = null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }catch(Exception e){
|
|
|
+ BaseUtil.getLogger().error(e.toString());
|
|
|
+ e.printStackTrace();
|
|
|
+ }finally{
|
|
|
+ if(statement!=null){
|
|
|
+ try {
|
|
|
+ statement.close();
|
|
|
+ } catch (SQLException e1) {
|
|
|
+ BaseUtil.getLogger().error(e1.toString());
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ statement = null;
|
|
|
+ }
|
|
|
+ if(client!=null){
|
|
|
+ FtpUtil.closeFtpClient(client);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|