Skip to content

Commit 157ad5a

Browse files
committed
Lesson_2
1 parent f80fc0d commit 157ad5a

33 files changed

+957
-24
lines changed

pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1818
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1919

20+
<spring.version>4.2.1.RELEASE</spring.version>
21+
2022
<!-- Logging -->
2123
<logback.version>1.1.2</logback.version>
2224
<slf4j.version>1.7.7</slf4j.version>
@@ -76,6 +78,24 @@
7678
<scope>provided</scope>
7779
</dependency>
7880

81+
<dependency>
82+
<groupId>javax.servlet</groupId>
83+
<artifactId>jstl</artifactId>
84+
<version>1.2</version>
85+
</dependency>
86+
87+
<!-- Spring -->
88+
<dependency>
89+
<groupId>org.springframework</groupId>
90+
<artifactId>spring-context</artifactId>
91+
<version>${spring.version}</version>
92+
<exclusions>
93+
<exclusion>
94+
<groupId>commons-logging</groupId>
95+
<artifactId>commons-logging</artifactId>
96+
</exclusion>
97+
</exclusions>
98+
</dependency>
7999
</dependencies>
80100

81101
<profiles>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.javawebinar.topjava;
2+
3+
/**
4+
* GKislin
5+
* 06.03.2015.
6+
*/
7+
public class LoggedUser {
8+
9+
public static int id() {
10+
return 1;
11+
}
12+
}

src/main/java/ru/javawebinar/topjava/LoggerWrapper.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
5+
import ru.javawebinar.topjava.util.exception.NotFoundException;
56

