/*
 * <p>豆浆包子</p>
 * <p>Email:doujiangbaozi@gmail.com</p>
 * <p>QQ:6041738</p>
 * <p>Description:</p>
 */
package cn.sliver.application.action.system;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.opensymphony.xwork2.validator.annotations.EmailValidator;
import com.opensymphony.xwork2.validator.annotations.FieldExpressionValidator;
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

import cn.sliver.application.entity.system.SystemModule;
import cn.sliver.application.entity.system.SystemPrivilege;
import cn.sliver.application.entity.system.SystemUser;
import cn.sliver.application.service.SystemService;
import cn.sliver.framework.action.BaseAction;
import cn.sliver.framework.util.UuidUtil;

@JsonIgnoreProperties({
    "groupID","groupName","privileges","modules","rePassword","userName","userPassword",
    "userBirthdate","userRole","userPhone","userMobile","userEmail","userDescription",
    "userID","userGender","u"
})
public class UserManager extends BaseAction {

    static final long            serialVersionUID = -5115071122726345857L;
    private SystemUser            u;
    private SystemService         service;
    private String                rePassword;
    private List<SystemPrivilege> privileges;
    private List<SystemModule>    modules;
    private Boolean               success;

    @Validations(
        requiredStrings = {
            @RequiredStringValidator(fieldName="userName",       key="errors.required"),
            @RequiredStringValidator(fieldName="userPassword",   key="errors.required"),
            @RequiredStringValidator(fieldName="rePassword",     key="errors.required"),
            //@RequiredStringValidator(fieldName="userBirthdate",  key="errors.required")//,
            //@RequiredStringValidator(fieldName="userRole",       key="errors.required"),
            //@RequiredStringValidator(fieldName="userPhone",      key="errors.required"),
            //@RequiredStringValidator(fieldName="userMobile",     key="errors.required"),
            //@RequiredStringValidator(fieldName="userEmail",      key="errors.required")
        },
        stringLengthFields = {
            @StringLengthFieldValidator(fieldName="userName",    key="errors.length", minLength = "2", maxLength = "18"),
            @StringLengthFieldValidator(fieldName="userPassword",key="errors.length", minLength = "6", maxLength = "18"),
            @StringLengthFieldValidator(fieldName="rePassword",  key="errors.length", minLength = "6", maxLength = "18")
        },
        fieldExpressions = {
            @FieldExpressionValidator(fieldName="rePassword",    key = "errors.password.confirm",expression="userPassword.equals(rePassword)")
        },
        emails={
            @EmailValidator(fieldName="userEmail",               key="errors.email")
        },
        regexFields={
            @RegexFieldValidator(fieldName="userBirthdate",      regexExpression="[0-9]{1,4}-[0-9]{1,2}-[0-9]{1,2}",key="errors.phone"),
            @RegexFieldValidator(fieldName="userPhone",          regexExpression="^(([0\\+]\\d{2,3}-)?(0\\d{2,3})-)?(\\d{7,8})(-(\\d{3,}))?$",key="errors.phone"),
            @RegexFieldValidator(fieldName="userMobile",         regexExpression="^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\\d{8}$",key="errors.phone")
        }
    )
    @Action(value="/system/insertUser",
        results={@Result(name="input", location="/error.jsp")}
    )
    public String execute(){
        try{
            this.u.getPrivileges().addAll(this.privileges);
            this.privileges.clear();
            this.u.getMds().addAll(this.modules);
            this.modules.clear();

            String id = UuidUtil.id();
            u.setUserID(id);
            Boolean f = service.insertUser(u);
            if(!f){
                this.addFieldError("userName", getText("errors.exist"));
                this.setSuccess(false);
                return INPUT;
            }else{
                this.setSuccess(true);
                jackson(this);
                return null;
            }
        }catch(Exception e){
            e.printStackTrace();
            log.error(e);
            this.setSuccess(false);
            return INPUT;
        }
    }

    @Action(value="/system/updateUser",
        results={@Result(name="input",  location="/error.jsp")}
    )
    public String update(){
        try{
            log.info(this.getClass().getName());
            SystemUser usr = service.checkUserByName(this.u);
            if(null!=usr){
                this.addFieldError("userName", getText("errors.exist"));
                this.setSuccess(false);
                return INPUT;
            }else{
                this.u.getPrivileges().addAll(this.privileges);
                this.privileges.clear();
                this.u.getMds().addAll(this.modules);
                this.modules.clear();

                service.updateUser(u);
                this.setSuccess(true);
                jackson(this);
                return null;
            }
        }catch(Exception e){
            e.printStackTrace();
            log.error(e);
            this.setSuccess(false);
            return INPUT;
        }
    }

    @SkipValidation
    @Action(value="/system/removeUser")
    public String remove(){
        try{
            if(null==this.getUserID()){
                this.setSuccess(false);
            }else{
                service.removeUser(this.getUserID().toString());
                this.setSuccess(true);
            }
        }catch(Exception e){
            log.error(e);
            this.setSuccess(false);
        }
        jackson(this);
        return null;
    }

    @SkipValidation
    @Action(value="/system/moveUser")
    public String move(){
        try{
            if((null==this.getUserID())||(null==this.getUserID())){
                this.setSuccess(false);
            }else{
                this.service.updateUserGroup(this.u);
                this.setSuccess(true);
            }
        }catch(Exception e){
            log.error(e);
            this.setSuccess(false);
        }
        jackson(this);
        return null;
    }


    /**
     * @return the success
     */
    public Boolean getSuccess() {
        return success;
    }

    /**
     * @param success the success to set
     */
    public void setSuccess(Boolean success) {
        this.success = success;
    }

    /**
     * @param u the u to set
     */
    public void setU(SystemUser u) {
        this.u = u;
    }


