码迷,mamicode.com
首页 > 其他好文 > 详细

Air21

时间:2018-06-18 21:42:44      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:city   create   parse   import   author   check   ####   lis   other   

handler

技术分享图片
package com.icil.edi.ws.milestoneService.handler;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.icil.edi.dao.common.dao.EdiHistoryDAO;
import com.icil.edi.ws.common.utils.ObjectUtils;
import com.icil.edi.ws.common.utils.PropertiesUtils;
import com.icil.edi.ws.milestoneService.Air21MileStoneBean.Air21MileStoneStatus;
import com.icil.edi.ws.milestoneService.Air21MileStoneBean.Packages;
import com.icil.edi.ws.milestoneService.Air21MileStoneBean.Scan_detail;
import com.icil.edi.ws.milestoneService.Air21MileStoneBean.Shipment_tracking;
import com.icil.edi.ws.milestoneService.constant.MilestoneConstant;
import com.icil.sofs.dao.edi.dao.OrderStatusDAO;
import com.icil.sofs.dao.edi.model.OrderStatusDO;
import com.icil.sofs.dao.global.decode.dao.LocationDAO;
import com.icil.sofs.dao.global.decode.model.LocationDO;

/**
 * 
 * @ClassName: Air21OrderStatusConverter
 * @Description:convert Air21 order status to table OrderStatus
 * @author: Sea
 * @date: 15 Jun 2018 7:24:58 PM
 * 
 * @Copyright: 2018 ICIL. All rights reserved.
 */
@Component("air21OrderStatusHandler")
public class Air21OrderStatusHandler {
    /********************************
     * 1.convert bean
     * 
     * 2.if status is DDL or DL1 ,update edi_history isOverMileStone="true"
     * 
     * 3.delete old milestone info
     * 
     * 4.save new milestone to orderstatus table
     ***************************************/
    @Autowired
    private EdiHistoryDAO ediHistoryDAO;

    @Autowired
    private LocationDAO LocationDAO;

    @Autowired
    private OrderStatusDAO OrderStatusDAO;

    private static final Logger LOGGER = LoggerFactory.getLogger(Air21OrderStatusHandler.class);

