Skip to content

Commit a3e8faa

Browse files
committed
URL PATH of Actions - Controller Methods Refactoring
1 parent 706d4c6 commit a3e8faa

20 files changed

+156
-239
lines changed
Lines changed: 15 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.woehlke.simpleworklist.context;
22

3+
import lombok.*;
34
import org.hibernate.validator.constraints.Length;
45
import javax.validation.constraints.NotBlank;
56
import org.hibernate.validator.constraints.SafeHtml;
@@ -29,26 +30,31 @@
2930
@Index(name = "ix_context_row_created_at", columnList = "row_created_at")
3031
}
3132
)
33+
@Getter
34+
@Setter
35+
@NoArgsConstructor
36+
@EqualsAndHashCode
37+
@ToString
3238
public class Context extends AuditModel implements Serializable, ComparableById<Context> {
3339

3440
private static final long serialVersionUID = -5035732370606951871L;
3541

3642
@Id
3743
@GeneratedValue(generator = "context_generator")
3844
@SequenceGenerator(
39-
name = "context_generator",
40-
sequenceName = "context_sequence",
41-
initialValue = 1000
45+
name = "context_generator",
46+
sequenceName = "context_sequence",
47+
initialValue = 1000
4248
)
4349
private Long id;
4450

4551
@ManyToOne(
46-
fetch = FetchType.LAZY,
47-
optional = false,
48-
cascade = {
49-
CascadeType.MERGE,
50-
CascadeType.REFRESH
51-
})
52+
fetch = FetchType.LAZY,
53+
optional = false,
54+
cascade = {
55+
CascadeType.MERGE,
56+
CascadeType.REFRESH
57+
})
5258
@JoinColumn(name = "user_account_id")
5359
private UserAccount userAccount;
5460

@@ -64,9 +70,6 @@ public class Context extends AuditModel implements Serializable, ComparableById<
6470
@Column(name = "name_en", nullable = false)
6571
private String nameEn;
6672

67-
public Context() {
68-
}
69-
7073
public Context(String nameDe, String nameEn) {
7174
this.nameDe = nameDe;
7275
this.nameEn = nameEn;
@@ -99,66 +102,4 @@ public boolean equalsByUuid(Context otherObject) {
99102
return super.equalsByMyUuid(otherObject);
100103
}
101104

102-
public Long getId() {
103-
return id;
104-
}
105-
106-
public void setId(Long id) {
107-
this.id = id;
108-
}
109-
110-
public UserAccount getUserAccount() {
111-
return userAccount;
112-
}
113-
114-
public void setUserAccount(UserAccount userAccount) {
115-
this.userAccount = userAccount;
116-
}
117-
118-
public String getNameDe() {
119-
return nameDe;
120-
}
121-
122-
public void setNameDe(String name) {
123-
this.nameDe = name;
124-
}
125-
126-
public String getNameEn() {
127-
return nameEn;
128-
}
129-
130-
public void setNameEn(String nameEn) {
131-
this.nameEn = nameEn;
132-
}
133-
134-
@Override
135-
public boolean equals(Object o) {
136-
if (this == o) return true;
137-
if (!(o instanceof Context)) return false;
138-
if (!super.equals(o)) return false;
139-
Context context = (Context) o;
140-
return Objects.equals(getId(), context.getId()) &&
141-
getUserAccount().equals(context.getUserAccount()) &&
142-
getNameDe().equals(context.getNameDe()) &&
143-
getNameEn().equals(context.getNameEn());
144-
}
145-
146-
@Override
147-
public int hashCode() {
148-
return Objects.hash(super.hashCode(), getId(), getUserAccount(), getNameDe(), getNameEn());
149-
}
150-
151-
@Override
152-
public String toString() {
153-
return "Context{" +
154-
"id=" + id +
155-
", userAccount=" + userAccount +
156-
", nameDe='" + nameDe + '\'' +
157-
", nameEn='" + nameEn + '\'' +
158-
", uuid='" + uuid + '\'' +
159-
", rowCreatedAt=" + rowCreatedAt +
160-
", rowUpdatedAt=" + rowUpdatedAt +
161-
'}';
162-
}
163-
164105
}

src/main/java/org/woehlke/simpleworklist/context/ContextController.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ public ContextController(ContextService contextService) {
2828
}
2929

