com.neusoft.mid.clwapi.service.news.NewsServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.neusoft.mid.clwapi.service.news.NewsServiceImpl.java

Source

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

import java.text.ParseException;
import java.util.Date;
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.news.CheckNewsInfo;
import com.neusoft.mid.clwapi.entity.news.CheckNewsRequ;
import com.neusoft.mid.clwapi.entity.news.NewsInfo;
import com.neusoft.mid.clwapi.entity.news.NewsRequ;
import com.neusoft.mid.clwapi.exception.common.ApplicationException;
import com.neusoft.mid.clwapi.mapper.CommonMapper;
import com.neusoft.mid.clwapi.mapper.NewsMapper;
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-13 ?11:34:16
 */
public class NewsServiceImpl implements NewsService {

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

    @Autowired
    private CommonMapper iCommonMapper;

    @Autowired
    private NewsMapper iNewsMapper;

    @Context
    private MessageContext context;

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

        // ??
        NewsRequ iNewsRequ = JacksonUtils.fromJsonRuntimeException(body, NewsRequ.class);
        // ??
        iNewsRequ.setEndTime(StringUtils.strip(iNewsRequ.getEndTime()));
        iNewsRequ.setStartTime(StringUtils.strip(iNewsRequ.getStartTime()));
        iNewsRequ.setNum(StringUtils.strip(iNewsRequ.getNum()));
        iNewsRequ.setType(StringUtils.strip(iNewsRequ.getType()));

        // ??
        logger.info("??");
        if (StringUtils.isEmpty(iNewsRequ.getType())) {
            logger.error("??");
            throw new ApplicationException(ErrorConstant.ERROR10001, Response.Status.BAD_REQUEST);
        } else if (StringUtils.isEmpty(iNewsRequ.getStartTime()) || StringUtils.isEmpty(iNewsRequ.getEndTime())) {
            logger.error("????");
            throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
        } else if (HttpConstant.TIME_15_DAY_AGO.equalsIgnoreCase(iNewsRequ.getStartTime())
                && !HttpConstant.TIME_ZERO.equals(iNewsRequ.getEndTime())) {
            logger.error(
                    "??-dd15????0");
            throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
        }

        // numNumnull??num?
        if (StringUtils.isEmpty(iNewsRequ.getNum())) {
            iNewsRequ.setNum(null);
        } else if (!StringUtils.isNumeric(iNewsRequ.getNum())) {
            logger.error("??num?");
            throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
        }

        // ????
        if (!HttpConstant.TIME_ZERO.equals(iNewsRequ.getStartTime())
                && !HttpConstant.TIME_15_DAY_AGO.equalsIgnoreCase(iNewsRequ.getStartTime())) {
            try {
                TimeUtil.parseStringToDate(iNewsRequ.getStartTime(), HttpConstant.TIME_FORMAT);
            } catch (ParseException e) {
                logger.error("????" + e.getMessage());
                throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
            }
        }
        if (!HttpConstant.TIME_ZERO.equals(iNewsRequ.getEndTime())) {
            try {
                TimeUtil.parseStringToDate(iNewsRequ.getEndTime(), HttpConstant.TIME_FORMAT);
            } catch (ParseException e) {
                logger.error("?????" + e.getMessage());
                throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
            }
        }

        if (HttpConstant.TIME_ZERO.equals(iNewsRequ.getStartTime())) {
            iNewsRequ.setStartTime(null);
        } else if (HttpConstant.TIME_15_DAY_AGO.equalsIgnoreCase(iNewsRequ.getStartTime())) {
            Date dBTime = iCommonMapper.getDBTime();
            Date fifteenDayAgo = TimeUtil.get15Ago(dBTime);
            String startTime = TimeUtil.formatDateToString(fifteenDayAgo, HttpConstant.TIME_FORMAT);
            iNewsRequ.setStartTime(startTime);
        }
        if (HttpConstant.TIME_ZERO.equals(iNewsRequ.getEndTime())) {
            iNewsRequ.setEndTime(null);
        }

        // Type
        if (iNewsRequ.getType().equalsIgnoreCase("-1")) {
            iNewsRequ.setType(null);
        } else if (iNewsRequ.getType().equals("0") || iNewsRequ.getType().equals("1")) {
            // ??????
        } else {
            logger.error("??type");
            throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
        }

        // 0

        logger.info("??");
        logger.info("???");

        // ???
        List<NewsInfo> list = iNewsMapper.getNewsList(iNewsRequ);
        logger.debug("list=" + (list == null ? "NULL" : list.size()));

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

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

        // ?
        Map<String, List<NewsInfo>> map = new HashMap<String, List<NewsInfo>>();
        map.put("news", list);

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

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

        CheckNewsRequ iCheckNewsRequ = JacksonUtils.fromJsonRuntimeException(body, CheckNewsRequ.class);
        // ???ID
        iCheckNewsRequ.setUsrId(context.getHttpHeaders().getHeaderString(UserInfoKey.USR_ID));

        // ??
        if (StringUtils.isBlank(iCheckNewsRequ.getNewsId()) || StringUtils.isBlank(iCheckNewsRequ.getNewsTime())
                || StringUtils.isBlank(iCheckNewsRequ.getNewsType())) {
            logger.error("??");
            throw new ApplicationException(ErrorConstant.ERROR10001, Response.Status.BAD_REQUEST);
        }

        // ?
        iCheckNewsRequ.setNewsId(StringUtils.strip(iCheckNewsRequ.getNewsId()));
        iCheckNewsRequ.setNewsTime(StringUtils.strip(iCheckNewsRequ.getNewsTime()));
        iCheckNewsRequ.setNewsType(StringUtils.strip(iCheckNewsRequ.getNewsType()));

        // ??
        logger.info("ID:" + iCheckNewsRequ.getUsrId());
        logger.info("?ID:" + iCheckNewsRequ.getNewsId());
        logger.info("??:" + iCheckNewsRequ.getNewsTime());
        logger.info("?:" + iCheckNewsRequ.getNewsType());

        logger.info("??");
        logger.info("?");

        // ?ID?ID??
        CheckNewsInfo iCheckNewsInfo = iNewsMapper.getCheckNewsInfo(iCheckNewsRequ);

        // ????
        if (iCheckNewsInfo == null) {
            logger.info("??");
            iNewsMapper.addCheckNewsInfo(iCheckNewsRequ);
            logger.info("?");
        } else {
            logger.info("???");
            // 
            iCheckNewsInfo.setCount(String.valueOf(Integer.valueOf(iCheckNewsInfo.getCount()) + 1));
            logger.debug("??" + iCheckNewsInfo.toString());
            iNewsMapper.updateCheckNewsInfo(iCheckNewsInfo);
            logger.info("?");
        }

        logger.info("????");

        return HttpConstant.RESP_200;
    }
}