    @Transactional
    public void handerStatus(List<Air21MileStoneStatus> mileStoneStatusList) {

        LOGGER.info("Enter into the method Air21OrderStatusHandler-->handerStatus() ");

        ArrayList<OrderStatusDO> orderStatusDOs = new ArrayList<OrderStatusDO>();
        Properties prorperties = PropertiesUtils
                .getProperties(MilestoneConstant.AIR21_MILESTONE_STATUS__MAPPING_PROPERTIES);

        for (Air21MileStoneStatus air21MileStoneStatus : mileStoneStatusList) {
            if(air21MileStoneStatus==null){continue;}
            Shipment_tracking shipment_tracking = air21MileStoneStatus.getShipment_tracking();
            Packages packages = shipment_tracking.getPackages();
            if(packages==null){continue;}
            // reference_number is orderNo
            String reference_number = packages.getReference_number();
            // user to query edi_history
            String orderNoJson = "";
            if (reference_number != null&&) {
                orderNoJson = ObjectUtils.map2Json("orderNo", reference_number);

                List<Scan_detail> scan_details = packages.getScan_details();
                /*************
                 * start convert bean
                 ***************/
                for (Scan_detail scan_detail : scan_details) {
                    OrderStatusDO orderStatusDO = new OrderStatusDO();
                    orderStatusDO.setDataSource(MilestoneConstant.Air21);
                    if (scan_detail != null) {
                        String statusCode = prorperties.getProperty(scan_detail.getScan_code());
                        statusCode = statusCode.trim();
                        orderStatusDO.setStatus(statusCode);
                        orderStatusDO.setReferenceNo(reference_number);
                        // if the shipment has delivered ,update the edi_history
                        // respnse_summary "isOver"
                        if (statusCode.equalsIgnoreCase("DDL") || statusCode.equalsIgnoreCase("DL1")) {
                            ediHistoryDAO.updateResponseSummaryIsOverMileStoneByCourierBillNo("true", orderNoJson);
                        }

                        String post_date = scan_detail.getPost_date();// 07/04/2013
                        String post_time = scan_detail.getPost_time(); // 11:28
                        // LUCENA CITY
                        String post_location = scan_detail.getPost_location();
                        // shipment is on Ari21 vehicle for delivery
                        String status = scan_detail.getStatus();

                        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
                        Date actionDateLoc = format.parse(post_date + post_time);
                        orderStatusDO.setActionDateLoc(actionDateLoc);// 2018-02-10_04:45:00

                        /**
                         * TODO :timeZone
                         */
                        // queryActionDateTz(post_location,actionDateLoc,orderStatusDO);
                        orderStatusDO.setActionDateGMT(actionDateLoc);
                        orderStatusDO.setCompletedDateLoc(actionDateLoc);
                        orderStatusDO.setCompletedDateTz("GMT+7");
                        orderStatusDO.setOtherInfo(status);
                        orderStatusDO.setActionPlace1(post_location);
                        orderStatusDOs.add(orderStatusDO);
                    }
                }
                // delete the old mileStone records from Air21 before new
                // records into AFSA2 DB.
                // query the records which dataSource is Air21

                String sql = "select OrderStatusDO from OrderStatusDO OrderStatusDO where OrderStatusDO.orderNo=‘"
                        + reference_number + "‘ and OrderStatusDO.dataSource=‘Air21‘";
                List<OrderStatusDO> oldMileStoneList = OrderStatusDAO.queryByJPQL(sql);
                if (!oldMileStoneList.isEmpty()) {
                    OrderStatusDAO.delete(oldMileStoneList);
                }

                // save the new mileStone info to database
                try {
                    if (orderStatusDOs != null && orderStatusDOs.size() > 0) {
                        LOGGER.info("Start to insert new  records to OrderStatus which from Air21");
                        Date startInsertDate = new Date();
                        OrderStatusDAO.insert(orderStatusDOs, false);
                        Date endInsertDate = new Date();
                        ediCheckpointHistoryService.saveCheckpoint(startInsertDate, endInsertDate,
                                CheckpointConstant.CONNECT_DB, 1, CheckpointConstant.SAVE_TO_DB);
                        LOGGER.info("End of insert records to OrderStatus");
                        //clear the list of orderStatusDOs
                        orderStatusDOs.clear();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    LOGGER.error("OrderStatusHandler.saveMileStoneRecord() occurred exception,the error is "
                            + e.getMessage());
                    ediCheckpointHistoryService.saveCheckpoint(new Date(), new Date(), CheckpointConstant.CONNECT_DB, 0,
                            CheckpointConstant.SAVE_TO_DB);
                    String subject = "Save data to DB failed";
                    String content = "query milestone from aftership in OrderStatusHandler.saveMileStoneRecord() occurred exception ";
                    EmailUtils.sendExceptionEmailNotify(subject, content, e);

                }

            }

        }

        // return orderStatusDOs;

    }

    private void queryActionDateTz(String post_location, Date date, OrderStatusDO orderStatusDO) {
        String fTimeZone = "";
        Map criteriaMap = new HashMap<String, String>();
        criteriaMap.put("location", post_location);
        List<LocationDO> locationDOlist = (List<LocationDO>) LocationDAO.query(criteriaMap);
        if (!locationDOlist.isEmpty()) {
            fTimeZone = locationDOlist.get(0).getTimezone();
        } else {
            fTimeZone = "US/Pacific";
        }

        /***/
        String strTimeZone = date.substring(date.lastIndexOf("T") + 9);
        String lastTimeZone = "";
        if ("".equals(strTimeZone)) {
            lastTimeZone = getTimeZone(checkpoint);
            if (lastTimeZone == null || "".equals(lastTimeZone)) {
                lastTimeZone = fTimeZone;
            }
        } else {
            lastTimeZone = "GMT" + strTimeZone;
        }
        orderStatusDO.setActionDateTz(lastTimeZone);
        Date actionDateGMT = DateUtils.getGMTDate(date, lastTimeZone, MilestoneConstant.TDate_Time_Format);
        orderStatusDO.setActionDateGMT(actionDateGMT);
        orderStatusDO.setCompletedDateLoc(actionDateLoc);
        orderStatusDO.setCompletedDateTz(lastTimeZone);

        /****/
    }

}
View Code

 

impl

技术分享图片
package com.icil.edi.ws.milestoneService.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

import org.apache.xmlbeans.impl.jam.internal.elements.VoidClassImpl;
import org.glassfish.jersey.message.MessageBodyWorkers;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.icil.edi.dao.common.dao.EdiHistoryDAO;
import com.icil.edi.dao.common.exception.DAOExceptionHandler;
import com.icil.edi.dao.common.model.EdiHistoryDO;
import com.icil.edi.ws.common.constant.EdiServiceConstant;
import com.icil.edi.ws.common.utils.EmailUtils;
import com.icil.edi.ws.common.utils.PropertiesUtils;
import com.icil.edi.ws.common.utils.TypeAndPartyUtils;
import com.icil.edi.ws.common.utils.URLHttpUtils;
import com.icil.edi.ws.milestoneService.Air21MileStoneBean.Air21MileStoneStatus;
import com.icil.edi.ws.milestoneService.constant.MilestoneConstant;
import com.icil.edi.ws.milestoneService.handler.Air21OrderStatusHandler;

import javassist.expr.NewArray;

/**
 * 
 * @ClassName: Air21QueryMileStoneServiceImpl
 * @Description:the logic of Air21 query milestone
 * @author: Sea
 * @param <E>
 * @date: 15 June 2018 12:06:28 PM
 * 
 * @Copyright: 2018 ICIL. All rights reserved.
 */
@Service
public class Air21QueryMileStoneServiceImpl<E> {

