com.neusoft.mid.clwapi.service.pushRule.PushRuleServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.neusoft.mid.clwapi.service.pushRule.PushRuleServiceImpl.java

Source

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

import java.text.ParseException;
import java.util.ArrayList;
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.entity.pushRule.PushRuleInfo;
import com.neusoft.mid.clwapi.entity.pushRule.PushRuleResp;
import com.neusoft.mid.clwapi.exception.common.ApplicationException;
import com.neusoft.mid.clwapi.mapper.OauthMapper;
import com.neusoft.mid.clwapi.mapper.PushRuleMapper;
import com.neusoft.mid.clwapi.tools.BeanUtil;
import com.neusoft.mid.clwapi.tools.JacksonUtils;

/**
 * @author <a href="mailto:yi_liu@neusoft.com">yi_liu </a>
 * @version $Revision 1.0 $ 2013-4-8 ?9:43:48
 */
public class PushRuleServiceImpl implements PushRuleService {

    @Context
    private MessageContext context;

    @Autowired
    private PushRuleMapper iPushRuleMapper;

    @Autowired
    private OauthMapper oauthMapper;

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

    /**
     * ???
     * 
     * @param token
     *            
     * @param eTag
     *            ?
     * @return ?
     */
    @Override
    public Object getRule(String token, String eTag) {
        // ??
        boolean checkEtag = true;
        // ?ID
        String usrId = context.getHttpHeaders().getHeaderString("usr_id");
        String enId = context.getHttpHeaders().getHeaderString("enterprise_id");
        String plaETag = context.getHttpHeaders().getHeaderString("rule_ETag");
        logger.info("???[" + usrId + "]?");
        logger.info("?ETag:" + eTag);
        logger.info("??ETag:" + plaETag);

        if (eTag == null) {
            logger.error("If-None-Match");
            throw new ApplicationException(ErrorConstant.ERROR10001, Response.Status.BAD_REQUEST);
        }

        // ?
        Date dateTer = null;
        // ??
        Date datePla = null;

        // 
        if (!eTag.equals("0")) {
            try {
                dateTer = BeanUtil.checkTimeForm(eTag, HttpConstant.TIME_FORMAT);
            } catch (ParseException e) {
                logger.error("??" + e.getMessage());
                throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
            }
            // ??
            try {
                datePla = BeanUtil.checkTimeForm(plaETag, HttpConstant.TIME_FORMAT);
            } catch (ParseException e) {
                logger.error("???" + e.getMessage());
                throw new ApplicationException(ErrorConstant.ERROR90000, Response.Status.INTERNAL_SERVER_ERROR);
            }
        } else {
            // ???
            checkEtag = false;
        }

        logger.info("??");
        // ??
        List<PushRuleInfo> list = iPushRuleMapper.getPushRules(usrId, enId);
        logger.info("???");
        logger.debug("list = " + (list == null ? "NULL" : list.size()));

        // ?
        if (list == null || list.size() == 0) {

            logger.info("?????");
            // ??
            List<PushRuleInfo> enList = iPushRuleMapper
                    .getEnterpriseRule(context.getHttpHeaders().getHeaderString("enterprise_id"));
            // ???
            if (enList == null || enList.size() == 0) {
                logger.error("??");
                throw new ApplicationException(ErrorConstant.ERROR10104, Response.Status.NOT_FOUND);
            }
            logger.info("??");

            // ?UUID
            String[] uuids = BeanUtil.getUUIDs(enList.size());

            // ????
            Iterator<PushRuleInfo> it = enList.iterator();
            logger.info("??");
            int i = 0;
            // 
            List<PushRuleInfo> temp = new ArrayList<PushRuleInfo>();
            while (it.hasNext()) {
                PushRuleInfo iPushRuleInfo = it.next();
                // ?
                iPushRuleInfo.setUsrId(usrId);
                iPushRuleInfo.setRuleId(uuids[i++]);
                temp.add(iPushRuleInfo);
            }

            logger.debug("???:");
            if (logger.isDebugEnabled()) {
                it = temp.iterator();
                while (it.hasNext()) {
                    PushRuleInfo iPushRuleInfo = it.next();
                    logger.debug(iPushRuleInfo.toString());
                }
            }

            // ???
            iPushRuleMapper.insertPersonalRule(temp);

            // ???
            list = iPushRuleMapper.getPushRules(usrId, enId);
            if (list == null || list.size() == 0) {
                logger.error("??");
                throw new ApplicationException(ErrorConstant.ERROR10010, Response.Status.NOT_FOUND);
            }
            logger.info("?");
            plaETag = list.get(0).getOperateTime();
            logger.debug("??" + plaETag);
            // ??
            upUserETag(token, usrId, plaETag);
            logger.info("???");
        } else {
            logger.debug("?");
            if (logger.isDebugEnabled()) {
                Iterator<PushRuleInfo> it = list.iterator();
                int i = 0;
                while (it.hasNext()) {
                    PushRuleInfo iPushRuleInfo = it.next();
                    logger.debug(++i + iPushRuleInfo.toString());
                }
            }
            // ??
            String e = list.get(0).getOperateTime();
            logger.info("????");
            logger.debug("???" + e);
            logger.debug("??" + plaETag);
            // ?
            if (!StringUtils.equals(e, plaETag)) {
                logger.info("?");
                // ??ETag
                upUserETag(token, usrId, e);
                plaETag = e;
            }
        }

        logger.info("???");

        // ?
        if ((list == null || list.size() == 0) && !plaETag.equals("0")) {
            logger.info("??");
            return Response.noContent().build();
        } else if (checkEtag && dateTer.compareTo(datePla) == 0) {
            // ??
            logger.info("???");
            return Response.notModified().build();
        } else if (checkEtag && dateTer.compareTo(datePla) > 0) {
            logger.error("???");
            throw new ApplicationException(ErrorConstant.ERROR10103, Response.Status.BAD_REQUEST);
        }

        // ?
        PushRuleResp iPushRuleResp = new PushRuleResp();
        iPushRuleResp.seteTag(plaETag);
        iPushRuleResp.setRuleContent(list);
        return JacksonUtils.toJsonRuntimeException(iPushRuleResp);
    }

