com.neusoft.mid.clwapi.service.photo.PhotoServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.neusoft.mid.clwapi.service.photo.PhotoServiceImpl.java

Source

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

import java.text.ParseException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

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

import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.ext.MessageContext;
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.UserInfoKey;
import com.neusoft.mid.clwapi.entity.photo.PhotoInfo;
import com.neusoft.mid.clwapi.entity.photo.PhotoRequ;
import com.neusoft.mid.clwapi.entity.photo.PhotoResp;
import com.neusoft.mid.clwapi.entity.photo.PhotoSignInfo;
import com.neusoft.mid.clwapi.entity.photo.PhotoSignRequ;
import com.neusoft.mid.clwapi.exception.common.ApplicationException;
import com.neusoft.mid.clwapi.mapper.PhotoMapper;
import com.neusoft.mid.clwapi.process.delivermsg.DeliverMsgResult;
import com.neusoft.mid.clwapi.process.delivermsg.SendDeliverMsgService;
import com.neusoft.mid.clwapi.tools.BeanUtil;
import com.neusoft.mid.clwapi.tools.JacksonUtils;
import com.neusoft.mid.clwapi.tools.TimeUtil;

/**
 * @author <a href="mailto:yi_liu@neusoft.com">yi_liu </a>
 * @version $Revision 1.0 $ 2013-4-9 ?3:19:49
 */
public class PhotoServiceImpl implements PhotoService {
    @Autowired
    private PhotoMapper iPhotoMapper;

    @Autowired
    private SendDeliverMsgService iSendDeliverMsgService;

    @Context
    private MessageContext context;

    private Logger logger = LoggerFactory.getLogger(PhotoServiceImpl.class);