    /***********************************************************************
     * 1.create logger
     * 
     * 1.1 do some prepare work
     * 
     * 2.get data source form database
     * 
     * 2.1.save edi_checkpoint history edi_history and save request data to
     * server 2.2 collect edi_batchindo data
     * 
     * 3.send courierbillNo to Air21 and get milestone 3.1 here need to try 3
     * time
     * 
     * 4.parse response
     * 
     * 5.convert response_data_xml to bean
     * 
     * 6.delete old milestone according to courierBillNo
     * 
     * 7.save new milestone 7.1 here need time zone mapping
     * 
     * 8.update edi_history filed response_summary isOverMileStone:true if
     * status is DDl or Dl1 8.1 update edi_history 8.2 save response data to
     * local 8.3 save batch_info .checkpoint history
     * 
     ***********************************************************************/
    @Autowired
    private EdiHistoryDAO ediHistoryDAO;

    @Autowired
    private Air21OrderStatusHandler air21OrderStatusHandler;

    private static final Logger LOGGER = LoggerFactory.getLogger(Air21QueryMileStoneServiceImpl.class);

    public void queryMileStone() {
        // 1 .create logger
        // addTypeAndPartyToMDC(String party,String type)
        TypeAndPartyUtils.addTypeAndPartyToMDC("EDI", MilestoneConstant.AS_QUERY_MILESTONE);

        /*
         * *******************************************************************
         * 2.get data source form database 2.1.save edi_checkpoint history
         * edi_history and save request data to server 2.2 collect edi_batchindo
         * data
         ******************************************************************/
        LinkedList<String> air21QueryMileStoneDataSource = getAir21QueryMileStoneDataSource();
        if (air21QueryMileStoneDataSource != null && air21QueryMileStoneDataSource.size() > 0) {

            /*******************************
             * 3.send courierbillNo to Air21 and get milestone 3.1 here need to
             * try 3 time
             ********************************/

            String AirQueryMileStone_Url = (String) PropertiesUtils.getProperties(EdiServiceConstant.WEBSERVICE_URL,
                    MilestoneConstant.AIR210QUERYMILESTONE_URL);

            /**
             * 4 start query milestone ####
             */
            Gson gson = new Gson();
            ArrayList<Air21MileStoneStatus> mileStoneStatusList = new ArrayList<Air21MileStoneStatus>();
            for (String ref_Number : air21QueryMileStoneDataSource) {
                String response = sendRequest(AirQueryMileStone_Url + "&c=" + ref_Number);
                /*********************************
                 * 4.parse response 5.convert response_data_xml to bean
                 *********************************/
                if (response == null) {
                    // TODO:here need add some record
                    continue;
                }
                
                /**
                 * TODO: jsonrespnse  XML.toJSONObject(response);
                 * aa= jsonrespnse.getkey("shipment_tracking");
                 * bb=new JSONObject(aa);
                 * if(bb.get("reference_number")!=null){continue;}
                 */

                // TODO: here need save response data to server .update
                // edi_history edi_checkpoint

                // response is_Xml here : 1. parse it to _json 2. parse to bean
                // 3.collect air21MileStoneStatus
                try {
                    String responseStr = XML.toJSONObject(response).toString();
                    Air21MileStoneStatus air21MileStoneStatus = gson.fromJson(responseStr, Air21MileStoneStatus.class);
                    mileStoneStatusList.add(air21MileStoneStatus);
                } catch (JSONException e) {
                    LOGGER.info("response xml convert to json exceptoin ");
                    e.printStackTrace();
                }

            }

            // end query milestone

            /*****************************************************
             * 5 .convert bean 6.delete old milestone according to courierBillNo
             * 7.save new milestone 7.1 here need time zone mapping 7.2 parse
             * response result ":1.if status is DDL or DL1 ,update edi_history
             * isOverMileStone="true"
             ***************************************************/

            air21OrderStatusHandler.handerStatus(mileStoneStatusList);
        }

    }

