Java tutorial
/** * @(#)AlarmServiceImpl.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.alarm; import java.text.ParseException; import java.util.ArrayList; import java.util.Date; 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.alarm.AlarmInfo; import com.neusoft.mid.clwapi.entity.alarm.AlarmRequ; import com.neusoft.mid.clwapi.entity.alarm.AlarmResp; import com.neusoft.mid.clwapi.exception.common.ApplicationException; import com.neusoft.mid.clwapi.mapper.AlarmMapper; import com.neusoft.mid.clwapi.mapper.CommonMapper; 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 ?9:41:30 */ public class AlarmServiceImpl implements AlarmService { private Logger logger = LoggerFactory.getLogger(AlarmServiceImpl.class); @Autowired private AlarmMapper iAlarmMapper; @Autowired private CommonMapper iCommonMapper; @Context private MessageContext context; /** * ??? * * @param token * token * @param body * ? * @return */ @Override public Object getAlarmList(String token, String body) { logger.info("???"); String organizationId = context.getHttpHeaders().getHeaderString(UserInfoKey.ORGANIZATION_ID); // ?? AlarmRequ iAlarmRequ = JacksonUtils.fromJsonRuntimeException(body, AlarmRequ.class); // ?? iAlarmRequ.setEndTime(StringUtils.strip(iAlarmRequ.getEndTime())); iAlarmRequ.setStartTime(StringUtils.strip(iAlarmRequ.getStartTime())); iAlarmRequ.setNum(StringUtils.strip(iAlarmRequ.getNum())); iAlarmRequ.setOperat(StringUtils.strip(iAlarmRequ.getOperat())); iAlarmRequ.setOrganizationId(organizationId); // ?? logger.info("??"); if (StringUtils.isEmpty(iAlarmRequ.getOperat()) || StringUtils.isEmpty(iAlarmRequ.getNum()) || StringUtils.isEmpty(iAlarmRequ.getType())) { logger.error("??"); throw new ApplicationException(ErrorConstant.ERROR10001, Response.Status.BAD_REQUEST); } else if (StringUtils.isEmpty(iAlarmRequ.getStartTime()) || StringUtils.isEmpty(iAlarmRequ.getEndTime())) { logger.error("????"); throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST); } else if (HttpConstant.TIME_ZERO.equals(iAlarmRequ.getStartTime()) && HttpConstant.TIME_ZERO.equals(iAlarmRequ.getEndTime())) { logger.error("?????0"); throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST); } else if (HttpConstant.TIME_24_HOURS_AGO.equalsIgnoreCase(iAlarmRequ.getStartTime()) && !HttpConstant.TIME_ZERO.equals(iAlarmRequ.getEndTime())) { logger.error( "??-hh24????0"); throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST); } else if (!StringUtils.isNumeric(iAlarmRequ.getNum())) { logger.error("??num?"); throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST); } else if (!StringUtils.isEmpty(iAlarmRequ.getPage()) && !StringUtils.isNumeric(iAlarmRequ.getPage())) { logger.error("??page?"); throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST); } // page?0 iAlarmRequ.setPage(StringUtils.isEmpty(iAlarmRequ.getPage()) ? HttpConstant.ZERO : iAlarmRequ.getPage()); // ??? int page = Integer.valueOf(iAlarmRequ.getPage()); int num = Integer.valueOf(iAlarmRequ.getNum()); // ???? if (page < 0) { logger.error("??page?0"); throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST); } // ? // if (iAlarmRequ.getOperat().equals(HttpConstant.ALARMPORT_OPERATE_UNTREATED)) { iAlarmRequ.setOperat(HttpConstant.ALARMPORT_OPERATE_UNTREATED_DB); } else if (iAlarmRequ.getOperat().equals(HttpConstant.ALARMPORT_OPERATE_TREATED)) { iAlarmRequ.setOperat(HttpConstant.ALARMPORT_OPERATE_TREATED_DB); } else if (iAlarmRequ.getOperat().equals(HttpConstant.ALARMPORT_OPERATE_ALL)) { iAlarmRequ.setOperat(HttpConstant.ALARMPORT_OPERATE_ALL_DB); } else { logger.error("???operat?0?1?2"); throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST); } if (iAlarmRequ.getType().equals(HttpConstant.ALARMPORT_TYPE_OVERSPEED)) { iAlarmRequ.setSpeedFlag(true); iAlarmRequ.setOverLoadFlag(null); } else if (iAlarmRequ.getType().equals(HttpConstant.ALARMPORT_TYPE_OVERLOAD)) { iAlarmRequ.setSpeedFlag(null); iAlarmRequ.setOverLoadFlag(true); } else if (iAlarmRequ.getType().equals(HttpConstant.ALARMPORT_TYPE_ALL)) { iAlarmRequ.setSpeedFlag(null); iAlarmRequ.setOverLoadFlag(null); } else { logger.error("???type?0?1?2"); throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST); } // ???? if (!HttpConstant.TIME_ZERO.equals(iAlarmRequ.getStartTime()) && !HttpConstant.TIME_24_HOURS_AGO.equalsIgnoreCase(iAlarmRequ.getStartTime())) { try { TimeUtil.parseStringToDate(iAlarmRequ.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(iAlarmRequ.getEndTime())) { try { TimeUtil.parseStringToDate(iAlarmRequ.getEndTime(), HttpConstant.TIME_FORMAT); } catch (ParseException e) { logger.error("?????" + e.getMessage()); throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST); } } logger.info("???"); // ? if (HttpConstant.TIME_ZERO.equals(iAlarmRequ.getStartTime())) { iAlarmRequ.setStartTime(null); } else if (HttpConstant.TIME_24_HOURS_AGO.equalsIgnoreCase(iAlarmRequ.getStartTime())) { Date dBTime = iCommonMapper.getDBTime(); Date oneDayAgo = TimeUtil.get24Ago(dBTime); String startTime = TimeUtil.formatDateToString(oneDayAgo, HttpConstant.TIME_FORMAT); iAlarmRequ.setStartTime(startTime); } if (HttpConstant.TIME_ZERO.equals(iAlarmRequ.getEndTime())) { iAlarmRequ.setEndTime(null); } iAlarmRequ.setEnId(context.getHttpHeaders().getHeaderString(UserInfoKey.ENTERPRISE_ID)); // ? if (page != 0) { iAlarmRequ.setStartRow(String.valueOf(page * num - num)); iAlarmRequ.setEndRow(String.valueOf(page * num)); } else if (num != 0) { iAlarmRequ.setStartRow(HttpConstant.ZERO); iAlarmRequ.setEndRow(String.valueOf(num)); } else { iAlarmRequ.setStartRow(null); iAlarmRequ.setEndRow(null); } logger.info("????"); // ? List<AlarmInfo> temp = iAlarmMapper.getAlarmList(iAlarmRequ); logger.info("??"); // if (temp == null || temp.size() == 0) { logger.info("??"); return Response.noContent().build(); } logger.info("??"); if (logger.isDebugEnabled()) { logger.debug("List size:" + temp.size()); Iterator<AlarmInfo> it = temp.iterator(); while (it.hasNext()) { logger.debug(it.next().toString()); } } // List<AlarmInfo> content = new ArrayList<AlarmInfo>(); Iterator<AlarmInfo> it = temp.iterator(); while (it.hasNext()) { AlarmInfo a = it.next(); if (!StringUtils.isEmpty(a.getSpeeding())) { a.setAlarmCont(a.getSpeeding()); } else if (!StringUtils.isEmpty(a.getPhotoId())) { a.setAlarmCont(a.getPhotoId()); } else { a.setAlarmCont(null); } content.add(a); } // AlarmResp iAlarmResp = new AlarmResp(); iAlarmResp.setContent(content); logger.info("????"); return JacksonUtils.toJsonRuntimeException(iAlarmResp); } /** * ??API * * @param token * token * @param warnId * ID * @param body * ?? */ @Override public String dealAlarm(String token, String warnId, String alarmTime, String body) { logger.info("??"); @SuppressWarnings("unchecked") Map<String, String> map = (Map<String, String>) JacksonUtils.jsonToMapRuntimeException(body); logger.info("ID" + warnId); logger.info("?" + alarmTime); logger.info("??[" + map.get(HttpConstant.ALARMPORT_DEAL_KEY) + "]"); // ???? if (StringUtils.isEmpty(warnId) || StringUtils.isEmpty(alarmTime)) { logger.error("??"); throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST); } String usrId = context.getHttpHeaders().getHeaderString(UserInfoKey.USR_ID); logger.info("ID" + usrId); logger.info("??"); logger.info("????"); // ?? iAlarmMapper.dealAlarm(warnId, map.get(HttpConstant.ALARMPORT_DEAL_KEY), usrId, alarmTime); logger.info("??"); logger.info("???"); return HttpConstant.RESP_200; } /** * ???API * * @param token * * @param body * * @return ?? */ @Override public Object getAlarmInfo(String token, String body) { logger.info("????"); AlarmRequ iAlarmRequ = JacksonUtils.fromJsonRuntimeException(body, AlarmRequ.class); // ?? iAlarmRequ.setAlarmId(StringUtils.strip(iAlarmRequ.getAlarmId())); iAlarmRequ.setConfTime(StringUtils.strip(iAlarmRequ.getConfTime())); // ? if (StringUtils.isEmpty(iAlarmRequ.getAlarmId()) || StringUtils.isEmpty(iAlarmRequ.getConfTime()) || StringUtils.isEmpty(iAlarmRequ.getAlarmTime())) { logger.error("???"); throw new ApplicationException(ErrorConstant.ERROR10001, Response.Status.BAD_REQUEST); } // ?? if (iAlarmRequ.getConfTime().equals(HttpConstant.TIME_ZERO)) { iAlarmRequ.setConfTime(null); } try { if (iAlarmRequ.getConfTime() != null) { TimeUtil.parseStringToDate(iAlarmRequ.getConfTime(), HttpConstant.TIME_FORMAT); } TimeUtil.parseStringToDate(iAlarmRequ.getAlarmTime(), HttpConstant.TIME_FORMAT); } catch (ParseException e) { logger.error("???"); throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST); } logger.info("???"); logger.info("???"); // ??? AlarmInfo iAlarmInfo = iAlarmMapper.getAlarmDealInfo(iAlarmRequ); logger.info("??"); if (iAlarmInfo == null) { logger.error("????"); return Response.notModified().build(); } logger.info("????"); // ???? logger.debug(iAlarmInfo.toString()); logger.info(""); logger.info("?????"); return JacksonUtils.toJsonRuntimeException(iAlarmInfo); } }