com.wonders.bud.freshmommy.web.user.controller.UserController.java Source code

Java tutorial

Introduction

Here is the source code for com.wonders.bud.freshmommy.web.user.controller.UserController.java

Source

/** 
* 
 * Copyright (c) 1995-2012 Wonders Information Co.,Ltd. 
 * 1518 Lianhang Rd,Shanghai 201112.P.R.C.
 * All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Wonders Group.
 * (Social Security Department). 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 Wonders Group. 
 *
 * Distributable under GNU LGPL license by gnu.org
 */

package com.wonders.bud.freshmommy.web.user.controller;

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

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
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.ResponseBody;

import com.wonders.bud.framework.common.page.Page;
import com.wonders.bud.framework.common.page.PageUtil;
import com.wonders.bud.framework.common.page.PageVO;
import com.wonders.bud.framework.common.util.QueryParam;
import com.wonders.bud.framework.common.util.RestMsg;
import com.wonders.bud.freshmommy.service.user.user.model.po.UserPO;
import com.wonders.bud.freshmommy.service.user.user.service.UserService;
import com.wonders.bud.freshmommy.web.user.vo.UserVO;

/**<p>
 * Title: freshmommy_[]_[?]
 * </p>
 * <p>
 * Description: [Controller]
 * </p>
 * 
 * @author linqin
 * @version $Revision$ 2014-11-20
 * @author (lastest modification by $Author$)
 * @since 20100901
 */
@Controller
@RequestMapping("api/user")
public class UserController {

    protected Logger log = Logger.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * <p>
     * Description:[?]
     * </p>
     * 
     * Created by linqin [2014-11-20]
     * Midified by [] []
     * @param request
     * @return
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public RestMsg<PageVO<UserVO>> findUsersByPage(HttpServletRequest request) {

        RestMsg<PageVO<UserVO>> rm = new RestMsg<PageVO<UserVO>>();

        String uName = request.getParameter("uName");
        String uSex = request.getParameter("uSex");
        String uDistrict = request.getParameter("uDistrict");
        String status = request.getParameter("status");
        String row = request.getParameter("row");
        String start = request.getParameter("start");
        String sortorder = request.getParameter("sord"); //?ASC or DESC?DESC
        String sortfield = request.getParameter("sidx");

        try {
            Page<UserPO> page = new Page<UserPO>();
            QueryParam param = new QueryParam();

            Map<String, Object> eq = new HashMap<String, Object>();
            Map<String, Object> like = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(uSex)) {
                eq.put("uSex", Integer.valueOf(uSex));
                param.setEq(eq);
            }
            if (StringUtils.isNotBlank(uDistrict)) {
                eq.put("uDistrict", Integer.valueOf(uDistrict));
                param.setEq(eq);
            }
            if (StringUtils.isNotBlank(status)) {
                eq.put("status", Integer.valueOf(status));
                param.setEq(eq);
            }
            if (StringUtils.isNotBlank(uName)) {
                like.put("uName", uName);
                param.setLike(like);
            }
            page.setParam(param);
            PageUtil.pageSplit(page, start, row, sortorder, sortfield, null);

            page = userService.findUsersByPage(page);
            PageVO<UserVO> pageVO = new PageVO<UserVO>();
            List<UserVO> data = new ArrayList<UserVO>();
            List<UserPO> list = page.getResult();
            if (null != list && list.size() > 0) {
                for (UserPO po : list) {
                    UserVO vo = new UserVO();
                    BeanUtils.copyProperties(vo, po);
                    data.add(vo);
                }
            }
            pageVO.setData(data);
            PageUtil.pageVOSplit(page, pageVO);
            rm.setResult(pageVO);
            rm = rm.successMsg();
        } catch (Exception e) {
            rm.errorMsg("?");
            log.error(e.getLocalizedMessage());
        }
        return rm;
    }

    /**
     * <p>
     * Description:[?id?]
     * </p>
     * 
     * Created by linqin [2014-11-20]
     * Midified by [] []
     * @param request
     * @return
     */
    @RequestMapping(value = "/user", method = RequestMethod.GET)
    @ResponseBody
    public RestMsg<UserVO> findUserById(HttpServletRequest request) {

        RestMsg<UserVO> rm = new RestMsg<UserVO>();

        String uId = request.getParameter("uId");

        try {
            if (StringUtils.isBlank(uId)) {
                rm.errorMsg("??");
                return rm;
            }
            UserPO po = userService.findUserById(Integer.valueOf(uId));
            UserVO vo = new UserVO();
            BeanUtils.copyProperties(vo, po);
            rm.setResult(vo);
            rm.successMsg();
        } catch (Exception e) {
            rm.errorMsg("??");
            log.error(e.getLocalizedMessage());
        }
        return rm;
    }
}