    private String sendRequest(String url) {
        LOGGER.info("start invoke Air21 get milestone ");
        String response = URLHttpUtils.getURLRequest(url, null);
        if (response == null) {
            LOGGER.info("connect Air21 exception , start second connect it ");
            response = URLHttpUtils.getURLRequest(url, null);
            if (response == null) {
                LOGGER.info("connect Air21 exception , start third times connect it ");
                response = URLHttpUtils.getURLRequest(url, null);
                String subject = "Query milestone from Air21 Failed";
                String content = "query milestone from Air21 failed,connect Air21 exception,plase check the net ";
                EmailUtils.sendExceptionEmailNotify(subject, content, null);
            }
        }

        return response;
    }

    private LinkedList<String> getAir21QueryMileStoneDataSource() {
        // get data source from database
        List<String> air21QueryMileStoneDataSource = null;
        try {
            air21QueryMileStoneDataSource = ediHistoryDAO.findAir21QueryMileStoneDataSource();
        } catch (Exception e) {
            DAOExceptionHandler.handle(e);
        }

        // convert to only courierBillNo data list
        LinkedList<String> air21DataSource = new LinkedList<String>();
        for (String dataSource : air21QueryMileStoneDataSource) {
            try {
                JSONArray data = new JSONArray(dataSource);
                String dataStr = data.get(0).toString();
                JSONObject dataJson = new JSONObject(dataStr);
                String orderNo = dataJson.get("orderNo").toString();
                air21DataSource.add(orderNo);
            } catch (Exception exception) {
                exception.printStackTrace();
                LOGGER.info("dataSource is exception");
            }
        }
        return air21DataSource;
    }

}
View Code

 

Air21

标签:city   create   parse   import   author   check   ####   lis   other   

原文地址:https://www.cnblogs.com/lshan/p/9196112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!