    /**
     * ??.
     * 
     * @param token
     *            ??. edit by majch,mybatis?.
     * @param userID
     *            ID
     * @param ETag
     *            ??.
     */
    private void upUserETag(String token, String userID, String ETag) {
        oauthMapper.updateUserRuleEtag(token, ETag, userID);
    }

    /**
     * ??
     * 
     * @param token
     *            
     * @param content
     *            ??
     * @return ?
     */
    @Override
    public String setRule(String token, String content) {
        logger.info("??");
        @SuppressWarnings("unchecked")
        Map<String, List<Map<String, String>>> tm = (Map<String, List<Map<String, String>>>) JacksonUtils
                .jsonToMapRuntimeException(content);
        List<Map<String, String>> list = tm.get("rule_content");
        // ?ID
        String usrId = context.getHttpHeaders().getHeaderString("usr_id");
        String enId = context.getHttpHeaders().getHeaderString("enterprise_id");
        String plaETag = context.getHttpHeaders().getHeaderString("rule_ETag");

        // ??
        if (list == null || list.size() == 0) {
            logger.error("????");
            throw new ApplicationException(ErrorConstant.ERROR10001, Response.Status.BAD_REQUEST);
        }
        Iterator<Map<String, String>> it = list.iterator();
        List<Map<String, String>> temp = new ArrayList<Map<String, String>>();
        while (it.hasNext()) {
            Map<String, String> map = it.next();
            if (StringUtils.isEmpty(map.get("rule_id")) || StringUtils.isEmpty(map.get("on_off"))
                    || StringUtils.isEmpty(map.get("flag"))) {
                logger.error("????");
                throw new ApplicationException(ErrorConstant.ERROR10001, Response.Status.BAD_REQUEST);
            } else if (!StringUtils.equals(map.get("on_off"), "0") && !StringUtils.equals(map.get("on_off"), "1")) {
                logger.error("????on_off?01");
                throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
            } else if (!StringUtils.equals(map.get("flag"), "0") && !StringUtils.equals(map.get("flag"), "1")) {
                logger.error("????flag?01");
                throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST);
            }
            Map<String, String> m = new HashMap<String, String>(map);
            m.put("usrId", usrId);
            temp.add(m);
        }
        list = temp;
        logger.info("???");
        // ?
        iPushRuleMapper.updatePersonalRule(list);
        logger.info("??");

        // ?
        List<PushRuleInfo> t = iPushRuleMapper.getPushRules(usrId, enId);
        logger.info("??");

        // ???
        String newEtag = t.get(0).getOperateTime();
        logger.debug("[" + newEtag + "]?[" + plaETag + "]");

        logger.info("?");
        // 
        upUserETag(token, usrId, newEtag);
        logger.info("??" + newEtag);

        // ?
        Map<String, String> respMap = new HashMap<String, String>();
        respMap.put("ETag", newEtag);
        return JacksonUtils.toJsonRuntimeException(respMap);
    }
}