    /**
     * ??API
     * 
     * @param token
     *            token
     * @param body
     *            ?
     * @return
     */
    @Override
    public Object getPhotoList(String token, String body) {
        logger.info("???");

        String organizationId = context.getHttpHeaders().getHeaderString(UserInfoKey.ORGANIZATION_ID);

        // ??
        PhotoRequ iPhotoRequ = JacksonUtils.fromJsonRuntimeException(body, PhotoRequ.class);
        // ??
        iPhotoRequ.setEndTime(StringUtils.strip(iPhotoRequ.getEndTime()));
        iPhotoRequ.setStartTime(StringUtils.strip(iPhotoRequ.getStartTime()));
        iPhotoRequ.setNum(StringUtils.strip(iPhotoRequ.getNum()));
        iPhotoRequ.setType(StringUtils.strip(iPhotoRequ.getType()));
        iPhotoRequ.setPage(StringUtils.strip(iPhotoRequ.getPage()));
        iPhotoRequ.setOrganizationId(organizationId);

        // ??
        logger.info("??");
        if (StringUtils.isEmpty(iPhotoRequ.getType()) || StringUtils.isEmpty(iPhotoRequ.getNum())
                || StringUtils.isEmpty(iPhotoRequ.getPage())) {
            logger.error("??");
            throw new ApplicationException(ErrorConstant.ERROR10001, Response.Status.BAD_REQUEST);
        } else if (StringUtils.isEmpty(iPhotoRequ.getStartTime()) || StringUtils.isEmpty(iPhotoRequ.getEndTime())) {
            logger.error("????");
            throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
        } else if (!iPhotoRequ.getType().equals(HttpConstant.PHOTOPORT_TYPE_ORDER)
                && !iPhotoRequ.getType().equals(HttpConstant.PHOTOPORT_TYPE_ALL)) {
            logger.error("??type?1?2");
            throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
        } else if (!StringUtils.isNumeric(iPhotoRequ.getNum())) {
            logger.error("??num?");
            throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
        } else if (!StringUtils.isNumeric(iPhotoRequ.getPage())) {
            logger.error("??page?");
            throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
        }

        // ???
        if (!HttpConstant.TIME_ZERO.equals(iPhotoRequ.getStartTime())) {
            try {
                TimeUtil.parseStringToDate(iPhotoRequ.getStartTime(), HttpConstant.TIME_FORMAT);
            } catch (ParseException e) {
                logger.error("???????", e);
                throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
            }
        } else {
            iPhotoRequ.setStartTime(null);
        }
        if (!HttpConstant.TIME_ZERO.equals(iPhotoRequ.getEndTime())) {
            try {
                TimeUtil.parseStringToDate(iPhotoRequ.getEndTime(), HttpConstant.TIME_FORMAT);
            } catch (ParseException e) {
                logger.error("????????", e);
                throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
            }
        } else {
            iPhotoRequ.setEndTime(null);
        }

        // ?????
        int page = Integer.valueOf(iPhotoRequ.getPage());
        int num = Integer.valueOf(iPhotoRequ.getNum());

        // ????
        if (page <= 0) {
            logger.error("??page?0");
            throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
        }

        logger.info("???");

        // ?
        iPhotoRequ.setVin(StringUtils.isEmpty(iPhotoRequ.getVin()) ? null : iPhotoRequ.getVin());
        iPhotoRequ.setOrgaId(StringUtils.isEmpty(iPhotoRequ.getOrgaId()) ? null : iPhotoRequ.getOrgaId());

        // ??
        // IDEN_ID
        iPhotoRequ.setUsrId(context.getHttpHeaders().getHeaderString(UserInfoKey.USR_ID));
        iPhotoRequ.setEnId(context.getHttpHeaders().getHeaderString(UserInfoKey.ENTERPRISE_ID));

        // ?
        iPhotoRequ.setStartRow(String.valueOf(page * num - num));
        iPhotoRequ.setEndRow(String.valueOf(page * num));
        // 2type??sql
        iPhotoRequ.setType(
                iPhotoRequ.getType().equals(HttpConstant.PHOTOPORT_TYPE_ALL) ? null : iPhotoRequ.getType());

        logger.info("?");
        logger.debug("?");
        logger.debug(iPhotoRequ.toString());

        // ?
        List<PhotoInfo> imgs = iPhotoMapper.getPhotoList(iPhotoRequ);
        logger.info("??");
        logger.debug("imgs=" + (imgs == null ? "NULL" : imgs.size()));

        if (imgs == null || imgs.size() == 0) {
            logger.info("?");
            return Response.noContent().build();
        }

        logger.debug("?");
        if (logger.isDebugEnabled()) {
            Iterator<PhotoInfo> it = imgs.iterator();
            while (it.hasNext()) {
                PhotoInfo temp = it.next();
                logger.debug(temp.toString());
            }
        }

        // 
        PhotoResp iPhotoResp = new PhotoResp();
        iPhotoResp.setImgs(imgs);
        logger.info("???");

        return JacksonUtils.toJsonRuntimeException(iPhotoResp);
    }

    /**
     * ??
     * 
     * @param token
     *            token
     * @param body
     *            ?
     * @return
     */
    @Override
    public Object getSignInfo(String token, String body) {
        logger.info("???");

        PhotoSignRequ iPhotoSignRequ = JacksonUtils.fromJsonRuntimeException(body, PhotoSignRequ.class);
        logger.info("ID" + iPhotoSignRequ.getImgId());
        logger.info("" + iPhotoSignRequ.getUpTime());
        logger.info("??");
        // ?
        if (StringUtils.isEmpty(iPhotoSignRequ.getImgId()) || StringUtils.isEmpty(iPhotoSignRequ.getUpTime())) {
            logger.error("??");
            throw new ApplicationException(ErrorConstant.ERROR10001, Response.Status.BAD_REQUEST);
        }

        // 0?
        if (iPhotoSignRequ.getUpTime().equals(HttpConstant.TIME_ZERO)) {
            iPhotoSignRequ.setUpTime(null);
        } else {
            // ?
            try {
                BeanUtil.checkTimeForm(iPhotoSignRequ.getUpTime(), HttpConstant.TIME_FORMAT);
            } catch (ParseException e) {
                logger.error("?" + e.getMessage());
                throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
            }
        }

        logger.info("??");
        // ??
        PhotoSignInfo iPhotoSignInfo = iPhotoMapper.getPhotoSign(iPhotoSignRequ);

        // ?
        if (iPhotoSignInfo == null || iPhotoSignInfo.getInfo() == null) {
            logger.info("?");
            return Response.noContent().build();
        }

        logger.info("????");
        logger.debug(iPhotoSignInfo.toString());

        logger.info("????");
        return JacksonUtils.toJsonRuntimeException(iPhotoSignInfo);
    }

