对象转json

package com.sf.dtx5.reponse;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;

import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.commons.lang.StringUtils;

import com.sf.dtx5.Config;
import com.sf.dtx5.db.ConnectionPool;
import com.sf.dtx5.db.DbConnect;
import com.sf.dtx5.domain.OPQMUploadImages;
import com.sf.dtx5.util.ParseProperties;
import net.sf.json.JSONObject;
import sun.misc.BASE64Decoder;
/**
 * 1.接收图片及其信息,记录流水
 * 2.通过WebService上传图片到XXXX
 */
public class CRequestUploadOPQMImageS  {
	/**
	 * 1.接收图片及其信息,记录流水
	 * 2.通过WebService上传图片到XXXX
	 * @throws ServiceException 
	 */
	public boolean uploadImages(OPQMUploadImages imagesjson) {
		boolean bRet=false;
		try {
			BASE64Decoder decoder = new BASE64Decoder();
			byte[] b = decoder.decodeBuffer(imagesjson.getImageContent());
			JSONObject jsonObjSon = new JSONObject();
			jsonObjSon.put("waybillNos", imagesjson.getWaybillNos());
			jsonObjSon.put("childWaybillNos", imagesjson.getChildWaybillNos());
			jsonObjSon.put("parentWaybillNos", imagesjson.getParentWaybillNos());
			jsonObjSon.put("type", imagesjson.getType());
			jsonObjSon.put("complainReason", imagesjson.getComplainReason());
			jsonObjSon.put("complaintScene", imagesjson.getComplaintScene());
			jsonObjSon.put("complaintContent", imagesjson.getComplaintContent());
			jsonObjSon.put("correctRecord", imagesjson.getCorrectRecord());
			jsonObjSon.put("errorRecod", imagesjson.getErrorRecod());
			jsonObjSon.put("relationWaybillNo", imagesjson.getRelationWaybillNo());
			jsonObjSon.put("correctSend", imagesjson.getCorrectSend());
			jsonObjSon.put("errorSend", imagesjson.getErrorSend());
			jsonObjSon.put("securityInspectionMethod", imagesjson.getSecurityInspectionMethod());
			jsonObjSon.put("length", imagesjson.getLength());
			jsonObjSon.put("width", imagesjson.getWidth());
			jsonObjSon.put("height", imagesjson.getHeight());
			jsonObjSon.put("weight", imagesjson.getWeight());
			jsonObjSon.put("price", imagesjson.getPrice());
			jsonObjSon.put("fillCase", imagesjson.getFillCase());
			jsonObjSon.put("goodsType", imagesjson.getGoodsType());
			jsonObjSon.put("discoverNode", imagesjson.getDiscoverNode());
			jsonObjSon.put("externalCondition", imagesjson.getExternalCondition());
			jsonObjSon.put("goodsDamagedCondition", imagesjson.getGoodsDamagedCondition());
			jsonObjSon.put("packagingSpecification", imagesjson.getPackagingSpecification());
			jsonObjSon.put("shootTime", imagesjson.getShootTime());
			jsonObjSon.put("uploadTime", imagesjson.getUploadTime());
			jsonObjSon.put("imageContent", b);
			jsonObjSon.put("size", imagesjson.getSize());
			jsonObjSon.put("picNum", imagesjson.getPicNum());
			jsonObjSon.put("netPointCode", imagesjson.getNetPointCode());
			jsonObjSon.put("userName", imagesjson.getUserName());
			jsonObjSon.put("systemCode", imagesjson.getSystemCode());

			// 1.接收图片信息并保存到本地数据库中
			if (executeSQLImages(imagesjson, b)) {
				bRet = true;
				Config.log.writeLog(2, "uploadImage--[SUCCESS]--数据存储本地库成功");
			} else {
				Config.log.writeLog(2, "uploadImage--[FAIL]--数据存储本地库失败");
			}
			if(bRet){
				bRet=false;
				if (uploadOPQMImage(jsonObjSon.toString())) {
					if(uploadState(imagesjson)){
						bRet = true;
					}else{
						Config.log.writeLog(2, "uploadImage--XXXX--[SUCCESS]---数据上传XXXX成功状态更改失败...");
					}
					Config.log.writeLog(2, "uploadImage--XXXX--[SUCCESS]---数据上传XXXX成功...");
				} else {
					Config.log.writeLog(2, "uploadImage--XXXX--[FAIL]---数据上传XXXX失败...");
				}
			}
		} catch (IOException e) {
			Config.log.writeLog(2, "uploadImage--XXXX--[FAIL]---数据上传XXXX失败...",e);
		}
		return bRet;
	}
	/**
	 * 
	 * @param cmdChar
	 *            命令
	 * @param barText
	 *            上传运单信息
	 * @param ver
	 *            版本号
	 * @return
	 */
	public  boolean executeSQLImages(OPQMUploadImages item, byte[] b) {
		boolean bRet = false;
		Connection con = ConnectionPool.getInstance().getConnection();
		if (null != con) {
			PreparedStatement pstm = null;
			try {
				con.setAutoCommit(false);
				pstm = con.prepareStatement("insert into TT_IMAGES (xh,waybillNos,childWaybillNos,parentWaybillNos,type,complainReason,complaintScene,complaintContent,correctRecord,"
				+ "errorRecod,relationWaybillNo,correctSend,errorSend,securityInspectionMethod,length,width,height,weight,price,fillCase,goodsType,discoverNode,externalCondition,goodsDamagedCondition,packagingSpecification,"
				+ "shootTime,uploadTime,imageContent,sizes,picNum,netPointCode,userName,systemCode,insert_tm) values (" 
				+"SEQ_UPLOADIMAGE_LOG.nextval,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,sysdate)");
				pstm.clearParameters();
				if(item.getWaybillNos()!=null){
					pstm.setString(1,StringUtils.join(item.getWaybillNos()));					
				}
				if(item.getChildWaybillNos()!=null){
					pstm.setString(2,StringUtils.join(item.getChildWaybillNos()));
				}else{	pstm.setString(2,null);}
				
				if(item.getParentWaybillNos()!=null){
					pstm.setString(3,StringUtils.join(item.getParentWaybillNos()));
				}else{	pstm.setString(3,null);}
				pstm.setString(4,item.getType());
				pstm.setString(5,item.getComplainReason());
				pstm.setString(6,item.getComplaintScene());
				pstm.setString(7,item.getComplaintContent());
				pstm.setString(8,item.getCorrectRecord());
				pstm.setString(9,item.getErrorRecod());
				pstm.setString(10,item.getRelationWaybillNo());
				pstm.setString(11,item.getCorrectSend());
				pstm.setString(12,item.getErrorRecod());
				pstm.setString(13,item.getSecurityInspectionMethod());
				if(item.getLength()!=null){
					pstm.setDouble(14,item.getLength());
				}else{
					pstm.setDouble(14,0.00);
				}
				if(item.getWidth()!=null){
					pstm.setDouble(15,item.getWidth());
				}else{
					pstm.setDouble(15,0.00);
				}
				if(item.getHeight()!=null){
					pstm.setDouble(16,item.getHeight());
				}else{
					pstm.setDouble(16,0.00);
				}
				if(item.getWeight()!=null){
					pstm.setDouble(17,item.getWeight());
				}else{
					pstm.setDouble(17,0.00);
				}
				if(item.getPrice()!=null){
					pstm.setDouble(18,item.getPrice());
				}else{
					pstm.setDouble(18,0.00);
				}
				pstm.setString(19,item.getFillCase());
				pstm.setString(20,item.getGoodsType());
				pstm.setString(21,item.getDiscoverNode());
				pstm.setString(22,item.getExternalCondition());
				pstm.setString(23,item.getGoodsDamagedCondition());
				pstm.setString(24,item.getPackagingSpecification());
				pstm.setString(25,item.getShootTime());
				pstm.setString(26,item.getUploadTime());
				pstm.setBytes(27,b);
				pstm.setString(28,item.getSize());
				pstm.setString(29,item.getPicNum());
				pstm.setString(30,item.getNetPointCode());
				pstm.setString(31,item.getUserName());
				pstm.setString(32,item.getSystemCode());
				pstm.execute();
				con.commit();
				bRet = true;
			} catch (SQLException e) {
				Config.log.writeLog(2, "回滚数据失败:%s", e);
				try {
					if (con != null)
						con.rollback();
				} catch (SQLException e1) {
					Config.log.writeLog(2, "回滚失败:%s", e1);
				}
				
			} catch(RuntimeException  e){
				Config.log.writeLog(2, "回滚失败:%s", e);
				
			}finally {
				try {
					DbConnect.closeOthers(pstm, null);
					if (con != null)
						con.setAutoCommit(true);
				} catch (SQLException e) {
					Config.log.writeLog(2, "关闭连接失败:%s", e);
				}
			}
			ConnectionPool.getInstance().putConnection(con);
		} else {
			Config.log.writeLog(2, "连接数据库失败");
		}
		return bRet;
	}
	/**
	 * 调用webService 上传图片到XXXX
	 * @throws IOException 
	 * @throws ParseException 
	 * @return result
	 * @throws ServiceException 
	 * **/
	public  boolean uploadOPQMImage(String json) {
		boolean bResult = false;
		String val = null;
		try {
			String urlname = ParseProperties.getValue("XXXX.address");
			Service s = new Service();
			Call call = (Call) s.createCall();
			call.setOperation("syncMultPicV2");
			call.setTargetEndpointAddress(urlname);
			call.addParameter("in0", org.apache.axis.encoding.XMLType.XSD_ANYTYPE, javax.xml.rpc.ParameterMode.IN);// 接口的参数
			call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型

			val = (String) call.invoke(new Object[] { json });
			if("1".equals(val)){
				bResult = true;
			}else{
				Config.log.writeLog(2, "uploadImage to XXXX FAIL"+ val);
			}
			} catch (Exception e) {
				Config.log.writeLog(2, "uploadImage to XXXX FAIL"+ val,e);
			}
				
			return bResult;
				
		}
	
