com.neusoft.mid.clwapi.service.tacks.TacksServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.neusoft.mid.clwapi.service.tacks.TacksServiceImpl.java

Source

/**
 * @(#)TacksServiceImpl.java 2013-3-25
 *
 * Copyright 2013 Neusoft Group Ltd. All rights reserved.
 * Neusoft PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.neusoft.mid.clwapi.service.tacks;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.neusoft.mid.clwapi.common.ErrorConstant;
import com.neusoft.mid.clwapi.common.HttpConstant;
import com.neusoft.mid.clwapi.common.ModCommonConstant;
import com.neusoft.mid.clwapi.entity.tacks.TackDetailInfo;
import com.neusoft.mid.clwapi.entity.tacks.TackDetailResp;
import com.neusoft.mid.clwapi.entity.tacks.TackListInfo;
import com.neusoft.mid.clwapi.entity.tacks.TackListResp;
import com.neusoft.mid.clwapi.entity.tacks.TackResp;
import com.neusoft.mid.clwapi.exception.common.ApplicationException;
import com.neusoft.mid.clwapi.mapper.TacksMapper;
import com.neusoft.mid.clwapi.tools.BusinessUtil;
import com.neusoft.mid.clwapi.tools.CheckRequestParam;
import com.neusoft.mid.clwapi.tools.JacksonUtils;
import com.neusoft.mid.clwapi.tools.TimeUtil;

/**
 * @author <a href="mailto:suyingtao@neusoft.com">majch </a>
 * @version $Revision 1.0 $ 2013-3-25 ?7:52:17
 */
public class TacksServiceImpl implements TacksService {

    /**
     * .
     */
    private static Logger logger = LoggerFactory.getLogger(ModCommonConstant.LOGGER_NAME);

    @Autowired
    private TacksMapper tcMapper;

    /**
     * ???.
     * 
     * @param token
     *            ?.
     * 
     * @param vin
     *            vin?.
     * 
     * @param date
     *            ,?yyyymmdd.
     * @return ??.
     */
    @Override
    public Response getVinTacksList(String token, String vin, String date) {
        vin = StringUtils.strip(vin);
        date = StringUtils.strip(date);
        logger.info("-vin?:" + vin + ",:" + date);

        try {
            TimeUtil.parseStringToDate(date, HttpConstant.DAY_FORMAT);
        } catch (ParseException e) {
            logger.error(
                    "-" + date + "?yyyyMMdd?" + e.getMessage());
            throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
        }

        List<TackListInfo> resultList = tcMapper.getCarRunRecs(date, vin);
        if (CheckRequestParam.isEmpty(resultList)) {
            logger.info("-VIN:" + vin + "" + date
                    + "");
            return Response.status(Response.Status.NO_CONTENT).header(HttpHeaders.CACHE_CONTROL, "no-store")
                    .header("Pragma", "no-cache").build();
        }

        TackListResp resp = new TackListResp();
        resp.setResultList(resultList);

        return Response.ok(JacksonUtils.toJsonRuntimeException(resp)).header(HttpHeaders.CACHE_CONTROL, "no-store")
                .header("Pragma", "no-cache").build();
    }

    /**
     * ??.
     * 
     * @param token
     *            ?.
     * 
     * @param vin
     *            vin?.
     * 
     * @param startTime
     *            ,?yyyymmddhh24miss.
     * 
     * @param endTime
     *            ,?yyyymmddhh24miss.
     * @return ?.
     */
    @Override
    public Response getVinTacksDetail(String token, String vin, String startTime, String endTime) {
        vin = StringUtils.strip(vin);
        startTime = StringUtils.strip(startTime);
        endTime = StringUtils.strip(endTime);
        logger.info("-vin?:" + vin + ",:" + startTime + ",:"
                + endTime);

        Date sTime;
        Date eTime;
        try {
            sTime = TimeUtil.parseStringToDate(startTime, HttpConstant.TIME_FORMAT);
            eTime = TimeUtil.parseStringToDate(endTime, HttpConstant.TIME_FORMAT);
        } catch (ParseException e) {
            logger.info("-?" + startTime + "" + endTime
                    + "??yyyyMMddHHmmss");
            throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
        }

        if (sTime.after(eTime)) {
            logger.info("-?" + startTime + "" + endTime
                    + ",?");
            throw new ApplicationException(ErrorConstant.ERROR10003, Response.Status.BAD_REQUEST);
        }

        List<TackDetailInfo> resultList = getTackPonits(vin, startTime, endTime);
        if (CheckRequestParam.isEmpty(resultList)) {
            logger.info("-VIN:" + vin + "" + startTime + "-"
                    + endTime + "");
            return Response.status(Response.Status.NO_CONTENT).header(HttpHeaders.CACHE_CONTROL, "no-store")
                    .header("Pragma", "no-cache").build();
        }
        TackDetailResp resp = new TackDetailResp();
        resp.setResultList(resultList);

        return Response.ok(JacksonUtils.toJsonRuntimeException(resp)).header(HttpHeaders.CACHE_CONTROL, "no-store")
                .header("Pragma", "no-cache").build();
    }