    /**
     * @param service the service to set
     */
    public void setService(SystemService service) {
        this.service = service;
    }

    /**
     * @return
     * @see cn.sliver.application.entity.system.SystemUser#getGroupID()
     */
    public String getGroupID() {
        return u.getGroupID();
    }

    /**
     * @return
     * @see cn.sliver.application.entity.system.SystemUser#getGroupName()
     */
    public String getGroupName() {
        return u.getGroupName();
    }

    /**
     * @return
     * @see cn.sliver.application.entity.system.SystemUser#getUserBirthdate()
     */
    public String getUserBirthdate() {
        return u.getUserBirthdate();
    }

    /**
     * @return
     * @see cn.sliver.application.entity.system.SystemUser#getUserDescription()
     */
    public String getUserDescription() {
        return u.getUserDescription();
    }

    /**
     * @return
     * @see cn.sliver.application.entity.system.SystemUser#getUserEmail()
     */
    public String getUserEmail() {
        return u.getUserEmail();
    }

    /**
     * @return
     * @see cn.sliver.application.entity.system.SystemUser#getUserGender()
     */
    public Boolean getUserGender() {
        return u.getUserGender();
    }

    /**
     * @return
     * @see cn.sliver.application.entity.system.SystemUser#getUserID()
     */
    public String getUserID() {
        return u.getUserID();
    }

    /**
     * @return
     * @see cn.sliver.application.entity.system.SystemUser#getUserMobile()
     */
    public String getUserMobile() {
        return u.getUserMobile();
    }

    /**
     * @return
     * @see cn.sliver.application.entity.system.SystemUser#getUserName()
     */
    public String getUserName() {
        return u.getUserName();
    }

    /**
     * @return
     * @see cn.sliver.application.entity.system.SystemUser#getUserPassword()
     */
    public String getUserPassword() {
        return u.getUserPassword();
    }

    /**
     * @return
     * @see cn.sliver.application.entity.system.SystemUser#getUserPhone()
     */
    public String getUserPhone() {
        return u.getUserPhone();
    }

    /**
     * @return
     * @see cn.sliver.application.entity.system.SystemUser#getUserRole()
     */
    public String getUserRole() {
        return u.getUserRole();
    }

    /**
     * @param groupID
     * @see cn.sliver.application.entity.system.SystemUser#setGroupID(java.lang.Integer)
     */
    public void setGroupID(String groupID) {
        u.setGroupID(groupID);
    }

    /**
     * @param groupName
     * @see cn.sliver.application.entity.system.SystemUser#setGroupName(java.lang.String)
     */
    public void setGroupName(String groupName) {
        u.setGroupName(groupName);
    }

    /**
     * @param userBirthdate
     * @see cn.sliver.application.entity.system.SystemUser#setUserBirthdate(java.lang.String)
     */
    public void setUserBirthdate(String userBirthdate) {
        u.setUserBirthdate(userBirthdate);
    }

    /**
     * @param userDescription
     * @see cn.sliver.application.entity.system.SystemUser#setUserDescription(java.lang.String)
     */
    public void setUserDescription(String userDescription) {
        u.setUserDescription(userDescription);
    }

    /**
     * @param userEmail
     * @see cn.sliver.application.entity.system.SystemUser#setUserEmail(java.lang.String)
     */
    public void setUserEmail(String userEmail) {
        u.setUserEmail(userEmail);
    }

    /**
     * @param userGender
     * @see cn.sliver.application.entity.system.SystemUser#setUserGender(java.lang.Boolean)
     */
    public void setUserGender(Boolean userGender) {
        u.setUserGender(userGender);
    }

    /**
     * @param userID
     * @see cn.sliver.application.entity.system.SystemUser#setUserID(java.lang.Integer)
     */
    public void setUserID(String userID) {
        u.setUserID(userID);
    }

    /**
     * @param userMobile
     * @see cn.sliver.application.entity.system.SystemUser#setUserMobile(java.lang.String)
     */
    public void setUserMobile(String userMobile) {
        u.setUserMobile(userMobile);
    }

    /**
     * @param username
     * @see cn.sliver.application.entity.system.SystemUser#setUserName(java.lang.String)
     */
    public void setUserName(String username) {
        u.setUserName(username.trim());
    }

    /**
     * @param userPassword
     * @see cn.sliver.application.entity.system.SystemUser#setUserPassword(java.lang.String)
     */
    public void setUserPassword(String userPassword) {
        u.setUserPassword(userPassword);
    }

    /**
     * @param userPhone
     * @see cn.sliver.application.entity.system.SystemUser#setUserPhone(java.lang.String)
     */
    public void setUserPhone(String userPhone) {
        u.setUserPhone(userPhone);
    }

    /**
     * @param userRole
     * @see cn.sliver.application.entity.system.SystemUser#setUserRole(java.lang.String)
     */
    public void setUserRole(String userRole) {
        u.setUserRole(userRole);
    }

    /**
     * @return the rePassword
     */
    public String getRePassword() {
        return rePassword;
    }

    /**
     * @param rePassword the rePassword to set
     */
    public void setRePassword(String rePassword) {
        this.rePassword = rePassword;
    }

    /**
     * @return the u
     */
    public SystemUser getU() {
        return u;
    }

    /**
     * @return the privileges
     */
    public List<SystemPrivilege> getPrivileges() {
        return privileges;
    }

    /**
     * @param privileges the privileges to set
     */
    public void setPrivileges(List<SystemPrivilege> privileges) {
        this.privileges = privileges;
    }

    /**
     * @return the modules
     */
    public List<SystemModule> getModules() {
        return modules;
    }

    /**
     * @param modules the modules to set
     */
    public void setModules(List<SystemModule> modules) {
        this.modules = modules;
    }


}