	public boolean uploadState(OPQMUploadImages item){
		boolean bResult = false;
		Connection con = ConnectionPool.getInstance().getConnection();
		if (null != con) {
			PreparedStatement pstm = null;
			try {
				con.setAutoCommit(false);
				pstm = con.prepareStatement("update TT_IMAGES set deal_flg = 2 ,deal_tm = current_timestamp where deal_flg=0 and sizes=? and waybillnos=? and picnum=? and netpointcode=? and username=?"); 
				pstm.clearParameters();
				pstm.setString(1,item.getSize());
				pstm.setString(2,StringUtils.join(item.getWaybillNos()));
				pstm.setString(3,item.getPicNum());
				pstm.setString(4,item.getNetPointCode());
				pstm.setString(5,item.getUserName());
				pstm.execute();
				con.commit();
				bResult = true;
			} catch (SQLException e) {
				Config.log.writeLog(2, "XXXX回滚数据失败:%s", e);
				try {
						con.rollback();
				} catch (SQLException e1) {
					Config.log.writeLog(2, "XXXX回滚数据失败:%s", e1);
				}
				
			} catch(RuntimeException  e){
				Config.log.writeLog(2, "XXXX回滚失败:%s", e);
				
			}finally {
				try {
					DbConnect.closeOthers(pstm, null);
						con.setAutoCommit(true);
				} catch (SQLException e) {
					Config.log.writeLog(2, "关闭连接失败:%s", e);
				}
			}
			ConnectionPool.getInstance().putConnection(con);
		} else {
			Config.log.writeLog(2, "连接数据库失败");
		}
		return bResult;
		
	}

}

  

posted @ 2017-12-19 17:51  杯子茶壶  阅读(132)  评论(0)    收藏  举报