com.greenline.guahao.web.module.home.controllers.json.area.JsonAreaController.java Source code

Java tutorial

Introduction

Here is the source code for com.greenline.guahao.web.module.home.controllers.json.area.JsonAreaController.java

Source

/**
 * Project: guahao-portal-web-home
 * 
 * File Created at 2012-11-26
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package com.greenline.guahao.web.module.home.controllers.json.area;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.greenline.guahao.biz.manager.area.AreaManager;
import com.greenline.guahao.biz.manager.area.dataobject.AreaDO;
import com.greenline.guahao.biz.manager.cache.JackJsonUtil;
import com.greenline.guahao.biz.manager.hrs.HospitalManager;
import com.greenline.guahao.biz.manager.hrs.dataobject.HospitalDO;
import com.greenline.guahao.web.module.common.annotation.MethodRemark;
import com.greenline.guahao.web.module.common.constants.JsonPathConstants;
import com.greenline.guahao.web.module.common.json.OperationJsonObject;
import com.greenline.guahao.web.module.common.utils.IpUtil;
import com.greenline.guahao.web.module.common.vo.ValueTextVO;
import com.greenline.guahao.web.module.home.controllers.expert.FastOrderProcess;

/**
 * @Type JsonAreaController
 * @Desc
 * @author jianyun.zheng
 * @date 2012-11-26
 * @Version V1.0
 */
@Controller
public class JsonAreaController {

    private static final Log logger = LogFactory.getLog(JsonAreaController.class);

    @Resource
    private FastOrderProcess fastOrderProcess;
    @Resource
    private JackJsonUtil jackjson;
    @Resource
    private AreaManager areaManager;
    @Resource
    private HttpServletRequest request;
    @Resource
    private HospitalManager hospitalManager;
    @Resource
    private JsonAreaProcess jsonAreaProcess;

    private static final String LOCATION_CITY_SUFFIX = "";

    private static final String LOCATION_PROVINCE_SUFFIX = "?";

    /**
     * ?json?
     * 
     * @param provinceId
     * @return List<ValueTextVO>
     */
    @MethodRemark(value = "remark=?json?,method=ajax,provinceId=?id")
    @RequestMapping(value = JsonPathConstants.J_AREA_CITYS_PATH, method = RequestMethod.GET)
    public @ResponseBody List<ValueTextVO> getCitysByJson(@RequestParam(value = "provinceId") String provinceId) {
        return jsonAreaProcess.getJsonCitys(provinceId);
    }

    /**
     * ?json?
     * 
     * @param provinceId
     * @return List<ValueTextVO>
     */
    @MethodRemark(value = "remark=??json?,method=ajax")
    @RequestMapping(value = JsonPathConstants.J_AREA_PROVINCES_PATH, method = RequestMethod.GET)
    public @ResponseBody List<ValueTextVO> getProvinceByJson() {
        return jsonAreaProcess.getJsonProvinces();
    }

    @MethodRemark(value = "remark=??json?,method=ajax")
    @RequestMapping(value = "/json/white/area/position", method = RequestMethod.GET)
    public @ResponseBody OperationJsonObject areaPosition() {
        OperationJsonObject json = new OperationJsonObject();
        Map<String, String> dataMap = new HashMap<String, String>();

        String position = StringUtils.EMPTY;
        try {
            // ip???
            position = areaManager.getAreaInfo(IpUtil.getIpAddr(request));
        } catch (Exception e) {
            logger.error("ip?", e);
        }

        String provinceName = StringUtils.EMPTY;
        String cityName = StringUtils.EMPTY;
        if (StringUtils.isNotBlank(position)) {
            if (StringUtils.contains(position, LOCATION_PROVINCE_SUFFIX)) {
                String[] positionArr = StringUtils.split(position, LOCATION_PROVINCE_SUFFIX);
                provinceName = positionArr[0];
                if (positionArr.length > 1) {
                    cityName = positionArr[1];
                    if (StringUtils.contains(cityName, LOCATION_CITY_SUFFIX)) {
                        cityName = StringUtils.substring(cityName, 0, cityName.length() - 1);
                    }
                }
            } else {
                if (StringUtils.contains(position, LOCATION_CITY_SUFFIX)) {
                    String[] positionArr = StringUtils.split(position, LOCATION_CITY_SUFFIX);
                    provinceName = positionArr[0];
                    if (positionArr.length > 1) {
                        cityName = positionArr[1];
                    }
                }
            }
        }

        if (StringUtils.isNotBlank(provinceName)) {
            dataMap.put("provice", provinceName);
        }

        if (StringUtils.isNotBlank(cityName)) {
            dataMap.put("city", cityName);
        }

        json.setData(dataMap);
        return json;
    }

    @MethodRemark(value = "remark=??jsonp?,method=ajax")
    @RequestMapping(value = "/json/white/area/position/jsonp", method = RequestMethod.GET)
    public @ResponseBody String getAreaPositionByJsonp() {
        String jsoncallback = request.getParameter("jsoncallback");

        String position = StringUtils.EMPTY;
        try {
            // ip???
            position = areaManager.getAreaInfo(IpUtil.getIpAddr(request));
        } catch (Exception e) {
            logger.error("ip?", e);
        }

        Map<String, String> dataMap = areaPosition(position);

        String result = "(" + jackjson.writeString(dataMap) + ")";
        if (StringUtils.isBlank(jsoncallback)) {
            result = "position" + result;
        } else {
            result = jsoncallback + result;
        }

        return result;
    }

