Advertisement
Guest User

Untitled

a guest
Feb 10th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.82 KB | None | 0 0
  1. package com.technologies.Lunch_Voting.model;
  2.  
  3. import javax.persistence.*;
  4.  
  5. /**
  6. * Created by medniy on 19.01.2017.
  7. */
  8.  
  9. @MappedSuperclass
  10. @Access(AccessType.FIELD)
  11. public class BaseEntity {
  12. public static final int START_SEQ = 100000;
  13.  
  14. @Id
  15. @GeneratedValue(strategy = GenerationType.IDENTITY)
  16. protected Integer id;
  17.  
  18. public BaseEntity() {
  19. }
  20.  
  21. public BaseEntity(Integer id) {
  22. this.id = id;
  23.  
  24. }
  25.  
  26. public int getId() {
  27. return id;
  28. }
  29.  
  30. public void setId(Integer id) {
  31. this.id = id;
  32. }
  33.  
  34.  
  35. public boolean isNew(){
  36. return (this.id == null);
  37. }
  38.  
  39. @Override
  40. public boolean equals(Object o) {
  41. if (this == o) {
  42. return true;
  43. }
  44. if (o == null || getClass() != o.getClass()) {
  45. return false;
  46. }
  47. BaseEntity that = (BaseEntity) o;
  48. if (id == null || that.id == null) {
  49. return false;
  50. }
  51. return id.equals(that.id);
  52. }
  53.  
  54. @Override
  55. public int hashCode() {
  56. return (id == null) ? 0 : id;
  57. }
  58.  
  59. @Override
  60. public String toString() {
  61. return "BaseEntity{" +
  62. "id=" + id +
  63. '}';
  64. }
  65. }
  66.  
  67. package com.technologies.Lunch_Voting.model;
  68.  
  69. import org.hibernate.validator.constraints.NotEmpty;
  70.  
  71. import javax.persistence.Column;
  72. import javax.persistence.MappedSuperclass;
  73.  
  74. /**
  75. * Created by medniy on 19.01.2017.
  76. */
  77.  
  78. @MappedSuperclass
  79. public class NamedEntity extends BaseEntity{
  80.  
  81. @NotEmpty
  82. @Column(name = "name", nullable = false)
  83. protected String name;
  84.  
  85. public NamedEntity() {
  86. }
  87.  
  88. public NamedEntity(int id, String name) {
  89. super(id);
  90. this.name = name;
  91. }
  92.  
  93. public String getName() {
  94. return name;
  95. }
  96.  
  97. public void setName(String name) {
  98. this.name = name;
  99. }
  100.  
  101. @Override
  102. public String toString() {
  103. return name;
  104. }
  105.  
  106. }
  107.  
  108. package com.technologies.Lunch_Voting.model;
  109.  
  110. import org.hibernate.annotations.Cache;
  111. import org.hibernate.annotations.CacheConcurrencyStrategy;
  112. import org.hibernate.validator.constraints.Email;
  113. import org.hibernate.validator.constraints.Length;
  114. import org.hibernate.validator.constraints.NotEmpty;
  115.  
  116. import javax.persistence.*;
  117. import java.time.LocalDateTime;
  118. import java.util.Set;
  119.  
  120. /**
  121. * Created by medniy on 19.01.2017.
  122. */
  123.  
  124. @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
  125. @Entity
  126. @Table(name = "users", uniqueConstraints = {@UniqueConstraint(columnNames = "email", name = "users_unique_email_idx")})
  127. public class User extends NamedEntity {
  128.  
  129. @Column(name = "password", nullable = false)
  130. @NotEmpty
  131. @Length(min = 5)
  132. private String password;
  133.  
  134. @Column(name = "email", nullable = false, unique = true)
  135. @Email
  136. @NotEmpty
  137. private String email;
  138.  
  139. @Column(name = "enabled", nullable = false)
  140. private boolean enabled = true;
  141.  
  142. @Column(name = "registered", columnDefinition = "timestamp default now()")
  143. private LocalDateTime registered;
  144.  
  145. @Enumerated(EnumType.STRING)
  146. @CollectionTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"))
  147. @Column(name = "role")
  148. @ElementCollection(fetch = FetchType.EAGER)
  149. @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
  150. private Set<Role> roles;
  151.  
  152. @OneToOne(fetch= FetchType.LAZY, mappedBy="user")
  153. private Vote vote;
  154.  
  155. public User(){
  156. }
  157.  
  158. public User(Integer id, String name, String password, String email, boolean enabled, LocalDateTime registered, Set<Role> roles, Vote vote) {
  159. super(id, name);
  160. this.password = password;
  161. this.email = email;
  162. this.enabled = enabled;
  163. this.registered = registered;
  164. this.roles = roles;
  165. this.vote = vote;
  166. }
  167.  
  168. public User(String name, String password, String email, boolean enabled, LocalDateTime registered, Set<Role> roles) {
  169. this(null,name,password,email,enabled,registered,roles,null);
  170. }
  171.  
  172. public String getPassword() {
  173. return password;
  174. }
  175.  
  176. public void setPassword(String password) {
  177. this.password = password;
  178. }
  179.  
  180. public String getEmail() {
  181. return email;
  182. }
  183.  
  184. public void setEmail(String email) {
  185. this.email = email;
  186. }
  187.  
  188. public boolean isEnabled() {
  189. return enabled;
  190. }
  191.  
  192. public void setEnabled(boolean enabled) {
  193. this.enabled = enabled;
  194. }
  195.  
  196. public LocalDateTime getRegistered() {
  197. return registered;
  198. }
  199.  
  200. public void setRegistered(LocalDateTime registered) {
  201. this.registered = registered;
  202. }
  203.  
  204. public Set<Role> getRoles() {
  205. return roles;
  206. }
  207.  
  208. public void setRoles(Set<Role> roles) {
  209. this.roles = roles;
  210. }
  211.  
  212. public Vote getVote() {
  213. return vote;
  214. }
  215.  
  216. public void setVote(Vote vote) {
  217. this.vote = vote;
  218. }
  219.  
  220. @Override
  221. public String toString() {
  222. return "User{" +
  223. "id=" + id +
  224. ", name=" + name +
  225. ", email='" + email +
  226. ", enabled=" + enabled +
  227. ", registered=" + registered +
  228. ", roles=" + roles +
  229. ", vote=" + vote +
  230. "} ";
  231. }
  232. }
  233.  
  234. package com.technologies.Lunch_Voting.model;
  235.  
  236. import org.hibernate.validator.constraints.Range;
  237.  
  238. import javax.persistence.*;
  239. import java.time.LocalDateTime;
  240.  
  241. /**
  242. * Created by medniy on 19.01.2017.
  243. */
  244.  
  245. @Entity
  246. @Table(name = "votes", uniqueConstraints = {@UniqueConstraint(columnNames = "user_id", name = "vote_unique_user_idx")})
  247. public class Vote extends BaseEntity{
  248.  
  249. @Column(name = "mark", nullable = false)
  250. @Range(min = 0, max = 100)
  251. private Integer mark;
  252.  
  253. @Column(name = "marked", columnDefinition = "timestamp default now()")
  254. private LocalDateTime marked;
  255.  
  256.  
  257. @OneToOne(fetch= FetchType.LAZY)
  258. @JoinColumn(name="user_id")
  259. private User user;
  260.  
  261. @ManyToOne(fetch=FetchType.LAZY)
  262. @JoinColumn(name="restaurant_id")
  263. private Restaurant restaurant;
  264.  
  265. public Vote() {
  266. }
  267.  
  268. public Vote(Integer id, Integer mark, LocalDateTime marked) {
  269. super(id);
  270. this.mark = mark;
  271. this.marked = marked;
  272. }
  273.  
  274. public Vote(Integer mark, LocalDateTime marked) {
  275. this(null,mark,marked);
  276. }
  277.  
  278.  
  279. public Integer getMark() {
  280. return mark;
  281. }
  282.  
  283. public void setMark(Integer mark) {
  284. this.mark = mark;
  285. }
  286.  
  287. public LocalDateTime getMarked() {
  288. return marked;
  289. }
  290.  
  291. public void setMarked(LocalDateTime marked) {
  292. this.marked = marked;
  293. }
  294.  
  295. public User getUser() {
  296. return user;
  297. }
  298.  
  299. public void setUser(User user) {
  300. this.user = user;
  301. }
  302.  
  303. public Restaurant getRestaurant() {
  304. return restaurant;
  305. }
  306.  
  307. public void setRestaurant(Restaurant restaurant) {
  308. this.restaurant = restaurant;
  309. }
  310.  
  311. @Override
  312. public String toString() {
  313. return "Vote{" +
  314. "id=" + id +
  315. ", mark=" + mark +
  316. ", marked=" + marked +
  317. '}';
  318. }
  319. }
  320.  
  321. package com.technologies.Lunch_Voting.model;
  322.  
  323. import org.hibernate.validator.constraints.NotEmpty;
  324.  
  325. import javax.persistence.*;
  326. import javax.validation.constraints.NotNull;
  327. import java.time.LocalDateTime;
  328. import java.util.Collections;
  329. import java.util.List;
  330.  
  331. /**
  332. * Created by medniy on 19.01.2017.
  333. */
  334. @Entity
  335. @Table(name = "restaurants")
  336. public class Restaurant extends NamedEntity {
  337.  
  338. @Column(name = "created", nullable = false)
  339. @NotNull
  340. private LocalDateTime created;
  341.  
  342. @Column(name = "description", nullable = false)
  343. @NotEmpty
  344. private String description;
  345.  
  346. @OneToMany(mappedBy="restaurant")
  347. private List<Dish> menu;
  348.  
  349. @OneToMany(mappedBy="restaurant")
  350. private List<Vote> votes;
  351.  
  352. @ManyToOne(fetch= FetchType.LAZY)
  353. @JoinColumn(name="user_id")
  354. private User user;
  355.  
  356. public Restaurant() {
  357. }
  358.  
  359. public Restaurant(Integer id, String name, LocalDateTime created, String description, List<Dish> menu, List<Vote> votes) {
  360. super(id, name);
  361. this.created = created;
  362. this.description = description;
  363. this.menu = menu;
  364. this.votes = votes;
  365. }
  366.  
  367. public Restaurant(String name, LocalDateTime registered, String description) {
  368. this(null,name,registered,description, Collections.EMPTY_LIST, Collections.EMPTY_LIST);
  369. }
  370.  
  371. public LocalDateTime getCreated() {
  372. return created;
  373. }
  374.  
  375. public void setCreated(LocalDateTime created) {
  376. this.created = created;
  377. }
  378.  
  379. public String getDescription() {
  380. return description;
  381. }
  382.  
  383. public void setDescription(String description) {
  384. this.description = description;
  385. }
  386.  
  387. public List<Dish> getMenu() {
  388. return menu;
  389. }
  390.  
  391. public void setMenu(List<Dish> menu) {
  392. this.menu = menu;
  393. }
  394.  
  395. public List<Vote> getVotes() {
  396. return votes;
  397. }
  398.  
  399. public void setVotes(List<Vote> votes) {
  400. this.votes = votes;
  401. }
  402.  
  403. public User getUser() {
  404. return user;
  405. }
  406.  
  407. public void setUser(User user) {
  408. this.user = user;
  409. }
  410.  
  411. public int getTotalMark(){
  412. int mark = 0;
  413. if (votes.size()>1)
  414. mark = this.votes.stream().mapToInt((v) -> v.getMark()).sum();
  415. return mark;
  416. }
  417.  
  418. @Override
  419. public String toString() {
  420. return "Restaurant{" +
  421. "id=" + id +
  422. ", name=" + name +
  423. ", created=" + created +
  424. ", description=" + description +
  425. ", menu=" + menu +
  426. ",v otes=" + votes +
  427. '}';
  428. }
  429. }
  430.  
  431. package com.technologies.Lunch_Voting.model;
  432.  
  433. import org.hibernate.validator.constraints.NotEmpty;
  434. import org.hibernate.validator.constraints.Range;
  435.  
  436. import javax.persistence.*;
  437.  
  438. /**
  439. * Created by medniy on 19.01.2017.
  440. */
  441.  
  442. @Entity
  443. @Table(name = "dishes", uniqueConstraints = {@UniqueConstraint(columnNames = {"id", "restaurant_id"}, name = "dishes_unique_rest_id_idx")})
  444. public class Dish extends NamedEntity{
  445.  
  446. @Column(name = "description", nullable = false)
  447. @NotEmpty
  448. private String description;
  449.  
  450. @Column(name = "price", nullable = false)
  451. @Range(min = 0, max = 50000)
  452. private Integer price;
  453.  
  454. @ManyToOne(fetch= FetchType.EAGER)
  455. @JoinColumn(name="restaurant_id")
  456. private Restaurant restaurant;
  457.  
  458. public Dish(){
  459.  
  460. }
  461.  
  462. public Dish(Integer id ,String name, String description, Integer price) {
  463. super(id, name);
  464. this.description = description;
  465. this.price = price;
  466. }
  467. public Dish(String name, String description, Integer price) {
  468. this(null,name,description,price);
  469. }
  470.  
  471. public Integer getPrice() {
  472. return price;
  473. }
  474.  
  475. public void setPrice(Integer price) {
  476. this.price = price;
  477. }
  478.  
  479. public String getDescription() {
  480. return description;
  481. }
  482.  
  483. public void setDescription(String description) {
  484. this.description = description;
  485. }
  486.  
  487. public Restaurant getRestaurant() {
  488. return restaurant;
  489. }
  490.  
  491. public void setRestaurant(Restaurant restaurant) {
  492. this.restaurant = restaurant;
  493. }
  494.  
  495. @Override
  496. public String toString() {
  497. return "Dish{" +
  498. "id=" + id +
  499. ", name='" + name +
  500. ", description='" + description +
  501. ", price='" + price +
  502. '}';
  503. }
  504. }
  505.  
  506. public class Main {
  507. public static void main(String[] args) {
  508. try (ConfigurableApplicationContext appCtx = new ClassPathXmlApplicationContext("spring/spring_app.xml", "spring/spring_db.xml")) {
  509. System.out.println("Bean definition names: " + Arrays.toString(appCtx.getBeanDefinitionNames()));
  510.  
  511. AdminRestController adminUserController = appCtx.getBean(AdminRestController.class);
  512. System.out.println(adminUserController.get(100001));
  513.  
  514. VoteRestController voteRestController = appCtx.getBean(VoteRestController.class);
  515. System.out.println(voteRestController.get());
  516.  
  517.  
  518. }
  519. }
  520. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement