Package com.gtp.controller

Source Code of com.gtp.controller.FreeChairAddDialog

package com.gtp.controller;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import com.gtp.domain.FreeChair;
import com.gtp.domain.User;
import com.gtp.service.FreeChairService;
import com.gtp.service.UserService;

import commons.ExistedException;
import commons.NotFoundException;

public class FreeChairAddDialog {
  private FreeChairService service;
  private String header;
  private String body;
  private User user;
  private UserService userService;
  public String finish() {
    FreeChair freeChair = new FreeChair();
    freeChair.setBody(body);
    freeChair.setHeader(header);
    freeChair.setUser(getAuthenticatedUser());
    try {
      service.addFreeChair(freeChair);
    } catch (ExistedException e) {
      e.printStackTrace();
    }
    return "OK";
  }

  public FreeChairService getService() {
    return service;
  }

  public void setService(FreeChairService service) {
    this.service = service;
  }

  public String getHeader() {
    return header;
  }

  public void setHeader(String header) {
    this.header = header;
  }

  public String getBody() {
    return body;
  }

  public void setBody(String body) {
    this.body = body;
  }

  public User getUser() {
    return user;
  }

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

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

  public User getAuthenticatedUser() {
    if (!SecurityContextHolder.getContext().getAuthentication()
        .isAuthenticated()) {
      return null;
    }
    String email;
    Object principal = SecurityContextHolder.getContext()
        .getAuthentication().getPrincipal();
    if (principal instanceof UserDetails) {
      email = ((UserDetails) principal).getUsername();
    } else {
      email = principal.toString();
    }
    try {
      user = userService.findUser(email);
    } catch (NotFoundException e) {
      user = null;
      e.printStackTrace();
    }
    return user;
  }
}
TOP

Related Classes of com.gtp.controller.FreeChairAddDialog

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.