controllers.user.UserController.java Source code

Java tutorial

Introduction

Here is the source code for controllers.user.UserController.java

Source

/*****************************************************************************
 * ?????
 *****************************************************************************
 * LoginController.java
 *----------------------------------------------------------------------------
 * (C) ?
 *     SHENYANG EIGHTSPACE TECHNOLOGY DEVELOPMENT CO.,LTD.  2014
 *----------------------------------------------------------------------------
 * Version    Date       By            Comment
 * 1.0        2014-9-29    Poppy       
 *****************************************************************************/
package controllers.user;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import models.LogInfo;
import models.User;
import play.Logger;
import play.data.Form;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Http.Cookie;
import play.mvc.Http.MultipartFormData.FilePart;
import play.mvc.Result;
import uk.co.panaxiom.playjongo.PlayJongo;
import util.CommonUtil;
import util.IContent;
import views.html.produceBasicInfo;
import views.html.register;
import views.html.supervisoinShowComInfo;
import views.html.user;

import com.eightspace.server.common.convert.EmptyUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.mongodb.gridfs.GridFSInputFile;

import form.UserForm;

/**
 * ??Controller
 * 
 * @author Poppy
 * @date 2014-9-29
 * @since 1.0
 */
public class UserController extends Controller {

    /**
     * ????
     * 
     * @author hely
     * @date 2014-10-13
     * @return
     */
    public static Result init(Integer page, Integer pageSize) {
        List<User> spvions = User.findAll(page, pageSize);
        // 
        long count = User.count();
        Long totalPages = count / pageSize;
        if (count % pageSize != 0) {
            totalPages += 1;
        }

        if (page > 1 && spvions.isEmpty()) {
            return redirect("/user/init?page=" + (page - 1) + "&pageSize=" + pageSize);
        }
        return ok(user.render(spvions, page, totalPages));
    }

