Java tutorial
/** * @(#)HomePageServiceImpl.java 2013-3-26 * * Copyright 2013 Neusoft Group Ltd. All rights reserved. * Neusoft PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package com.neusoft.mid.clwapi.service.homePage; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; 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.homePage.PageInfoResp; import com.neusoft.mid.clwapi.entity.homePage.WeatherInfoResp; import com.neusoft.mid.clwapi.entity.monitor.MonitorResp; import com.neusoft.mid.clwapi.exception.common.ApplicationException; import com.neusoft.mid.clwapi.mapper.HomePageMapper; import com.neusoft.mid.clwapi.memcached.MemcacheCache; import com.neusoft.mid.clwapi.memcached.MemcacheCacheManager; import com.neusoft.mid.clwapi.service.common.CarMonitorService; import com.neusoft.mid.clwapi.service.common.UsrOauthService; import com.neusoft.mid.clwapi.tools.JacksonUtils; import com.neusoft.mid.clwapi.tools.TimeUtil; import com.neusoft.mid.clwapi.tools.WeatherReport; /** * @author <a href="mailto:yi_liu@neusoft.com">yi_liu </a> * @version $Revision 1.0 $ 2013-3-26 ?12:37:32 */ public class HomePageServiceImpl implements HomePageService { private Logger logger = LoggerFactory.getLogger(HomePageServiceImpl.class); @Context private MessageContext context; @Autowired private UsrOauthService usrOauthService; @Autowired private HomePageMapper homePageMapper; @Autowired private CarMonitorService carMonitorService; @Autowired private MemcacheCacheManager memcacheCacheManager; /** * ?? * * @param token * * @param eTag * ????|??? * @return ? */ @Override public String getHomePageInfo(String token, String uptime) { logger.info("???"); logger.info("[ " + uptime + " ](??|?)"); logger.info("????"); // ?up_time try { Map<?, ?> map = JacksonUtils.jsonToMap(uptime); uptime = (String) map.get(HttpConstant.UP_TIME); } catch (Exception e) { throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST); } logger.info("??"); String usrId = context.getHttpHeaders().getHeaderString(UserInfoKey.USR_ID); String enId = context.getHttpHeaders().getHeaderString(UserInfoKey.ENTERPRISE_ID); String organizationId = context.getHttpHeaders().getHeaderString(UserInfoKey.ORGANIZATION_ID); String alarmParName1 = null; String alarmParName2 = null; String timea = null; String timeb = null; // ? Date[] etag = getEtag(uptime); // ? String yesterday = TimeUtil.getSysdateYesterday(); String now = TimeUtil.getSysdate(); Date zeroTime = TimeUtil.getTodayZero(); // ??? if (etag[1] == null) { alarmParName1 = HttpConstant.ALARM_PARPITION + now; alarmParName2 = HttpConstant.ALARM_PARPITION + yesterday; timeb = TimeUtil.formatDateToString(etag[1], HttpConstant.TIME_FORMAT); } else if (zeroTime.compareTo(etag[1]) <= 0) { alarmParName1 = HttpConstant.ALARM_PARPITION + now; alarmParName2 = null; timea = TimeUtil.formatDateToString(etag[1], HttpConstant.TIME_FORMAT); } else { alarmParName1 = HttpConstant.ALARM_PARPITION + now; alarmParName2 = HttpConstant.ALARM_PARPITION + yesterday; timeb = TimeUtil.formatDateToString(etag[1], HttpConstant.TIME_FORMAT); } String newsNumTime = null; // ?? newsNumTime = etag[0] == null ? null : TimeUtil.formatDateToString(etag[0], HttpConstant.TIME_FORMAT); logger.info("???time=" + newsNumTime); // ??? String newsNum = homePageMapper.getNewsNum(newsNumTime); logger.info("????num=" + newsNum); logger.info("??time=" + timeb); // ? String alarmNum = homePageMapper.getAlarmNum(timea, timeb, enId, alarmParName1, alarmParName2, organizationId); logger.info("???num=" + alarmNum); logger.info("??"); // ??24?? List<String> photoList = homePageMapper.getPhotoList(usrId, organizationId); logger.debug("???"); if (photoList == null || photoList.size() == 0) { logger.info(""); if (photoList == null) { photoList = new ArrayList<String>(); } } else { logger.info("??"); if (logger.isDebugEnabled()) { Iterator<String> it = photoList.iterator(); while (it.hasNext()) { logger.debug(it.next()); } } } logger.info("??ID????"); // ??ID???? MonitorResp iMonitorResp = carMonitorService.getEpCarNums(enId, organizationId); logger.info("??"); logger.info("??"); List<String> monthList = homePageMapper.getEpReportMonth(enId); logger.info("??"); logger.info("??? "); String epNum = homePageMapper.getEpReportNum(enId); logger.info("??"); // PageInfoResp iPageInfoResp = new PageInfoResp(); iPageInfoResp.setMsgNum(newsNum); iPageInfoResp.setWarnNum(alarmNum); iPageInfoResp.setPhotoContent(photoList.size() == 0 ? null : photoList); iPageInfoResp.setTotal(iMonitorResp.getCarNum()); iPageInfoResp.setRunning(iMonitorResp.getRunNum()); iPageInfoResp.setStop(iMonitorResp.getStopNum()); iPageInfoResp.setEpMonth(monthList.size() == 0 ? null : monthList.get(0)); iPageInfoResp.setServerDate(now); iPageInfoResp.setHasReport("0".equals(epNum) ? HttpConstant.ZERO : HttpConstant.ONE); logger.info("????"); return JacksonUtils.toJsonRuntimeException(iPageInfoResp); } /** * ?? * * @param region * ?(?uri) * @return ? */ @Override public String getWeather(String token, String region) { logger.info("???"); logger.info("??[ " + region + " ]?"); region = StringUtils.strip(region); try { region = URLDecoder.decode(region, HttpConstant.CHARSET); } catch (UnsupportedEncodingException e) { logger.error("URL?" + e.getMessage()); throw new ApplicationException(ErrorConstant.ERROR90000, Response.Status.INTERNAL_SERVER_ERROR); } logger.info("URL?[ " + region + " ]"); if (region == null || region.equals("null")) { logger.error("?????NULL"); throw new ApplicationException(ErrorConstant.ERROR10001, Response.Status.BAD_REQUEST); } //2014-03-14 by wuxja ? MemcacheCache cache = (MemcacheCache) memcacheCacheManager.getCache("CLW_API_CACHE"); WeatherInfoResp iWeatherInfoResp = null; logger.info("dd" + cache.get("WEATHER_" + region)); if (cache.get("WEATHER_" + region) != null) iWeatherInfoResp = (WeatherInfoResp) cache.get("WEATHER_" + region).get(); else { iWeatherInfoResp = WeatherReport.getWeatherInfo(region); iWeatherInfoResp.setValidSeconds(18000); cache.put("WEATHER_" + region, iWeatherInfoResp); } logger.info("????"); return JacksonUtils.toJsonRuntimeException(iWeatherInfoResp); } /** * ??? * * @param uptime * @return */ // ??? private Date[] getEtag(String uptime) { if (StringUtils.isEmpty(uptime)) { logger.error(""); throw new ApplicationException(ErrorConstant.ERROR10001, Response.Status.BAD_REQUEST); } // ? String[] s = StringUtils.split(uptime, "|"); Date[] d = new Date[s.length]; if (s.length != 2) { logger.error("????[" + s.length + "]"); throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST); } for (int i = 0; i < s.length; i++) { s[i] = StringUtils.strip(s[i]); if (!s[i].equals(HttpConstant.TIME_ZERO)) { try { d[i] = TimeUtil.parseStringToDate(s[i], HttpConstant.TIME_FORMAT); } catch (ParseException e) { logger.error("?" + e.getMessage()); throw new ApplicationException(ErrorConstant.ERROR10002, Response.Status.BAD_REQUEST); } } else { s[i] = null; } } return d; } }