Guest User

Untitled

a guest
Jun 27th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. @Entity
  2. @Table(name = "users")
  3. public class User implements Serializable{
  4.  
  5. /**
  6. *
  7. */
  8. private static final long serialVersionUID = 1L;
  9.  
  10. @Id
  11. @GeneratedValue(strategy = GenerationType.IDENTITY)
  12. private long id;
  13.  
  14. @Column(unique = true)
  15. @NotBlank
  16. @Size(min=4,max=20)
  17. private String login;
  18.  
  19. @NotBlank
  20. private String password;
  21.  
  22. @Column(unique = true)
  23. @Email
  24. @NotBlank
  25. private String email;
  26.  
  27. private String permission;
  28.  
  29. @OneToMany()
  30. private List<Account> accounts;
  31.  
  32.  
  33. public User(final String login, final String password, final String email) {
  34. Preconditions.checkArgument(!Strings.isNullOrEmpty(login));
  35. Preconditions.checkArgument(!Strings.isNullOrEmpty(password));
  36. Preconditions.checkArgument(!Strings.isNullOrEmpty(email));
  37. this.login = login;
  38. this.password = password;
  39. this.email = email;
  40. }
  41.  
  42. public User() {
  43. }
  44. }
  45. + get/set
  46.  
  47. @Entity
  48. @Table(name = "accounts")
  49. public class Account {
  50.  
  51.  
  52. @Id
  53. @GeneratedValue(strategy = GenerationType.AUTO)
  54. private long id;
  55.  
  56. private boolean ifBasicAccount;
  57.  
  58. private String accTitle;
  59.  
  60. private String accFirstName;
  61.  
  62. private String accLastName;
  63.  
  64. private String accBirthdate;
  65.  
  66. private String accPhoneNumber;
  67.  
  68. private String accEducation;
  69.  
  70. private String accExperience;
  71.  
  72. private String accAbilities;
  73.  
  74. private String accInterests;
  75.  
  76. private String accProjects;
  77.  
  78. private String accDescription;
  79.  
  80. @Lob
  81. private byte[] accPicture;
  82.  
  83.  
  84. @ManyToOne
  85. private User user;
  86.  
  87.  
  88. public Account() {
  89. }
  90.  
  91. + get/set
  92.  
  93. @Controller
  94. public class AccountController {
  95.  
  96. @Autowired
  97. AccountRepository accountRepository;
  98.  
  99. @Autowired
  100. UserRepository userRepository;
  101.  
  102.  
  103. @RequestMapping(method = RequestMethod.GET, value ="addAccount")
  104. public String addAccount(Model model) {
  105. Account account = new Account();
  106. model.addAttribute("account", account);
  107.  
  108. return "addAccount";
  109. }
  110.  
  111. @RequestMapping(method = RequestMethod.POST, value ="addAccount")
  112. public String addAccount(@ModelAttribute Account account, HttpSession session) {
  113. User user = userRepository.findOne((Long) session.getAttribute("user_id"));
  114. account.setIfBasicAccount(false);
  115. account.setUser(user);
  116. accountRepository.save(account);
  117. return "redirect:/accounts";
  118. }
  119.  
  120. @RequestMapping("/accounts")
  121. public String accountList(Model model, HttpSession ses) {
  122. long userId = (Long) ses.getAttribute("user_id");
  123. List<Account> accounts = accountRepository.findUserAccounts(userId);
  124. model.addAttribute("accounts", accounts);
  125. return "accounts";
  126. }
  127.  
  128. @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
  129. public String editAccountForm(Model model, @PathVariable long id) {
  130. Account account = accountRepository.findOne(id);
  131. model.addAttribute("account",account);
  132. return "editAccountForm";
  133. }
  134.  
  135. @RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
  136. public String editAccount(@ModelAttribute Account account, @PathVariable long id) {
  137. Account accountToUpdate = accountRepository.findOne(id);
  138. accountToUpdate.setAccTitle(account.getAccTitle());
  139. accountToUpdate.setAccFirstName(account.getAccFirstName());
  140. accountToUpdate.setAccLastName(account.getAccLastName());
  141. accountToUpdate.setAccBirthdate(account.getAccBirthdate());
  142. accountToUpdate.setAccPhoneNumber(account.getAccPhoneNumber());
  143. accountToUpdate.setAccEducation(account.getAccEducation());
  144. accountToUpdate.setAccExperience(account.getAccExperience());
  145. accountToUpdate.setAccAbilities(account.getAccAbilities());
  146. accountToUpdate.setAccInterests(account.getAccInterests());
  147. accountToUpdate.setAccProjects(account.getAccProjects());
  148. accountToUpdate.setAccDescription(account.getAccDescription());
  149. accountRepository.save(accountToUpdate);
  150. return "redirect:/accounts";
  151. }
  152.  
  153. @RequestMapping("/delete")
  154. public String deleteAccount(Model model) {
  155. return "deleteAccount";
  156. }
  157.  
  158. @RequestMapping("/read/{id}")
  159. public String read(@PathVariable long id) {
  160. return accountRepository.findOne(id).toString();
  161. }
  162.  
  163. @RequestMapping("/delete/{id}")
  164. public String delete(@PathVariable long id) {
  165. Account account = accountRepository.findOne(id);
  166. accountRepository.delete(account);
  167. return "redirect:/accounts";
  168. }
  169. }
  170.  
  171. @Controller
  172. @RequestMapping("/user")
  173. public class ImageController {
  174.  
  175. private AccountRepository accountRepository;
  176.  
  177. @RequestMapping(value = "/accounts", method = RequestMethod.GET)
  178. public void showImage(@RequestParam("id") Long id, HttpServletResponse response, HttpServletRequest request)
  179. throws ServletException, IOException {
  180.  
  181. Account account = accountRepository.getOne(id);
  182. response.setContentType("image/jpeg, image/jpg, image/png, image/gif");
  183. response.getOutputStream().write(account.getAccPicture());
  184.  
  185. response.getOutputStream().close();
  186. }
  187. }
  188.  
  189. <%@ page language="java" contentType="text/html; charset=UTF-8"
  190. pageEncoding="UTF-8"%>
  191. <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
  192. <%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
  193. <%@ page isELIgnored="false" %>
  194. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  195. <%@ include file="/WEB-INF/parts/header.jsp" %>
  196. <html>
  197. <head>
  198. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  199. <title>Insert title here</title>
  200. </head>
  201. <body>
  202. <div align="center">
  203. <table class="table table-striped">
  204. <h1>Accounts:</h1>
  205. <c:forEach items="${accounts}" var="account" begin="0" varStatus="theCount">
  206.  
  207. <tr>
  208. <td>${theCount.index+1}</td>
  209. <td><b>Nazwa: </b>${account.accTitle}</td>
  210. <td><b>Opis: </b>${account.accDescription}</td>
  211. <td><img src="/ProjectProfile/user/accounts?id=${account.id}"/></td>
  212. <td><a style="width: 180px;height: 20px;" href="./edit/${account.id}" class="badge badge-primary">Show/Edit</a></td>
  213. <td><a style="width: 180px;height: 20px;" href="./delete/${account.id}" class="badge badge-danger">Delete</a></td>
  214. </tr>
  215. </c:forEach>
  216. </table>
  217.  
  218. <a href="<c:url value="/addAccount"/>">Add Account</a>
  219.  
  220. </body>
  221. </html>
Add Comment
Please, Sign In to add comment