    /**
     * 
     * ?
     * 
     * @author Frank
     * @date 20141017
     * @return
     * @since 1.0
     */
    public static Result importUser() {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            FilePart pro = request().body().asMultipartFormData().getFile("userImportFile");
            File file = pro.getFile();
            List<User> userList = CommonUtil.parseExcelforUser(file);
            for (User user : userList) {
                user.add();
            }
            result.put("status", 1);
            result.put("msg", "?");
        } catch (Exception e) {
            Logger.error("?", e);
            // return ok(error.render("??"));
            result.put("status", -1);
            result.put("msg", "");
        }
        return ok(Json.toJson(result));
    }

    /**
     * 
     * ??
     * 
     * @author hely
     * @date 20141014
     * @return
     */
    public static Result modpwd() {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            JsonNode modpwdData = request().body().asJson();
            String msg = User.modpwd(modpwdData);
            // 
            LogInfo.save(CommonUtil.getPlayUser(request()).get("username"), "??");
            result.put("status", 1);
            result.put("msg", msg);
        } catch (Exception e) {
            Logger.debug("?", e);
            result.put("status", -1);
            result.put("msg", "");
        }
        return ok(Json.toJson(result));
    }

    /**
     * ??
     * 
     * @return
     */
    public static Result lookUser() {
        JsonNode uJsonNode = request().body().asJson();
        String userid = uJsonNode.get("id").asText();
        User user = User.findById(userid);
        return ok(Json.toJson(user));
    }

    /**
     * ???
     * 
     * @author Poppy
     * @date 2014-9-29
     * @return
     * @since 1.0
     */
    public static Result registerInit() {
        UserForm form = new UserForm();
        form.enterpriseName = "";
        form.enterprisePrincipal = "";
        form.legalPerson = "";
        return ok(register.render(form, null));
    }

    /**
     * ??
     * 
     * @author Poppy
     * @date 2014-9-29
     * @return
     * @since 1.0
     */
    public static Result add() {
        String msg = "";
        UserForm form = new UserForm();
        User user = new User();
        try {
            // ??
            Form<UserForm> userForm = Form.form(UserForm.class);
            form = userForm.bindFromRequest().get();
            // ?cookie??
            Cookie cookie = request().cookie(IContent.VERIFYCODE_COOKIE);
            String cookieValue = (cookie == null) ? "" : cookie.value();
            String verifyCode = form.verifyCode;
            if (!verifyCode.toUpperCase().equals(cookieValue.toUpperCase())) {
                msg = "verifyWrong";
                return ok(register.render(form, msg));
            }
            // 
            FilePart pro = request().body().asMultipartFormData().getFile("productionCertificateUrl");
            FilePart food = request().body().asMultipartFormData().getFile("foodCirculationUrl");
            FilePart bus = request().body().asMultipartFormData().getFile("businessLicenseUrl");
            // ????
            if (EmptyUtil.isNotEmpty(pro)) {
                File proFile = pro.getFile();
                // ?
                String ext = "jpg";
                String proFileExt = pro.getFilename().substring(pro.getFilename().lastIndexOf(".") + 1)
                        .toLowerCase();
                if (!Arrays.<String>asList(ext.split(",")).contains(proFileExt)) {
                    msg = "extWrong";
                    return ok(register.render(form, msg));
                } else {
                    // 
                    GridFSInputFile gProFile = PlayJongo.gridfs().createFile(proFile);
                    gProFile.setFilename(pro.getFilename());
                    gProFile.setContentType(proFileExt);
                    gProFile.save();
                    user.productionCertificateUrl = "/common/download/" + gProFile.getId();
                }
            } else {
                user.productionCertificateUrl = "";
            }
            // ?????
            if (EmptyUtil.isNotEmpty(food)) {
                File foodFile = food.getFile();
                // ?
                String ext = "jpg";
                String foodFileExt = food.getFilename().substring(food.getFilename().lastIndexOf(".") + 1)
                        .toLowerCase();
                if (!Arrays.<String>asList(ext.split(",")).contains(foodFileExt)) {
                    msg = "extWrong";
                    return ok(register.render(form, msg));
                } else {
                    // 
                    GridFSInputFile gFoodFile = PlayJongo.gridfs().createFile(foodFile);
                    gFoodFile.setFilename(food.getFilename());
                    gFoodFile.setContentType(foodFileExt);
                    gFoodFile.save();
                    user.foodCirculationUrl = "/common/download/" + gFoodFile.getId();
                }
            } else {
                user.foodCirculationUrl = "";
            }
            // ??
            if (EmptyUtil.isNotEmpty(bus)) {
                File busFile = bus.getFile();
                // ?
                String ext = "jpg";
                String busFileExt = bus.getFilename().substring(bus.getFilename().lastIndexOf(".") + 1)
                        .toLowerCase();
                if (!Arrays.<String>asList(ext.split(",")).contains(busFileExt)) {
                    msg = "extWrong";
                    return ok(register.render(form, msg));
                } else {
                    // 
                    GridFSInputFile gBusFile = PlayJongo.gridfs().createFile(busFile);
                    gBusFile.setFilename(bus.getFilename());
                    gBusFile.setContentType(busFileExt);
                    gBusFile.save();
                    user.businessLicenseUrl = "/common/download/" + gBusFile.getId();
                }
            } else {
                user.businessLicenseUrl = "";
            }
            // form??
            user.addr = form.addr;
            user.businessLicenseCode = form.businessLicenseCode;
            user.city = form.city;
            user.enterpriseName = form.enterpriseName;
            user.enterprisePrincipal = form.enterprisePrincipal;
            user.enterpriseType = form.enterpriseType;
            user.executiveStandard = form.executiveStandard;
            user.legalPerson = form.legalPerson;
            user.organizationCode = form.organizationCode;
            user.postCode = form.postCode;
            user.productionAddr = form.productionAddr;
            user.province = form.province;
            user.pwd = form.pwd;
            user.qualityPrincipal = form.qualityPrincipal;
            user.qualityAuthorizer = form.qualityAuthorizer;
            user.tel = form.tel;
            user.email = form.email;
            user.productionCertificate = form.productionCertificate;
            user.variety = form.variety;
            user.enterpriseAddr = form.enterpriseAddr;
            user.busiScope = form.busiScope;
            user.busiMod = form.busiMod;
            user.foodCirculation = form.foodCirculation;
            user.approvalFlag = 0;
            user.updateFlag = 0;
            if (form.enterpriseType == 1) {
                user.userName = form.organizationCode;
                // ??
                Long checkResult = User.registerCheck(user.organizationCode);
                if (checkResult == 0) {
                    // ?form??
                    user.add();
                    msg = "success";
                } else {
                    msg = "checkWrong";
                }
            } else {
                user.userName = form.foodCirculation;
                // ?????
                Long checkResult = User.registerCheckBis(user.foodCirculation);
                if (checkResult == 0) {
                    // ?form??
                    user.add();
                    msg = "success";
                } else {
                    msg = "checkWrong";
                }
            }
        } catch (Exception e) {
            // 
            Logger.error("UserController-add:?", e);
            msg = "error";
        }
        // 
        return ok(register.render(form, msg));
    }

    /**
     * ??
     * 
     * @author Poppy
     * @date 2014-9-29
     * @param id
     * @return
     * @since 1.0
     */
    public static Result delete(String id) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            // 
            User.delete(id);
            // result
            result.put("status", true);
        } catch (Exception e) {
            // 
            Logger.error("UserController-delete:?", e);
            result.put("status", false);
        }
        // 
        return ok(Json.toJson(result));
    }

    /**
     * ?,?
     * 
     * @author Poppy
     * @date 2014-9-29
     * @return
     * @since 1.0
     */
    public static Result update() {
        String msg = "";
        // ??
        Form<UserForm> userForm = Form.form(UserForm.class);
        UserForm form = userForm.bindFromRequest().get();
        try {
            // cookie
            Map<String, String> userMap = CommonUtil.getPlayUser(request());
            User user = User.findById(userMap.get("id"));
            form.id = user.id.toString();
            Integer updateFlag = user.updateFlag;
            if (updateFlag == 0) {
                // form??(?)
                User.updateApproval(form);
            } else {
                // updateFlag?2form??
                User.update(form);
            }
            // 
            msg = "success";
        } catch (Exception e) {
            // 
            Logger.error("UserController-update:?", e);
            msg = "fail";
        }
        // cookie????
        Map<String, String> userMap = CommonUtil.getPlayUser(request());
        User user = User.findById(userMap.get("id"));
        return ok(produceBasicInfo.render(user, msg));
    }

    /**
     * ?id????
     * 
     * @author Poppy
     * @date 2014-10-22
     * @param id
     * @return
     * @since 1.0
     */
    public static Result findById(String id, Integer showType) {
        User user = new User();
        String msg = "";
        try {
            user = User.findById(id);
            msg = "success";
        } catch (Exception e) {
            // 
            Logger.error("UserController-findById:??", e);
            msg = "fail";
        }
        // ?
        return ok(supervisoinShowComInfo.render(user, msg, showType));
    }
}