Package com.gtp.controller

Source Code of com.gtp.controller.ArticleAddDialog

package com.gtp.controller;

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

import com.gtp.domain.Article;
import com.gtp.domain.User;
import com.gtp.service.ArticleService;
import com.gtp.service.UserService;

import commons.ExistedException;
import commons.NotFoundException;

public class ArticleAddDialog {
  private ArticleService service;
  private String header;
  private String body;
  private String description;
  private User user;
  private UserService userService;

  public String finish() {
    Article article = new Article();
    article.setBody(body);
    article.setHeader(header);
    article.setDescription(description);
    article.setUser(getAuthenticatedUser());
    try {
      service.addArticle(article);
    } catch (ExistedException e) {

      e.printStackTrace();
    }
    return "OK";
  }

  public ArticleService getService() {
    return service;
  }

  public void setService(ArticleService 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 String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  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;
  }

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

Related Classes of com.gtp.controller.ArticleAddDialog

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.