This repository was archived by the owner on Mar 14, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathMeal.java
More file actions
135 lines (111 loc) · 4.32 KB
/
Copy pathMeal.java
File metadata and controls
135 lines (111 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package ru.javawebinar.topjava.model;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonView;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.Range;
import org.hibernate.validator.constraints.SafeHtml;
import org.springframework.format.annotation.DateTimeFormat;
import ru.javawebinar.topjava.View;
import ru.javawebinar.topjava.util.DateTimeUtil;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
@SuppressWarnings("JpaQlInspection")
@NamedQueries({
@NamedQuery(name = Meal.ALL_SORTED, query = "SELECT m FROM Meal m WHERE m.user.id=:userId ORDER BY m.dateTime DESC"),
@NamedQuery(name = Meal.DELETE, query = "DELETE FROM Meal m WHERE m.id=:id AND m.user.id=:userId"),
@NamedQuery(name = Meal.GET_BETWEEN, query = "SELECT m FROM Meal m " +
"WHERE m.user.id=:userId AND m.dateTime BETWEEN :startDate AND :endDate ORDER BY m.dateTime DESC"),
// @NamedQuery(name = Meal.UPDATE, query = "UPDATE Meal m SET m.dateTime = :datetime, m.calories= :calories," +
// "m.description=:desc where m.id=:id and m.user.id=:userId")
})
@Entity
@Table(name = "meals", uniqueConstraints = {@UniqueConstraint(columnNames = {"user_id", "date_time"}, name = "meals_unique_user_datetime_idx")})
public class Meal extends BaseEntity {
public static final String ALL_SORTED = "Meal.getAll";
public static final String DELETE = "Meal.delete";
public static final String GET_BETWEEN = "Meal.getBetween";
@Column(name = "date_time", nullable = false)
@NotNull(groups = {View.ValidatedRestUI.class, Default.class})
@JsonView(View.JsonREST.class)
private LocalDateTime dateTime;
@Column(name = "description", nullable = false)
@NotBlank(groups = {View.ValidatedRestUI.class, Default.class})
@SafeHtml(groups = {View.ValidatedRestUI.class})
private String description;
@Column(name = "calories", nullable = false)
@Range(min = 10, max = 5000, groups = {View.ValidatedRestUI.class, Default.class})
@NotNull(groups = {View.ValidatedRestUI.class, Default.class})
private Integer calories;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@NotNull
private User user;
public Meal() {
}
public Meal(LocalDateTime dateTime, String description, int calories) {
this(null, dateTime, description, calories);
}
public Meal(Integer id, LocalDateTime dateTime, String description, int calories) {
super(id);
this.dateTime = dateTime;
this.description = description;
this.calories = calories;
}
public LocalDateTime getDateTime() {
return dateTime;
}
public String getDescription() {
return description;
}
public int getCalories() {
return calories;
}
public LocalDate getDate() {
return dateTime.toLocalDate();
}
public LocalTime getTime() {
return dateTime.toLocalTime();
}
public void setDateTime(LocalDateTime dateTime) {
this.dateTime = dateTime;
}
public void setDescription(String description) {
this.description = description;
}
public void setCalories(Integer calories) {
this.calories = calories;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@JsonGetter
@JsonView(View.JsonUI.class)
@JsonFormat(pattern = DateTimeUtil.DATE_TIME_PATTERN)
public LocalDateTime getDateTimeUI() {
return dateTime;
}
@DateTimeFormat(pattern = DateTimeUtil.DATE_TIME_PATTERN)
public void setDateTimeUI(LocalDateTime dateTime) {
this.dateTime = dateTime;
}
@Override
public String toString() {
return "Meal{" +
"id=" + getId() +
", dateTime=" + dateTime +
", description='" + description + '\'' +
", calories=" + calories +
'}';
}
}