Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml"
- xmlns:h="http://xmlns.jcp.org/jsf/html"
- xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
- xmlns:f="http://xmlns.jcp.org/jsf/core"
- xmlns:p="http://primefaces.org/ui">
- <h:head>
- <title>overview</title>
- <f:metadata>
- <f:viewParam name="userId" value="#{userController.userId}" />
- <f:event type="preRenderView" listener="#{userController.retrieveUser()}"/>
- </f:metadata>
- </h:head>
- <h:body>
- <ui:composition template="template/layout.xhtml">
- <ui:define name="content">
- <f:view>
- <h:panelGrid columns="2" cellpadding="5">
- <p:outputLabel value="Name:" for="username"/>
- <p:inputText id="username" target="#{userController.selectedUser}" value="#{userController.selectedUser.username}" editable="false" />
- <p:outputLabel value="Type:" for="authorizationLevel" />
- <p:selectOneMenu id="authorizationLevel" target="#{userController.selectedUser}" value="#{userController.selectedUser.authorizationLevel}" style="width:125px">
- <f:selectItem itemLabel="USER" itemValue="USER" />
- <f:selectItem itemLabel="MODERATOR" itemValue="MODERATOR" />
- </p:selectOneMenu>
- <p:outputLabel value="Biography" for="bio" />
- <p:inputTextarea id="bio" rows="10" cols="50" target="#{userController.selectedUser}" value="#{userController.selectedUser.bio}"/>
- <p:outputLabel value="Location" for="location" />
- <p:inputText id="location" target="#{userController.selectedUser}" value="#{userController.selectedUser.location}"/>
- <p:outputLabel value="Website" for="web"/>
- <p:inputText id="web" target="#{userController.selectedUser}" value="#{userController.selectedUser.website}"/>
- </h:panelGrid>
- <p:commandButton value="Submit" action="#{userController.editUser}"/>
- </f:view>
- </ui:define>
- </ui:composition>
- package boundary.controllers;
- import domain.AuthorizationLevel;
- import domain.Tweet;
- import domain.User;
- import service.TweetService;
- import service.UserService;
- import javax.enterprise.context.RequestScoped;
- import javax.faces.context.FacesContext;
- import javax.inject.Inject;
- import javax.inject.Named;
- import java.util.Collection;
- import java.util.Map;
- @Named
- @RequestScoped
- public class UserController {
- private long userId;
- private User selectedUser;
- public long getUserId() {
- return userId;
- }
- public void setUserId(long userId) {
- this.userId = userId;
- }
- public User getSelectedUser() {
- return selectedUser;
- }
- public void setSelectedUser(User selectedUser) {
- this.selectedUser = selectedUser;
- }
- @Inject
- private TweetService tweetService;
- @Inject
- private UserService userService;
- /**
- * gets all users from the database
- * @return Response
- */
- public Collection<User> getAllUsers(){
- Collection<User> userlist = userService.getAllUsers();
- for (User u : userlist){
- if(u.getAuthorizationLevel() == AuthorizationLevel.MODERATOR){
- userlist.remove(u);
- break;
- }
- }
- return userlist;
- }
- /**
- * deletes a user from database
- */
- public void delete(){
- FacesContext fc = FacesContext.getCurrentInstance();
- Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
- Long id = Long.parseLong(params.get("userId"));
- Collection<Tweet> postedTweets = tweetService.findTweetsByUser(id);
- for(Tweet t: postedTweets){
- tweetService.removeTweet(t.getId());
- System.out.println("tweetdelete");
- }
- userService.deleteUserByID(id);
- }
- /**
- * retrieves data from user and sets it to selectedUser object
- */
- public void retrieveUser(){
- this.selectedUser = userService.findUserById(userId);
- }
- /**
- * edits a user object
- * @return String
- */
- public String editUser(){
- userService.changeUser(userId, this.selectedUser);
- return "/useroverview?faces-redirect=true";
- }
- package domain;
- import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
- import javax.json.bind.annotation.JsonbTransient;
- import javax.persistence.*;
- import java.util.ArrayList;
- import java.util.List;
- @Entity
- @Table(name = "User")
- @NamedQueries(value = {
- @NamedQuery(name = "User.getAllUsers", query = "SELECT u FROM User u"),
- @NamedQuery(name = "User.getUserByID", query = "SELECT u FROM User u WHERE u.id = :userID"),
- @NamedQuery(name = "User.getUserByUsername", query = "SELECT u FROM User u WHERE u.username = :username"),
- @NamedQuery(name = "User.getUsersByUsername", query = "SELECT u FROM User u WHERE u.username LIKE :username"),
- })
- public class User {
- private static final long serialVersionUID = 1L;
- @Id
- @Column(name = "id")
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Long id;
- @Column(name = "username", unique = true, length = 50)
- private String username;
- @Column(name = "password", length = 64, nullable = false)
- private String password;
- @Column(name = "email", unique = true, nullable = false)
- private String email;
- @Column(name = "bio", length = 160)
- private String bio;
- @Column(name = "location")
- private String location;
- @Column(name = "website")
- private String website;
- @Enumerated(EnumType.STRING)
- @Column(name = "isModerator", nullable = false)
- private AuthorizationLevel authorizationLevel;
- @ManyToMany
- @JsonbTransient
- @JoinTable(name = "follow",
- joinColumns = @JoinColumn(name = "follower_id", referencedColumnName = "id"),
- inverseJoinColumns = @JoinColumn(name = "following_id", referencedColumnName = "id"))
- private List<User> followers;
- @ManyToMany(mappedBy = "followers")
- @JsonbTransient
- private List<User> following;
- @JsonbTransient
- @JoinColumn(name = "posted_tweet")
- @OneToMany(mappedBy = "postedBy", cascade = CascadeType.PERSIST)
- private List<Tweet> postedTweets;
- @JsonbTransient
- @ManyToMany(mappedBy="likedBy")
- private List<Tweet> likedTweets;
- public User() {
- }
- public User(String username, String password) {
- this.username = username;
- this.password = password;
- }
- public User(String username, String password, String email, String bio, String location, String website, AuthorizationLevel lvl) {
- this.username = username;
- this.password = password;
- this.email = email;
- this.bio = bio;
- this.location = location;
- this.website = website;
- this.authorizationLevel = lvl;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public List<Tweet> getLikedTweets() {
- return likedTweets;
- }
- public void setLikedTweets(List<Tweet> likedTweets) {
- this.likedTweets = likedTweets;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public String getBio() {
- return bio;
- }
- public void setBio(String bio) {
- this.bio = bio;
- }
- public String getLocation() {
- return location;
- }
- public void setLocation(String location) {
- this.location = location;
- }
- public String getWebsite() {
- return website;
- }
- public void setWebsite(String website) {
- this.website = website;
- }
- public AuthorizationLevel getAuthorizationLevel() {
- return authorizationLevel;
- }
- public void setAuthorizationLevel(AuthorizationLevel authorizationLevel) {
- this.authorizationLevel = authorizationLevel;
- }
- public List<User> getFollowers() {
- if (followers == null) {
- followers = new ArrayList<User>();
- }
- return followers;
- }
- public void setFollowers(List<User> followers) {
- this.followers = followers;
- }
- public List<User> getFollowing() {
- if (following == null) {
- following = new ArrayList<User>();
- }
- return following;
- }
- public void setFollowing(List<User> following) {
- this.following = following;
- }
- public List<Tweet> getPostedTweets() {
- if (postedTweets == null) {
- postedTweets = new ArrayList<Tweet>();
- }
- return postedTweets;
- }
- public void setPostedTweets(List<Tweet> postedTweets) {
- if (postedTweets == null) {
- postedTweets = new ArrayList<Tweet>();
- }
- this.postedTweets = postedTweets;
- }
Add Comment
Please, Sign In to add comment