67
/**
78
* User: gkislin
@@ -69,4 +70,9 @@ public UnsupportedOperationException getUnsupportedOperationException(String msg
6970
logger.error(msg);
7071
return new UnsupportedOperationException(msg);
7172
}
72-
}
73+
74+
public NotFoundException getNotFoundException(String reason) {
75+
logger.error("No data found");
76+
return new NotFoundException(reason);
77+
}
78+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package ru.javawebinar.topjava;
2+
3+
import org.springframework.context.ConfigurableApplicationContext;
4+
import org.springframework.context.support.ClassPathXmlApplicationContext;
5+
import ru.javawebinar.topjava.model.Role;
6+
import ru.javawebinar.topjava.model.User;
7+
import ru.javawebinar.topjava.web.user.AdminRestController;
8+
9+
import java.util.Arrays;
10+
11+
/**
12+
* User: gkislin
13+
* Date: 22.08.2014
14+
*/
15+
public class SpringMain {
16+
public static void main(String[] args) {
17+
// java 7 Automatic resource management
18+
try (ConfigurableApplicationContext appCtx = new ClassPathXmlApplicationContext("spring/spring-app.xml")) {
19+
System.out.println(Arrays.toString(appCtx.getBeanDefinitionNames()));
20+
AdminRestController adminUserController = appCtx.getBean(AdminRestController.class);
21+
System.out.println(adminUserController.create(new User(1, "userName", "email", "password", Role.ROLE_ADMIN)));
22+
}
23+
}
24+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package ru.javawebinar.topjava.model;
2+
3+
import ru.javawebinar.topjava.LoggerWrapper;
4+
5+
/**
6+
* User: gkislin
7+
* Date: 22.08.2014
8+
*/
9+
public class BaseEntity {
10+
protected static final LoggerWrapper LOG = LoggerWrapper.get(BaseEntity.class);
11+
12+
protected Integer id;
13+
14+
public BaseEntity() {
15+
}
16+
17+
protected BaseEntity(Integer id) {
18+
this.id = id;
19+
}
20+
21+
public void setId(Integer id) {
22+
this.id = id;
23+
}
24+
25+
public Integer getId() {
26+
return id;
27+
}
28+
29+
public boolean isNew() {
30+
return (this.id == null);
31+
}
32+
33+
@Override
34+
public boolean equals(Object o) {
35+
if (this == o) {
36+
return true;
37+
}
38+
if (o == null || getClass() != o.getClass()) {
39+
return false;
40+
}
41+
BaseEntity that = (BaseEntity) o;
42+
if (id == null || that.id == null) {
43+
throw LOG.getIllegalStateException("Equals '" + this + "' and '" + that + "' with null id");
44+
}
45+
return id.equals(that.id);
46+
}
47+
48+
@Override
49+
public int hashCode() {
50+
return (id == null) ? 0 : id;
51+
}
52+
53+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package ru.javawebinar.topjava.model;
2+
3+
/**
4+
* User: gkislin
5+
* Date: 22.08.2014
6+
*/
7+
public class NamedEntity extends BaseEntity {
8+
9+
protected String name;
10+
11+
public NamedEntity() {
12+
}
13+
14+
protected NamedEntity(Integer id, String name) {
15+
super(id);
16+
this.name = name;
17+
}
18+
19+
public void setName(String name) {
20+
this.name = name;
21+
}
22+
23+
public String getName() {
24+
return this.name;
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return name;
30+
}
31+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ru.javawebinar.topjava.model;
2+
3+
/**
4+
* User: gkislin
5+
* Date: 22.08.2014
6+
*/
7+
public enum Role {
8+
ROLE_USER,
9+
ROLE_ADMIN
10+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package ru.javawebinar.topjava.model;
2+
3+
import java.util.Date;
4+
import java.util.EnumSet;
5+
import java.util.Set;
6+
7+
/**
8+
* User: gkislin
9+
* Date: 22.08.2014
10+
*/
11+
public class User extends NamedEntity {
12+
13+
private static final int DEFAULT_CALORIES_PER_DAY = 2000;
14+
15+
protected String email;
16+
17+
protected String password;
18+
19+
protected boolean enabled = true;
20+
21+
protected Date registered = new Date();
22+
23+
protected Set<Role> roles;
24+
25+
protected int caloriesPerDay = DEFAULT_CALORIES_PER_DAY;
26+
27+
public User() {
28+
}
29+
30+
public User(Integer id, String name, String email, String password, Role role, Role... roles) {
31+
this(id, name, email, password, DEFAULT_CALORIES_PER_DAY, true, EnumSet.of(role, roles));
32+
}
33+
34+
public User(Integer id, String name, String email, String password, int caloriesPerDay, boolean enabled, Set<Role> roles) {
35+
super(id, name);
36+
this.email = email;
37+
this.password = password;
38+
this.caloriesPerDay = caloriesPerDay;
39+
this.enabled = enabled;
40+
this.roles = roles;
41+
}
42+
43+
public String getEmail() {
44+
return email;
45+
}
46+
47+
public void setEmail(String email) {
48+
this.email = email;
49+
}
50+
51+
public void setPassword(String password) {
52+
this.password = password;
53+
}
54+
55+
public Date getRegistered() {
56+
return registered;
57+
}
58+
59+
public void setRegistered(Date registered) {
60+
this.registered = registered;
61+
}
62+
63+
public void setEnabled(boolean enabled) {
64+
this.enabled = enabled;
65+
}
66+
67+
public int getCaloriesPerDay() {
68+
return caloriesPerDay;
69+
}
70+
71+
public void setCaloriesPerDay(int caloriesPerDay) {
72+
this.caloriesPerDay = caloriesPerDay;
73+
}
74+
75+
public boolean isEnabled() {
76+
return enabled;
77+
}
78+
79+
public Set<Role> getRoles() {
80+
return roles;
81+
}
82+
83+
public String getPassword() {
84+
return password;
85+
}
86+
87+
@Override
88+
public String toString() {
89+
return "User (" +
90+
"id=" + id +
91+
", email=" + email +
92+
", name=" + name +
93+
", enabled=" + enabled +
94+
", roles=" + roles +
95+
", caloriesPerDay=" + caloriesPerDay +
96+
')';
97+
}
98+
}

src/main/java/ru/javawebinar/topjava/model/UserMeal.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,29 @@
77
* 11.01.2015.
88
*/
99
public class UserMeal {
10+
protected Integer id;
11+
1012
protected final LocalDateTime dateTime;
1113

1214
protected final String description;
1315

1416
protected final int calories;
1517

1618
public UserMeal(LocalDateTime dateTime, String description, int calories) {
19+
this(null, dateTime, description, calories);
20+
}
21+
22+
public UserMeal(Integer id, LocalDateTime dateTime, String description, int calories) {
23+
this.id = id;
1724
this.dateTime = dateTime;
1825
this.description = description;
1926
this.calories = calories;
2027
}
2128

29+
public void setId(int id) {
30+
this.id = id;
31+
}
32+
2233
public LocalDateTime getDateTime() {
2334
return dateTime;
2435
}
@@ -30,4 +41,22 @@ public String getDescription() {
3041
public int getCalories() {
3142
return calories;
3243
}
44+
45+
public Integer getId() {
46+
return id;
47+
}
48+
49+
public boolean isNew() {
50+
return id == null;
51+
}
52+
53+
@Override
54+
public String toString() {
55+
return "UserMeal{" +
56+
"id=" + id +
57+
", dateTime=" + dateTime +
58+
", description='" + description + '\'' +
59+
", calories=" + calories +
60+
'}';
61+
}
3362
}

src/main/java/ru/javawebinar/topjava/model/UserMealWithExceed.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* 11.01.2015.
88
*/
99
public class UserMealWithExceed {
10+
protected Integer id;
11+
1012
protected final LocalDateTime dateTime;
1113

1214
protected final String description;
@@ -16,16 +18,42 @@ public class UserMealWithExceed {
1618
protected final boolean exceed;
1719

1820
public UserMealWithExceed(LocalDateTime dateTime, String description, int calories, boolean exceed) {
21+
this(null, dateTime, description, calories, exceed);
22+
}
23+
24+
public UserMealWithExceed(Integer id, LocalDateTime dateTime, String description, int calories, boolean exceed) {
25+
this.id = id;
1926
this.dateTime = dateTime;
2027
this.description = description;
2128
this.calories = calories;
2229
this.exceed = exceed;
2330
}
2431

32+
public Integer getId() {
33+
return id;
34+
}
35+
36+
public LocalDateTime getDateTime() {
37+
return dateTime;
38+
}
39+
40+
public String getDescription() {
41+
return description;
42+
}
43+
44+
public int getCalories() {
45+
return calories;
46+
}
47+
48+
public boolean isExceed() {
49+
return exceed;
50+
}
51+
2552
@Override
2653
public String toString() {
2754
return "UserMealWithExceed{" +
28-
"dateTime=" + dateTime +
55+
"id=" + id +
56+
", dateTime=" + dateTime +
2957
", description='" + description + '\'' +
3058
", calories=" + calories +
3159
", exceed=" + exceed +

0 commit comments

Comments
 (0)