Subversion Repositories SmartDukaan

Rev

Rev 29418 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.eclipsesource.json.JsonArray;
import com.spice.profitmandi.common.model.CustomNotifyOrder;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.dao.entity.transaction.NotifyCancel;
import com.spice.profitmandi.dao.entity.transaction.Order;
import com.spice.profitmandi.model.tracking.ScannedDetailModel;
import com.spice.profitmandi.model.tracking.TrackingDetailModel;

import in.shop2020.model.v1.order.OrderStatus;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

@Controller
@Transactional(rollbackFor = Throwable.class)
public class TrackingController {

        @Value("${delhivery.tracking.token}")
        private String token;

        @Autowired
        private ResponseSender<?> responseSender;

        private static final Logger LOGGER = LogManager.getLogger(TrackingController.class);

        @RequestMapping(value = "/order/tracking", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                        @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
        public ResponseEntity<?> getNotifyCancelOrders(HttpServletRequest request, HttpServletResponse response,
                        @RequestParam String airwayBillNo) throws Throwable {

                String url = "https://track.delhivery.com/api/v1/packages/json/";

                OkHttpClient client = new OkHttpClient();

                Request request1 = new Request.Builder()
                                .url(url + "?waybill=" + String.join(",", airwayBillNo) + "&token=" + token).get().build();

                LOGGER.info("request1" + request1);

                Response response1 = client.newCall(request1).execute();

                LOGGER.info("jsonbj" + response1.body());

                JSONObject jsonObj = new JSONObject(response1.body().string());

                LOGGER.info("jsonObj" + jsonObj);

                JSONArray shipmentData = jsonObj.getJSONArray("ShipmentData");

                TrackingDetailModel tdm = new TrackingDetailModel();

                List<ScannedDetailModel> sdms = new ArrayList<>();

                for (int i = 0; i < shipmentData.length(); i++) {
                        JSONObject jsonObject = shipmentData.getJSONObject(i);
                        JSONObject shipment = jsonObject.getJSONObject("Shipment");

                        JSONArray scans = shipment.getJSONArray("Scans");

                        for (int j = 0; j < scans.length(); j++) {
                                JSONObject scanObject = scans.getJSONObject(j);

                                JSONObject scandetails = scanObject.getJSONObject("ScanDetail");

                                ScannedDetailModel sdm = new ScannedDetailModel();
                                sdm.setStatusDateTime(scandetails.getString("ScanDateTime"));
                                sdm.setStatusLocation(scandetails.getString("ScannedLocation"));
                                sdm.setInstruction(scandetails.getString("Instructions"));
                                sdms.add(sdm);

                        }

                        tdm.setExpectedDelivery(shipment.getString("ExpectedDeliveryDate"));
                        tdm.setAwb(shipment.getString("AWB"));

                }

                tdm.setSdm(sdms);
                LOGGER.info("tdm" + tdm);

                return responseSender.ok(tdm);
        }

}