Package com.gtp.controller

Source Code of com.gtp.controller.UserController

package com.gtp.controller;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.model.DualListModel;

import com.gtp.domain.Authority;
import com.gtp.domain.User;
import com.gtp.service.AuthoryService;
import com.gtp.service.UserService;
import commons.ExistedException;
import commons.NotFoundException;
import commons.UmsUtil;

public class UserController {
  private boolean inUser = false;
  private String name = "";
  private String surname = "";
  private String email = "";
  private String password = "";
  private String secretQuestion = "";
  private String secretAnswer = "";
  Object[] selectedAuthorities;
  UserService userService;
  User user;
  List<User> users = null;
  List<Authority> authorities = null;
  AuthoryService authoryService;
  private DualListModel<Authority> dualListModel = new DualListModel<Authority>(
      new ArrayList<Authority>(), new ArrayList<Authority>());

  public String addUser() {

    user = new User(name, surname, password, email, secretQuestion,
        secretAnswer, UmsUtil.toSetFromAuthorities(selectedAuthorities));
    try {
      userService.addUser(user);
    } catch (ExistedException e) {
      FacesContext.getCurrentInstance().addMessage(
          null,
          new FacesMessage(FacesMessage.SEVERITY_WARN,
              "User already exists",
              "Please choose different one"));
      setName("");
      setEmail("");
      setPassword("");
      setSecretAnswer("");
      setPassword("");
      setSecretQuestion("");
      e.printStackTrace();
      user = null;
      return null;
    }
    FacesContext.getCurrentInstance().addMessage(null,
        new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Successful"));
    setName("");
    setEmail("");
    setPassword("");
    setSecretAnswer("");
    setPassword("");
    setSecretQuestion("");
    user = null;
    return null;
  }

  public String update() {
    user.setAuthorities(UmsUtil.toSetFromAuthorities(dualListModel
        .getTarget().toArray()));
    try {
      userService.updateUser(user);
    } catch (ExistedException e) {
      FacesContext.getCurrentInstance().addMessage(
          null,
          new FacesMessage(FacesMessage.SEVERITY_ERROR,
              "User already exists",
              "Please choose different one"));
      e.printStackTrace();
      setName("");
      user = null;
      return null;
    } catch (NotFoundException e) {
      FacesContext.getCurrentInstance().addMessage(
          null,
          new FacesMessage(FacesMessage.SEVERITY_ERROR, "",
              "User not found"));
      e.printStackTrace();
      setName("");
      user = null;
      return null;
    }
    FacesContext.getCurrentInstance().addMessage(null,
        new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Successful"));
    setName("");
    setEmail("");
    setPassword("");
    setSecretAnswer("");
    setPassword("");
    setSecretQuestion("");
    user = null;
    return null;
  }

  public void setUserService(UserService service) {
    this.userService = service;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getSurname() {
    return surname;
  }

  public void setSurname(String surname) {
    this.surname = surname;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public String getSecretQuestion() {
    return secretQuestion;
  }

  public void setSecretQuestion(String secretQuestion) {
    this.secretQuestion = secretQuestion;
  }

  public String getSecretAnswer() {
    return secretAnswer;
  }

  public void setSecretAnswer(String secretAnswer) {
    this.secretAnswer = secretAnswer;
  }

  public UserService getUserService() {
    return userService;
  }

  public User getUser() {
    return user;
  }

  public void setUser(User user) {
    this.user = user;
  }

  public List<User> getUsers() {
    return userService.findAllUsers();
  }

  public void setUsers(List<User> users) {
    this.users = users;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public boolean isInUser() {
    return inUser;
  }

  public void setInUser(boolean inUser) {
    this.inUser = inUser;
  }

  public DualListModel<Authority> getDualListModel() {
    if (dualListModel != null && user != null) {
      authorities = getAuthorities();
      List<Authority> target = UmsUtil.toSetFromObject(user
          .getAuthorities().toArray());
      for (Authority r : target) {
        if (authorities.contains(r))
          authorities.remove(r);
      }
      dualListModel = new DualListModel<Authority>(authorities, target);
    }
    return dualListModel;
  }

  public void setDualListModel(DualListModel<Authority> dualListModel) {
    this.dualListModel = dualListModel;
  }

  public List<Authority> getAuthorities() {
    return authoryService.findAllAuthories();
  }

  public Object[] getSelectedAuthorities() {
    if (user != null)
      return user.getAuthorities().toArray();
    return selectedAuthorities;
  }
  public void setSelectedAuthorities(Object[] selectedAuthorities) {
    this.selectedAuthorities = selectedAuthorities;
  }
  public void setAuthorities(List<Authority> authorities) {
    this.authorities = authorities;
  }
 
  public AuthoryService getAuthoryService() {
    return authoryService;
  }

  public void setAuthoryService(AuthoryService authoryService) {
    this.authoryService = authoryService;
  }

}
TOP

Related Classes of com.gtp.controller.UserController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.