    /**
     * ???.
     * 
     * @param vin
     *            vin?.
     * 
     * @param sTime
     *            ,?yyyymmddhh24miss.
     * 
     * @param eTime
     *            ?,?yyyymmddhh24miss.
     * @return ?.
     */
    private List<TackDetailInfo> getTackPonits(String vin, String sTime, String eTime) {
        List<TackDetailInfo> tempList = tcMapper.getTackPonits(sTime, eTime, vin);
        if (CheckRequestParam.isEmpty(tempList)) {
            return null;
        }

        String doorPriorFlag = "0";
        String recordID = "";
        List<TackDetailInfo> resultList = new ArrayList<TackDetailInfo>();
        for (TackDetailInfo obj : tempList) {
            // ???
            if (!HttpConstant.EVENT_TYPE_ST.equals(obj.getAlarmEventType())) {
                //tempList???????????
                if (!recordID.equals(obj.getId())) {
                    String[] state = BusinessUtil.decodeState(obj.getAccFlg());
                    String alarmTypeID = obj.getAlarmTypeID();

                    //??
                    if (CheckRequestParam.isEmpty(alarmTypeID) || HttpConstant.EVENT_TYPE_CAR.equals(alarmTypeID)) {
                        if ("1".equals(state[0]) && "0".equals(doorPriorFlag)) {
                            alarmTypeID = HttpConstant.ALARM_DOOR_PLAT;
                            obj.setAlarmTypeID(HttpConstant.ALARM_DOOR_PLAT);
                            obj.setAlarmEventType(HttpConstant.EVENT_TYPE_DOOR);
                        }
                    }
                    //?
                    if (CheckRequestParam.isEmpty(alarmTypeID)) {
                        delNeedlessInfo(obj);
                    } else {//
                            // ?
                        obj.setDirection(BusinessUtil.decodeDirction(obj.getAngle()));
                        // ??
                        obj.setAccFlg(state[2]);
                        // ??
                        obj.setLocState(state[1]);
                    }
                    if (!"".equals(state[0])) {
                        doorPriorFlag = state[0];
                    }
                    resultList.add(obj);
                    recordID = obj.getId();
                }
            } else {
                resultList.add(obj);
            }
        }
        return resultList;
    }

    /**
     * ?value.
     * 
     * @param obj
     *            
     * @return  .
     */
    private TackDetailInfo delNeedlessInfo(TackDetailInfo obj) {
        obj.setSpeed(null);
        obj.setEngSpeed(null);
        obj.setPassengerNumber(null);
        obj.setLimitNumber(null);
        obj.setOil(null);
        obj.setAccFlg(null);
        return obj;
    }

    /**
     * ?TackDetailInfo.
     * 
     * @param obj
     *            
     * @return ?.
     */
    private TackDetailInfo copyInfo(TackDetailInfo obj) {
        TackDetailInfo other = new TackDetailInfo();
        other.setTime(obj.getTime());
        other.setLatitude(obj.getLatitude());
        other.setLongitude(obj.getLongitude());
        other.setLocState(obj.getLocState());
        other.setSpeed(obj.getSpeed());
        other.setAngle(obj.getAngle());
        other.setDirection(obj.getDirection());
        other.setEngSpeed(obj.getEngSpeed());
        other.setPassengerNumber(obj.getPassengerNumber());
        other.setLimitNumber(obj.getLimitNumber());
        other.setOil(obj.getOil());
        other.setAccFlg(obj.getAccFlg());
        return other;
    }

    /**
     * ???.
     * 
     * @param token
     *            ?.
     * 
     * @param vin
     *            vin?.
     * 
     * @param alarmTime
     *            ,?yyyymmddhh24miss.
     * 
     * @param alarmID
     *            ID.
     * @return .
     */
    @Override
    public Response getVinTackByAlarm(String token, String alarmID, String vin, String alarmTime) {
        vin = StringUtils.strip(vin);
        alarmID = StringUtils.strip(alarmID);
        alarmTime = StringUtils.strip(alarmTime);
        logger.info("vin?:" + vin + ",:" + alarmTime + ",ID:" + alarmID);

        try {
            TimeUtil.parseStringToDate(alarmTime, HttpConstant.TIME_FORMAT);
        } catch (ParseException e) {
            logger.info("-?" + alarmTime
                    + "??yyyyMMddHHmmss");
            throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
        }

        List<TackResp> trackList = tcMapper.getRunRecsByTime(vin, alarmTime);
        if (CheckRequestParam.isEmpty(trackList)) {
            logger.info("-VIN:" + vin + "" + alarmTime
                    + "");
            return Response.status(Response.Status.NO_CONTENT).header(HttpHeaders.CACHE_CONTROL, "no-store")
                    .header("Pragma", "no-cache").build();
        }

        TackResp driveRec = trackList.get(0);
        return Response.ok(JacksonUtils.toJsonRuntimeException(driveRec))
                .header(HttpHeaders.CACHE_CONTROL, "no-store").header("Pragma", "no-cache").build();
    }
}