    private Map<String, String> areaPosition(String position) {

        Map<String, String> dataMap = new HashMap<String, String>();

        String provinceName = StringUtils.EMPTY;
        String cityName = StringUtils.EMPTY;
        if (StringUtils.isNotBlank(position)) {
            if (StringUtils.contains(position, LOCATION_PROVINCE_SUFFIX)) {
                String[] positionArr = StringUtils.split(position, LOCATION_PROVINCE_SUFFIX);
                provinceName = positionArr[0];
                if (positionArr.length > 1) {
                    cityName = positionArr[1];
                    if (StringUtils.contains(cityName, LOCATION_CITY_SUFFIX)) {
                        cityName = StringUtils.substring(cityName, 0, cityName.length() - 1);
                    }
                }
            } else {
                if (StringUtils.contains(position, LOCATION_CITY_SUFFIX)) {
                    String[] positionArr = StringUtils.split(position, LOCATION_CITY_SUFFIX);
                    provinceName = positionArr[0];
                    if (positionArr.length > 1) {
                        cityName = positionArr[1];
                    }
                }
            }
        }

        if (StringUtils.isNotBlank(provinceName)) {
            dataMap.put("provice", provinceName);
        }

        if (StringUtils.isNotBlank(cityName)) {
            dataMap.put("city", cityName);
        }

        return dataMap;
    }

    @MethodRemark(value = "remark=?id???jsonp?,method=ajax")
    @RequestMapping(value = "/json/white/areainfo/hospid/jsonp", method = RequestMethod.GET)
    public @ResponseBody String getAreaInfoByHospIdJsonp(@RequestParam(value = "hospitalId") String hospitalId) {
        HospitalDO hospitalDO = hospitalManager.getHospital(hospitalId);
        HospitalDO hospital = new HospitalDO();
        hospital.setId(hospitalDO.getId());
        hospital.setName(hospitalDO.getName());
        hospital.setCity(hospitalDO.getCity());
        hospital.setCityid(hospitalDO.getCityid());
        hospital.setProvince(hospitalDO.getProvince());
        hospital.setProvinceid(hospitalDO.getProvinceid());

        String jsoncallback = request.getParameter("jsoncallback");
        String result = "(" + jackjson.writeString(hospital) + ")";
        if (StringUtils.isBlank(jsoncallback)) {
            result = "getInfoByHosp" + result;
        } else {
            result = jsoncallback + result;
        }

        return result;
    }

    @MethodRemark(value = "remark=?id???jsonp?,method=ajax")
    @RequestMapping(value = "/json/white/area/cityid/jsonp", method = RequestMethod.GET)
    public @ResponseBody String getAreaInfoByCityIdJsonp(@RequestParam(value = "cityId") String cityId) {
        AreaDO cityAarea = areaManager.getArea(cityId);
        AreaDO provinceArea = areaManager.getArea(cityAarea.getParentId());
        cityAarea.setParentName(provinceArea.getName());

        String jsoncallback = request.getParameter("jsoncallback");
        String result = "(" + jackjson.writeString(cityAarea) + ")";
        if (StringUtils.isBlank(jsoncallback)) {
            result = "getInfoByCity" + result;
        } else {
            result = jsoncallback + result;
        }

        return result;
    }

    @MethodRemark(value = "remark=??jsonp?")
    @RequestMapping(value = "/json/white/area/provinces/jsonp")
    public @ResponseBody String getProvincesByJsonp() {
        List<ValueTextVO> list = fastOrderProcess.getJsonProvinces();

        String jsoncallback = request.getParameter("jsoncallback");
        String result = "(" + jackjson.writeString(list) + ")";
        if (StringUtils.isBlank(jsoncallback)) {
            result = "provinces" + result;
        } else {
            result = jsoncallback + result;
        }

        return result;
    }

    @MethodRemark(value = "remark=?jsonp?,method=ajax,provinceId=?id")
    @RequestMapping(value = "/json/white/area/citys/jsonp")
    public @ResponseBody String getCitysByJsonp(@RequestParam(value = "provinceId") String provinceId) {
        List<ValueTextVO> list = fastOrderProcess.getJsonCitys(provinceId);

        String jsoncallback = request.getParameter("jsoncallback");
        String result = "(" + jackjson.writeString(list) + ")";
        if (StringUtils.isBlank(jsoncallback)) {
            result = "citys" + result;
        } else {
            result = jsoncallback + result;
        }

        return result;
    }

    @MethodRemark(value = "remark=??,method=ajax,provinceId=?id,cityId=id")
    @RequestMapping(value = "/json/white/area/hospitals/jsonp")
    public @ResponseBody String getHospitalsByJsonp(@RequestParam(value = "provinceId") String provinceId,
            @RequestParam(value = "cityId") String cityId) {
        List<ValueTextVO> list = fastOrderProcess.getHospitalsByArea(provinceId, cityId);

        String jsoncallback = request.getParameter("jsoncallback");
        String result = "(" + jackjson.writeString(list) + ")";
        if (StringUtils.isBlank(jsoncallback)) {
            result = "hospitals" + result;
        } else {
            result = jsoncallback + result;
        }

        return result;
    }

    @MethodRemark(value = "remark=?,method=ajax,hospitalId=id")
    @RequestMapping(value = "/json/white/hospDeptsAndClinicTypes/jsonp")
    public @ResponseBody String getHospDeptsAndClinicTypesByJsonp(
            @RequestParam(value = "hospitalId") String hospitalId) {
        Map<String, Object> map = fastOrderProcess.getDeptsAndCliTypes(hospitalId);

        String jsoncallback = request.getParameter("jsoncallback");
        String result = "(" + jackjson.writeString(map) + ")";
        if (StringUtils.isBlank(jsoncallback)) {
            result = "hospDeptsAndClinicTypes" + result;
        } else {
            result = jsoncallback + result;
        }

        return result;
    }
}