3030
@RequestMapping(path = "/choose/{newContextId}", method = RequestMethod.GET)
31-
public String switchContxt(@PathVariable("newContextId") Context setContext,
32-
@ModelAttribute("userSession") UserSessionBean userSession, Model model){
31+
public String switchContxt(
32+
@PathVariable("newContextId") Context setContext,
33+
@ModelAttribute("userSession") UserSessionBean userSession,
34+
Model model
35+
){
36+
log.info("switchContxt");
3337
Context isContext = super.getContext(userSession);
3438
if (setContext != null) {
3539
userSession.setContextId(setContext.getId());

src/main/java/org/woehlke/simpleworklist/context/ContextServiceImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.woehlke.simpleworklist.context;
22

3+
import lombok.extern.slf4j.Slf4j;
34
import org.springframework.stereotype.Service;
45
import org.springframework.transaction.annotation.Propagation;
56
import org.springframework.transaction.annotation.Transactional;
@@ -13,14 +14,13 @@
1314
/**
1415
* Created by tw on 13.03.16.
1516
*/
17+
@Slf4j
1618
@Service
1719
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
1820
public class ContextServiceImpl implements ContextService {
1921

2022
private final ContextRepository contextRepository;
21-
2223
private final TaskRepository taskRepository;
23-
2424
private final ProjectRepository projectRepository;
2525

2626
@Autowired
@@ -32,11 +32,13 @@ public ContextServiceImpl(ContextRepository contextRepository, TaskRepository ta
3232

3333
@Override
3434
public List<Context> getAllForUser(UserAccount user) {
35+
log.info("getAllForUser");
3536
return contextRepository.findByUserAccount(user);
3637
}
3738

3839
@Override
3940
public Context findByIdAndUserAccount(long newContextId, UserAccount userAccount) {
41+
log.info("findByIdAndUserAccount");
4042
if(newContextId == 0){
4143
newContextId = userAccount.getDefaultContext().getId();
4244
}
@@ -46,6 +48,7 @@ public Context findByIdAndUserAccount(long newContextId, UserAccount userAccount
4648
@Override
4749
@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false)
4850
public void createNewContext(NewContextForm newContext, UserAccount user) {
51+
log.info("createNewContext");
4952
Context context = new Context();
5053
context.setNameEn(newContext.getNameEn());
5154
context.setNameDe(newContext.getNameDe());
@@ -56,19 +59,22 @@ public void createNewContext(NewContextForm newContext, UserAccount user) {
5659
@Override
5760
@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false)
5861
public void updateContext(Context context) {
62+
log.info("updateContext");
5963
contextRepository.saveAndFlush(context);
6064
}
6165

6266
@Override
6367
@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false)
6468
public boolean delete(Context context) {
69+
log.info("delete");
6570
long contextId = context.getId();
6671
contextRepository.delete(context);
6772
return (!contextRepository.existsById(contextId));
6873
}
6974

7075
@Override
7176
public boolean contextHasItems(Context context) {
77+
log.info("contextHasItems");
7278
long numberOfTasks = taskRepository.findByContext(context).size();
7379
int numberOfProjects = projectRepository.findByContext(context).size();
7480
return ((numberOfTasks + numberOfProjects) > 0);
Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.woehlke.simpleworklist.context;
22

3+
import lombok.*;
34
import org.hibernate.validator.constraints.Length;
45
import javax.validation.constraints.NotBlank;
56
import org.hibernate.validator.constraints.SafeHtml;
@@ -9,6 +10,12 @@
910
/**
1011
* Created by tw on 15.03.16.
1112
*/
13+
@Getter
14+
@Setter
15+
@EqualsAndHashCode
16+
@ToString
17+
@NoArgsConstructor
18+
@AllArgsConstructor
1219
public class NewContextForm implements Serializable {
1320

1421
private static final long serialVersionUID = -937143305653156981L;
@@ -23,46 +30,4 @@ public class NewContextForm implements Serializable {
2330
@Length(min = 1, max = 255)
2431
private String nameEn;
2532

26-
public String getNameDe() {
27-
return nameDe;
28-
}
29-
30-
public void setNameDe(String nameDe) {
31-
this.nameDe = nameDe;
32-
}
33-
34-
public String getNameEn() {
35-
return nameEn;
36-
}
37-
38-
public void setNameEn(String nameEn) {
39-
this.nameEn = nameEn;
40-
}
41-
42-
@Override
43-
public boolean equals(Object o) {
44-
if (this == o) return true;
45-
if (!(o instanceof NewContextForm)) return false;
46-
47-
NewContextForm that = (NewContextForm) o;
48-
49-
if (nameDe != null ? !nameDe.equals(that.nameDe) : that.nameDe != null) return false;
50-
return nameEn != null ? nameEn.equals(that.nameEn) : that.nameEn == null;
51-
52-
}
53-
54-
@Override
55-
public int hashCode() {
56-
int result = nameDe != null ? nameDe.hashCode() : 0;
57-
result = 31 * result + (nameEn != null ? nameEn.hashCode() : 0);
58-
return result;
59-
}
60-
61-
@Override
62-
public String toString() {
63-
return "NewContextForm{" +
64-
"nameDe='" + nameDe + '\'' +
65-
", nameEn='" + nameEn + '\'' +
66-
'}';
67-
}
6833
}
Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
package org.woehlke.simpleworklist.context;
22

3+
import lombok.*;
4+
35
import javax.validation.constraints.NotNull;
46
import java.io.Serializable;
57

68
/**
79
* Created by Fert on 16.03.2016.
810
*/
11+
@Getter
12+
@Setter
13+
@EqualsAndHashCode
14+
@ToString
15+
@AllArgsConstructor
16+
@NoArgsConstructor
917
public class UserChangeDefaultContextForm implements Serializable {
1018

1119
private static final long serialVersionUID = -8592295563275083292L;
@@ -16,46 +24,4 @@ public class UserChangeDefaultContextForm implements Serializable {
1624
@NotNull
1725
private Context defaultContext;
1826

19-
public Long getId() {
20-
return id;
21-
}
22-
23-
public void setId(Long id) {
24-
this.id = id;
25-
}
26-
27-
public Context getDefaultContext() {
28-
return defaultContext;
29-
}
30-
31-
public void setDefaultContext(Context defaultContext) {
32-
this.defaultContext = defaultContext;
33-
}
34-
35-
@Override
36-
public boolean equals(Object o) {
37-
if (this == o) return true;
38-
if (!(o instanceof UserChangeDefaultContextForm)) return false;
39-
40-
UserChangeDefaultContextForm that = (UserChangeDefaultContextForm) o;
41-
42-
if (id != null ? !id.equals(that.id) : that.id != null) return false;
43-
return defaultContext != null ? defaultContext.equals(that.defaultContext) : that.defaultContext == null;
44-
45-
}
46-
47-
@Override
48-
public int hashCode() {
49-
int result = id != null ? id.hashCode() : 0;
50-
result = 31 * result + (defaultContext != null ? defaultContext.hashCode() : 0);
51-
return result;
52-
}
53-
54-
@Override
55-
public String toString() {
56-
return "UserChangeDefaultContextForm{" +
57-
"id=" + id +
58-
", defaultContext=" + defaultContext +
59-
'}';
60-
}
6127
}

src/main/java/org/woehlke/simpleworklist/error/MyErrorController.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,29 @@
55
import org.springframework.boot.web.servlet.error.ErrorController;
66
import org.springframework.http.HttpStatus;
77
import org.springframework.stereotype.Controller;
8-
import org.springframework.ui.Model;
98
import org.springframework.web.bind.annotation.RequestMapping;
10-
import org.springframework.web.bind.annotation.RequestMethod;
119

12-
import javax.servlet.RequestDispatcher;
1310
import javax.servlet.http.HttpServletRequest;
1411

12+
import static javax.servlet.RequestDispatcher.*;
13+
import static org.springframework.web.bind.annotation.RequestMethod.*;
14+
1515

1616
@Slf4j
1717
@Controller
18+
@RequestMapping(path="/fehler")
1819
public class MyErrorController implements ErrorController {
1920

20-
@RequestMapping(path="/fehler", method={RequestMethod.GET,RequestMethod.POST, RequestMethod.PUT})
21-
public String handleError(HttpServletRequest request, Model model) {
22-
String errorMessage = (String) request.getAttribute(RequestDispatcher.ERROR_MESSAGE);
21+
@RequestMapping(path="/", method={ GET, POST, PUT, HEAD, PATCH, DELETE, OPTIONS, TRACE })
22+
public String handleError(
23+
HttpServletRequest request
24+
) {
25+
log.info("handleError");
26+
String errorMessage = (String) request.getAttribute(ERROR_MESSAGE);
2327
if(errorMessage!=null){
2428
log.warn("errorMessage :"+errorMessage);
2529
}
26-
Integer statusCode = (Integer) request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
30+
Integer statusCode = (Integer) request.getAttribute(ERROR_STATUS_CODE);
2731
if(statusCode != null){
2832
HttpStatus httpStatus = HttpStatus.valueOf(statusCode);
2933
log.warn(httpStatus.value()+""+httpStatus.getReasonPhrase());
@@ -80,7 +84,7 @@ public String handleError(HttpServletRequest request, Model model) {
8084
return "redirect:/login?login_error=1";
8185
}
8286
}
83-
Throwable exception = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
87+
Throwable exception = (Throwable) request.getAttribute(ERROR_EXCEPTION);
8488
if(exception != null) {
8589
log.warn("##################################################");
8690
log.warn("Exception :" + exception.getMessage());

0 commit comments

Comments
 (0)