    /**
     * ?
     * 
     * @param token
     *            token
     * @param body
     *            ?
     * @return
     */
    @Override
    public String signPhoto(String token, String body) {
        logger.info("?");
        boolean informFlag = false;

        logger.info("??");
        PhotoSignRequ iPhotoSignRequ = JacksonUtils.fromJsonRuntimeException(body, PhotoSignRequ.class);
        logger.info("ID" + iPhotoSignRequ.getImgId());
        logger.info("FLAG: " + iPhotoSignRequ.getFlag());
        logger.info("?" + iPhotoSignRequ.getInfo());

        // ?
        if (StringUtils.isEmpty(iPhotoSignRequ.getImgId()) || StringUtils.isEmpty(iPhotoSignRequ.getFlag())
                || StringUtils.isEmpty(iPhotoSignRequ.getVin())) {
            logger.error("??");
            throw new ApplicationException(ErrorConstant.ERROR10001, Response.Status.BAD_REQUEST);
        }

        if (iPhotoSignRequ.getFlag().equals(HttpConstant.SINGPHOTOPORT_FLAG_OVERLOAD)) {
            logger.info("");
            informFlag = true;
        } else if (iPhotoSignRequ.getFlag().equals(HttpConstant.SINGPHOTOPORT_FLAG_NORMAL)) {
            logger.info("");
            iPhotoSignRequ.setFlag(null);
        } else {
            logger.error("??flag?1?2");
            throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
        }
        logger.info("??");
        // ID
        iPhotoSignRequ.setUsrId(context.getHttpHeaders().getHeaderString(UserInfoKey.USR_ID));
        PhotoSignInfo iPhotoSignInfo = null;
        if (informFlag) {
            // ??
            iPhotoSignInfo = iPhotoMapper.getPhotoSign(iPhotoSignRequ);
        }
        // ??
        if (informFlag && iPhotoSignInfo == null) {
            logger.error("?ID" + iPhotoSignRequ.getImgId() + "?");
            throw new ApplicationException(ErrorConstant.ERROR10010, Response.Status.INTERNAL_SERVER_ERROR);
        }
        // ?
        if (informFlag && iPhotoSignInfo.getFlag().equals("1")) {
            logger.info("ID:" + iPhotoSignRequ.getImgId()
                    + "?,??");
            informFlag = false;
        }
        // 
        if (informFlag) {

            logger.info("??");
            DeliverMsgResult iDeliverMsgResult = iSendDeliverMsgService.sendMarkAlarmToCoreService(
                    iPhotoSignRequ.getVin(), HttpConstant.TERMINAL_ALARM_TYPE, iPhotoSignRequ.getImgId(),
                    context.getHttpHeaders().getHeaderString(UserInfoKey.ENTERPRISE_ID));
            if (null != iDeliverMsgResult && iDeliverMsgResult.getCode().equals(HttpConstant.NO_ERROR)) {
                logger.info("??");
            } else {
                logger.info("?");
                logger.error(
                        "????");
                throw new ApplicationException(ErrorConstant.ERROR10106, Response.Status.INTERNAL_SERVER_ERROR);
            }
        } else {
            logger.info("??");
        }

        logger.info("?");
        logger.debug(iPhotoSignRequ.toString());
        // ?
        iPhotoMapper.signPhoto(iPhotoSignRequ);
        logger.info("??");
        logger.info("?");
        String upTime = iPhotoMapper.getUpDate(iPhotoSignRequ);
        logger.info("??");
        logger.debug(upTime);

        // 
        Map<String, String> response = new HashMap<String, String>();
        response.put(HttpConstant.UP_TIME, upTime);

        logger.info("??");
        return JacksonUtils.toJsonRuntimeException(response);
    }
}