Guest User

Untitled

a guest
Mar 25th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.35 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml"
  5. xmlns:h="http://xmlns.jcp.org/jsf/html"
  6. xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
  7. xmlns:f="http://xmlns.jcp.org/jsf/core"
  8. xmlns:p="http://primefaces.org/ui">
  9. <h:head>
  10. <title>overview</title>
  11.  
  12. <f:metadata>
  13. <f:viewParam name="userId" value="#{userController.userId}" />
  14. <f:event type="preRenderView" listener="#{userController.retrieveUser()}"/>
  15. </f:metadata>
  16. </h:head>
  17. <h:body>
  18. <ui:composition template="template/layout.xhtml">
  19. <ui:define name="content">
  20. <f:view>
  21. <h:panelGrid columns="2" cellpadding="5">
  22. <p:outputLabel value="Name:" for="username"/>
  23. <p:inputText id="username" target="#{userController.selectedUser}" value="#{userController.selectedUser.username}" editable="false" />
  24.  
  25. <p:outputLabel value="Type:" for="authorizationLevel" />
  26. <p:selectOneMenu id="authorizationLevel" target="#{userController.selectedUser}" value="#{userController.selectedUser.authorizationLevel}" style="width:125px">
  27. <f:selectItem itemLabel="USER" itemValue="USER" />
  28. <f:selectItem itemLabel="MODERATOR" itemValue="MODERATOR" />
  29. </p:selectOneMenu>
  30.  
  31. <p:outputLabel value="Biography" for="bio" />
  32. <p:inputTextarea id="bio" rows="10" cols="50" target="#{userController.selectedUser}" value="#{userController.selectedUser.bio}"/>
  33.  
  34. <p:outputLabel value="Location" for="location" />
  35. <p:inputText id="location" target="#{userController.selectedUser}" value="#{userController.selectedUser.location}"/>
  36.  
  37. <p:outputLabel value="Website" for="web"/>
  38. <p:inputText id="web" target="#{userController.selectedUser}" value="#{userController.selectedUser.website}"/>
  39. </h:panelGrid>
  40.  
  41. <p:commandButton value="Submit" action="#{userController.editUser}"/>
  42. </f:view>
  43. </ui:define>
  44. </ui:composition>
  45.  
  46. package boundary.controllers;
  47.  
  48. import domain.AuthorizationLevel;
  49. import domain.Tweet;
  50. import domain.User;
  51. import service.TweetService;
  52. import service.UserService;
  53.  
  54. import javax.enterprise.context.RequestScoped;
  55. import javax.faces.context.FacesContext;
  56. import javax.inject.Inject;
  57. import javax.inject.Named;
  58. import java.util.Collection;
  59. import java.util.Map;
  60.  
  61. @Named
  62. @RequestScoped
  63. public class UserController {
  64.  
  65. private long userId;
  66.  
  67. private User selectedUser;
  68.  
  69. public long getUserId() {
  70. return userId;
  71. }
  72.  
  73. public void setUserId(long userId) {
  74. this.userId = userId;
  75. }
  76.  
  77. public User getSelectedUser() {
  78. return selectedUser;
  79. }
  80.  
  81. public void setSelectedUser(User selectedUser) {
  82. this.selectedUser = selectedUser;
  83. }
  84.  
  85. @Inject
  86. private TweetService tweetService;
  87.  
  88. @Inject
  89. private UserService userService;
  90.  
  91. /**
  92. * gets all users from the database
  93. * @return Response
  94. */
  95. public Collection<User> getAllUsers(){
  96. Collection<User> userlist = userService.getAllUsers();
  97. for (User u : userlist){
  98. if(u.getAuthorizationLevel() == AuthorizationLevel.MODERATOR){
  99. userlist.remove(u);
  100. break;
  101. }
  102. }
  103. return userlist;
  104. }
  105.  
  106. /**
  107. * deletes a user from database
  108. */
  109. public void delete(){
  110. FacesContext fc = FacesContext.getCurrentInstance();
  111. Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
  112.  
  113. Long id = Long.parseLong(params.get("userId"));
  114. Collection<Tweet> postedTweets = tweetService.findTweetsByUser(id);
  115.  
  116. for(Tweet t: postedTweets){
  117. tweetService.removeTweet(t.getId());
  118. System.out.println("tweetdelete");
  119. }
  120.  
  121. userService.deleteUserByID(id);
  122. }
  123.  
  124. /**
  125. * retrieves data from user and sets it to selectedUser object
  126. */
  127. public void retrieveUser(){
  128. this.selectedUser = userService.findUserById(userId);
  129. }
  130.  
  131. /**
  132. * edits a user object
  133. * @return String
  134. */
  135. public String editUser(){
  136. userService.changeUser(userId, this.selectedUser);
  137. return "/useroverview?faces-redirect=true";
  138. }
  139.  
  140. package domain;
  141.  
  142. import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  143.  
  144. import javax.json.bind.annotation.JsonbTransient;
  145. import javax.persistence.*;
  146. import java.util.ArrayList;
  147. import java.util.List;
  148.  
  149.  
  150. @Entity
  151. @Table(name = "User")
  152. @NamedQueries(value = {
  153. @NamedQuery(name = "User.getAllUsers", query = "SELECT u FROM User u"),
  154. @NamedQuery(name = "User.getUserByID", query = "SELECT u FROM User u WHERE u.id = :userID"),
  155. @NamedQuery(name = "User.getUserByUsername", query = "SELECT u FROM User u WHERE u.username = :username"),
  156. @NamedQuery(name = "User.getUsersByUsername", query = "SELECT u FROM User u WHERE u.username LIKE :username"),
  157. })
  158. public class User {
  159. private static final long serialVersionUID = 1L;
  160.  
  161. @Id
  162. @Column(name = "id")
  163. @GeneratedValue(strategy = GenerationType.IDENTITY)
  164. private Long id;
  165.  
  166. @Column(name = "username", unique = true, length = 50)
  167. private String username;
  168.  
  169. @Column(name = "password", length = 64, nullable = false)
  170. private String password;
  171.  
  172. @Column(name = "email", unique = true, nullable = false)
  173. private String email;
  174.  
  175. @Column(name = "bio", length = 160)
  176. private String bio;
  177.  
  178. @Column(name = "location")
  179. private String location;
  180.  
  181. @Column(name = "website")
  182. private String website;
  183.  
  184. @Enumerated(EnumType.STRING)
  185. @Column(name = "isModerator", nullable = false)
  186. private AuthorizationLevel authorizationLevel;
  187.  
  188. @ManyToMany
  189. @JsonbTransient
  190. @JoinTable(name = "follow",
  191. joinColumns = @JoinColumn(name = "follower_id", referencedColumnName = "id"),
  192. inverseJoinColumns = @JoinColumn(name = "following_id", referencedColumnName = "id"))
  193. private List<User> followers;
  194.  
  195. @ManyToMany(mappedBy = "followers")
  196. @JsonbTransient
  197. private List<User> following;
  198.  
  199. @JsonbTransient
  200. @JoinColumn(name = "posted_tweet")
  201. @OneToMany(mappedBy = "postedBy", cascade = CascadeType.PERSIST)
  202. private List<Tweet> postedTweets;
  203.  
  204. @JsonbTransient
  205. @ManyToMany(mappedBy="likedBy")
  206. private List<Tweet> likedTweets;
  207.  
  208. public User() {
  209. }
  210.  
  211. public User(String username, String password) {
  212. this.username = username;
  213. this.password = password;
  214. }
  215.  
  216. public User(String username, String password, String email, String bio, String location, String website, AuthorizationLevel lvl) {
  217. this.username = username;
  218. this.password = password;
  219. this.email = email;
  220. this.bio = bio;
  221. this.location = location;
  222. this.website = website;
  223. this.authorizationLevel = lvl;
  224. }
  225.  
  226. public String getUsername() {
  227. return username;
  228. }
  229.  
  230. public void setUsername(String username) {
  231. this.username = username;
  232. }
  233.  
  234. public Long getId() {
  235. return id;
  236. }
  237.  
  238. public void setId(Long id) {
  239. this.id = id;
  240. }
  241.  
  242. public List<Tweet> getLikedTweets() {
  243. return likedTweets;
  244. }
  245.  
  246. public void setLikedTweets(List<Tweet> likedTweets) {
  247. this.likedTweets = likedTweets;
  248. }
  249.  
  250. public String getPassword() {
  251.  
  252. return password;
  253. }
  254.  
  255. public void setPassword(String password) {
  256. this.password = password;
  257. }
  258.  
  259. public String getEmail() {
  260. return email;
  261. }
  262.  
  263. public void setEmail(String email) {
  264. this.email = email;
  265. }
  266.  
  267. public String getBio() {
  268. return bio;
  269. }
  270.  
  271. public void setBio(String bio) {
  272. this.bio = bio;
  273. }
  274.  
  275. public String getLocation() {
  276. return location;
  277. }
  278.  
  279. public void setLocation(String location) {
  280. this.location = location;
  281. }
  282.  
  283. public String getWebsite() {
  284. return website;
  285. }
  286.  
  287. public void setWebsite(String website) {
  288. this.website = website;
  289. }
  290.  
  291. public AuthorizationLevel getAuthorizationLevel() {
  292. return authorizationLevel;
  293. }
  294.  
  295. public void setAuthorizationLevel(AuthorizationLevel authorizationLevel) {
  296. this.authorizationLevel = authorizationLevel;
  297. }
  298.  
  299. public List<User> getFollowers() {
  300. if (followers == null) {
  301. followers = new ArrayList<User>();
  302. }
  303. return followers;
  304. }
  305.  
  306. public void setFollowers(List<User> followers) {
  307. this.followers = followers;
  308. }
  309.  
  310. public List<User> getFollowing() {
  311. if (following == null) {
  312. following = new ArrayList<User>();
  313. }
  314. return following;
  315. }
  316.  
  317. public void setFollowing(List<User> following) {
  318. this.following = following;
  319. }
  320.  
  321. public List<Tweet> getPostedTweets() {
  322. if (postedTweets == null) {
  323. postedTweets = new ArrayList<Tweet>();
  324. }
  325. return postedTweets;
  326. }
  327.  
  328. public void setPostedTweets(List<Tweet> postedTweets) {
  329. if (postedTweets == null) {
  330. postedTweets = new ArrayList<Tweet>();
  331. }
  332. this.postedTweets = postedTweets;
  333. }
Add Comment
Please, Sign In to add comment