From 48afbc51feb8aaba926c318bb562ca8cbee53355 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Parejo=20Maestre?= Date: Thu, 10 Sep 2020 21:20:01 +0200 Subject: [PATCH 01/27] Improved security module with an relation between Users an Authorities (one to many). Services and data initialization scripts also modified --- .../samples/petclinic/model/Authorities.java | 16 ++++++++++++---- .../samples/petclinic/model/User.java | 7 +++++++ .../petclinic/service/AuthoritiesService.java | 18 ++++++++++++++---- .../samples/petclinic/service/UserService.java | 6 ++++++ src/main/resources/db/hsqldb/data.sql | 6 +++--- 5 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/model/Authorities.java b/src/main/java/org/springframework/samples/petclinic/model/Authorities.java index 26df5b5f7ef..522b215ce7a 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Authorities.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Authorities.java @@ -1,16 +1,24 @@ package org.springframework.samples.petclinic.model; import javax.persistence.Entity; -import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; import javax.persistence.Table; +import javax.validation.constraints.Size; import lombok.Data; @Data @Entity @Table(name = "authorities") -public class Authorities { - @Id - String username; +public class Authorities extends BaseEntity{ + + @ManyToOne + @JoinColumn(name = "username") + User user; + + @Size(min = 3, max = 50) String authority; + + } diff --git a/src/main/java/org/springframework/samples/petclinic/model/User.java b/src/main/java/org/springframework/samples/petclinic/model/User.java index ac2cdd2c4ea..b027024d1dc 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/User.java +++ b/src/main/java/org/springframework/samples/petclinic/model/User.java @@ -1,7 +1,11 @@ package org.springframework.samples.petclinic.model; +import java.util.Set; + +import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.Id; +import javax.persistence.OneToMany; import javax.persistence.Table; import lombok.Data; @@ -16,4 +20,7 @@ public class User{ String password; boolean enabled; + + @OneToMany(cascade = CascadeType.ALL, mappedBy = "user") + private Set authorities; } diff --git a/src/main/java/org/springframework/samples/petclinic/service/AuthoritiesService.java b/src/main/java/org/springframework/samples/petclinic/service/AuthoritiesService.java index aad666b11c2..8b9219e4c29 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/AuthoritiesService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/AuthoritiesService.java @@ -16,9 +16,12 @@ package org.springframework.samples.petclinic.service; +import java.util.Optional; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.samples.petclinic.model.Authorities; +import org.springframework.samples.petclinic.model.User; import org.springframework.samples.petclinic.repository.AuthoritiesRepository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -33,10 +36,12 @@ public class AuthoritiesService { private AuthoritiesRepository authoritiesRepository; + private UserService userService; @Autowired - public AuthoritiesService(AuthoritiesRepository authoritiesRepository) { + public AuthoritiesService(AuthoritiesRepository authoritiesRepository,UserService userService) { this.authoritiesRepository = authoritiesRepository; + this.userService = userService; } @Transactional @@ -47,9 +52,14 @@ public void saveAuthorities(Authorities authorities) throws DataAccessException @Transactional public void saveAuthorities(String username, String role) throws DataAccessException { Authorities authority = new Authorities(); - authority.setUsername(username); - authority.setAuthority(role); - authoritiesRepository.save(authority); + Optional user = userService.findUser(username); + if(user.isPresent()) { + authority.setUser(user.get()); + authority.setAuthority(role); + //user.get().getAuthorities().add(authority); + authoritiesRepository.save(authority); + }else + throw new DataAccessException("User '"+username+"' not found!") {}; } diff --git a/src/main/java/org/springframework/samples/petclinic/service/UserService.java b/src/main/java/org/springframework/samples/petclinic/service/UserService.java index 21eec789e5c..882bd2759ee 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/UserService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/UserService.java @@ -16,6 +16,8 @@ package org.springframework.samples.petclinic.service; +import java.util.Optional; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.samples.petclinic.model.User; @@ -44,4 +46,8 @@ public void saveUser(User user) throws DataAccessException { user.setEnabled(true); userRepository.save(user); } + + public Optional findUser(String username) { + return userRepository.findById(username); + } } diff --git a/src/main/resources/db/hsqldb/data.sql b/src/main/resources/db/hsqldb/data.sql index 78b09f9e18a..164773b3fa8 100644 --- a/src/main/resources/db/hsqldb/data.sql +++ b/src/main/resources/db/hsqldb/data.sql @@ -1,12 +1,12 @@ -- One admin user, named admin1 with passwor 4dm1n and authority admin INSERT INTO users(username,password,enabled) VALUES ('admin1','4dm1n',TRUE); -INSERT INTO authorities VALUES ('admin1','admin'); +INSERT INTO authorities(id,username,authority) VALUES (1,'admin1','admin'); -- One owner user, named owner1 with passwor 0wn3r INSERT INTO users(username,password,enabled) VALUES ('owner1','0wn3r',TRUE); -INSERT INTO authorities VALUES ('owner1','owner'); +INSERT INTO authorities(id,username,authority) VALUES (2,'owner1','owner'); -- One vet user, named vet1 with passwor v3t INSERT INTO users(username,password,enabled) VALUES ('vet1','v3t',TRUE); -INSERT INTO authorities VALUES ('vet1','veterinarian'); +INSERT INTO authorities(id,username,authority) VALUES (3,'vet1','veterinarian'); INSERT INTO vets VALUES (1, 'James', 'Carter'); INSERT INTO vets VALUES (2, 'Helen', 'Leary'); From 5b74d6493ad27436cd636b86356bd9e610ca828a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Parejo=20Maestre?= Date: Wed, 23 Sep 2020 00:37:56 +0200 Subject: [PATCH 02/27] Added base a extended class diagramas of the data model of our domain. The extended diagram describes a vademecum with diagnoses and treatments associated to the visits. --- .../spring-petclinic-with-vademecum.uxf | 463 ++++++++++++++++++ .../resources/diagrams/spring-petclinic.uxf | 342 +++++++++++++ 2 files changed, 805 insertions(+) create mode 100644 src/main/resources/diagrams/spring-petclinic-with-vademecum.uxf create mode 100644 src/main/resources/diagrams/spring-petclinic.uxf diff --git a/src/main/resources/diagrams/spring-petclinic-with-vademecum.uxf b/src/main/resources/diagrams/spring-petclinic-with-vademecum.uxf new file mode 100644 index 00000000000..730e950c32f --- /dev/null +++ b/src/main/resources/diagrams/spring-petclinic-with-vademecum.uxf @@ -0,0 +1,463 @@ + + + // Uncomment the following line to change the fontsize and font: +// fontsize=28 +// fontfamily=SansSerif //possible: SansSerif,Serif,Monospaced + + +////////////////////////////////////////////////////////////////////////////////////////////// +// Welcome to UMLet! +// +// Double-click on elements to add them to the diagram, or to copy them +// Edit elements by modifying the text in this panel +// Hold Ctrl to select multiple elements +// Use Ctrl+mouse to select via lasso +// +// Use +/- or Ctrl+mouse wheel to zoom +// Drag a whole relation at its central square icon +// +// Press Ctrl+C to copy the whole diagram to the system clipboard (then just paste it to, eg, Word) +// Edit the files in the "palettes" directory to create your own element palettes +// +// Select "Custom Elements > New..." to create new element types +////////////////////////////////////////////////////////////////////////////////////////////// + + +// This text will be stored with each diagram; use it for notes. + 10 + + UMLClass + + 620 + 370 + 280 + 70 + + Pet +-- +birthDate: LocalDate {Date} + + + + + UMLClass + + 100 + 620 + 100 + 40 + + Vet + + + + Relation + + 700 + 330 + 30 + 60 + + lt=<<<- + 10.0;10.0;10.0;40.0 + + + UMLClass + + 650 + 280 + 130 + 60 + + NamedEntity +-- +name: String + + + + + + UMLClass + + 250 + 370 + 230 + 90 + + Visit +-- +date: LocalDate {Date} +description: String + + + + UMLClass + + 860 + 480 + 120 + 40 + + PetType + + + + + Relation + + 470 + 390 + 170 + 60 + + lt=- +m1=1 +m2=0..n + + 150.0;30.0;10.0;30.0 + + + Relation + + 700 + 330 + 380 + 270 + + lt=<<<- + 10.0;10.0;10.0;30.0;360.0;30.0;360.0;250.0 + + + Relation + + 770 + 430 + 110 + 90 + + lt=<- + 90.0;70.0;10.0;70.0;10.0;10.0 + + + UMLClass + + 1010 + 580 + 180 + 90 + + Disease +-- +description: String + + + + Relation + + 700 + 330 + 270 + 170 + + lt=<<<- + 10.0;10.0;10.0;30.0;250.0;30.0;250.0;150.0 + + + UMLClass + + 1190 + 700 + 100 + 50 + + Symptom + + + + + Relation + + 700 + 330 + 530 + 390 + + lt=<<<- + 10.0;10.0;10.0;30.0;510.0;30.0;510.0;370.0 + + + UMLClass + + 350 + 600 + 190 + 60 + + Diagnosis +-- +description: String + + + + UMLClass + + 390 + 740 + 260 + 80 + + Treatment +-- +description: String +dose: int + + + + UMLClass + + 1250 + 790 + 100 + 40 + + Drug + + + + Relation + + 700 + 330 + 640 + 480 + + lt=<<<- + 10.0;10.0;10.0;30.0;620.0;30.0;620.0;460.0 + + + Relation + + 370 + 450 + 60 + 170 + + lt=-> +m1=0..1 +m2=1 + 10.0;150.0;10.0;10.0 + + + Relation + + 190 + 610 + 180 + 60 + + lt=<- +m1=1 +m2=0..n + + 10.0;30.0;160.0;30.0 + + + Relation + + 400 + 650 + 60 + 110 + + lt=- +m1=1 +m2=0..n + + 10.0;10.0;10.0;90.0 + + + Relation + + 640 + 780 + 630 + 60 + + lt=- +m1=0..n +m2=0..1 + + 10.0;30.0;610.0;30.0 + + + Relation + + 530 + 600 + 500 + 60 + + lt=<- +m1=1 +m2=0..n + + 480.0;30.0;10.0;30.0 + + + Relation + + 1070 + 660 + 140 + 100 + + lt=- +m1=1..n +m2=1..n + + 120.0;70.0;10.0;70.0;10.0;10.0 + + + UMLClass + + 760 + 700 + 200 + 80 + + Prevalence +-- +percentage: int +usualSeverity: String + + + + Relation + + 950 + 710 + 150 + 60 + + lt=. + + 10.0;40.0;130.0;10.0 + + + Relation + + 870 + 510 + 160 + 120 + + lt=- +m1=0..n +m2=1..n +develops + 140.0;90.0;10.0;90.0;10.0;10.0 + + + UMLNote + + 0 + 840 + 490 + 80 + + *Business Rule 1* +fg=red +A diagnose should be associated to a disease that +is compatible with the type of pet of its related visit + + + + Relation + + 190 + 650 + 210 + 210 + + lt=. +fg=red + 10.0;190.0;190.0;10.0 + + + UMLClass + + 1470 + 780 + 140 + 50 + + ActivePrinciple + + + + Relation + + 700 + 330 + 840 + 470 + + lt=<<<- + 10.0;10.0;10.0;30.0;820.0;30.0;820.0;450.0 + + + Relation + + 1340 + 780 + 150 + 60 + + lt=-> +m1=0..n +m2=1 +contains + 10.0;30.0;130.0;30.0 + + + Relation + + 1540 + 790 + 270 + 110 + + lt=- +m1=1..n +m2=1..n +incompatible +with + 70.0;10.0;140.0;10.0;140.0;90.0;10.0;90.0;10.0;40.0 + + + UMLNote + + 540 + 850 + 640 + 80 + + *Business Rule 2* +fg=red +A diagnose should not be associated a pair of treatmens whose drugs +contain incompatible active principles + + + + Relation + + 510 + 810 + 140 + 60 + + lt=. +fg=red + 120.0;40.0;10.0;10.0 + + diff --git a/src/main/resources/diagrams/spring-petclinic.uxf b/src/main/resources/diagrams/spring-petclinic.uxf new file mode 100644 index 00000000000..3738cdced34 --- /dev/null +++ b/src/main/resources/diagrams/spring-petclinic.uxf @@ -0,0 +1,342 @@ + + + // Uncomment the following line to change the fontsize and font: + fontsize=28 +// fontfamily=SansSerif //possible: SansSerif,Serif,Monospaced + + +////////////////////////////////////////////////////////////////////////////////////////////// +// Welcome to UMLet! +// +// Double-click on elements to add them to the diagram, or to copy them +// Edit elements by modifying the text in this panel +// Hold Ctrl to select multiple elements +// Use Ctrl+mouse to select via lasso +// +// Use +/- or Ctrl+mouse wheel to zoom +// Drag a whole relation at its central square icon +// +// Press Ctrl+C to copy the whole diagram to the system clipboard (then just paste it to, eg, Word) +// Edit the files in the "palettes" directory to create your own element palettes +// +// Select "Custom Elements > New..." to create new element types +////////////////////////////////////////////////////////////////////////////////////////////// + + +// This text will be stored with each diagram; use it for notes. + 10 + + UMLClass + + 750 + 370 + 280 + 70 + + Pet +-- +birthDate: LocalDate {Date} + + + + + UMLClass + + 70 + 640 + 100 + 40 + + Vet + + + + UMLClass + + 540 + 170 + 100 + 60 + + BaseEntity +-- +id: int + + + + + + Relation + + 830 + 330 + 30 + 60 + + lt=<<<- + 10.0;10.0;10.0;40.0 + + + UMLClass + + 780 + 280 + 130 + 60 + + NamedEntity +-- +name: String + + + + + + Relation + + 570 + 220 + 280 + 80 + + lt=<<<- + 10.0;10.0;10.0;40.0;260.0;40.0;260.0;60.0 + + + UMLClass + + 390 + 370 + 230 + 90 + + Visit +-- +date: LocalDate {Date} +description: String + + + + UMLClass + + 170 + 480 + 150 + 90 + + Owner +-- +address: String +city: String + + + + Relation + + 310 + 430 + 520 + 130 + + lt=- +m1=1 +m2=0..n +owns > + 10.0;100.0;470.0;100.0;470.0;10.0 + + + UMLClass + + 1020 + 510 + 120 + 40 + + PetType + + + + + UMLClass + + 180 + 280 + 170 + 90 + + Person +-- +firstName: String +lastName: String + + + + Relation + + 260 + 220 + 340 + 80 + + lt=<<<- + 320.0;10.0;320.0;40.0;10.0;40.0;10.0;60.0 + + + Relation + + 230 + 360 + 30 + 140 + + lt=<<<- + 10.0;10.0;10.0;120.0 + + + Relation + + 610 + 390 + 160 + 60 + + lt=- +m1=1 +m2=0..n + + 140.0;30.0;10.0;30.0 + + + Relation + + 500 + 220 + 100 + 170 + + lt=<<<- + 80.0;10.0;80.0;40.0;10.0;40.0;10.0;150.0 + + + Relation + + 110 + 360 + 150 + 300 + + lt=<<<- + 130.0;10.0;130.0;40.0;10.0;40.0;10.0;280.0 + + + UMLClass + + 1110 + 640 + 100 + 40 + + Specialty + + + + + Relation + + 830 + 330 + 370 + 330 + + lt=<<<- + 10.0;10.0;10.0;30.0;350.0;30.0;350.0;310.0 + + + Relation + + 160 + 640 + 970 + 60 + + lt=- +m1=1..n +m2=0..n + + 950.0;30.0;10.0;30.0 + + + UMLClass + + 720 + 170 + 160 + 60 + + Authorities +-- +authority: String + + + + UMLClass + + 1010 + 140 + 170 + 90 + + User +-- +username: String +password: String + + + + Relation + + 870 + 160 + 160 + 60 + + lt=- +m1=0..n + + + 10.0;30.0;140.0;30.0 + + + Relation + + 630 + 180 + 110 + 30 + + lt=<<<- + 10.0;10.0;90.0;10.0 + + + Relation + + 830 + 330 + 260 + 200 + + lt=<<<- + 10.0;10.0;10.0;30.0;240.0;30.0;240.0;180.0 + + + Relation + + 900 + 430 + 140 + 120 + + lt=<- + 120.0;100.0;10.0;100.0;10.0;10.0 + + From e20022e6ad62c0bddca51ffea2a6855e60686a20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Parejo=20Maestre?= Date: Wed, 23 Sep 2020 19:18:49 +0200 Subject: [PATCH 03/27] Mejoras en las anotaciones del diagrama --- .../spring-petclinic-with-vademecum.uxf | 70 +++++++++---------- .../resources/diagrams/spring-petclinic.uxf | 56 +++++++-------- 2 files changed, 63 insertions(+), 63 deletions(-) diff --git a/src/main/resources/diagrams/spring-petclinic-with-vademecum.uxf b/src/main/resources/diagrams/spring-petclinic-with-vademecum.uxf index 730e950c32f..b6ae14310c2 100644 --- a/src/main/resources/diagrams/spring-petclinic-with-vademecum.uxf +++ b/src/main/resources/diagrams/spring-petclinic-with-vademecum.uxf @@ -29,7 +29,7 @@ UMLClass 620 - 370 + 250 280 70 @@ -43,7 +43,7 @@ birthDate: LocalDate {Date} UMLClass 100 - 620 + 500 100 40 @@ -54,7 +54,7 @@ birthDate: LocalDate {Date} Relation 700 - 330 + 210 30 60 @@ -65,7 +65,7 @@ birthDate: LocalDate {Date} UMLClass 650 - 280 + 160 130 60 @@ -80,7 +80,7 @@ name: String UMLClass 250 - 370 + 250 230 90 @@ -94,7 +94,7 @@ description: String UMLClass 860 - 480 + 360 120 40 @@ -106,7 +106,7 @@ description: String Relation 470 - 390 + 270 170 60 @@ -120,7 +120,7 @@ m2=0..n Relation 700 - 330 + 210 380 270 @@ -131,7 +131,7 @@ m2=0..n Relation 770 - 430 + 310 110 90 @@ -142,7 +142,7 @@ m2=0..n UMLClass 1010 - 580 + 460 180 90 @@ -155,7 +155,7 @@ description: String Relation 700 - 330 + 210 270 170 @@ -166,7 +166,7 @@ description: String UMLClass 1190 - 700 + 580 100 50 @@ -178,7 +178,7 @@ description: String Relation 700 - 330 + 210 530 390 @@ -189,7 +189,7 @@ description: String UMLClass 350 - 600 + 480 190 60 @@ -202,21 +202,21 @@ description: String UMLClass 390 - 740 + 620 260 80 Treatment -- description: String -dose: int +dose: Integer UMLClass 1250 - 790 + 670 100 40 @@ -227,7 +227,7 @@ dose: int Relation 700 - 330 + 210 640 480 @@ -238,7 +238,7 @@ dose: int Relation 370 - 450 + 330 60 170 @@ -251,7 +251,7 @@ m2=1 Relation 190 - 610 + 490 180 60 @@ -265,7 +265,7 @@ m2=0..n Relation 400 - 650 + 530 60 110 @@ -279,7 +279,7 @@ m2=0..n Relation 640 - 780 + 660 630 60 @@ -293,7 +293,7 @@ m2=0..1 Relation 530 - 600 + 480 500 60 @@ -307,7 +307,7 @@ m2=0..n Relation 1070 - 660 + 540 140 100 @@ -321,7 +321,7 @@ m2=1..n UMLClass 760 - 700 + 580 200 80 @@ -335,7 +335,7 @@ usualSeverity: String Relation 950 - 710 + 590 150 60 @@ -347,7 +347,7 @@ usualSeverity: String Relation 870 - 510 + 390 160 120 @@ -361,7 +361,7 @@ develops UMLNote 0 - 840 + 720 490 80 @@ -375,7 +375,7 @@ is compatible with the type of pet of its related visit Relation 190 - 650 + 530 210 210 @@ -387,7 +387,7 @@ fg=red UMLClass 1470 - 780 + 660 140 50 @@ -398,7 +398,7 @@ fg=red Relation 700 - 330 + 210 840 470 @@ -409,7 +409,7 @@ fg=red Relation 1340 - 780 + 660 150 60 @@ -423,7 +423,7 @@ contains Relation 1540 - 790 + 670 270 110 @@ -438,7 +438,7 @@ with UMLNote 540 - 850 + 730 640 80 @@ -452,7 +452,7 @@ contain incompatible active principles Relation 510 - 810 + 690 140 60 diff --git a/src/main/resources/diagrams/spring-petclinic.uxf b/src/main/resources/diagrams/spring-petclinic.uxf index 3738cdced34..b59b13e9305 100644 --- a/src/main/resources/diagrams/spring-petclinic.uxf +++ b/src/main/resources/diagrams/spring-petclinic.uxf @@ -30,12 +30,12 @@ 750 370 - 280 + 210 70 Pet -- -birthDate: LocalDate {Date} +birthDate: Date {Past} @@ -81,12 +81,12 @@ id: int 780 280 - 130 + 230 60 NamedEntity -- -name: String +name: String {NotBlank} @@ -112,8 +112,8 @@ name: String Visit -- -date: LocalDate {Date} -description: String +date: Date +description: String {NotBlank} @@ -121,28 +121,28 @@ description: String 170 480 - 150 + 250 90 Owner -- -address: String -city: String +address: String {NotBlank} +city: String {NotBlank} Relation - 310 + 410 430 - 520 + 420 130 lt=- m1=1 m2=0..n owns > - 10.0;100.0;470.0;100.0;470.0;10.0 + 10.0;100.0;370.0;100.0;370.0;10.0 UMLClass @@ -159,15 +159,15 @@ owns > UMLClass - 180 + 90 280 - 170 + 260 90 Person -- -firstName: String -lastName: String +firstName: String {NotBlank} +lastName: String {NotBlank} @@ -268,54 +268,54 @@ m2=0..n UMLClass - 720 + 680 170 - 160 + 250 60 Authorities -- -authority: String +authority: String {NotBlank} UMLClass - 1010 + 1030 140 - 170 + 310 90 User -- -username: String -password: String +username: String {Length(4,50)} +password: String {Length(8,50)} Relation - 870 + 920 160 - 160 + 130 60 lt=- m1=0..n - 10.0;30.0;140.0;30.0 + 10.0;30.0;110.0;30.0 Relation 630 180 - 110 + 70 30 lt=<<<- - 10.0;10.0;90.0;10.0 + 10.0;10.0;50.0;10.0 Relation From c46f23f4753ffd10cb3311ad34c6bd2a2573af0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Parejo=20Maestre?= Date: Thu, 24 Sep 2020 13:03:27 +0200 Subject: [PATCH 04/27] Improved diagram annotations. --- .../spring-petclinic-with-vademecum.uxf | 122 +++++++++--------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/src/main/resources/diagrams/spring-petclinic-with-vademecum.uxf b/src/main/resources/diagrams/spring-petclinic-with-vademecum.uxf index b6ae14310c2..2bf65cd732d 100644 --- a/src/main/resources/diagrams/spring-petclinic-with-vademecum.uxf +++ b/src/main/resources/diagrams/spring-petclinic-with-vademecum.uxf @@ -29,13 +29,13 @@ UMLClass 620 - 250 + 100 280 70 Pet -- -birthDate: LocalDate {Date} +birthDate: Date {Past} @@ -43,7 +43,7 @@ birthDate: LocalDate {Date} UMLClass 100 - 500 + 350 100 40 @@ -54,7 +54,7 @@ birthDate: LocalDate {Date} Relation 700 - 210 + 60 30 60 @@ -65,14 +65,13 @@ birthDate: LocalDate {Date} UMLClass 650 - 160 - 130 - 60 + 20 + 190 + 50 NamedEntity -- -name: String - +name: String {NotEmpty} @@ -80,21 +79,21 @@ name: String UMLClass 250 - 250 + 100 230 90 Visit -- -date: LocalDate {Date} -description: String +date: Date +description: String {NotEmpty} UMLClass 860 - 360 + 210 120 40 @@ -106,21 +105,21 @@ description: String Relation 470 - 270 + 130 170 - 60 + 50 lt=- m1=1 m2=0..n - 150.0;30.0;10.0;30.0 + 150.0;20.0;10.0;20.0 Relation 700 - 210 + 60 380 270 @@ -131,7 +130,7 @@ m2=0..n Relation 770 - 310 + 160 110 90 @@ -142,20 +141,20 @@ m2=0..n UMLClass 1010 - 460 + 310 180 90 Disease -- -description: String +description: String {NotEmpty} Relation 700 - 210 + 60 270 170 @@ -166,7 +165,7 @@ description: String UMLClass 1190 - 580 + 430 100 50 @@ -178,7 +177,7 @@ description: String Relation 700 - 210 + 60 530 390 @@ -189,7 +188,7 @@ description: String UMLClass 350 - 480 + 330 190 60 @@ -202,21 +201,22 @@ description: String UMLClass 390 - 620 + 470 260 80 Treatment -- -description: String -dose: Integer +description: String {NotEmpty} +dose: Integer {Min(1)} +periodicity: String UMLClass 1250 - 670 + 520 100 40 @@ -227,7 +227,7 @@ dose: Integer Relation 700 - 210 + 60 640 480 @@ -238,8 +238,8 @@ dose: Integer Relation 370 - 330 - 60 + 180 + 50 170 lt=-> @@ -251,22 +251,22 @@ m2=1 Relation 190 - 490 + 350 180 - 60 + 50 lt=<- m1=1 m2=0..n - 10.0;30.0;160.0;30.0 + 10.0;20.0;160.0;20.0 Relation 400 - 530 - 60 + 380 + 50 110 lt=- @@ -279,35 +279,35 @@ m2=0..n Relation 640 - 660 + 520 630 - 60 + 50 lt=- m1=0..n m2=0..1 - 10.0;30.0;610.0;30.0 + 10.0;20.0;610.0;20.0 Relation 530 - 480 + 340 500 - 60 + 50 lt=<- m1=1 m2=0..n - 480.0;30.0;10.0;30.0 + 480.0;20.0;10.0;20.0 Relation 1070 - 540 + 390 140 100 @@ -320,22 +320,22 @@ m2=1..n UMLClass - 760 - 580 - 200 + 750 + 430 + 210 80 Prevalence -- -percentage: int -usualSeverity: String +percentage: int {Min(0),Max(100)} +usualSeverity: String {NotEmpty} Relation 950 - 590 + 440 150 60 @@ -347,7 +347,7 @@ usualSeverity: String Relation 870 - 390 + 240 160 120 @@ -361,7 +361,7 @@ develops UMLNote 0 - 720 + 570 490 80 @@ -375,7 +375,7 @@ is compatible with the type of pet of its related visit Relation 190 - 530 + 380 210 210 @@ -387,7 +387,7 @@ fg=red UMLClass 1470 - 660 + 510 140 50 @@ -398,7 +398,7 @@ fg=red Relation 700 - 210 + 60 840 470 @@ -409,22 +409,22 @@ fg=red Relation 1340 - 660 + 520 150 - 60 + 50 lt=-> m1=0..n m2=1 contains - 10.0;30.0;130.0;30.0 + 10.0;20.0;130.0;20.0 Relation 1540 - 670 - 270 + 520 + 240 110 lt=- @@ -438,7 +438,7 @@ with UMLNote 540 - 730 + 580 640 80 @@ -452,7 +452,7 @@ contain incompatible active principles Relation 510 - 690 + 540 140 60 From 8899ac636ed8293b09fa99d6f33ce54cb14872f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Parejo=20Maestre?= Date: Fri, 2 Oct 2020 11:33:36 +0200 Subject: [PATCH 05/27] Refactoring of the repositories for a clearer structure. First new Entity: Disease without relationships) --- .../samples/petclinic/model/Disease.java | 10 +++++ .../petclinic/repository/OwnerRepository.java | 40 +++++++++--------- .../petclinic/repository/PetRepository.java | 16 ++++--- .../petclinic/repository/VetRepository.java | 3 +- .../petclinic/repository/VisitRepository.java | 3 +- .../SpringDataOwnerRepository.java | 42 ------------------- .../SpringDataPetRepository.java | 39 ----------------- .../SpringDataVetRepository.java | 30 ------------- .../SpringDataVisitRepository.java | 30 ------------- .../resources/diagrams/spring-petclinic.uxf | 2 +- 10 files changed, 43 insertions(+), 172 deletions(-) create mode 100644 src/main/java/org/springframework/samples/petclinic/model/Disease.java delete mode 100644 src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataOwnerRepository.java delete mode 100644 src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetRepository.java delete mode 100644 src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVetRepository.java delete mode 100644 src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVisitRepository.java diff --git a/src/main/java/org/springframework/samples/petclinic/model/Disease.java b/src/main/java/org/springframework/samples/petclinic/model/Disease.java new file mode 100644 index 00000000000..17d712532e4 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/model/Disease.java @@ -0,0 +1,10 @@ +package org.springframework.samples.petclinic.model; + +import javax.persistence.Entity; +import javax.persistence.Table; + +@Entity +@Table(name = "diseases") +public class Disease extends NamedEntity{ + +} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java index dde900ab7d7..21eb1b7e50b 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java @@ -18,21 +18,27 @@ import java.util.Collection; import org.springframework.dao.DataAccessException; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.Repository; +import org.springframework.data.repository.query.Param; import org.springframework.samples.petclinic.model.BaseEntity; import org.springframework.samples.petclinic.model.Owner; +import org.springframework.samples.petclinic.repository.OwnerRepository; /** - * Repository class for Owner domain objects All method names are compliant - * with Spring Data naming conventions so this interface can easily be extended for Spring - * Data See here: - * http://static.springsource.org/spring-data/jpa/docs/current/reference/html/jpa.repositories.html#jpa.query-methods.query-creation + * Spring Data JPA OwnerRepository interface * - * @author Ken Krebs - * @author Juergen Hoeller - * @author Sam Brannen * @author Michael Isvy + * @since 15.1.2013 */ -public interface OwnerRepository { +public interface OwnerRepository extends Repository { + + /** + * Save an Owner to the data store, either inserting or updating it. + * @param owner the Owner to save + * @see BaseEntity#isNew + */ + void save(Owner owner) throws DataAccessException; /** * Retrieve Owners from the data store by last name, returning all owners @@ -40,22 +46,18 @@ public interface OwnerRepository { * @param lastName Value to search for * @return a Collection of matching Owners (or an empty * Collection if none found) - */ - Collection findByLastName(String lastName) throws DataAccessException; + */ + @Query("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName%") + public Collection findByLastName(@Param("lastName") String lastName); + /** * Retrieve an Owner from the data store by id. * @param id the id to search for * @return the Owner if found * @throws org.springframework.dao.DataRetrievalFailureException if not found - */ - Owner findById(int id) throws DataAccessException; - - /** - * Save an Owner to the data store, either inserting or updating it. - * @param owner the Owner to save - * @see BaseEntity#isNew - */ - void save(Owner owner) throws DataAccessException; + */ + @Query("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id") + public Owner findById(@Param("id") int id); } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java index b4c665b7400..c9848dd6e67 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java @@ -18,29 +18,27 @@ import java.util.List; import org.springframework.dao.DataAccessException; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.Repository; import org.springframework.samples.petclinic.model.BaseEntity; import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.model.PetType; /** - * Repository class for Pet domain objects All method names are compliant - * with Spring Data naming conventions so this interface can easily be extended for Spring - * Data See here: - * http://static.springsource.org/spring-data/jpa/docs/current/reference/html/jpa.repositories.html#jpa.query-methods.query-creation + * Spring Data JPA specialization of the {@link PetRepository} interface * - * @author Ken Krebs - * @author Juergen Hoeller - * @author Sam Brannen * @author Michael Isvy + * @since 15.1.2013 */ -public interface PetRepository { +public interface PetRepository extends Repository { /** * Retrieve all PetTypes from the data store. * @return a Collection of PetTypes */ + @Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name") List findPetTypes() throws DataAccessException; - + /** * Retrieve a Pet from the data store by id. * @param id the id to search for diff --git a/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java index 66821b6e076..a83ff1b3cca 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java @@ -18,6 +18,7 @@ import java.util.Collection; import org.springframework.dao.DataAccessException; +import org.springframework.data.repository.Repository; import org.springframework.samples.petclinic.model.Vet; /** @@ -31,7 +32,7 @@ * @author Sam Brannen * @author Michael Isvy */ -public interface VetRepository { +public interface VetRepository extends Repository{ /** * Retrieve all Vets from the data store. diff --git a/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java index dc1669d1191..fd3d9181d51 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java @@ -18,6 +18,7 @@ import java.util.List; import org.springframework.dao.DataAccessException; +import org.springframework.data.repository.Repository; import org.springframework.samples.petclinic.model.BaseEntity; import org.springframework.samples.petclinic.model.Visit; @@ -32,7 +33,7 @@ * @author Sam Brannen * @author Michael Isvy */ -public interface VisitRepository { +public interface VisitRepository extends Repository { /** * Save a Visit to the data store, either inserting or updating it. diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataOwnerRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataOwnerRepository.java deleted file mode 100644 index 41d29978026..00000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataOwnerRepository.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2002-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.samples.petclinic.repository.springdatajpa; - -import java.util.Collection; - -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.Repository; -import org.springframework.data.repository.query.Param; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.repository.OwnerRepository; - -/** - * Spring Data JPA specialization of the {@link OwnerRepository} interface - * - * @author Michael Isvy - * @since 15.1.2013 - */ -public interface SpringDataOwnerRepository extends OwnerRepository, Repository { - - @Override - @Query("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName%") - public Collection findByLastName(@Param("lastName") String lastName); - - @Override - @Query("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id") - public Owner findById(@Param("id") int id); - -} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetRepository.java deleted file mode 100644 index 9f88eaec724..00000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetRepository.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2002-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.samples.petclinic.repository.springdatajpa; - -import java.util.List; - -import org.springframework.dao.DataAccessException; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.Repository; -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.PetType; -import org.springframework.samples.petclinic.repository.PetRepository; - -/** - * Spring Data JPA specialization of the {@link PetRepository} interface - * - * @author Michael Isvy - * @since 15.1.2013 - */ -public interface SpringDataPetRepository extends PetRepository, Repository { - - @Override - @Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name") - List findPetTypes() throws DataAccessException; - -} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVetRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVetRepository.java deleted file mode 100644 index 6c5b543b9bd..00000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVetRepository.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2002-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.samples.petclinic.repository.springdatajpa; - -import org.springframework.data.repository.Repository; -import org.springframework.samples.petclinic.model.Vet; -import org.springframework.samples.petclinic.repository.VetRepository; - -/** - * Spring Data JPA specialization of the {@link VetRepository} interface - * - * @author Michael Isvy - * @since 15.1.2013 - */ -public interface SpringDataVetRepository extends VetRepository, Repository { - -} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVisitRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVisitRepository.java deleted file mode 100644 index 31141e1bae8..00000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVisitRepository.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2002-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.samples.petclinic.repository.springdatajpa; - -import org.springframework.data.repository.Repository; -import org.springframework.samples.petclinic.model.Visit; -import org.springframework.samples.petclinic.repository.VisitRepository; - -/** - * Spring Data JPA specialization of the {@link VisitRepository} interface - * - * @author Michael Isvy - * @since 15.1.2013 - */ -public interface SpringDataVisitRepository extends VisitRepository, Repository { - -} diff --git a/src/main/resources/diagrams/spring-petclinic.uxf b/src/main/resources/diagrams/spring-petclinic.uxf index b59b13e9305..257b694759b 100644 --- a/src/main/resources/diagrams/spring-petclinic.uxf +++ b/src/main/resources/diagrams/spring-petclinic.uxf @@ -1,7 +1,7 @@ // Uncomment the following line to change the fontsize and font: - fontsize=28 +// fontsize=28 // fontfamily=SansSerif //possible: SansSerif,Serif,Monospaced From db2003e03f72e7eb15aa9b7b7ec198765c3648c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Parejo=20Maestre?= Date: Fri, 2 Oct 2020 12:35:31 +0200 Subject: [PATCH 06/27] Eliminados scripts de mysql para simplificar la estructura del repo --- .../db/hsqldb/old-schema-used-previously.sql | 80 ------------------- src/main/resources/db/mysql/data.sql | 53 ------------ .../db/mysql/petclinic_db_setup_mysql.txt | 31 ------- src/main/resources/db/mysql/schema.sql | 55 ------------- src/main/resources/db/mysql/user.sql | 7 -- 5 files changed, 226 deletions(-) delete mode 100644 src/main/resources/db/hsqldb/old-schema-used-previously.sql delete mode 100644 src/main/resources/db/mysql/data.sql delete mode 100644 src/main/resources/db/mysql/petclinic_db_setup_mysql.txt delete mode 100644 src/main/resources/db/mysql/schema.sql delete mode 100644 src/main/resources/db/mysql/user.sql diff --git a/src/main/resources/db/hsqldb/old-schema-used-previously.sql b/src/main/resources/db/hsqldb/old-schema-used-previously.sql deleted file mode 100644 index 2afed9c830c..00000000000 --- a/src/main/resources/db/hsqldb/old-schema-used-previously.sql +++ /dev/null @@ -1,80 +0,0 @@ -DROP TABLE vet_specialties IF EXISTS; -DROP TABLE vets IF EXISTS; -DROP TABLE specialties IF EXISTS; -DROP TABLE visits IF EXISTS; -DROP TABLE pets IF EXISTS; -DROP TABLE types IF EXISTS; -DROP TABLE owners IF EXISTS; -DROP TABLE users IF EXISTS; -DROP TABLE authorities IF EXISTS; - -CREATE TABLE vets ( - id INTEGER IDENTITY PRIMARY KEY, - first_name VARCHAR(30), - last_name VARCHAR(30) -); -CREATE INDEX vets_last_name ON vets (last_name); - -CREATE TABLE specialties ( - id INTEGER IDENTITY PRIMARY KEY, - name VARCHAR(80) -); -CREATE INDEX specialties_name ON specialties (name); - -CREATE TABLE vet_specialties ( - vet_id INTEGER NOT NULL, - specialty_id INTEGER NOT NULL -); -ALTER TABLE vet_specialties ADD CONSTRAINT fk_vet_specialties_vets FOREIGN KEY (vet_id) REFERENCES vets (id); -ALTER TABLE vet_specialties ADD CONSTRAINT fk_vet_specialties_specialties FOREIGN KEY (specialty_id) REFERENCES specialties (id); - -CREATE TABLE types ( - id INTEGER IDENTITY PRIMARY KEY, - name VARCHAR(80) -); -CREATE INDEX types_name ON types (name); - -CREATE TABLE owners ( - id INTEGER IDENTITY PRIMARY KEY, - first_name VARCHAR(30), - last_name VARCHAR_IGNORECASE(30), - address VARCHAR(255), - city VARCHAR(80), - telephone VARCHAR(20) -); -CREATE INDEX owners_last_name ON owners (last_name); - -CREATE TABLE pets ( - id INTEGER IDENTITY PRIMARY KEY, - name VARCHAR(30), - birth_date DATE, - type_id INTEGER NOT NULL, - owner_id INTEGER NOT NULL -); -ALTER TABLE pets ADD CONSTRAINT fk_pets_owners FOREIGN KEY (owner_id) REFERENCES owners (id); -ALTER TABLE pets ADD CONSTRAINT fk_pets_types FOREIGN KEY (type_id) REFERENCES types (id); -CREATE INDEX pets_name ON pets (name); - -CREATE TABLE visits ( - id INTEGER IDENTITY PRIMARY KEY, - pet_id INTEGER NOT NULL, - visit_date DATE, - description VARCHAR(255) -); -ALTER TABLE visits ADD CONSTRAINT fk_visits_pets FOREIGN KEY (pet_id) REFERENCES pets (id); -CREATE INDEX visits_pet_id ON visits (pet_id); - -CREATE TABLE users( - username varchar_ignorecase(255) NOT NULL PRIMARY KEY, - password varchar_ignorecase(255) NOT NULL, - enabled BOOLEAN NOT NULL -); - -CREATE TABLE authorities ( - username varchar_ignorecase(50) NOT NULL, - authority varchar_ignorecase(50) NOT NULL, -); -ALTER TABLE authorities ADD CONSTRAINT fk_authorities_users FOREIGN KEY (username) REFERENCES users(username); - -CREATE UNIQUE INDEX ix_auth_username ON authorities (username,authority); - diff --git a/src/main/resources/db/mysql/data.sql b/src/main/resources/db/mysql/data.sql deleted file mode 100644 index 3f1dcf8ea16..00000000000 --- a/src/main/resources/db/mysql/data.sql +++ /dev/null @@ -1,53 +0,0 @@ -INSERT IGNORE INTO vets VALUES (1, 'James', 'Carter'); -INSERT IGNORE INTO vets VALUES (2, 'Helen', 'Leary'); -INSERT IGNORE INTO vets VALUES (3, 'Linda', 'Douglas'); -INSERT IGNORE INTO vets VALUES (4, 'Rafael', 'Ortega'); -INSERT IGNORE INTO vets VALUES (5, 'Henry', 'Stevens'); -INSERT IGNORE INTO vets VALUES (6, 'Sharon', 'Jenkins'); - -INSERT IGNORE INTO specialties VALUES (1, 'radiology'); -INSERT IGNORE INTO specialties VALUES (2, 'surgery'); -INSERT IGNORE INTO specialties VALUES (3, 'dentistry'); - -INSERT IGNORE INTO vet_specialties VALUES (2, 1); -INSERT IGNORE INTO vet_specialties VALUES (3, 2); -INSERT IGNORE INTO vet_specialties VALUES (3, 3); -INSERT IGNORE INTO vet_specialties VALUES (4, 2); -INSERT IGNORE INTO vet_specialties VALUES (5, 1); - -INSERT IGNORE INTO types VALUES (1, 'cat'); -INSERT IGNORE INTO types VALUES (2, 'dog'); -INSERT IGNORE INTO types VALUES (3, 'lizard'); -INSERT IGNORE INTO types VALUES (4, 'snake'); -INSERT IGNORE INTO types VALUES (5, 'bird'); -INSERT IGNORE INTO types VALUES (6, 'hamster'); - -INSERT IGNORE INTO owners VALUES (1, 'George', 'Franklin', '110 W. Liberty St.', 'Madison', '6085551023'); -INSERT IGNORE INTO owners VALUES (2, 'Betty', 'Davis', '638 Cardinal Ave.', 'Sun Prairie', '6085551749'); -INSERT IGNORE INTO owners VALUES (3, 'Eduardo', 'Rodriquez', '2693 Commerce St.', 'McFarland', '6085558763'); -INSERT IGNORE INTO owners VALUES (4, 'Harold', 'Davis', '563 Friendly St.', 'Windsor', '6085553198'); -INSERT IGNORE INTO owners VALUES (5, 'Peter', 'McTavish', '2387 S. Fair Way', 'Madison', '6085552765'); -INSERT IGNORE INTO owners VALUES (6, 'Jean', 'Coleman', '105 N. Lake St.', 'Monona', '6085552654'); -INSERT IGNORE INTO owners VALUES (7, 'Jeff', 'Black', '1450 Oak Blvd.', 'Monona', '6085555387'); -INSERT IGNORE INTO owners VALUES (8, 'Maria', 'Escobito', '345 Maple St.', 'Madison', '6085557683'); -INSERT IGNORE INTO owners VALUES (9, 'David', 'Schroeder', '2749 Blackhawk Trail', 'Madison', '6085559435'); -INSERT IGNORE INTO owners VALUES (10, 'Carlos', 'Estaban', '2335 Independence La.', 'Waunakee', '6085555487'); - -INSERT IGNORE INTO pets VALUES (1, 'Leo', '2000-09-07', 1, 1); -INSERT IGNORE INTO pets VALUES (2, 'Basil', '2002-08-06', 6, 2); -INSERT IGNORE INTO pets VALUES (3, 'Rosy', '2001-04-17', 2, 3); -INSERT IGNORE INTO pets VALUES (4, 'Jewel', '2000-03-07', 2, 3); -INSERT IGNORE INTO pets VALUES (5, 'Iggy', '2000-11-30', 3, 4); -INSERT IGNORE INTO pets VALUES (6, 'George', '2000-01-20', 4, 5); -INSERT IGNORE INTO pets VALUES (7, 'Samantha', '1995-09-04', 1, 6); -INSERT IGNORE INTO pets VALUES (8, 'Max', '1995-09-04', 1, 6); -INSERT IGNORE INTO pets VALUES (9, 'Lucky', '1999-08-06', 5, 7); -INSERT IGNORE INTO pets VALUES (10, 'Mulligan', '1997-02-24', 2, 8); -INSERT IGNORE INTO pets VALUES (11, 'Freddy', '2000-03-09', 5, 9); -INSERT IGNORE INTO pets VALUES (12, 'Lucky', '2000-06-24', 2, 10); -INSERT IGNORE INTO pets VALUES (13, 'Sly', '2002-06-08', 1, 10); - -INSERT IGNORE INTO visits VALUES (1, 7, '2010-03-04', 'rabies shot'); -INSERT IGNORE INTO visits VALUES (2, 8, '2011-03-04', 'rabies shot'); -INSERT IGNORE INTO visits VALUES (3, 8, '2009-06-04', 'neutered'); -INSERT IGNORE INTO visits VALUES (4, 7, '2008-09-04', 'spayed'); diff --git a/src/main/resources/db/mysql/petclinic_db_setup_mysql.txt b/src/main/resources/db/mysql/petclinic_db_setup_mysql.txt deleted file mode 100644 index f6ce6523c37..00000000000 --- a/src/main/resources/db/mysql/petclinic_db_setup_mysql.txt +++ /dev/null @@ -1,31 +0,0 @@ -================================================================================ -=== Spring PetClinic sample application - MySQL Configuration === -================================================================================ - -@author Sam Brannen -@author Costin Leau -@author Dave Syer - --------------------------------------------------------------------------------- - -1) Download and install the MySQL database (e.g., MySQL Community Server 5.1.x), - which can be found here: https://dev.mysql.com/downloads/. Or run the - "docker-compose.yml" from the root of the project (if you have docker installed - locally): - - $ docker-compose up - ... - mysql_1_eedb4818d817 | MySQL init process done. Ready for start up. - ... - -2) (Once only) create the PetClinic database and user by executing the "db/mysql/user.sql" - scripts. You can connect to the database running in the docker container using - `mysql -u root -h localhost --protocol tcp`, but you don't need to run the script there - because the petclinic user is already set up if you use the provided `docker-compose.yaml`. - -3) Run the app with `spring.profiles.active=mysql` (e.g. as a System property via the command - line, but any way that sets that property in a Spring Boot app should work). - -N.B. the "petclinic" database has to exist for the app to work with the JDBC URL value -as it is configured by default. This condition is taken care of by the docker-compose -configuration provided, or by the `schema.sql` if you can run that as root. diff --git a/src/main/resources/db/mysql/schema.sql b/src/main/resources/db/mysql/schema.sql deleted file mode 100644 index eb5d7d5d0c2..00000000000 --- a/src/main/resources/db/mysql/schema.sql +++ /dev/null @@ -1,55 +0,0 @@ -CREATE TABLE IF NOT EXISTS vets ( - id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, - first_name VARCHAR(30), - last_name VARCHAR(30), - INDEX(last_name) -) engine=InnoDB; - -CREATE TABLE IF NOT EXISTS specialties ( - id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, - name VARCHAR(80), - INDEX(name) -) engine=InnoDB; - -CREATE TABLE IF NOT EXISTS vet_specialties ( - vet_id INT(4) UNSIGNED NOT NULL, - specialty_id INT(4) UNSIGNED NOT NULL, - FOREIGN KEY (vet_id) REFERENCES vets(id), - FOREIGN KEY (specialty_id) REFERENCES specialties(id), - UNIQUE (vet_id,specialty_id) -) engine=InnoDB; - -CREATE TABLE IF NOT EXISTS types ( - id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, - name VARCHAR(80), - INDEX(name) -) engine=InnoDB; - -CREATE TABLE IF NOT EXISTS owners ( - id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, - first_name VARCHAR(30), - last_name VARCHAR(30), - address VARCHAR(255), - city VARCHAR(80), - telephone VARCHAR(20), - INDEX(last_name) -) engine=InnoDB; - -CREATE TABLE IF NOT EXISTS pets ( - id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, - name VARCHAR(30), - birth_date DATE, - type_id INT(4) UNSIGNED NOT NULL, - owner_id INT(4) UNSIGNED NOT NULL, - INDEX(name), - FOREIGN KEY (owner_id) REFERENCES owners(id), - FOREIGN KEY (type_id) REFERENCES types(id) -) engine=InnoDB; - -CREATE TABLE IF NOT EXISTS visits ( - id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, - pet_id INT(4) UNSIGNED NOT NULL, - visit_date DATE, - description VARCHAR(255), - FOREIGN KEY (pet_id) REFERENCES pets(id) -) engine=InnoDB; diff --git a/src/main/resources/db/mysql/user.sql b/src/main/resources/db/mysql/user.sql deleted file mode 100644 index 60abcee729a..00000000000 --- a/src/main/resources/db/mysql/user.sql +++ /dev/null @@ -1,7 +0,0 @@ -CREATE DATABASE IF NOT EXISTS petclinic; - -ALTER DATABASE petclinic - DEFAULT CHARACTER SET utf8 - DEFAULT COLLATE utf8_general_ci; - -GRANT ALL PRIVILEGES ON petclinic.* TO 'petclinic@%' IDENTIFIED BY 'petclinic'; From 42546d67e754e99d54c2fd2984a8fae82a37c2d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Parejo=20Maestre?= Date: Sat, 3 Oct 2020 12:54:18 +0200 Subject: [PATCH 07/27] Removed disease for setting an starting point for the exercises --- .../samples/petclinic/model/Disease.java | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 src/main/java/org/springframework/samples/petclinic/model/Disease.java diff --git a/src/main/java/org/springframework/samples/petclinic/model/Disease.java b/src/main/java/org/springframework/samples/petclinic/model/Disease.java deleted file mode 100644 index 17d712532e4..00000000000 --- a/src/main/java/org/springframework/samples/petclinic/model/Disease.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.springframework.samples.petclinic.model; - -import javax.persistence.Entity; -import javax.persistence.Table; - -@Entity -@Table(name = "diseases") -public class Disease extends NamedEntity{ - -} From 116cbda192b18dcefc692eb1d8bfde5c6be84016 Mon Sep 17 00:00:00 2001 From: Jose Antonio Parejo Maestre Date: Wed, 7 Oct 2020 10:40:42 +0200 Subject: [PATCH 08/27] Modified links from DP2 to DP1 --- readme.md | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/readme.md b/readme.md index 0242b887323..94555867657 100644 --- a/readme.md +++ b/readme.md @@ -29,22 +29,13 @@ Or you can run it from Maven directly using the Spring Boot Maven plugin. If you ``` ## In case you find a bug/suggested improvement for Spring Petclinic -Our issue tracker is available here: https://github.com/gii-is-DP2/spring-petclinic/issues +Our issue tracker is available here: https://github.com/gii-is-DP1/spring-petclinic/issues ## Database configuration In its default configuration, Petclinic uses an in-memory database (H2) which -gets populated at startup with data. A similar setup is provided for MySql in case a persistent database configuration is needed. -Note that whenever the database type is changed, the app needs to be run with a different profile: `spring.profiles.active=mysql` for MySql. - -You could start MySql locally with whatever installer works for your OS, or with docker: - -``` -docker run -e MYSQL_USER=petclinic -e MYSQL_PASSWORD=petclinic -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=petclinic -p 3306:3306 mysql:5.7.8 -``` - -Further documentation is provided [here](https://github.com/gii-is-DP2/spring-petclinic/blob/master/src/main/resources/db/mysql/petclinic_db_setup_mysql.txt). +gets populated at startup with data. ## Working with Petclinic in your IDE @@ -63,7 +54,7 @@ The following items should be installed in your system: 1) On the command line ``` -git clone https://github.com/gii-is-DP2/spring-petclinic.git +git clone https://github.com/gii-is-DP1/spring-petclinic.git ``` 2) Inside Eclipse or STS ``` @@ -92,9 +83,9 @@ Visit [http://localhost:8080](http://localhost:8080) in your browser. |Spring Boot Configuration | Class or Java property files | |--------------------------|---| -|The Main Class | [PetClinicApplication](https://github.com/gii-is-DP2/spring-petclinic/blob/master/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java) | -|Properties Files | [application.properties](https://github.com/gii-is-DP2/spring-petclinic/blob/master/src/main/resources) | -|Caching | [CacheConfiguration](https://github.com/gii-is-DP2/spring-petclinic/blob/master/src/main/java/org/springframework/samples/petclinic/system/CacheConfiguration.java) | +|The Main Class | [PetClinicApplication](https://github.com/gii-is-DP1/spring-petclinic/blob/master/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java) | +|Properties Files | [application.properties](https://github.com/gii-is-DP1/spring-petclinic/blob/master/src/main/resources) | +|Caching | [CacheConfiguration](https://github.com/gii-is-DP1/spring-petclinic/blob/master/src/main/java/org/springframework/samples/petclinic/system/CacheConfiguration.java) | ## Interesting Spring Petclinic branches and forks @@ -106,7 +97,7 @@ that could be used to implement the Pet Clinic then please join the community th # Contributing -The [issue tracker](https://github.com/gii-is-DP2/spring-petclinic/issues) is the preferred channel for bug reports, features requests and submitting pull requests. +The [issue tracker](https://github.com/gii-is-DP1/spring-petclinic/issues) is the preferred channel for bug reports, features requests and submitting pull requests. For pull requests, editor preferences are available in the [editor config](.editorconfig) for easy use in common text editors. Read more and download plugins at . If you have not previously done so, please fill out and submit the [Contributor License Agreement](https://cla.pivotal.io/sign/spring). From ba815e4eecd035ece253e03d8757bf2c6817f89f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Parejo=20Maestre?= Date: Wed, 7 Oct 2020 11:46:23 +0200 Subject: [PATCH 09/27] Changes to support submenus in menu items --- src/main/webapp/WEB-INF/tags/menuItem.tag | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/webapp/WEB-INF/tags/menuItem.tag b/src/main/webapp/WEB-INF/tags/menuItem.tag index 8c14dbbc545..1530f5179db 100644 --- a/src/main/webapp/WEB-INF/tags/menuItem.tag +++ b/src/main/webapp/WEB-INF/tags/menuItem.tag @@ -1,13 +1,23 @@ <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> - +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ attribute name="active" required="true" rtexprvalue="true" %> <%@ attribute name="url" required="true" rtexprvalue="true" %> <%@ attribute name="title" required="false" rtexprvalue="true" %> - -
  • +<%@ attribute name="dropdown" required="false" rtexprvalue="true" %> +
  • " - title="${fn:escapeXml(title)}"> + title="${fn:escapeXml(title)}" class="${dropdown ? 'dropdown-toggle' : ''}" + ${dropdown ? 'data-toggle="dropdown"' : ''}> + + + + + ${title} + + + +
  • From 537797d620ae9541c4f61330bd74479be650ae5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Parejo=20Maestre?= Date: Wed, 7 Oct 2020 19:53:19 +0200 Subject: [PATCH 10/27] Added messaging notifications --- src/main/webapp/WEB-INF/tags/layout.tag | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/webapp/WEB-INF/tags/layout.tag b/src/main/webapp/WEB-INF/tags/layout.tag index 870c735b2d9..ac63028616a 100644 --- a/src/main/webapp/WEB-INF/tags/layout.tag +++ b/src/main/webapp/WEB-INF/tags/layout.tag @@ -1,5 +1,6 @@ <%@ tag trimDirectiveWhitespaces="true" %> <%@ taglib prefix="petclinic" tagdir="/WEB-INF/tags" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ attribute name="pageName" required="true" %> <%@ attribute name="customScript" required="false" fragment="true"%> @@ -13,6 +14,14 @@
    + + + From 80cf8ba9ef35aa0961711415c1debfaf35fcf8c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Mar=C3=ADa=20Garc=C3=ADa?= Date: Tue, 13 Oct 2020 23:36:08 +0200 Subject: [PATCH 11/27] Update readme.md Typos fixed --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 94555867657..67424390b53 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ # Spring PetClinic Sample Application -This is a fork of https://github.com/spring-projects/spring-petclinic to be used for the DP2 course. The main changes that have been performed were: +This is a fork of https://github.com/spring-projects/spring-petclinic to be used for the DP1 course. The main changes that have been performed were: - Trimming several parts of the application to keep the example low - Reorganize some parts of the code according to best practices introduced in the course @@ -12,7 +12,7 @@ Petclinic is a [Spring Boot](https://spring.io/guides/gs/spring-boot) applicatio ``` -git clone https://github.com/gii-is-DP2/spring-petclinic.git +git clone https://github.com/gii-is-DP1/spring-petclinic.git cd spring-petclinic ./mvnw package java -jar target/*.jar From f72b8ac79f60404397783cd6701e409b32011659 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Parejo=20Maestre?= Date: Fri, 16 Oct 2020 21:07:41 +0200 Subject: [PATCH 12/27] Added generic Entity converter for ease of use and simplicty. --- .../GenericIdToEntityConverter.java | 60 +++++++++++++++++++ .../petclinic/configuration/WebConfig.java | 37 ++++++++++++ .../repository/AbstractJPARepository.java | 53 ++++++++++++++++ .../repository/GenericJPARepository.java | 13 ++++ 4 files changed, 163 insertions(+) create mode 100644 src/main/java/org/springframework/samples/petclinic/configuration/GenericIdToEntityConverter.java create mode 100644 src/main/java/org/springframework/samples/petclinic/configuration/WebConfig.java create mode 100644 src/main/java/org/springframework/samples/petclinic/repository/AbstractJPARepository.java create mode 100644 src/main/java/org/springframework/samples/petclinic/repository/GenericJPARepository.java diff --git a/src/main/java/org/springframework/samples/petclinic/configuration/GenericIdToEntityConverter.java b/src/main/java/org/springframework/samples/petclinic/configuration/GenericIdToEntityConverter.java new file mode 100644 index 00000000000..cab4316a351 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/configuration/GenericIdToEntityConverter.java @@ -0,0 +1,60 @@ +package org.springframework.samples.petclinic.configuration; + +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.Converter; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.convert.ConversionService; +import org.springframework.core.convert.TypeDescriptor; +import org.springframework.core.convert.converter.ConditionalGenericConverter; +import org.springframework.core.convert.support.DefaultConversionService; +import org.springframework.samples.petclinic.model.BaseEntity; +import org.springframework.stereotype.Component; + +@Component() +public final class GenericIdToEntityConverter implements ConditionalGenericConverter { + private static final Logger log = LoggerFactory.getLogger(GenericIdToEntityConverter.class); + + private final ConversionService conversionService=new DefaultConversionService(); + + @Autowired(required = false) + private EntityManager entityManager; + + + + public Set getConvertibleTypes() { + Set result=new HashSet(); + result.add(new ConvertiblePair(Number.class, BaseEntity.class)); + result.add(new ConvertiblePair(CharSequence.class, BaseEntity.class)); + return result; + } + + public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { + return BaseEntity.class.isAssignableFrom(targetType.getType()) + && this.conversionService.canConvert(sourceType, TypeDescriptor.valueOf(Integer.class)); + } + + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { + if (source == null || entityManager==null) { + return null; + } + + Integer id = (Integer) this.conversionService.convert(source, sourceType, TypeDescriptor.valueOf(Integer.class)); + + Object entity = entityManager.find(targetType.getType(), id); + if (entity == null) { + log.info("Did not find an entity with id {} of type {}", id, targetType.getType()); + return null; + } + + return entity; + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/configuration/WebConfig.java b/src/main/java/org/springframework/samples/petclinic/configuration/WebConfig.java new file mode 100644 index 00000000000..27371adfe0a --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/configuration/WebConfig.java @@ -0,0 +1,37 @@ +package org.springframework.samples.petclinic.configuration; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.convert.ConversionService; +import org.springframework.format.FormatterRegistry; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.view.InternalResourceViewResolver; +import org.springframework.web.servlet.view.JstlView; + + +public class WebConfig implements WebMvcConfigurer { + + @Autowired + GenericIdToEntityConverter idToEntityConverter; + + @Override + public void addFormatters(FormatterRegistry registry) { + + registry.addConverter(idToEntityConverter); + } + + @Override + public void configureViewResolvers(ViewResolverRegistry registry) { + InternalResourceViewResolver resolver = new InternalResourceViewResolver(); + resolver.setPrefix("/WEB-INF/jsp/"); + resolver.setSuffix(".jsp"); + resolver.setViewClass(JstlView.class); + registry.viewResolver(resolver); + } +} \ No newline at end of file diff --git a/src/main/java/org/springframework/samples/petclinic/repository/AbstractJPARepository.java b/src/main/java/org/springframework/samples/petclinic/repository/AbstractJPARepository.java new file mode 100644 index 00000000000..04a4d90f307 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/repository/AbstractJPARepository.java @@ -0,0 +1,53 @@ +package org.springframework.samples.petclinic.repository; + +import java.io.Serializable; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +public abstract class AbstractJPARepository< T extends Serializable > { + + private Class< T > clazz; + + @PersistenceContext + EntityManager entityManager; + + public void setClazz( Class< T > clazzToSet ) { + this.clazz = clazzToSet; + } + + public T findOne( Long id ){ + return entityManager.find( clazz, id ); + } + public List< T > findAll(){ + return entityManager.createQuery( "from " + clazz.getName() ) + .getResultList(); + } + + public void save( T entity ){ + entityManager.persist( entity ); + } + + public void update( T entity ){ + entityManager.merge( entity ); + } + + public boolean delete( T entity ){ + if(entity == null) + return false; + else { + try { + entityManager.remove( entity ); + return true; + }catch(IllegalArgumentException ex) { + ex.printStackTrace(); + return false; + } + } + } + public boolean deleteById( Long entityId ){ + T entity = findOne( entityId ); + return delete( entity ); + } + } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/GenericJPARepository.java b/src/main/java/org/springframework/samples/petclinic/repository/GenericJPARepository.java new file mode 100644 index 00000000000..ae5024174c7 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/repository/GenericJPARepository.java @@ -0,0 +1,13 @@ +package org.springframework.samples.petclinic.repository; + +import java.io.Serializable; + +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Repository; + +@Repository +@Scope( BeanDefinition.SCOPE_PROTOTYPE ) +public class GenericJPARepository< T extends Serializable > extends AbstractJPARepository< T > { + +} From b3aee02342f0f721f2818395b12ac5deba64035e Mon Sep 17 00:00:00 2001 From: Jose Antonio Parejo Maestre Date: Mon, 19 Oct 2020 13:28:16 +0200 Subject: [PATCH 13/27] Removed unused import causing errors in JDK11 --- .../samples/petclinic/service/PetServiceTests.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/org/springframework/samples/petclinic/service/PetServiceTests.java b/src/test/java/org/springframework/samples/petclinic/service/PetServiceTests.java index 7f23839a5b5..936e735f3d5 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/PetServiceTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/PetServiceTests.java @@ -24,7 +24,6 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.context.annotation.ComponentScan; From 14403794c5281a9980d086a99a359a402473baad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Parejo=20Maestre?= Date: Tue, 27 Oct 2020 09:47:37 +0100 Subject: [PATCH 14/27] Arrangement of diagrams in conceptual models and domain models. Conceptual models added. --- .../conceptual/petclinic-with-vademecum.uxf | 462 ++++++++++++++++++ .../diagrams/conceptual/petclinic.uxf | 35 ++ .../spring-petclinic-with-vademecum.uxf | 299 ++++++------ .../{ => domain}/spring-petclinic.uxf | 0 src/main/webapp/WEB-INF/tags/footer.tag | 1 + 5 files changed, 660 insertions(+), 137 deletions(-) create mode 100644 src/main/resources/diagrams/conceptual/petclinic-with-vademecum.uxf create mode 100644 src/main/resources/diagrams/conceptual/petclinic.uxf rename src/main/resources/diagrams/{ => domain}/spring-petclinic-with-vademecum.uxf (71%) rename src/main/resources/diagrams/{ => domain}/spring-petclinic.uxf (100%) diff --git a/src/main/resources/diagrams/conceptual/petclinic-with-vademecum.uxf b/src/main/resources/diagrams/conceptual/petclinic-with-vademecum.uxf new file mode 100644 index 00000000000..77351760695 --- /dev/null +++ b/src/main/resources/diagrams/conceptual/petclinic-with-vademecum.uxf @@ -0,0 +1,462 @@ + + + // Uncomment the following line to change the fontsize and font: +// fontsize=28 +// fontfamily=SansSerif //possible: SansSerif,Serif,Monospaced + + +////////////////////////////////////////////////////////////////////////////////////////////// +// Welcome to UMLet! +// +// Double-click on elements to add them to the diagram, or to copy them +// Edit elements by modifying the text in this panel +// Hold Ctrl to select multiple elements +// Use Ctrl+mouse to select via lasso +// +// Use +/- or Ctrl+mouse wheel to zoom +// Drag a whole relation at its central square icon +// +// Press Ctrl+C to copy the whole diagram to the system clipboard (then just paste it to, eg, Word) +// Edit the files in the "palettes" directory to create your own element palettes +// +// Select "Custom Elements > New..." to create new element types +////////////////////////////////////////////////////////////////////////////////////////////// + + +// This text will be stored with each diagram; use it for notes. + 15 + + UMLClass + + 15 + 240 + 315 + 105 + + Owner +-- +address +city +telephone + + + + + UMLClass + + 630 + 255 + 315 + 105 + + Vet +-- + + + + + UMLClass + + 0 + 495 + 315 + 105 + + Pet +-- +name +birthDate + + + + UMLClass + + 390 + 750 + 315 + 105 + + Visit +-- +date +description + + + + UMLClass + + 15 + 750 + 315 + 105 + + PetType +-- +name + + + + UMLClass + + 630 + 495 + 180 + 105 + + Specialty +-- +name + + + + UMLClass + + 240 + 0 + 315 + 105 + + Person +-- +firstName +lastName + + + + Relation + + 690 + 345 + 195 + 180 + + lt=->>>> +m1=0..n +m2=0..n +specializes in > + 10.0;10.0;10.0;100.0 + + + Relation + + 135 + 585 + 150 + 195 + + lt=->>>> +m1=0..n +m2=1 +is of type > + 10.0;10.0;10.0;110.0 + + + Relation + + 300 + 540 + 285 + 240 + + lt=- +m1=1 +m2=0..n +m2pos=-5,-10 +has visits > + + 10.0;10.0;170.0;140.0 + + + Relation + + 150 + 330 + 120 + 195 + + lt=- +m1=1 +m2=0..n +owns > + 10.0;10.0;10.0;110.0 + + + Relation + + 150 + 90 + 270 + 180 + + lt=<<- + 160.0;10.0;160.0;60.0;10.0;60.0;10.0;100.0 + + + Relation + + 375 + 90 + 435 + 195 + + lt=<<- + 10.0;10.0;10.0;60.0;270.0;60.0;270.0;110.0 + + + UMLClass + + 525 + 975 + 315 + 105 + + Diagnose +-- +fg=Red +description + + + + UMLClass + + 45 + 990 + 315 + 105 + + Disease +-- +fg=Red +name +description + + + + Relation + + 165 + 840 + 195 + 180 + + lt=- +fg=red +m1=1..n +m2=0..n +could develop > + 10.0;10.0;10.0;100.0 + + + Relation + + 555 + 840 + 135 + 165 + + lt=- +fg=red +m1=1 +m2=0..1 +leads to > + 10.0;10.0;10.0;90.0 + + + Relation + + 345 + 1005 + 210 + 75 + + lt=- +fg=red +m1=1 +m2=0..n + + 10.0;20.0;120.0;20.0 + + + Relation + + 780 + 345 + 225 + 660 + + lt=- +fg=red +m1=1 +m2=0..n +performs a > + + 80.0;10.0;10.0;420.0 + + + UMLClass + + 510 + 1245 + 210 + 120 + + Treatment +fg=red +-- +description +dose +periodicity + + + + Relation + + 600 + 1065 + 165 + 210 + + lt=- +fg=red +m1=0..n +m2=1 +prescribes > + + 10.0;120.0;10.0;10.0 + + + UMLClass + + 0 + 1260 + 315 + 120 + + Prevalence +-- +fg=red +percentage: int {Min(0),Max(100)} +usualSeverity: String {NotEmpty} + + + + Relation + + 120 + 1080 + 210 + 150 + + lt=- +m1=1..n +m2=1..n +fg=red +manifests > + + 120.0;70.0;10.0;70.0;10.0;10.0 + + + UMLClass + + 300 + 1140 + 150 + 90 + + Symptom +-- +fg=red +name +description + + + + Relation + + 60 + 1170 + 120 + 120 + + lt=. +fg=red + + 10.0;60.0;60.0;10.0 + + + Relation + + 1035 + 1050 + 300 + 165 + + lt=- +fg=red +m1=1..n +m2=1..n +incompatible +with > + 40.0;10.0;100.0;10.0;100.0;90.0;10.0;90.0;10.0;40.0 + + + UMLClass + + 885 + 1035 + 210 + 75 + + ActivePrinciple +-- +fg=red +name + + + + Relation + + 900 + 1095 + 150 + 210 + + lt=- +fg=red +m1=0..n +m2=1 +< contains + 10.0;120.0;10.0;10.0 + + + UMLClass + + 885 + 1275 + 150 + 90 + + Drug +-- +fg=red +name +posology + + + + Relation + + 705 + 1275 + 210 + 75 + + lt=- +fg=red +m1=0..n +m2=1 +contains > + + 10.0;20.0;120.0;20.0 + + diff --git a/src/main/resources/diagrams/conceptual/petclinic.uxf b/src/main/resources/diagrams/conceptual/petclinic.uxf new file mode 100644 index 00000000000..8caef5647f3 --- /dev/null +++ b/src/main/resources/diagrams/conceptual/petclinic.uxf @@ -0,0 +1,35 @@ +10UMLClass6018021070Owner +-- +address +city +telephone +UMLClass47019021070Vet +-- +UMLClass5035021070Pet +-- +name +birthDateUMLClass31052021070Visit +-- +date +descriptionUMLClass6052021070PetType +-- +nameUMLClass47035021070Specialty +-- +nameUMLClass2102021070Person +-- +firstName +lastNameRelation560250110120lt=->>>> +m1=0..n +m2=0..n +specializes in >10;10;10;100Relation14041090130lt=->>>> +m1=0..n +m2=1 +is of type >10;10;10;110Relation250380190160lt=- +m1=1 +m2=0..n +m2pos=-5,-10 +has visits > +10;10;170;140Relation15024070130lt=- +m1=1 +m2=0..n +owns >10;10;10;110Relation15080180120lt=<<-160;10;160;60;10;60;10;100Relation30080290130lt=<<-10;10;10;60;270;60;270;110 \ No newline at end of file diff --git a/src/main/resources/diagrams/spring-petclinic-with-vademecum.uxf b/src/main/resources/diagrams/domain/spring-petclinic-with-vademecum.uxf similarity index 71% rename from src/main/resources/diagrams/spring-petclinic-with-vademecum.uxf rename to src/main/resources/diagrams/domain/spring-petclinic-with-vademecum.uxf index 2bf65cd732d..f69281d624e 100644 --- a/src/main/resources/diagrams/spring-petclinic-with-vademecum.uxf +++ b/src/main/resources/diagrams/domain/spring-petclinic-with-vademecum.uxf @@ -24,14 +24,14 @@ // This text will be stored with each diagram; use it for notes. - 10 + 13 UMLClass - 620 - 100 - 280 - 70 + 806 + 104 + 364 + 91 Pet -- @@ -42,10 +42,10 @@ birthDate: Date {Past} UMLClass - 100 - 350 - 100 - 40 + 130 + 429 + 130 + 52 Vet @@ -53,10 +53,10 @@ birthDate: Date {Past} Relation - 700 - 60 - 30 - 60 + 910 + 52 + 39 + 78 lt=<<<- 10.0;10.0;10.0;40.0 @@ -64,10 +64,10 @@ birthDate: Date {Past} UMLClass - 650 - 20 - 190 - 50 + 845 + 0 + 247 + 65 NamedEntity -- @@ -78,10 +78,10 @@ name: String {NotEmpty} UMLClass - 250 - 100 - 230 - 90 + 325 + 104 + 299 + 117 Visit -- @@ -92,10 +92,10 @@ description: String {NotEmpty} UMLClass - 860 - 210 - 120 - 40 + 1118 + 247 + 156 + 52 PetType @@ -104,10 +104,10 @@ description: String {NotEmpty} Relation - 470 - 130 - 170 - 50 + 611 + 143 + 221 + 65 lt=- m1=1 @@ -118,10 +118,10 @@ m2=0..n Relation - 700 - 60 - 380 - 270 + 910 + 52 + 494 + 351 lt=<<<- 10.0;10.0;10.0;30.0;360.0;30.0;360.0;250.0 @@ -129,10 +129,10 @@ m2=0..n Relation - 770 - 160 - 110 - 90 + 1001 + 182 + 143 + 117 lt=<- 90.0;70.0;10.0;70.0;10.0;10.0 @@ -140,10 +140,10 @@ m2=0..n UMLClass - 1010 - 310 - 180 - 90 + 1313 + 377 + 234 + 117 Disease -- @@ -153,10 +153,10 @@ description: String {NotEmpty} Relation - 700 - 60 - 270 - 170 + 910 + 52 + 351 + 221 lt=<<<- 10.0;10.0;10.0;30.0;250.0;30.0;250.0;150.0 @@ -164,10 +164,10 @@ description: String {NotEmpty} UMLClass - 1190 - 430 - 100 - 50 + 1547 + 533 + 130 + 65 Symptom @@ -176,10 +176,10 @@ description: String {NotEmpty} Relation - 700 - 60 - 530 - 390 + 910 + 52 + 689 + 507 lt=<<<- 10.0;10.0;10.0;30.0;510.0;30.0;510.0;370.0 @@ -187,10 +187,10 @@ description: String {NotEmpty} UMLClass - 350 - 330 - 190 - 60 + 455 + 403 + 247 + 78 Diagnosis -- @@ -200,12 +200,13 @@ description: String UMLClass - 390 - 470 - 260 - 80 + 507 + 585 + 338 + 104 Treatment +fg=red -- description: String {NotEmpty} dose: Integer {Min(1)} @@ -215,21 +216,44 @@ periodicity: String UMLClass - 1250 - 520 - 100 - 40 + 1625 + 650 + 130 + 52 - Drug + // Uncomment the following line to change the fontsize and font: +// fontsize=28 +// fontfamily=SansSerif //possible: SansSerif,Serif,Monospaced + + +////////////////////////////////////////////////////////////////////////////////////////////// +// Welcome to UMLet! +// +// Double-click on elements to add them to the diagram, or to copy them +// Edit elements by modifying the text in this panel +// Hold Ctrl to select multiple elements +// Use Ctrl+mouse to select via lasso +// +// Use +/- or Ctrl+mouse wheel to zoom +// Drag a whole relation at its central square icon +// +// Press Ctrl+C to copy the whole diagram to the system clipboard (then just paste it to, eg, Word) +// Edit the files in the "palettes" directory to create your own element palettes +// +// Select "Custom Elements > New..." to create new element types +////////////////////////////////////////////////////////////////////////////////////////////// + + +// This text will be stored with each diagram; use it for notes. Relation - 700 - 60 - 640 - 480 + 910 + 52 + 832 + 624 lt=<<<- 10.0;10.0;10.0;30.0;620.0;30.0;620.0;460.0 @@ -237,10 +261,10 @@ periodicity: String Relation - 370 - 180 - 50 - 170 + 481 + 208 + 65 + 221 lt=-> m1=0..1 @@ -250,10 +274,10 @@ m2=1 Relation - 190 - 350 - 180 - 50 + 247 + 429 + 234 + 65 lt=<- m1=1 @@ -264,10 +288,10 @@ m2=0..n Relation - 400 - 380 - 50 - 110 + 520 + 468 + 65 + 143 lt=- m1=1 @@ -278,12 +302,13 @@ m2=0..n Relation - 640 - 520 - 630 - 50 + 832 + 650 + 819 + 65 lt=- +fg=red m1=0..n m2=0..1 @@ -292,10 +317,10 @@ m2=0..1 Relation - 530 - 340 - 500 - 50 + 689 + 416 + 650 + 65 lt=<- m1=1 @@ -306,10 +331,10 @@ m2=0..n Relation - 1070 - 390 - 140 - 100 + 1391 + 481 + 182 + 130 lt=- m1=1..n @@ -320,10 +345,10 @@ m2=1..n UMLClass - 750 - 430 - 210 - 80 + 975 + 533 + 273 + 104 Prevalence -- @@ -334,10 +359,10 @@ usualSeverity: String {NotEmpty} Relation - 950 - 440 - 150 - 60 + 1235 + 546 + 195 + 78 lt=. @@ -346,10 +371,10 @@ usualSeverity: String {NotEmpty} Relation - 870 - 240 - 160 - 120 + 1131 + 286 + 208 + 156 lt=- m1=0..n @@ -361,9 +386,9 @@ develops UMLNote 0 - 570 - 490 - 80 + 715 + 637 + 104 *Business Rule 1* fg=red @@ -374,10 +399,10 @@ is compatible with the type of pet of its related visit Relation - 190 - 380 - 210 - 210 + 247 + 468 + 273 + 273 lt=. fg=red @@ -386,10 +411,10 @@ fg=red UMLClass - 1470 - 510 - 140 - 50 + 1911 + 637 + 182 + 65 ActivePrinciple @@ -397,10 +422,10 @@ fg=red Relation - 700 - 60 - 840 - 470 + 910 + 52 + 1092 + 611 lt=<<<- 10.0;10.0;10.0;30.0;820.0;30.0;820.0;450.0 @@ -408,10 +433,10 @@ fg=red Relation - 1340 - 520 - 150 - 50 + 1742 + 650 + 195 + 65 lt=-> m1=0..n @@ -422,10 +447,10 @@ contains Relation - 1540 - 520 - 240 - 110 + 2002 + 650 + 312 + 143 lt=- m1=1..n @@ -437,10 +462,10 @@ with UMLNote - 540 - 580 - 640 - 80 + 702 + 728 + 832 + 104 *Business Rule 2* fg=red @@ -451,10 +476,10 @@ contain incompatible active principles Relation - 510 - 540 - 140 - 60 + 663 + 676 + 182 + 78 lt=. fg=red diff --git a/src/main/resources/diagrams/spring-petclinic.uxf b/src/main/resources/diagrams/domain/spring-petclinic.uxf similarity index 100% rename from src/main/resources/diagrams/spring-petclinic.uxf rename to src/main/resources/diagrams/domain/spring-petclinic.uxf diff --git a/src/main/webapp/WEB-INF/tags/footer.tag b/src/main/webapp/WEB-INF/tags/footer.tag index fc38b255f2b..81a71b4e94e 100644 --- a/src/main/webapp/WEB-INF/tags/footer.tag +++ b/src/main/webapp/WEB-INF/tags/footer.tag @@ -12,3 +12,4 @@ + From 241afd61ed3457c79d40291124bd63711b71dba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Parejo=20Maestre?= Date: Tue, 27 Oct 2020 10:04:04 +0100 Subject: [PATCH 15/27] Minor fixes in conceptual diagram of petclinic extensio n --- .../service/businessrules/ElementInCollectionValidator.java | 5 +++++ .../diagrams/conceptual/petclinic-with-vademecum.uxf | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/springframework/samples/petclinic/service/businessrules/ElementInCollectionValidator.java diff --git a/src/main/java/org/springframework/samples/petclinic/service/businessrules/ElementInCollectionValidator.java b/src/main/java/org/springframework/samples/petclinic/service/businessrules/ElementInCollectionValidator.java new file mode 100644 index 00000000000..33bc7f67d1a --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/service/businessrules/ElementInCollectionValidator.java @@ -0,0 +1,5 @@ +package org.springframework.samples.petclinic.service.businessrules; + +public class ElementInCollectionValidator { + +} diff --git a/src/main/resources/diagrams/conceptual/petclinic-with-vademecum.uxf b/src/main/resources/diagrams/conceptual/petclinic-with-vademecum.uxf index 77351760695..fa03da586c8 100644 --- a/src/main/resources/diagrams/conceptual/petclinic-with-vademecum.uxf +++ b/src/main/resources/diagrams/conceptual/petclinic-with-vademecum.uxf @@ -329,14 +329,14 @@ prescribes > 0 1260 - 315 + 195 120 Prevalence -- fg=red -percentage: int {Min(0),Max(100)} -usualSeverity: String {NotEmpty} +percentage +usualSeverity From 232c9e5dae508c028362e8156be9215af162e539 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Parejo=20Maestre?= Date: Tue, 27 Oct 2020 14:57:42 +0100 Subject: [PATCH 16/27] Removed superfluous classes with no use --- .../repository/AbstractJPARepository.java | 53 ------------------- .../repository/GenericJPARepository.java | 13 ----- 2 files changed, 66 deletions(-) delete mode 100644 src/main/java/org/springframework/samples/petclinic/repository/AbstractJPARepository.java delete mode 100644 src/main/java/org/springframework/samples/petclinic/repository/GenericJPARepository.java diff --git a/src/main/java/org/springframework/samples/petclinic/repository/AbstractJPARepository.java b/src/main/java/org/springframework/samples/petclinic/repository/AbstractJPARepository.java deleted file mode 100644 index 04a4d90f307..00000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/AbstractJPARepository.java +++ /dev/null @@ -1,53 +0,0 @@ -package org.springframework.samples.petclinic.repository; - -import java.io.Serializable; -import java.util.List; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; - -public abstract class AbstractJPARepository< T extends Serializable > { - - private Class< T > clazz; - - @PersistenceContext - EntityManager entityManager; - - public void setClazz( Class< T > clazzToSet ) { - this.clazz = clazzToSet; - } - - public T findOne( Long id ){ - return entityManager.find( clazz, id ); - } - public List< T > findAll(){ - return entityManager.createQuery( "from " + clazz.getName() ) - .getResultList(); - } - - public void save( T entity ){ - entityManager.persist( entity ); - } - - public void update( T entity ){ - entityManager.merge( entity ); - } - - public boolean delete( T entity ){ - if(entity == null) - return false; - else { - try { - entityManager.remove( entity ); - return true; - }catch(IllegalArgumentException ex) { - ex.printStackTrace(); - return false; - } - } - } - public boolean deleteById( Long entityId ){ - T entity = findOne( entityId ); - return delete( entity ); - } - } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/GenericJPARepository.java b/src/main/java/org/springframework/samples/petclinic/repository/GenericJPARepository.java deleted file mode 100644 index ae5024174c7..00000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/GenericJPARepository.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.springframework.samples.petclinic.repository; - -import java.io.Serializable; - -import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.context.annotation.Scope; -import org.springframework.stereotype.Repository; - -@Repository -@Scope( BeanDefinition.SCOPE_PROTOTYPE ) -public class GenericJPARepository< T extends Serializable > extends AbstractJPARepository< T > { - -} From e42f27b1733a446e75c1614c085ce8b5f65f43ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Parejo=20Maestre?= Date: Mon, 21 Dec 2020 17:16:31 +0100 Subject: [PATCH 17/27] @Data annotation removed due to the undesired effects of the auto-generated equals when we have bidirectional relationships. --- .../samples/petclinic/model/Authorities.java | 5 ++++- .../org/springframework/samples/petclinic/model/User.java | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/model/Authorities.java b/src/main/java/org/springframework/samples/petclinic/model/Authorities.java index 522b215ce7a..405e91135bc 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Authorities.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Authorities.java @@ -7,8 +7,11 @@ import javax.validation.constraints.Size; import lombok.Data; +import lombok.Getter; +import lombok.Setter; -@Data +@Getter +@Setter @Entity @Table(name = "authorities") public class Authorities extends BaseEntity{ diff --git a/src/main/java/org/springframework/samples/petclinic/model/User.java b/src/main/java/org/springframework/samples/petclinic/model/User.java index b027024d1dc..9a05f6fb1ef 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/User.java +++ b/src/main/java/org/springframework/samples/petclinic/model/User.java @@ -8,9 +8,11 @@ import javax.persistence.OneToMany; import javax.persistence.Table; -import lombok.Data; +import lombok.Getter; +import lombok.Setter; -@Data +@Getter +@Setter @Entity @Table(name = "users") public class User{ From f2c431d4949827b92112a62d77c4604aea968d7a Mon Sep 17 00:00:00 2001 From: Manuel Resinas Date: Thu, 30 Sep 2021 19:37:56 +0200 Subject: [PATCH 18/27] Updated to Java 11 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 780b38b9a94..d2f022b4765 100644 --- a/pom.xml +++ b/pom.xml @@ -17,7 +17,7 @@ - 1.8 + 11 UTF-8 UTF-8 From 48dc23970c55c66b51b99642acb0f3fd74da0b0b Mon Sep 17 00:00:00 2001 From: Manuel Resinas Date: Thu, 30 Sep 2021 20:19:16 +0200 Subject: [PATCH 19/27] Reorganized packages to a domain-oriented approach The packages are owner, pet, vet and user. We chose to keep pet out of owner because it is the central element of the application. However, visit is included in pet because a visit heavily depends on the pet that makes it. These were also the same packages that were used to organized the JSP code. --- .../petclinic/{model => owner}/Owner.java | 361 +++++++++--------- .../{web => owner}/OwnerController.java | 286 +++++++------- .../OwnerRepository.java | 5 +- .../{service => owner}/OwnerService.java | 158 ++++---- .../samples/petclinic/{model => pet}/Pet.java | 6 +- .../petclinic/{web => pet}/PetController.java | 14 +- .../{repository => pet}/PetRepository.java | 4 +- .../{service => pet}/PetService.java | 159 ++++---- .../petclinic/{model => pet}/PetType.java | 4 +- .../{web => pet}/PetTypeFormatter.java | 6 +- .../petclinic/{web => pet}/PetValidator.java | 3 +- .../petclinic/{model => pet}/Visit.java | 3 +- .../{web => pet}/VisitController.java | 7 +- .../{repository => pet}/VisitRepository.java | 3 +- .../DuplicatedPetNameException.java | 2 +- .../ElementInCollectionValidator.java | 5 - .../{model => user}/Authorities.java | 4 +- .../AuthoritiesRepository.java | 3 +- .../{service => user}/AuthoritiesService.java | 129 +++---- .../petclinic/{model => user}/User.java | 2 +- .../{web => user}/UserController.java | 148 ++++--- .../{repository => user}/UserRepository.java | 3 +- .../{service => user}/UserService.java | 104 +++-- .../petclinic/{model => vet}/Specialty.java | 4 +- .../samples/petclinic/{model => vet}/Vet.java | 3 +- .../petclinic/{web => vet}/VetController.java | 4 +- .../{repository => vet}/VetRepository.java | 3 +- .../{service => vet}/VetService.java | 116 +++--- .../petclinic/{model => vet}/Vets.java | 2 +- .../petclinic/web/WelcomeController.java | 4 +- .../{web => owner}/OwnerControllerTests.java | 13 +- .../{service => owner}/OwnerServiceTests.java | 19 +- .../{web => pet}/PetControllerTests.java | 16 +- .../{service => pet}/PetServiceTests.java | 17 +- .../{web => pet}/PetTypeFormatterTests.java | 9 +- .../{web => pet}/VisitControllerTests.java | 9 +- .../{web => vet}/VetControllerTests.java | 9 +- .../{service => vet}/VetServiceTests.java | 19 +- 38 files changed, 824 insertions(+), 842 deletions(-) rename src/main/java/org/springframework/samples/petclinic/{model => owner}/Owner.java (94%) rename src/main/java/org/springframework/samples/petclinic/{web => owner}/OwnerController.java (88%) rename src/main/java/org/springframework/samples/petclinic/{repository => owner}/OwnerRepository.java (92%) rename src/main/java/org/springframework/samples/petclinic/{service => owner}/OwnerService.java (72%) rename src/main/java/org/springframework/samples/petclinic/{model => pet}/Pet.java (92%) rename src/main/java/org/springframework/samples/petclinic/{web => pet}/PetController.java (89%) rename src/main/java/org/springframework/samples/petclinic/{repository => pet}/PetRepository.java (91%) rename src/main/java/org/springframework/samples/petclinic/{service => pet}/PetService.java (81%) rename src/main/java/org/springframework/samples/petclinic/{model => pet}/PetType.java (87%) rename src/main/java/org/springframework/samples/petclinic/{web => pet}/PetTypeFormatter.java (90%) rename src/main/java/org/springframework/samples/petclinic/{web => pet}/PetValidator.java (94%) rename src/main/java/org/springframework/samples/petclinic/{model => pet}/Visit.java (95%) rename src/main/java/org/springframework/samples/petclinic/{web => pet}/VisitController.java (90%) rename src/main/java/org/springframework/samples/petclinic/{repository => pet}/VisitRepository.java (93%) rename src/main/java/org/springframework/samples/petclinic/{service => pet}/exceptions/DuplicatedPetNameException.java (81%) delete mode 100644 src/main/java/org/springframework/samples/petclinic/service/businessrules/ElementInCollectionValidator.java rename src/main/java/org/springframework/samples/petclinic/{model => user}/Authorities.java (79%) rename src/main/java/org/springframework/samples/petclinic/{repository => user}/AuthoritiesRepository.java (55%) rename src/main/java/org/springframework/samples/petclinic/{service => user}/AuthoritiesService.java (86%) rename src/main/java/org/springframework/samples/petclinic/{model => user}/User.java (89%) rename src/main/java/org/springframework/samples/petclinic/{web => user}/UserController.java (81%) rename src/main/java/org/springframework/samples/petclinic/{repository => user}/UserRepository.java (54%) rename src/main/java/org/springframework/samples/petclinic/{service => user}/UserService.java (86%) rename src/main/java/org/springframework/samples/petclinic/{model => vet}/Specialty.java (88%) rename src/main/java/org/springframework/samples/petclinic/{model => vet}/Vet.java (95%) rename src/main/java/org/springframework/samples/petclinic/{web => vet}/VetController.java (91%) rename src/main/java/org/springframework/samples/petclinic/{repository => vet}/VetRepository.java (92%) rename src/main/java/org/springframework/samples/petclinic/{service => vet}/VetService.java (65%) rename src/main/java/org/springframework/samples/petclinic/{model => vet}/Vets.java (95%) rename src/test/java/org/springframework/samples/petclinic/{web => owner}/OwnerControllerTests.java (94%) rename src/test/java/org/springframework/samples/petclinic/{service => owner}/OwnerServiceTests.java (88%) rename src/test/java/org/springframework/samples/petclinic/{web => pet}/PetControllerTests.java (90%) rename src/test/java/org/springframework/samples/petclinic/{service => pet}/PetServiceTests.java (93%) rename src/test/java/org/springframework/samples/petclinic/{web => pet}/PetTypeFormatterTests.java (86%) rename src/test/java/org/springframework/samples/petclinic/{web => pet}/VisitControllerTests.java (91%) rename src/test/java/org/springframework/samples/petclinic/{web => vet}/VetControllerTests.java (90%) rename src/test/java/org/springframework/samples/petclinic/{service => vet}/VetServiceTests.java (84%) diff --git a/src/main/java/org/springframework/samples/petclinic/model/Owner.java b/src/main/java/org/springframework/samples/petclinic/owner/Owner.java similarity index 94% rename from src/main/java/org/springframework/samples/petclinic/model/Owner.java rename to src/main/java/org/springframework/samples/petclinic/owner/Owner.java index 31b311aa9a6..8a2976828e1 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Owner.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/Owner.java @@ -1,179 +1,182 @@ -/* - * Copyright 2002-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.samples.petclinic.model; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import javax.persistence.CascadeType; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.JoinColumn; -import javax.persistence.OneToMany; -import javax.persistence.OneToOne; -import javax.persistence.Table; -import javax.validation.constraints.Digits; -import javax.validation.constraints.NotEmpty; - -import org.springframework.beans.support.MutableSortDefinition; -import org.springframework.beans.support.PropertyComparator; -import org.springframework.core.style.ToStringCreator; - -/** - * Simple JavaBean domain object representing an owner. - * - * @author Ken Krebs - * @author Juergen Hoeller - * @author Sam Brannen - * @author Michael Isvy - */ -@Entity -@Table(name = "owners") -public class Owner extends Person { - - @Column(name = "address") - @NotEmpty - private String address; - - @Column(name = "city") - @NotEmpty - private String city; - - @Column(name = "telephone") - @NotEmpty - @Digits(fraction = 0, integer = 10) - private String telephone; - - @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner") - private Set pets; - - // - @OneToOne(cascade = CascadeType.ALL) - @JoinColumn(name = "username", referencedColumnName = "username") - private User user; - // - - public String getAddress() { - return this.address; - } - - public User getUser() { - return user; - } - - public void setUser(User user) { - this.user = user; - } - - public void setAddress(String address) { - this.address = address; - } - - public String getCity() { - return this.city; - } - - public void setCity(String city) { - this.city = city; - } - - public String getTelephone() { - return this.telephone; - } - - public void setTelephone(String telephone) { - this.telephone = telephone; - } - - protected Set getPetsInternal() { - if (this.pets == null) { - this.pets = new HashSet<>(); - } - return this.pets; - } - - protected void setPetsInternal(Set pets) { - this.pets = pets; - } - - public List getPets() { - List sortedPets = new ArrayList<>(getPetsInternal()); - PropertyComparator.sort(sortedPets, new MutableSortDefinition("name", true, true)); - return Collections.unmodifiableList(sortedPets); - } - - public void addPet(Pet pet) { - getPetsInternal().add(pet); - pet.setOwner(this); - } - - public boolean removePet(Pet pet) { - return getPetsInternal().remove(pet); - } - - /** - * Return the Pet with the given name, or null if none found for this Owner. - * @param name to test - * @return true if pet name is already in use - */ - public Pet getPet(String name) { - return getPet(name, false); - } - - public Pet getPetwithIdDifferent(String name,Integer id) { - name = name.toLowerCase(); - for (Pet pet : getPetsInternal()) { - String compName = pet.getName(); - compName = compName.toLowerCase(); - if (compName.equals(name) && pet.getId()!=id) { - return pet; - } - } - return null; - } - - /** - * Return the Pet with the given name, or null if none found for this Owner. - * @param name to test - * @return true if pet name is already in use - */ - public Pet getPet(String name, boolean ignoreNew) { - name = name.toLowerCase(); - for (Pet pet : getPetsInternal()) { - if (!ignoreNew || !pet.isNew()) { - String compName = pet.getName(); - compName = compName.toLowerCase(); - if (compName.equals(name)) { - return pet; - } - } - } - return null; - } - - @Override - public String toString() { - return new ToStringCreator(this) - - .append("id", this.getId()).append("new", this.isNew()).append("lastName", this.getLastName()) - .append("firstName", this.getFirstName()).append("address", this.address).append("city", this.city) - .append("telephone", this.telephone).toString(); - } - -} +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.samples.petclinic.owner; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.JoinColumn; +import javax.persistence.OneToMany; +import javax.persistence.OneToOne; +import javax.persistence.Table; +import javax.validation.constraints.Digits; +import javax.validation.constraints.NotEmpty; + +import org.springframework.beans.support.MutableSortDefinition; +import org.springframework.beans.support.PropertyComparator; +import org.springframework.core.style.ToStringCreator; +import org.springframework.samples.petclinic.model.Person; +import org.springframework.samples.petclinic.pet.Pet; +import org.springframework.samples.petclinic.user.User; + +/** + * Simple JavaBean domain object representing an owner. + * + * @author Ken Krebs + * @author Juergen Hoeller + * @author Sam Brannen + * @author Michael Isvy + */ +@Entity +@Table(name = "owners") +public class Owner extends Person { + + @Column(name = "address") + @NotEmpty + private String address; + + @Column(name = "city") + @NotEmpty + private String city; + + @Column(name = "telephone") + @NotEmpty + @Digits(fraction = 0, integer = 10) + private String telephone; + + @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner") + private Set pets; + + // + @OneToOne(cascade = CascadeType.ALL) + @JoinColumn(name = "username", referencedColumnName = "username") + private User user; + // + + public String getAddress() { + return this.address; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getCity() { + return this.city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getTelephone() { + return this.telephone; + } + + public void setTelephone(String telephone) { + this.telephone = telephone; + } + + protected Set getPetsInternal() { + if (this.pets == null) { + this.pets = new HashSet<>(); + } + return this.pets; + } + + protected void setPetsInternal(Set pets) { + this.pets = pets; + } + + public List getPets() { + List sortedPets = new ArrayList<>(getPetsInternal()); + PropertyComparator.sort(sortedPets, new MutableSortDefinition("name", true, true)); + return Collections.unmodifiableList(sortedPets); + } + + public void addPet(Pet pet) { + getPetsInternal().add(pet); + pet.setOwner(this); + } + + public boolean removePet(Pet pet) { + return getPetsInternal().remove(pet); + } + + /** + * Return the Pet with the given name, or null if none found for this Owner. + * @param name to test + * @return true if pet name is already in use + */ + public Pet getPet(String name) { + return getPet(name, false); + } + + public Pet getPetwithIdDifferent(String name,Integer id) { + name = name.toLowerCase(); + for (Pet pet : getPetsInternal()) { + String compName = pet.getName(); + compName = compName.toLowerCase(); + if (compName.equals(name) && pet.getId()!=id) { + return pet; + } + } + return null; + } + + /** + * Return the Pet with the given name, or null if none found for this Owner. + * @param name to test + * @return true if pet name is already in use + */ + public Pet getPet(String name, boolean ignoreNew) { + name = name.toLowerCase(); + for (Pet pet : getPetsInternal()) { + if (!ignoreNew || !pet.isNew()) { + String compName = pet.getName(); + compName = compName.toLowerCase(); + if (compName.equals(name)) { + return pet; + } + } + } + return null; + } + + @Override + public String toString() { + return new ToStringCreator(this) + + .append("id", this.getId()).append("new", this.isNew()).append("lastName", this.getLastName()) + .append("firstName", this.getFirstName()).append("address", this.address).append("city", this.city) + .append("telephone", this.telephone).toString(); + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/web/OwnerController.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java similarity index 88% rename from src/main/java/org/springframework/samples/petclinic/web/OwnerController.java rename to src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java index 952d3b3033a..b923ad4faa3 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/OwnerController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java @@ -1,144 +1,142 @@ -/* - * Copyright 2002-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.samples.petclinic.web; - -import java.util.Collection; -import java.util.Map; - -import javax.validation.Valid; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.service.AuthoritiesService; -import org.springframework.samples.petclinic.service.OwnerService; -import org.springframework.samples.petclinic.service.VetService; -import org.springframework.samples.petclinic.service.UserService; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.validation.BindingResult; -import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.annotation.*; -import org.springframework.web.servlet.ModelAndView; - -/** - * @author Juergen Hoeller - * @author Ken Krebs - * @author Arjen Poutsma - * @author Michael Isvy - */ -@Controller -public class OwnerController { - - private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm"; - - private final OwnerService ownerService; - - @Autowired - public OwnerController(OwnerService ownerService, UserService userService, AuthoritiesService authoritiesService) { - this.ownerService = ownerService; - } - - @InitBinder - public void setAllowedFields(WebDataBinder dataBinder) { - dataBinder.setDisallowedFields("id"); - } - - @GetMapping(value = "/owners/new") - public String initCreationForm(Map model) { - Owner owner = new Owner(); - model.put("owner", owner); - return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; - } - - @PostMapping(value = "/owners/new") - public String processCreationForm(@Valid Owner owner, BindingResult result) { - if (result.hasErrors()) { - return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; - } - else { - //creating owner, user and authorities - this.ownerService.saveOwner(owner); - - return "redirect:/owners/" + owner.getId(); - } - } - - @GetMapping(value = "/owners/find") - public String initFindForm(Map model) { - model.put("owner", new Owner()); - return "owners/findOwners"; - } - - @GetMapping(value = "/owners") - public String processFindForm(Owner owner, BindingResult result, Map model) { - - // allow parameterless GET request for /owners to return all records - if (owner.getLastName() == null) { - owner.setLastName(""); // empty string signifies broadest possible search - } - - // find owners by last name - Collection results = this.ownerService.findOwnerByLastName(owner.getLastName()); - if (results.isEmpty()) { - // no owners found - result.rejectValue("lastName", "notFound", "not found"); - return "owners/findOwners"; - } - else if (results.size() == 1) { - // 1 owner found - owner = results.iterator().next(); - return "redirect:/owners/" + owner.getId(); - } - else { - // multiple owners found - model.put("selections", results); - return "owners/ownersList"; - } - } - - @GetMapping(value = "/owners/{ownerId}/edit") - public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { - Owner owner = this.ownerService.findOwnerById(ownerId); - model.addAttribute(owner); - return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; - } - - @PostMapping(value = "/owners/{ownerId}/edit") - public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, - @PathVariable("ownerId") int ownerId) { - if (result.hasErrors()) { - return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; - } - else { - owner.setId(ownerId); - this.ownerService.saveOwner(owner); - return "redirect:/owners/{ownerId}"; - } - } - - /** - * Custom handler for displaying an owner. - * @param ownerId the ID of the owner to display - * @return a ModelMap with the model attributes for the view - */ - @GetMapping("/owners/{ownerId}") - public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { - ModelAndView mav = new ModelAndView("owners/ownerDetails"); - mav.addObject(this.ownerService.findOwnerById(ownerId)); - return mav; - } - -} +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.samples.petclinic.owner; + +import java.util.Collection; +import java.util.Map; + +import javax.validation.Valid; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.samples.petclinic.user.AuthoritiesService; +import org.springframework.samples.petclinic.user.UserService; +import org.springframework.samples.petclinic.vet.VetService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.WebDataBinder; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; + +/** + * @author Juergen Hoeller + * @author Ken Krebs + * @author Arjen Poutsma + * @author Michael Isvy + */ +@Controller +public class OwnerController { + + private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm"; + + private final OwnerService ownerService; + + @Autowired + public OwnerController(OwnerService ownerService, UserService userService, AuthoritiesService authoritiesService) { + this.ownerService = ownerService; + } + + @InitBinder + public void setAllowedFields(WebDataBinder dataBinder) { + dataBinder.setDisallowedFields("id"); + } + + @GetMapping(value = "/owners/new") + public String initCreationForm(Map model) { + Owner owner = new Owner(); + model.put("owner", owner); + return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; + } + + @PostMapping(value = "/owners/new") + public String processCreationForm(@Valid Owner owner, BindingResult result) { + if (result.hasErrors()) { + return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; + } + else { + //creating owner, user and authorities + this.ownerService.saveOwner(owner); + + return "redirect:/owners/" + owner.getId(); + } + } + + @GetMapping(value = "/owners/find") + public String initFindForm(Map model) { + model.put("owner", new Owner()); + return "owners/findOwners"; + } + + @GetMapping(value = "/owners") + public String processFindForm(Owner owner, BindingResult result, Map model) { + + // allow parameterless GET request for /owners to return all records + if (owner.getLastName() == null) { + owner.setLastName(""); // empty string signifies broadest possible search + } + + // find owners by last name + Collection results = this.ownerService.findOwnerByLastName(owner.getLastName()); + if (results.isEmpty()) { + // no owners found + result.rejectValue("lastName", "notFound", "not found"); + return "owners/findOwners"; + } + else if (results.size() == 1) { + // 1 owner found + owner = results.iterator().next(); + return "redirect:/owners/" + owner.getId(); + } + else { + // multiple owners found + model.put("selections", results); + return "owners/ownersList"; + } + } + + @GetMapping(value = "/owners/{ownerId}/edit") + public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { + Owner owner = this.ownerService.findOwnerById(ownerId); + model.addAttribute(owner); + return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; + } + + @PostMapping(value = "/owners/{ownerId}/edit") + public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, + @PathVariable("ownerId") int ownerId) { + if (result.hasErrors()) { + return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; + } + else { + owner.setId(ownerId); + this.ownerService.saveOwner(owner); + return "redirect:/owners/{ownerId}"; + } + } + + /** + * Custom handler for displaying an owner. + * @param ownerId the ID of the owner to display + * @return a ModelMap with the model attributes for the view + */ + @GetMapping("/owners/{ownerId}") + public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { + ModelAndView mav = new ModelAndView("owners/ownerDetails"); + mav.addObject(this.ownerService.findOwnerById(ownerId)); + return mav; + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java similarity index 92% rename from src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java rename to src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java index 21eb1b7e50b..9ab61ac0842 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.repository; +package org.springframework.samples.petclinic.owner; import java.util.Collection; @@ -22,8 +22,7 @@ import org.springframework.data.repository.Repository; import org.springframework.data.repository.query.Param; import org.springframework.samples.petclinic.model.BaseEntity; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.repository.OwnerRepository; +import org.springframework.samples.petclinic.owner.OwnerRepository; /** * Spring Data JPA OwnerRepository interface diff --git a/src/main/java/org/springframework/samples/petclinic/service/OwnerService.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerService.java similarity index 72% rename from src/main/java/org/springframework/samples/petclinic/service/OwnerService.java rename to src/main/java/org/springframework/samples/petclinic/owner/OwnerService.java index 73e745095d3..8de1db1112d 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/OwnerService.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerService.java @@ -1,79 +1,79 @@ -/* - * Copyright 2002-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.samples.petclinic.service; - -import java.util.Collection; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.annotation.Cacheable; -import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.PetType; -import org.springframework.samples.petclinic.model.Vet; -import org.springframework.samples.petclinic.model.Visit; -import org.springframework.samples.petclinic.repository.OwnerRepository; -import org.springframework.samples.petclinic.repository.PetRepository; -import org.springframework.samples.petclinic.repository.VetRepository; -import org.springframework.samples.petclinic.repository.VisitRepository; -import org.springframework.samples.petclinic.service.exceptions.DuplicatedPetNameException; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.StringUtils; - -/** - * Mostly used as a facade for all Petclinic controllers Also a placeholder - * for @Transactional and @Cacheable annotations - * - * @author Michael Isvy - */ -@Service -public class OwnerService { - - private OwnerRepository ownerRepository; - - @Autowired - private UserService userService; - - @Autowired - private AuthoritiesService authoritiesService; - - @Autowired - public OwnerService(OwnerRepository ownerRepository) { - this.ownerRepository = ownerRepository; - } - - @Transactional(readOnly = true) - public Owner findOwnerById(int id) throws DataAccessException { - return ownerRepository.findById(id); - } - - @Transactional(readOnly = true) - public Collection findOwnerByLastName(String lastName) throws DataAccessException { - return ownerRepository.findByLastName(lastName); - } - - @Transactional - public void saveOwner(Owner owner) throws DataAccessException { - //creating owner - ownerRepository.save(owner); - //creating user - userService.saveUser(owner.getUser()); - //creating authorities - authoritiesService.saveAuthorities(owner.getUser().getUsername(), "owner"); - } - -} +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.samples.petclinic.owner; + +import java.util.Collection; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.dao.DataAccessException; +import org.springframework.samples.petclinic.pet.Pet; +import org.springframework.samples.petclinic.pet.PetRepository; +import org.springframework.samples.petclinic.pet.PetType; +import org.springframework.samples.petclinic.pet.Visit; +import org.springframework.samples.petclinic.pet.VisitRepository; +import org.springframework.samples.petclinic.pet.exceptions.DuplicatedPetNameException; +import org.springframework.samples.petclinic.user.AuthoritiesService; +import org.springframework.samples.petclinic.user.UserService; +import org.springframework.samples.petclinic.vet.Vet; +import org.springframework.samples.petclinic.vet.VetRepository; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; + +/** + * Mostly used as a facade for all Petclinic controllers Also a placeholder + * for @Transactional and @Cacheable annotations + * + * @author Michael Isvy + */ +@Service +public class OwnerService { + + private OwnerRepository ownerRepository; + + @Autowired + private UserService userService; + + @Autowired + private AuthoritiesService authoritiesService; + + @Autowired + public OwnerService(OwnerRepository ownerRepository) { + this.ownerRepository = ownerRepository; + } + + @Transactional(readOnly = true) + public Owner findOwnerById(int id) throws DataAccessException { + return ownerRepository.findById(id); + } + + @Transactional(readOnly = true) + public Collection findOwnerByLastName(String lastName) throws DataAccessException { + return ownerRepository.findByLastName(lastName); + } + + @Transactional + public void saveOwner(Owner owner) throws DataAccessException { + //creating owner + ownerRepository.save(owner); + //creating user + userService.saveUser(owner.getUser()); + //creating authorities + authoritiesService.saveAuthorities(owner.getUser().getUsername(), "owner"); + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/model/Pet.java b/src/main/java/org/springframework/samples/petclinic/pet/Pet.java similarity index 92% rename from src/main/java/org/springframework/samples/petclinic/model/Pet.java rename to src/main/java/org/springframework/samples/petclinic/pet/Pet.java index 06a3ae0c0b4..948215dbf81 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Pet.java +++ b/src/main/java/org/springframework/samples/petclinic/pet/Pet.java @@ -13,11 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.model; +package org.springframework.samples.petclinic.pet; import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.PropertyComparator; import org.springframework.format.annotation.DateTimeFormat; +import org.springframework.samples.petclinic.model.NamedEntity; +import org.springframework.samples.petclinic.owner.Owner; import javax.persistence.CascadeType; import javax.persistence.Column; @@ -83,7 +85,7 @@ public Owner getOwner() { return this.owner; } - protected void setOwner(Owner owner) { + public void setOwner(Owner owner) { this.owner = owner; } diff --git a/src/main/java/org/springframework/samples/petclinic/web/PetController.java b/src/main/java/org/springframework/samples/petclinic/pet/PetController.java similarity index 89% rename from src/main/java/org/springframework/samples/petclinic/web/PetController.java rename to src/main/java/org/springframework/samples/petclinic/pet/PetController.java index 725b2b48bec..437ab15788c 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/pet/PetController.java @@ -13,13 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.web; +package org.springframework.samples.petclinic.pet; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.PetType; -import org.springframework.samples.petclinic.service.VetService; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.util.StringUtils; @@ -34,10 +30,10 @@ import java.util.logging.Logger; import org.springframework.beans.BeanUtils; import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.model.Visit; -import org.springframework.samples.petclinic.service.OwnerService; -import org.springframework.samples.petclinic.service.PetService; -import org.springframework.samples.petclinic.service.exceptions.DuplicatedPetNameException; +import org.springframework.samples.petclinic.owner.Owner; +import org.springframework.samples.petclinic.owner.OwnerService; +import org.springframework.samples.petclinic.pet.exceptions.DuplicatedPetNameException; +import org.springframework.samples.petclinic.vet.VetService; /** * @author Juergen Hoeller diff --git a/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java b/src/main/java/org/springframework/samples/petclinic/pet/PetRepository.java similarity index 91% rename from src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java rename to src/main/java/org/springframework/samples/petclinic/pet/PetRepository.java index c9848dd6e67..3767b31bd4e 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/pet/PetRepository.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.repository; +package org.springframework.samples.petclinic.pet; import java.util.List; @@ -21,8 +21,6 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.Repository; import org.springframework.samples.petclinic.model.BaseEntity; -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.PetType; /** * Spring Data JPA specialization of the {@link PetRepository} interface diff --git a/src/main/java/org/springframework/samples/petclinic/service/PetService.java b/src/main/java/org/springframework/samples/petclinic/pet/PetService.java similarity index 81% rename from src/main/java/org/springframework/samples/petclinic/service/PetService.java rename to src/main/java/org/springframework/samples/petclinic/pet/PetService.java index f05f05fef38..09331bb7857 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/PetService.java +++ b/src/main/java/org/springframework/samples/petclinic/pet/PetService.java @@ -1,82 +1,77 @@ -/* - * Copyright 2002-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.samples.petclinic.service; - -import java.util.Collection; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.PetType; -import org.springframework.samples.petclinic.model.Visit; -import org.springframework.samples.petclinic.repository.PetRepository; -import org.springframework.samples.petclinic.repository.VisitRepository; -import org.springframework.samples.petclinic.service.exceptions.DuplicatedPetNameException; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.StringUtils; - -/** - * Mostly used as a facade for all Petclinic controllers Also a placeholder - * for @Transactional and @Cacheable annotations - * - * @author Michael Isvy - */ -@Service -public class PetService { - - private PetRepository petRepository; - - private VisitRepository visitRepository; - - - @Autowired - public PetService(PetRepository petRepository, - VisitRepository visitRepository) { - this.petRepository = petRepository; - this.visitRepository = visitRepository; - } - - @Transactional(readOnly = true) - public Collection findPetTypes() throws DataAccessException { - return petRepository.findPetTypes(); - } - - @Transactional - public void saveVisit(Visit visit) throws DataAccessException { - visitRepository.save(visit); - } - - @Transactional(readOnly = true) - public Pet findPetById(int id) throws DataAccessException { - return petRepository.findById(id); - } - - @Transactional(rollbackFor = DuplicatedPetNameException.class) - public void savePet(Pet pet) throws DataAccessException, DuplicatedPetNameException { - Pet otherPet=pet.getOwner().getPetwithIdDifferent(pet.getName(), pet.getId()); - if (StringUtils.hasLength(pet.getName()) && (otherPet!= null && otherPet.getId()!=pet.getId())) { - throw new DuplicatedPetNameException(); - }else - petRepository.save(pet); - } - - - public Collection findVisitsByPetId(int petId) { - return visitRepository.findByPetId(petId); - } - -} +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.samples.petclinic.pet; + +import java.util.Collection; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataAccessException; +import org.springframework.samples.petclinic.pet.exceptions.DuplicatedPetNameException; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; + +/** + * Mostly used as a facade for all Petclinic controllers Also a placeholder + * for @Transactional and @Cacheable annotations + * + * @author Michael Isvy + */ +@Service +public class PetService { + + private PetRepository petRepository; + + private VisitRepository visitRepository; + + + @Autowired + public PetService(PetRepository petRepository, + VisitRepository visitRepository) { + this.petRepository = petRepository; + this.visitRepository = visitRepository; + } + + @Transactional(readOnly = true) + public Collection findPetTypes() throws DataAccessException { + return petRepository.findPetTypes(); + } + + @Transactional + public void saveVisit(Visit visit) throws DataAccessException { + visitRepository.save(visit); + } + + @Transactional(readOnly = true) + public Pet findPetById(int id) throws DataAccessException { + return petRepository.findById(id); + } + + @Transactional(rollbackFor = DuplicatedPetNameException.class) + public void savePet(Pet pet) throws DataAccessException, DuplicatedPetNameException { + Pet otherPet=pet.getOwner().getPetwithIdDifferent(pet.getName(), pet.getId()); + if (StringUtils.hasLength(pet.getName()) && (otherPet!= null && otherPet.getId()!=pet.getId())) { + throw new DuplicatedPetNameException(); + }else + petRepository.save(pet); + } + + + public Collection findVisitsByPetId(int petId) { + return visitRepository.findByPetId(petId); + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/model/PetType.java b/src/main/java/org/springframework/samples/petclinic/pet/PetType.java similarity index 87% rename from src/main/java/org/springframework/samples/petclinic/model/PetType.java rename to src/main/java/org/springframework/samples/petclinic/pet/PetType.java index eb44dd8a683..54947771e3b 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/PetType.java +++ b/src/main/java/org/springframework/samples/petclinic/pet/PetType.java @@ -13,11 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.model; +package org.springframework.samples.petclinic.pet; import javax.persistence.Entity; import javax.persistence.Table; +import org.springframework.samples.petclinic.model.NamedEntity; + /** * @author Juergen Hoeller Can be Cat, Dog, Hamster... */ diff --git a/src/main/java/org/springframework/samples/petclinic/web/PetTypeFormatter.java b/src/main/java/org/springframework/samples/petclinic/pet/PetTypeFormatter.java similarity index 90% rename from src/main/java/org/springframework/samples/petclinic/web/PetTypeFormatter.java rename to src/main/java/org/springframework/samples/petclinic/pet/PetTypeFormatter.java index 6daac427ac9..587a415aae7 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/PetTypeFormatter.java +++ b/src/main/java/org/springframework/samples/petclinic/pet/PetTypeFormatter.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.web; +package org.springframework.samples.petclinic.pet; import java.text.ParseException; import java.util.Collection; @@ -21,9 +21,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.format.Formatter; -import org.springframework.samples.petclinic.model.PetType; -import org.springframework.samples.petclinic.service.PetService; -import org.springframework.samples.petclinic.service.VetService; +import org.springframework.samples.petclinic.vet.VetService; import org.springframework.stereotype.Component; /** diff --git a/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java b/src/main/java/org/springframework/samples/petclinic/pet/PetValidator.java similarity index 94% rename from src/main/java/org/springframework/samples/petclinic/web/PetValidator.java rename to src/main/java/org/springframework/samples/petclinic/pet/PetValidator.java index b70aac5fce7..9b1160613bd 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java +++ b/src/main/java/org/springframework/samples/petclinic/pet/PetValidator.java @@ -13,9 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.web; +package org.springframework.samples.petclinic.pet; -import org.springframework.samples.petclinic.model.Pet; import org.springframework.util.StringUtils; import org.springframework.validation.Errors; import org.springframework.validation.Validator; diff --git a/src/main/java/org/springframework/samples/petclinic/model/Visit.java b/src/main/java/org/springframework/samples/petclinic/pet/Visit.java similarity index 95% rename from src/main/java/org/springframework/samples/petclinic/model/Visit.java rename to src/main/java/org/springframework/samples/petclinic/pet/Visit.java index b626199c7bd..b91eeb76587 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Visit.java +++ b/src/main/java/org/springframework/samples/petclinic/pet/Visit.java @@ -13,9 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.model; +package org.springframework.samples.petclinic.pet; import org.springframework.format.annotation.DateTimeFormat; +import org.springframework.samples.petclinic.model.BaseEntity; import javax.persistence.Column; import javax.persistence.Entity; diff --git a/src/main/java/org/springframework/samples/petclinic/web/VisitController.java b/src/main/java/org/springframework/samples/petclinic/pet/VisitController.java similarity index 90% rename from src/main/java/org/springframework/samples/petclinic/web/VisitController.java rename to src/main/java/org/springframework/samples/petclinic/pet/VisitController.java index 0518de9732e..cd2731e5c78 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/VisitController.java +++ b/src/main/java/org/springframework/samples/petclinic/pet/VisitController.java @@ -13,17 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.web; +package org.springframework.samples.petclinic.pet; import java.util.Map; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.Visit; -import org.springframework.samples.petclinic.service.PetService; -import org.springframework.samples.petclinic.service.VetService; +import org.springframework.samples.petclinic.vet.VetService; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; diff --git a/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java b/src/main/java/org/springframework/samples/petclinic/pet/VisitRepository.java similarity index 93% rename from src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java rename to src/main/java/org/springframework/samples/petclinic/pet/VisitRepository.java index fd3d9181d51..b59136b8fe6 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/pet/VisitRepository.java @@ -13,14 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.repository; +package org.springframework.samples.petclinic.pet; import java.util.List; import org.springframework.dao.DataAccessException; import org.springframework.data.repository.Repository; import org.springframework.samples.petclinic.model.BaseEntity; -import org.springframework.samples.petclinic.model.Visit; /** * Repository class for Visit domain objects All method names are compliant diff --git a/src/main/java/org/springframework/samples/petclinic/service/exceptions/DuplicatedPetNameException.java b/src/main/java/org/springframework/samples/petclinic/pet/exceptions/DuplicatedPetNameException.java similarity index 81% rename from src/main/java/org/springframework/samples/petclinic/service/exceptions/DuplicatedPetNameException.java rename to src/main/java/org/springframework/samples/petclinic/pet/exceptions/DuplicatedPetNameException.java index b10347ea519..53a1d9e3175 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/exceptions/DuplicatedPetNameException.java +++ b/src/main/java/org/springframework/samples/petclinic/pet/exceptions/DuplicatedPetNameException.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package org.springframework.samples.petclinic.service.exceptions; +package org.springframework.samples.petclinic.pet.exceptions; /** * diff --git a/src/main/java/org/springframework/samples/petclinic/service/businessrules/ElementInCollectionValidator.java b/src/main/java/org/springframework/samples/petclinic/service/businessrules/ElementInCollectionValidator.java deleted file mode 100644 index 33bc7f67d1a..00000000000 --- a/src/main/java/org/springframework/samples/petclinic/service/businessrules/ElementInCollectionValidator.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.springframework.samples.petclinic.service.businessrules; - -public class ElementInCollectionValidator { - -} diff --git a/src/main/java/org/springframework/samples/petclinic/model/Authorities.java b/src/main/java/org/springframework/samples/petclinic/user/Authorities.java similarity index 79% rename from src/main/java/org/springframework/samples/petclinic/model/Authorities.java rename to src/main/java/org/springframework/samples/petclinic/user/Authorities.java index 405e91135bc..66e681fec9c 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Authorities.java +++ b/src/main/java/org/springframework/samples/petclinic/user/Authorities.java @@ -1,4 +1,4 @@ -package org.springframework.samples.petclinic.model; +package org.springframework.samples.petclinic.user; import javax.persistence.Entity; import javax.persistence.JoinColumn; @@ -6,6 +6,8 @@ import javax.persistence.Table; import javax.validation.constraints.Size; +import org.springframework.samples.petclinic.model.BaseEntity; + import lombok.Data; import lombok.Getter; import lombok.Setter; diff --git a/src/main/java/org/springframework/samples/petclinic/repository/AuthoritiesRepository.java b/src/main/java/org/springframework/samples/petclinic/user/AuthoritiesRepository.java similarity index 55% rename from src/main/java/org/springframework/samples/petclinic/repository/AuthoritiesRepository.java rename to src/main/java/org/springframework/samples/petclinic/user/AuthoritiesRepository.java index 4cd6137cf80..367cee340ad 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/AuthoritiesRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/user/AuthoritiesRepository.java @@ -1,7 +1,6 @@ -package org.springframework.samples.petclinic.repository; +package org.springframework.samples.petclinic.user; import org.springframework.data.repository.CrudRepository; -import org.springframework.samples.petclinic.model.Authorities; diff --git a/src/main/java/org/springframework/samples/petclinic/service/AuthoritiesService.java b/src/main/java/org/springframework/samples/petclinic/user/AuthoritiesService.java similarity index 86% rename from src/main/java/org/springframework/samples/petclinic/service/AuthoritiesService.java rename to src/main/java/org/springframework/samples/petclinic/user/AuthoritiesService.java index 8b9219e4c29..b7695965d93 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/AuthoritiesService.java +++ b/src/main/java/org/springframework/samples/petclinic/user/AuthoritiesService.java @@ -1,66 +1,63 @@ -/* - * Copyright 2002-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.samples.petclinic.service; - - -import java.util.Optional; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.model.Authorities; -import org.springframework.samples.petclinic.model.User; -import org.springframework.samples.petclinic.repository.AuthoritiesRepository; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -/** - * Mostly used as a facade for all Petclinic controllers Also a placeholder - * for @Transactional and @Cacheable annotations - * - * @author Michael Isvy - */ -@Service -public class AuthoritiesService { - - private AuthoritiesRepository authoritiesRepository; - private UserService userService; - - @Autowired - public AuthoritiesService(AuthoritiesRepository authoritiesRepository,UserService userService) { - this.authoritiesRepository = authoritiesRepository; - this.userService = userService; - } - - @Transactional - public void saveAuthorities(Authorities authorities) throws DataAccessException { - authoritiesRepository.save(authorities); - } - - @Transactional - public void saveAuthorities(String username, String role) throws DataAccessException { - Authorities authority = new Authorities(); - Optional user = userService.findUser(username); - if(user.isPresent()) { - authority.setUser(user.get()); - authority.setAuthority(role); - //user.get().getAuthorities().add(authority); - authoritiesRepository.save(authority); - }else - throw new DataAccessException("User '"+username+"' not found!") {}; - } - - -} +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.samples.petclinic.user; + + +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataAccessException; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * Mostly used as a facade for all Petclinic controllers Also a placeholder + * for @Transactional and @Cacheable annotations + * + * @author Michael Isvy + */ +@Service +public class AuthoritiesService { + + private AuthoritiesRepository authoritiesRepository; + private UserService userService; + + @Autowired + public AuthoritiesService(AuthoritiesRepository authoritiesRepository,UserService userService) { + this.authoritiesRepository = authoritiesRepository; + this.userService = userService; + } + + @Transactional + public void saveAuthorities(Authorities authorities) throws DataAccessException { + authoritiesRepository.save(authorities); + } + + @Transactional + public void saveAuthorities(String username, String role) throws DataAccessException { + Authorities authority = new Authorities(); + Optional user = userService.findUser(username); + if(user.isPresent()) { + authority.setUser(user.get()); + authority.setAuthority(role); + //user.get().getAuthorities().add(authority); + authoritiesRepository.save(authority); + }else + throw new DataAccessException("User '"+username+"' not found!") {}; + } + + +} diff --git a/src/main/java/org/springframework/samples/petclinic/model/User.java b/src/main/java/org/springframework/samples/petclinic/user/User.java similarity index 89% rename from src/main/java/org/springframework/samples/petclinic/model/User.java rename to src/main/java/org/springframework/samples/petclinic/user/User.java index 9a05f6fb1ef..11632e746fb 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/User.java +++ b/src/main/java/org/springframework/samples/petclinic/user/User.java @@ -1,4 +1,4 @@ -package org.springframework.samples.petclinic.model; +package org.springframework.samples.petclinic.user; import java.util.Set; diff --git a/src/main/java/org/springframework/samples/petclinic/web/UserController.java b/src/main/java/org/springframework/samples/petclinic/user/UserController.java similarity index 81% rename from src/main/java/org/springframework/samples/petclinic/web/UserController.java rename to src/main/java/org/springframework/samples/petclinic/user/UserController.java index a1de38ac5e3..4dbc6da2629 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/UserController.java +++ b/src/main/java/org/springframework/samples/petclinic/user/UserController.java @@ -1,75 +1,73 @@ -/* - * Copyright 2002-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.samples.petclinic.web; - -import java.util.Map; - -import javax.validation.Valid; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.service.AuthoritiesService; -import org.springframework.samples.petclinic.service.OwnerService; -import org.springframework.samples.petclinic.service.VetService; -import org.springframework.samples.petclinic.service.UserService; -import org.springframework.stereotype.Controller; -import org.springframework.validation.BindingResult; -import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.annotation.*; - -/** - * @author Juergen Hoeller - * @author Ken Krebs - * @author Arjen Poutsma - * @author Michael Isvy - */ -@Controller -public class UserController { - - private static final String VIEWS_OWNER_CREATE_FORM = "users/createOwnerForm"; - - private final OwnerService ownerService; - - @Autowired - public UserController(OwnerService clinicService) { - this.ownerService = clinicService; - } - - @InitBinder - public void setAllowedFields(WebDataBinder dataBinder) { - dataBinder.setDisallowedFields("id"); - } - - @GetMapping(value = "/users/new") - public String initCreationForm(Map model) { - Owner owner = new Owner(); - model.put("owner", owner); - return VIEWS_OWNER_CREATE_FORM; - } - - @PostMapping(value = "/users/new") - public String processCreationForm(@Valid Owner owner, BindingResult result) { - if (result.hasErrors()) { - return VIEWS_OWNER_CREATE_FORM; - } - else { - //creating owner, user, and authority - this.ownerService.saveOwner(owner); - return "redirect:/"; - } - } - -} +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.samples.petclinic.user; + +import java.util.Map; + +import javax.validation.Valid; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.samples.petclinic.owner.Owner; +import org.springframework.samples.petclinic.owner.OwnerService; +import org.springframework.samples.petclinic.vet.VetService; +import org.springframework.stereotype.Controller; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.WebDataBinder; +import org.springframework.web.bind.annotation.*; + +/** + * @author Juergen Hoeller + * @author Ken Krebs + * @author Arjen Poutsma + * @author Michael Isvy + */ +@Controller +public class UserController { + + private static final String VIEWS_OWNER_CREATE_FORM = "users/createOwnerForm"; + + private final OwnerService ownerService; + + @Autowired + public UserController(OwnerService clinicService) { + this.ownerService = clinicService; + } + + @InitBinder + public void setAllowedFields(WebDataBinder dataBinder) { + dataBinder.setDisallowedFields("id"); + } + + @GetMapping(value = "/users/new") + public String initCreationForm(Map model) { + Owner owner = new Owner(); + model.put("owner", owner); + return VIEWS_OWNER_CREATE_FORM; + } + + @PostMapping(value = "/users/new") + public String processCreationForm(@Valid Owner owner, BindingResult result) { + if (result.hasErrors()) { + return VIEWS_OWNER_CREATE_FORM; + } + else { + //creating owner, user, and authority + this.ownerService.saveOwner(owner); + return "redirect:/"; + } + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/UserRepository.java b/src/main/java/org/springframework/samples/petclinic/user/UserRepository.java similarity index 54% rename from src/main/java/org/springframework/samples/petclinic/repository/UserRepository.java rename to src/main/java/org/springframework/samples/petclinic/user/UserRepository.java index c85d5c0bc51..d325214ac96 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/UserRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/user/UserRepository.java @@ -1,7 +1,6 @@ -package org.springframework.samples.petclinic.repository; +package org.springframework.samples.petclinic.user; import org.springframework.data.repository.CrudRepository; -import org.springframework.samples.petclinic.model.User; public interface UserRepository extends CrudRepository{ diff --git a/src/main/java/org/springframework/samples/petclinic/service/UserService.java b/src/main/java/org/springframework/samples/petclinic/user/UserService.java similarity index 86% rename from src/main/java/org/springframework/samples/petclinic/service/UserService.java rename to src/main/java/org/springframework/samples/petclinic/user/UserService.java index 882bd2759ee..0de8ca36ded 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/UserService.java +++ b/src/main/java/org/springframework/samples/petclinic/user/UserService.java @@ -1,53 +1,51 @@ -/* - * Copyright 2002-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.samples.petclinic.service; - - -import java.util.Optional; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.model.User; -import org.springframework.samples.petclinic.repository.UserRepository; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -/** - * Mostly used as a facade for all Petclinic controllers Also a placeholder - * for @Transactional and @Cacheable annotations - * - * @author Michael Isvy - */ -@Service -public class UserService { - - private UserRepository userRepository; - - @Autowired - public UserService(UserRepository userRepository) { - this.userRepository = userRepository; - } - - @Transactional - public void saveUser(User user) throws DataAccessException { - user.setEnabled(true); - userRepository.save(user); - } - - public Optional findUser(String username) { - return userRepository.findById(username); - } -} +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.samples.petclinic.user; + + +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataAccessException; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * Mostly used as a facade for all Petclinic controllers Also a placeholder + * for @Transactional and @Cacheable annotations + * + * @author Michael Isvy + */ +@Service +public class UserService { + + private UserRepository userRepository; + + @Autowired + public UserService(UserRepository userRepository) { + this.userRepository = userRepository; + } + + @Transactional + public void saveUser(User user) throws DataAccessException { + user.setEnabled(true); + userRepository.save(user); + } + + public Optional findUser(String username) { + return userRepository.findById(username); + } +} diff --git a/src/main/java/org/springframework/samples/petclinic/model/Specialty.java b/src/main/java/org/springframework/samples/petclinic/vet/Specialty.java similarity index 88% rename from src/main/java/org/springframework/samples/petclinic/model/Specialty.java rename to src/main/java/org/springframework/samples/petclinic/vet/Specialty.java index 6ea209cd45a..826e04afce9 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Specialty.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/Specialty.java @@ -13,11 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.model; +package org.springframework.samples.petclinic.vet; import javax.persistence.Entity; import javax.persistence.Table; +import org.springframework.samples.petclinic.model.NamedEntity; + /** * Models a {@link Vet Vet's} specialty (for example, dentistry). * diff --git a/src/main/java/org/springframework/samples/petclinic/model/Vet.java b/src/main/java/org/springframework/samples/petclinic/vet/Vet.java similarity index 95% rename from src/main/java/org/springframework/samples/petclinic/model/Vet.java rename to src/main/java/org/springframework/samples/petclinic/vet/Vet.java index 9f5de9c523c..fbe814bd5c1 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Vet.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/Vet.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.model; +package org.springframework.samples.petclinic.vet; import java.util.ArrayList; import java.util.Collections; @@ -31,6 +31,7 @@ import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.PropertyComparator; +import org.springframework.samples.petclinic.model.Person; /** * Simple JavaBean domain object representing a veterinarian. diff --git a/src/main/java/org/springframework/samples/petclinic/web/VetController.java b/src/main/java/org/springframework/samples/petclinic/vet/VetController.java similarity index 91% rename from src/main/java/org/springframework/samples/petclinic/web/VetController.java rename to src/main/java/org/springframework/samples/petclinic/vet/VetController.java index 3696f20ab28..4dfdace1987 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/VetController.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/VetController.java @@ -13,11 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.web; +package org.springframework.samples.petclinic.vet; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.samples.petclinic.model.Vets; -import org.springframework.samples.petclinic.service.VetService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; diff --git a/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java b/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java similarity index 92% rename from src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java rename to src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java index a83ff1b3cca..c51a8b40724 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java @@ -13,13 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.repository; +package org.springframework.samples.petclinic.vet; import java.util.Collection; import org.springframework.dao.DataAccessException; import org.springframework.data.repository.Repository; -import org.springframework.samples.petclinic.model.Vet; /** * Repository class for Vet domain objects All method names are compliant diff --git a/src/main/java/org/springframework/samples/petclinic/service/VetService.java b/src/main/java/org/springframework/samples/petclinic/vet/VetService.java similarity index 65% rename from src/main/java/org/springframework/samples/petclinic/service/VetService.java rename to src/main/java/org/springframework/samples/petclinic/vet/VetService.java index 4516fd7380a..9d30bf19c97 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/VetService.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/VetService.java @@ -1,59 +1,57 @@ -/* - * Copyright 2002-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.samples.petclinic.service; - -import java.util.Collection; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.annotation.Cacheable; -import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.PetType; -import org.springframework.samples.petclinic.model.Vet; -import org.springframework.samples.petclinic.model.Visit; -import org.springframework.samples.petclinic.repository.OwnerRepository; -import org.springframework.samples.petclinic.repository.PetRepository; -import org.springframework.samples.petclinic.repository.VetRepository; -import org.springframework.samples.petclinic.repository.VisitRepository; -import org.springframework.samples.petclinic.service.exceptions.DuplicatedPetNameException; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.StringUtils; - -/** - * Mostly used as a facade for all Petclinic controllers Also a placeholder - * for @Transactional and @Cacheable annotations - * - * @author Michael Isvy - */ -@Service -public class VetService { - - private VetRepository vetRepository; - - - @Autowired - public VetService(VetRepository vetRepository) { - this.vetRepository = vetRepository; - } - - @Transactional(readOnly = true) - public Collection findVets() throws DataAccessException { - return vetRepository.findAll(); - } - -} +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.samples.petclinic.vet; + +import java.util.Collection; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.dao.DataAccessException; +import org.springframework.samples.petclinic.owner.Owner; +import org.springframework.samples.petclinic.owner.OwnerRepository; +import org.springframework.samples.petclinic.pet.Pet; +import org.springframework.samples.petclinic.pet.PetRepository; +import org.springframework.samples.petclinic.pet.PetType; +import org.springframework.samples.petclinic.pet.Visit; +import org.springframework.samples.petclinic.pet.VisitRepository; +import org.springframework.samples.petclinic.pet.exceptions.DuplicatedPetNameException; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; + +/** + * Mostly used as a facade for all Petclinic controllers Also a placeholder + * for @Transactional and @Cacheable annotations + * + * @author Michael Isvy + */ +@Service +public class VetService { + + private VetRepository vetRepository; + + + @Autowired + public VetService(VetRepository vetRepository) { + this.vetRepository = vetRepository; + } + + @Transactional(readOnly = true) + public Collection findVets() throws DataAccessException { + return vetRepository.findAll(); + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/model/Vets.java b/src/main/java/org/springframework/samples/petclinic/vet/Vets.java similarity index 95% rename from src/main/java/org/springframework/samples/petclinic/model/Vets.java rename to src/main/java/org/springframework/samples/petclinic/vet/Vets.java index c3b569a1c28..7c74b45330f 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Vets.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/Vets.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.model; +package org.springframework.samples.petclinic.vet; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/org/springframework/samples/petclinic/web/WelcomeController.java b/src/main/java/org/springframework/samples/petclinic/web/WelcomeController.java index b57ffa5a097..b1dc6cf2f38 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/WelcomeController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/WelcomeController.java @@ -3,10 +3,10 @@ import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.samples.petclinic.repository.UserRepository; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; -import org.springframework.samples.petclinic.model.User; +import org.springframework.samples.petclinic.user.User; +import org.springframework.samples.petclinic.user.UserRepository; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import java.util.List; diff --git a/src/test/java/org/springframework/samples/petclinic/web/OwnerControllerTests.java b/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java similarity index 94% rename from src/test/java/org/springframework/samples/petclinic/web/OwnerControllerTests.java rename to src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java index 4813861af39..59a05e2f3e0 100644 --- a/src/test/java/org/springframework/samples/petclinic/web/OwnerControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java @@ -1,12 +1,15 @@ -package org.springframework.samples.petclinic.web; +package org.springframework.samples.petclinic.owner; import org.assertj.core.util.Lists; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.samples.petclinic.configuration.SecurityConfiguration; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.service.VetService; +import org.springframework.samples.petclinic.owner.Owner; +import org.springframework.samples.petclinic.owner.OwnerController; +import org.springframework.samples.petclinic.owner.OwnerService; +import org.springframework.samples.petclinic.user.AuthoritiesService; +import org.springframework.samples.petclinic.user.UserService; import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @@ -18,9 +21,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.FilterType; -import org.springframework.samples.petclinic.service.AuthoritiesService; -import org.springframework.samples.petclinic.service.OwnerService; -import org.springframework.samples.petclinic.service.UserService; +import org.springframework.samples.petclinic.vet.VetService; import org.springframework.security.config.annotation.web.WebSecurityConfigurer; import org.springframework.security.test.context.support.WithMockUser; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; diff --git a/src/test/java/org/springframework/samples/petclinic/service/OwnerServiceTests.java b/src/test/java/org/springframework/samples/petclinic/owner/OwnerServiceTests.java similarity index 88% rename from src/test/java/org/springframework/samples/petclinic/service/OwnerServiceTests.java rename to src/test/java/org/springframework/samples/petclinic/owner/OwnerServiceTests.java index 863b3a40ab8..3001ebba592 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/OwnerServiceTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/OwnerServiceTests.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.service; +package org.springframework.samples.petclinic.owner; import static org.assertj.core.api.Assertions.assertThat; @@ -28,15 +28,16 @@ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.context.annotation.ComponentScan; import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.PetType; -import org.springframework.samples.petclinic.model.Vet; -import org.springframework.samples.petclinic.model.Visit; -import org.springframework.samples.petclinic.model.User; -import org.springframework.samples.petclinic.model.Authorities; -import org.springframework.samples.petclinic.service.exceptions.DuplicatedPetNameException; +import org.springframework.samples.petclinic.owner.Owner; +import org.springframework.samples.petclinic.owner.OwnerService; +import org.springframework.samples.petclinic.pet.Pet; +import org.springframework.samples.petclinic.pet.PetType; +import org.springframework.samples.petclinic.pet.Visit; +import org.springframework.samples.petclinic.pet.exceptions.DuplicatedPetNameException; +import org.springframework.samples.petclinic.user.Authorities; +import org.springframework.samples.petclinic.user.User; import org.springframework.samples.petclinic.util.EntityUtils; +import org.springframework.samples.petclinic.vet.Vet; import org.springframework.stereotype.Service; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringExtension; diff --git a/src/test/java/org/springframework/samples/petclinic/web/PetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/pet/PetControllerTests.java similarity index 90% rename from src/test/java/org/springframework/samples/petclinic/web/PetControllerTests.java rename to src/test/java/org/springframework/samples/petclinic/pet/PetControllerTests.java index 8764973e39a..83543cbdd0e 100644 --- a/src/test/java/org/springframework/samples/petclinic/web/PetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/pet/PetControllerTests.java @@ -1,4 +1,4 @@ -package org.springframework.samples.petclinic.web; +package org.springframework.samples.petclinic.pet; /* * Copyright 2012-2019 the original author or authors. @@ -33,12 +33,14 @@ import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.FilterType; import org.springframework.samples.petclinic.configuration.SecurityConfiguration; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.PetType; -import org.springframework.samples.petclinic.service.OwnerService; -import org.springframework.samples.petclinic.service.PetService; -import org.springframework.samples.petclinic.service.VetService; +import org.springframework.samples.petclinic.owner.Owner; +import org.springframework.samples.petclinic.owner.OwnerService; +import org.springframework.samples.petclinic.pet.Pet; +import org.springframework.samples.petclinic.pet.PetController; +import org.springframework.samples.petclinic.pet.PetService; +import org.springframework.samples.petclinic.pet.PetType; +import org.springframework.samples.petclinic.pet.PetTypeFormatter; +import org.springframework.samples.petclinic.vet.VetService; import org.springframework.security.config.annotation.web.WebSecurityConfigurer; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; diff --git a/src/test/java/org/springframework/samples/petclinic/service/PetServiceTests.java b/src/test/java/org/springframework/samples/petclinic/pet/PetServiceTests.java similarity index 93% rename from src/test/java/org/springframework/samples/petclinic/service/PetServiceTests.java rename to src/test/java/org/springframework/samples/petclinic/pet/PetServiceTests.java index 936e735f3d5..a028acb5e0b 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/PetServiceTests.java +++ b/src/test/java/org/springframework/samples/petclinic/pet/PetServiceTests.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.service; +package org.springframework.samples.petclinic.pet; import static org.assertj.core.api.Assertions.assertThat; @@ -28,14 +28,15 @@ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.context.annotation.ComponentScan; import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.PetType; -import org.springframework.samples.petclinic.model.Vet; -import org.springframework.samples.petclinic.model.Visit; -import org.springframework.samples.petclinic.model.User; -import org.springframework.samples.petclinic.service.exceptions.DuplicatedPetNameException; +import org.springframework.samples.petclinic.owner.Owner; +import org.springframework.samples.petclinic.owner.OwnerService; +import org.springframework.samples.petclinic.pet.Pet; +import org.springframework.samples.petclinic.pet.PetService; +import org.springframework.samples.petclinic.pet.PetType; +import org.springframework.samples.petclinic.pet.exceptions.DuplicatedPetNameException; +import org.springframework.samples.petclinic.user.User; import org.springframework.samples.petclinic.util.EntityUtils; +import org.springframework.samples.petclinic.vet.Vet; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; diff --git a/src/test/java/org/springframework/samples/petclinic/web/PetTypeFormatterTests.java b/src/test/java/org/springframework/samples/petclinic/pet/PetTypeFormatterTests.java similarity index 86% rename from src/test/java/org/springframework/samples/petclinic/web/PetTypeFormatterTests.java rename to src/test/java/org/springframework/samples/petclinic/pet/PetTypeFormatterTests.java index a90613f154c..c631040dabe 100644 --- a/src/test/java/org/springframework/samples/petclinic/web/PetTypeFormatterTests.java +++ b/src/test/java/org/springframework/samples/petclinic/pet/PetTypeFormatterTests.java @@ -1,4 +1,4 @@ -package org.springframework.samples.petclinic.web; +package org.springframework.samples.petclinic.pet; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -7,8 +7,9 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.samples.petclinic.model.PetType; -import org.springframework.samples.petclinic.service.VetService; +import org.springframework.samples.petclinic.pet.PetService; +import org.springframework.samples.petclinic.pet.PetType; +import org.springframework.samples.petclinic.pet.PetTypeFormatter; import java.text.ParseException; import java.util.ArrayList; @@ -17,7 +18,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.samples.petclinic.service.PetService; +import org.springframework.samples.petclinic.vet.VetService; /** * Test class for {@link PetTypeFormatter} diff --git a/src/test/java/org/springframework/samples/petclinic/web/VisitControllerTests.java b/src/test/java/org/springframework/samples/petclinic/pet/VisitControllerTests.java similarity index 91% rename from src/test/java/org/springframework/samples/petclinic/web/VisitControllerTests.java rename to src/test/java/org/springframework/samples/petclinic/pet/VisitControllerTests.java index 87451fd9462..bae4cf2fd11 100644 --- a/src/test/java/org/springframework/samples/petclinic/web/VisitControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/pet/VisitControllerTests.java @@ -1,4 +1,4 @@ -package org.springframework.samples.petclinic.web; +package org.springframework.samples.petclinic.pet; import static org.mockito.BDDMockito.given; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; @@ -15,9 +15,10 @@ import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.FilterType; import org.springframework.samples.petclinic.configuration.SecurityConfiguration; -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.service.PetService; -import org.springframework.samples.petclinic.service.VetService; +import org.springframework.samples.petclinic.pet.Pet; +import org.springframework.samples.petclinic.pet.PetService; +import org.springframework.samples.petclinic.pet.VisitController; +import org.springframework.samples.petclinic.vet.VetService; import org.springframework.security.config.annotation.web.WebSecurityConfigurer; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; diff --git a/src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java similarity index 90% rename from src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java rename to src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java index f648c48ba09..d12172c5405 100644 --- a/src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java @@ -1,4 +1,4 @@ -package org.springframework.samples.petclinic.web; +package org.springframework.samples.petclinic.vet; import org.assertj.core.util.Lists; import org.junit.jupiter.api.BeforeEach; @@ -6,9 +6,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.samples.petclinic.configuration.SecurityConfiguration; -import org.springframework.samples.petclinic.model.Specialty; -import org.springframework.samples.petclinic.model.Vet; -import org.springframework.samples.petclinic.service.VetService; +import org.springframework.samples.petclinic.vet.Specialty; +import org.springframework.samples.petclinic.vet.Vet; +import org.springframework.samples.petclinic.vet.VetController; +import org.springframework.samples.petclinic.vet.VetService; import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; diff --git a/src/test/java/org/springframework/samples/petclinic/service/VetServiceTests.java b/src/test/java/org/springframework/samples/petclinic/vet/VetServiceTests.java similarity index 84% rename from src/test/java/org/springframework/samples/petclinic/service/VetServiceTests.java rename to src/test/java/org/springframework/samples/petclinic/vet/VetServiceTests.java index 10732a019f6..f6948f6de1e 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/VetServiceTests.java +++ b/src/test/java/org/springframework/samples/petclinic/vet/VetServiceTests.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.service; +package org.springframework.samples.petclinic.vet; import static org.assertj.core.api.Assertions.assertThat; @@ -28,15 +28,16 @@ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.context.annotation.ComponentScan; import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.PetType; -import org.springframework.samples.petclinic.model.Vet; -import org.springframework.samples.petclinic.model.Visit; -import org.springframework.samples.petclinic.model.User; -import org.springframework.samples.petclinic.model.Authorities; -import org.springframework.samples.petclinic.service.exceptions.DuplicatedPetNameException; +import org.springframework.samples.petclinic.owner.Owner; +import org.springframework.samples.petclinic.pet.Pet; +import org.springframework.samples.petclinic.pet.PetType; +import org.springframework.samples.petclinic.pet.Visit; +import org.springframework.samples.petclinic.pet.exceptions.DuplicatedPetNameException; +import org.springframework.samples.petclinic.user.Authorities; +import org.springframework.samples.petclinic.user.User; import org.springframework.samples.petclinic.util.EntityUtils; +import org.springframework.samples.petclinic.vet.Vet; +import org.springframework.samples.petclinic.vet.VetService; import org.springframework.stereotype.Service; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringExtension; From 0c2033b9aadd399aac363d7d7d454f6008aa6957 Mon Sep 17 00:00:00 2001 From: Manuel Resinas Date: Thu, 30 Sep 2021 20:24:34 +0200 Subject: [PATCH 20/27] Removes warnings by cleaning up imports --- .../petclinic/PetclinicApplication.java | 1 - .../petclinic/owner/OwnerController.java | 6 +- .../samples/petclinic/owner/OwnerService.java | 10 -- .../samples/petclinic/pet/Pet.java | 24 ++-- .../samples/petclinic/pet/PetController.java | 24 ++-- .../petclinic/pet/PetTypeFormatter.java | 1 - .../samples/petclinic/pet/Visit.java | 9 +- .../petclinic/pet/VisitController.java | 7 +- .../samples/petclinic/user/Authorities.java | 1 - .../petclinic/user/UserController.java | 5 +- .../samples/petclinic/vet/VetService.java | 10 -- .../petclinic/web/WelcomeController.java | 6 - .../petclinic/owner/OwnerControllerTests.java | 103 +++++++----------- .../petclinic/owner/OwnerServiceTests.java | 16 --- .../petclinic/pet/PetControllerTests.java | 9 -- .../petclinic/pet/PetServiceTests.java | 6 - .../petclinic/pet/PetTypeFormatterTests.java | 19 ++-- .../petclinic/pet/VisitControllerTests.java | 31 ++---- .../petclinic/vet/VetControllerTests.java | 40 +++---- .../petclinic/vet/VetServiceTests.java | 17 --- .../petclinic/web/CrashControllerTests.java | 12 -- 21 files changed, 114 insertions(+), 243 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/PetclinicApplication.java b/src/main/java/org/springframework/samples/petclinic/PetclinicApplication.java index f33503dddbe..b31a7bbc519 100644 --- a/src/main/java/org/springframework/samples/petclinic/PetclinicApplication.java +++ b/src/main/java/org/springframework/samples/petclinic/PetclinicApplication.java @@ -1,7 +1,6 @@ package org.springframework.samples.petclinic; import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication() diff --git a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java index b923ad4faa3..578c3b0e4ee 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java @@ -23,12 +23,14 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.samples.petclinic.user.AuthoritiesService; import org.springframework.samples.petclinic.user.UserService; -import org.springframework.samples.petclinic.vet.VetService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.InitBinder; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.servlet.ModelAndView; /** diff --git a/src/main/java/org/springframework/samples/petclinic/owner/OwnerService.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerService.java index 8de1db1112d..ddabf5beb9a 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerService.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerService.java @@ -18,21 +18,11 @@ import java.util.Collection; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.annotation.Cacheable; import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.pet.Pet; -import org.springframework.samples.petclinic.pet.PetRepository; -import org.springframework.samples.petclinic.pet.PetType; -import org.springframework.samples.petclinic.pet.Visit; -import org.springframework.samples.petclinic.pet.VisitRepository; -import org.springframework.samples.petclinic.pet.exceptions.DuplicatedPetNameException; import org.springframework.samples.petclinic.user.AuthoritiesService; import org.springframework.samples.petclinic.user.UserService; -import org.springframework.samples.petclinic.vet.Vet; -import org.springframework.samples.petclinic.vet.VetRepository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.StringUtils; /** * Mostly used as a facade for all Petclinic controllers Also a placeholder diff --git a/src/main/java/org/springframework/samples/petclinic/pet/Pet.java b/src/main/java/org/springframework/samples/petclinic/pet/Pet.java index 948215dbf81..70bd4957b27 100644 --- a/src/main/java/org/springframework/samples/petclinic/pet/Pet.java +++ b/src/main/java/org/springframework/samples/petclinic/pet/Pet.java @@ -15,11 +15,12 @@ */ package org.springframework.samples.petclinic.pet; -import org.springframework.beans.support.MutableSortDefinition; -import org.springframework.beans.support.PropertyComparator; -import org.springframework.format.annotation.DateTimeFormat; -import org.springframework.samples.petclinic.model.NamedEntity; -import org.springframework.samples.petclinic.owner.Owner; +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; @@ -30,14 +31,11 @@ import javax.persistence.OneToMany; import javax.persistence.Table; -import java.time.LocalDate; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import javax.persistence.Temporal; -import javax.persistence.TemporalType; +import org.springframework.beans.support.MutableSortDefinition; +import org.springframework.beans.support.PropertyComparator; +import org.springframework.format.annotation.DateTimeFormat; +import org.springframework.samples.petclinic.model.NamedEntity; +import org.springframework.samples.petclinic.owner.Owner; /** * Simple business object representing a pet. diff --git a/src/main/java/org/springframework/samples/petclinic/pet/PetController.java b/src/main/java/org/springframework/samples/petclinic/pet/PetController.java index 437ab15788c..29ede194227 100644 --- a/src/main/java/org/springframework/samples/petclinic/pet/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/pet/PetController.java @@ -15,25 +15,25 @@ */ package org.springframework.samples.petclinic.pet; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Controller; -import org.springframework.ui.ModelMap; -import org.springframework.util.StringUtils; -import org.springframework.validation.BindingResult; -import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.annotation.*; +import java.util.Collection; import javax.validation.Valid; -import java.util.Collection; -import java.util.logging.Level; -import java.util.logging.Logger; import org.springframework.beans.BeanUtils; -import org.springframework.dao.DataAccessException; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.samples.petclinic.owner.Owner; import org.springframework.samples.petclinic.owner.OwnerService; import org.springframework.samples.petclinic.pet.exceptions.DuplicatedPetNameException; -import org.springframework.samples.petclinic.vet.VetService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.WebDataBinder; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.InitBinder; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; /** * @author Juergen Hoeller diff --git a/src/main/java/org/springframework/samples/petclinic/pet/PetTypeFormatter.java b/src/main/java/org/springframework/samples/petclinic/pet/PetTypeFormatter.java index 587a415aae7..e4fd01ef6b1 100644 --- a/src/main/java/org/springframework/samples/petclinic/pet/PetTypeFormatter.java +++ b/src/main/java/org/springframework/samples/petclinic/pet/PetTypeFormatter.java @@ -21,7 +21,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.format.Formatter; -import org.springframework.samples.petclinic.vet.VetService; import org.springframework.stereotype.Component; /** diff --git a/src/main/java/org/springframework/samples/petclinic/pet/Visit.java b/src/main/java/org/springframework/samples/petclinic/pet/Visit.java index b91eeb76587..5ba4e99cce0 100644 --- a/src/main/java/org/springframework/samples/petclinic/pet/Visit.java +++ b/src/main/java/org/springframework/samples/petclinic/pet/Visit.java @@ -15,8 +15,7 @@ */ package org.springframework.samples.petclinic.pet; -import org.springframework.format.annotation.DateTimeFormat; -import org.springframework.samples.petclinic.model.BaseEntity; +import java.time.LocalDate; import javax.persistence.Column; import javax.persistence.Entity; @@ -24,9 +23,9 @@ import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.validation.constraints.NotEmpty; -import java.time.LocalDate; -import javax.persistence.Temporal; -import javax.persistence.TemporalType; + +import org.springframework.format.annotation.DateTimeFormat; +import org.springframework.samples.petclinic.model.BaseEntity; /** * Simple JavaBean domain object representing a visit. diff --git a/src/main/java/org/springframework/samples/petclinic/pet/VisitController.java b/src/main/java/org/springframework/samples/petclinic/pet/VisitController.java index cd2731e5c78..0c78eab1b53 100644 --- a/src/main/java/org/springframework/samples/petclinic/pet/VisitController.java +++ b/src/main/java/org/springframework/samples/petclinic/pet/VisitController.java @@ -20,11 +20,14 @@ import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.samples.petclinic.vet.VetService; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.InitBinder; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; /** * @author Juergen Hoeller diff --git a/src/main/java/org/springframework/samples/petclinic/user/Authorities.java b/src/main/java/org/springframework/samples/petclinic/user/Authorities.java index 66e681fec9c..c4a76e671a6 100644 --- a/src/main/java/org/springframework/samples/petclinic/user/Authorities.java +++ b/src/main/java/org/springframework/samples/petclinic/user/Authorities.java @@ -8,7 +8,6 @@ import org.springframework.samples.petclinic.model.BaseEntity; -import lombok.Data; import lombok.Getter; import lombok.Setter; diff --git a/src/main/java/org/springframework/samples/petclinic/user/UserController.java b/src/main/java/org/springframework/samples/petclinic/user/UserController.java index 4dbc6da2629..8241dfe10d0 100644 --- a/src/main/java/org/springframework/samples/petclinic/user/UserController.java +++ b/src/main/java/org/springframework/samples/petclinic/user/UserController.java @@ -22,11 +22,12 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.samples.petclinic.owner.Owner; import org.springframework.samples.petclinic.owner.OwnerService; -import org.springframework.samples.petclinic.vet.VetService; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.InitBinder; +import org.springframework.web.bind.annotation.PostMapping; /** * @author Juergen Hoeller diff --git a/src/main/java/org/springframework/samples/petclinic/vet/VetService.java b/src/main/java/org/springframework/samples/petclinic/vet/VetService.java index 9d30bf19c97..09d50136303 100644 --- a/src/main/java/org/springframework/samples/petclinic/vet/VetService.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/VetService.java @@ -18,19 +18,9 @@ import java.util.Collection; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.annotation.Cacheable; import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.owner.Owner; -import org.springframework.samples.petclinic.owner.OwnerRepository; -import org.springframework.samples.petclinic.pet.Pet; -import org.springframework.samples.petclinic.pet.PetRepository; -import org.springframework.samples.petclinic.pet.PetType; -import org.springframework.samples.petclinic.pet.Visit; -import org.springframework.samples.petclinic.pet.VisitRepository; -import org.springframework.samples.petclinic.pet.exceptions.DuplicatedPetNameException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.StringUtils; /** * Mostly used as a facade for all Petclinic controllers Also a placeholder diff --git a/src/main/java/org/springframework/samples/petclinic/web/WelcomeController.java b/src/main/java/org/springframework/samples/petclinic/web/WelcomeController.java index b1dc6cf2f38..0877a4b876f 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/WelcomeController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/WelcomeController.java @@ -2,14 +2,8 @@ import java.util.Map; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.security.core.userdetails.UserDetailsService; -import org.springframework.samples.petclinic.user.User; -import org.springframework.samples.petclinic.user.UserRepository; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; -import java.util.List; @Controller public class WelcomeController { diff --git a/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java b/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java index 59a05e2f3e0..ed92dd8c0c9 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java @@ -1,33 +1,29 @@ package org.springframework.samples.petclinic.owner; +import static org.hamcrest.Matchers.hasProperty; +import static org.hamcrest.Matchers.is; +import static org.mockito.BDDMockito.given; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + import org.assertj.core.util.Lists; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.samples.petclinic.configuration.SecurityConfiguration; -import org.springframework.samples.petclinic.owner.Owner; -import org.springframework.samples.petclinic.owner.OwnerController; -import org.springframework.samples.petclinic.owner.OwnerService; -import org.springframework.samples.petclinic.user.AuthoritiesService; -import org.springframework.samples.petclinic.user.UserService; -import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; - -import static org.hamcrest.Matchers.hasProperty; -import static org.hamcrest.Matchers.is; -import static org.mockito.BDDMockito.given; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.FilterType; -import org.springframework.samples.petclinic.vet.VetService; +import org.springframework.samples.petclinic.configuration.SecurityConfiguration; +import org.springframework.samples.petclinic.user.AuthoritiesService; +import org.springframework.samples.petclinic.user.UserService; import org.springframework.security.config.annotation.web.WebSecurityConfigurer; import org.springframework.security.test.context.support.WithMockUser; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import org.springframework.test.web.servlet.MockMvc; /** * Test class for {@link OwnerController} @@ -35,9 +31,7 @@ * @author Colin But */ -@WebMvcTest(controllers=OwnerController.class, - excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = WebSecurityConfigurer.class), - excludeAutoConfiguration= SecurityConfiguration.class) +@WebMvcTest(controllers = OwnerController.class, excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = WebSecurityConfigurer.class), excludeAutoConfiguration = SecurityConfiguration.class) class OwnerControllerTests { private static final int TEST_OWNER_ID = 1; @@ -47,12 +41,12 @@ class OwnerControllerTests { @MockBean private OwnerService clinicService; - - @MockBean + + @MockBean private UserService userService; - - @MockBean - private AuthoritiesService authoritiesService; + + @MockBean + private AuthoritiesService authoritiesService; @Autowired private MockMvc mockMvc; @@ -74,47 +68,39 @@ void setup() { } @WithMockUser(value = "spring") - @Test + @Test void testInitCreationForm() throws Exception { mockMvc.perform(get("/owners/new")).andExpect(status().isOk()).andExpect(model().attributeExists("owner")) .andExpect(view().name("owners/createOrUpdateOwnerForm")); } @WithMockUser(value = "spring") - @Test + @Test void testProcessCreationFormSuccess() throws Exception { - mockMvc.perform(post("/owners/new").param("firstName", "Joe").param("lastName", "Bloggs") - .with(csrf()) - .param("address", "123 Caramel Street") - .param("city", "London") - .param("telephone", "01316761638")) + mockMvc.perform(post("/owners/new").param("firstName", "Joe").param("lastName", "Bloggs").with(csrf()) + .param("address", "123 Caramel Street").param("city", "London").param("telephone", "01316761638")) .andExpect(status().is3xxRedirection()); } @WithMockUser(value = "spring") - @Test + @Test void testProcessCreationFormHasErrors() throws Exception { - mockMvc.perform(post("/owners/new") - .with(csrf()) - .param("firstName", "Joe") - .param("lastName", "Bloggs") - .param("city", "London")) - .andExpect(status().isOk()) - .andExpect(model().attributeHasErrors("owner")) + mockMvc.perform(post("/owners/new").with(csrf()).param("firstName", "Joe").param("lastName", "Bloggs") + .param("city", "London")).andExpect(status().isOk()).andExpect(model().attributeHasErrors("owner")) .andExpect(model().attributeHasFieldErrors("owner", "address")) .andExpect(model().attributeHasFieldErrors("owner", "telephone")) .andExpect(view().name("owners/createOrUpdateOwnerForm")); } @WithMockUser(value = "spring") - @Test + @Test void testInitFindForm() throws Exception { mockMvc.perform(get("/owners/find")).andExpect(status().isOk()).andExpect(model().attributeExists("owner")) .andExpect(view().name("owners/findOwners")); } @WithMockUser(value = "spring") - @Test + @Test void testProcessFindFormSuccess() throws Exception { given(this.clinicService.findOwnerByLastName("")).willReturn(Lists.newArrayList(george, new Owner())); @@ -122,7 +108,7 @@ void testProcessFindFormSuccess() throws Exception { } @WithMockUser(value = "spring") - @Test + @Test void testProcessFindFormByLastName() throws Exception { given(this.clinicService.findOwnerByLastName(george.getLastName())).willReturn(Lists.newArrayList(george)); @@ -130,7 +116,7 @@ void testProcessFindFormByLastName() throws Exception { .andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID)); } - @WithMockUser(value = "spring") + @WithMockUser(value = "spring") @Test void testProcessFindFormNoOwnersFound() throws Exception { mockMvc.perform(get("/owners").param("lastName", "Unknown Surname")).andExpect(status().isOk()) @@ -139,7 +125,7 @@ void testProcessFindFormNoOwnersFound() throws Exception { .andExpect(view().name("owners/findOwners")); } - @WithMockUser(value = "spring") + @WithMockUser(value = "spring") @Test void testInitUpdateOwnerForm() throws Exception { mockMvc.perform(get("/owners/{ownerId}/edit", TEST_OWNER_ID)).andExpect(status().isOk()) @@ -152,36 +138,27 @@ void testInitUpdateOwnerForm() throws Exception { .andExpect(view().name("owners/createOrUpdateOwnerForm")); } - @WithMockUser(value = "spring") + @WithMockUser(value = "spring") @Test void testProcessUpdateOwnerFormSuccess() throws Exception { - mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID) - .with(csrf()) - .param("firstName", "Joe") - .param("lastName", "Bloggs") - .param("address", "123 Caramel Street") - .param("city", "London") - .param("telephone", "01616291589")) - .andExpect(status().is3xxRedirection()) + mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID).with(csrf()).param("firstName", "Joe") + .param("lastName", "Bloggs").param("address", "123 Caramel Street").param("city", "London") + .param("telephone", "01616291589")).andExpect(status().is3xxRedirection()) .andExpect(view().name("redirect:/owners/{ownerId}")); } - @WithMockUser(value = "spring") + @WithMockUser(value = "spring") @Test void testProcessUpdateOwnerFormHasErrors() throws Exception { - mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID) - .with(csrf()) - .param("firstName", "Joe") - .param("lastName", "Bloggs") - .param("city", "London")) - .andExpect(status().isOk()) + mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID).with(csrf()).param("firstName", "Joe") + .param("lastName", "Bloggs").param("city", "London")).andExpect(status().isOk()) .andExpect(model().attributeHasErrors("owner")) .andExpect(model().attributeHasFieldErrors("owner", "address")) .andExpect(model().attributeHasFieldErrors("owner", "telephone")) .andExpect(view().name("owners/createOrUpdateOwnerForm")); } - @WithMockUser(value = "spring") + @WithMockUser(value = "spring") @Test void testShowOwner() throws Exception { mockMvc.perform(get("/owners/{ownerId}", TEST_OWNER_ID)).andExpect(status().isOk()) diff --git a/src/test/java/org/springframework/samples/petclinic/owner/OwnerServiceTests.java b/src/test/java/org/springframework/samples/petclinic/owner/OwnerServiceTests.java index 3001ebba592..3b0889fa606 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/OwnerServiceTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/OwnerServiceTests.java @@ -17,30 +17,14 @@ import static org.assertj.core.api.Assertions.assertThat; -import java.time.LocalDate; import java.util.Collection; -import java.util.logging.Level; -import java.util.logging.Logger; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.context.annotation.ComponentScan; -import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.owner.Owner; -import org.springframework.samples.petclinic.owner.OwnerService; -import org.springframework.samples.petclinic.pet.Pet; -import org.springframework.samples.petclinic.pet.PetType; -import org.springframework.samples.petclinic.pet.Visit; -import org.springframework.samples.petclinic.pet.exceptions.DuplicatedPetNameException; -import org.springframework.samples.petclinic.user.Authorities; import org.springframework.samples.petclinic.user.User; -import org.springframework.samples.petclinic.util.EntityUtils; -import org.springframework.samples.petclinic.vet.Vet; import org.springframework.stereotype.Service; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.transaction.annotation.Transactional; /** diff --git a/src/test/java/org/springframework/samples/petclinic/pet/PetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/pet/PetControllerTests.java index 83543cbdd0e..9b50c3adb2c 100644 --- a/src/test/java/org/springframework/samples/petclinic/pet/PetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/pet/PetControllerTests.java @@ -35,12 +35,6 @@ import org.springframework.samples.petclinic.configuration.SecurityConfiguration; import org.springframework.samples.petclinic.owner.Owner; import org.springframework.samples.petclinic.owner.OwnerService; -import org.springframework.samples.petclinic.pet.Pet; -import org.springframework.samples.petclinic.pet.PetController; -import org.springframework.samples.petclinic.pet.PetService; -import org.springframework.samples.petclinic.pet.PetType; -import org.springframework.samples.petclinic.pet.PetTypeFormatter; -import org.springframework.samples.petclinic.vet.VetService; import org.springframework.security.config.annotation.web.WebSecurityConfigurer; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; @@ -60,9 +54,6 @@ class PetControllerTests { private static final int TEST_PET_ID = 1; - @Autowired - private PetController petController; - @MockBean private PetService petService; diff --git a/src/test/java/org/springframework/samples/petclinic/pet/PetServiceTests.java b/src/test/java/org/springframework/samples/petclinic/pet/PetServiceTests.java index a028acb5e0b..0972143655e 100644 --- a/src/test/java/org/springframework/samples/petclinic/pet/PetServiceTests.java +++ b/src/test/java/org/springframework/samples/petclinic/pet/PetServiceTests.java @@ -27,16 +27,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.context.annotation.ComponentScan; -import org.springframework.dao.DataAccessException; import org.springframework.samples.petclinic.owner.Owner; import org.springframework.samples.petclinic.owner.OwnerService; -import org.springframework.samples.petclinic.pet.Pet; -import org.springframework.samples.petclinic.pet.PetService; -import org.springframework.samples.petclinic.pet.PetType; import org.springframework.samples.petclinic.pet.exceptions.DuplicatedPetNameException; -import org.springframework.samples.petclinic.user.User; import org.springframework.samples.petclinic.util.EntityUtils; -import org.springframework.samples.petclinic.vet.Vet; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; diff --git a/src/test/java/org/springframework/samples/petclinic/pet/PetTypeFormatterTests.java b/src/test/java/org/springframework/samples/petclinic/pet/PetTypeFormatterTests.java index c631040dabe..eb527b10c89 100644 --- a/src/test/java/org/springframework/samples/petclinic/pet/PetTypeFormatterTests.java +++ b/src/test/java/org/springframework/samples/petclinic/pet/PetTypeFormatterTests.java @@ -1,5 +1,12 @@ package org.springframework.samples.petclinic.pet; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Locale; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -7,18 +14,6 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.samples.petclinic.pet.PetService; -import org.springframework.samples.petclinic.pet.PetType; -import org.springframework.samples.petclinic.pet.PetTypeFormatter; - -import java.text.ParseException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Locale; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.samples.petclinic.vet.VetService; /** * Test class for {@link PetTypeFormatter} diff --git a/src/test/java/org/springframework/samples/petclinic/pet/VisitControllerTests.java b/src/test/java/org/springframework/samples/petclinic/pet/VisitControllerTests.java index bae4cf2fd11..a72fc76ec17 100644 --- a/src/test/java/org/springframework/samples/petclinic/pet/VisitControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/pet/VisitControllerTests.java @@ -1,12 +1,13 @@ package org.springframework.samples.petclinic.pet; import static org.mockito.BDDMockito.given; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; -import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -15,10 +16,6 @@ import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.FilterType; import org.springframework.samples.petclinic.configuration.SecurityConfiguration; -import org.springframework.samples.petclinic.pet.Pet; -import org.springframework.samples.petclinic.pet.PetService; -import org.springframework.samples.petclinic.pet.VisitController; -import org.springframework.samples.petclinic.vet.VetService; import org.springframework.security.config.annotation.web.WebSecurityConfigurer; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; @@ -28,9 +25,7 @@ * * @author Colin But */ -@WebMvcTest(controllers = VisitController.class, - excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = WebSecurityConfigurer.class), - excludeAutoConfiguration= SecurityConfiguration.class) +@WebMvcTest(controllers = VisitController.class, excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = WebSecurityConfigurer.class), excludeAutoConfiguration = SecurityConfiguration.class) class VisitControllerTests { private static final int TEST_PET_ID = 1; @@ -49,35 +44,31 @@ void setup() { given(this.clinicService.findPetById(TEST_PET_ID)).willReturn(new Pet()); } - @WithMockUser(value = "spring") - @Test + @WithMockUser(value = "spring") + @Test void testInitNewVisitForm() throws Exception { mockMvc.perform(get("/owners/*/pets/{petId}/visits/new", TEST_PET_ID)).andExpect(status().isOk()) .andExpect(view().name("pets/createOrUpdateVisitForm")); } @WithMockUser(value = "spring") - @Test + @Test void testProcessNewVisitFormSuccess() throws Exception { - mockMvc.perform(post("/owners/*/pets/{petId}/visits/new", TEST_PET_ID).param("name", "George") - .with(csrf()) - .param("description", "Visit Description")) - .andExpect(status().is3xxRedirection()) + mockMvc.perform(post("/owners/*/pets/{petId}/visits/new", TEST_PET_ID).param("name", "George").with(csrf()) + .param("description", "Visit Description")).andExpect(status().is3xxRedirection()) .andExpect(view().name("redirect:/owners/{ownerId}")); } @WithMockUser(value = "spring") - @Test + @Test void testProcessNewVisitFormHasErrors() throws Exception { - mockMvc.perform(post("/owners/*/pets/{petId}/visits/new", TEST_PET_ID) - .with(csrf()) - .param("name", "George")) + mockMvc.perform(post("/owners/*/pets/{petId}/visits/new", TEST_PET_ID).with(csrf()).param("name", "George")) .andExpect(model().attributeHasErrors("visit")).andExpect(status().isOk()) .andExpect(view().name("pets/createOrUpdateVisitForm")); } @WithMockUser(value = "spring") - @Test + @Test void testShowVisits() throws Exception { mockMvc.perform(get("/owners/*/pets/{petId}/visits", TEST_PET_ID)).andExpect(status().isOk()) .andExpect(model().attributeExists("visits")).andExpect(view().name("visitList")); diff --git a/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java index d12172c5405..48542513885 100644 --- a/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java @@ -1,37 +1,31 @@ package org.springframework.samples.petclinic.vet; +import static org.hamcrest.xml.HasXPath.hasXPath; +import static org.mockito.BDDMockito.given; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + import org.assertj.core.util.Lists; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; -import org.springframework.samples.petclinic.configuration.SecurityConfiguration; -import org.springframework.samples.petclinic.vet.Specialty; -import org.springframework.samples.petclinic.vet.Vet; -import org.springframework.samples.petclinic.vet.VetController; -import org.springframework.samples.petclinic.vet.VetService; -import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.ResultActions; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; - -import static org.hamcrest.xml.HasXPath.hasXPath; -import static org.mockito.BDDMockito.given; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.FilterType; +import org.springframework.http.MediaType; +import org.springframework.samples.petclinic.configuration.SecurityConfiguration; import org.springframework.security.config.annotation.web.WebSecurityConfigurer; import org.springframework.security.test.context.support.WithMockUser; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import org.springframework.test.web.servlet.MockMvc; /** * Test class for the {@link VetController} */ -@WebMvcTest(controllers=VetController.class, - excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = WebSecurityConfigurer.class), - excludeAutoConfiguration= SecurityConfiguration.class) +@WebMvcTest(controllers = VetController.class, excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = WebSecurityConfigurer.class), excludeAutoConfiguration = SecurityConfiguration.class) class VetControllerTests { @Autowired @@ -60,16 +54,16 @@ void setup() { helen.addSpecialty(radiology); given(this.clinicService.findVets()).willReturn(Lists.newArrayList(james, helen)); } - - @WithMockUser(value = "spring") - @Test + + @WithMockUser(value = "spring") + @Test void testShowVetListHtml() throws Exception { mockMvc.perform(get("/vets")).andExpect(status().isOk()).andExpect(model().attributeExists("vets")) .andExpect(view().name("vets/vetList")); - } + } @WithMockUser(value = "spring") - @Test + @Test void testShowVetListXml() throws Exception { mockMvc.perform(get("/vets.xml").accept(MediaType.APPLICATION_XML)).andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_XML_VALUE)) diff --git a/src/test/java/org/springframework/samples/petclinic/vet/VetServiceTests.java b/src/test/java/org/springframework/samples/petclinic/vet/VetServiceTests.java index f6948f6de1e..cf6d169db8c 100644 --- a/src/test/java/org/springframework/samples/petclinic/vet/VetServiceTests.java +++ b/src/test/java/org/springframework/samples/petclinic/vet/VetServiceTests.java @@ -17,31 +17,14 @@ import static org.assertj.core.api.Assertions.assertThat; -import java.time.LocalDate; import java.util.Collection; -import java.util.logging.Level; -import java.util.logging.Logger; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.context.annotation.ComponentScan; -import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.owner.Owner; -import org.springframework.samples.petclinic.pet.Pet; -import org.springframework.samples.petclinic.pet.PetType; -import org.springframework.samples.petclinic.pet.Visit; -import org.springframework.samples.petclinic.pet.exceptions.DuplicatedPetNameException; -import org.springframework.samples.petclinic.user.Authorities; -import org.springframework.samples.petclinic.user.User; import org.springframework.samples.petclinic.util.EntityUtils; -import org.springframework.samples.petclinic.vet.Vet; -import org.springframework.samples.petclinic.vet.VetService; import org.springframework.stereotype.Service; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; -import org.springframework.transaction.annotation.Transactional; /** * Integration test of the Service and the Repository layer. diff --git a/src/test/java/org/springframework/samples/petclinic/web/CrashControllerTests.java b/src/test/java/org/springframework/samples/petclinic/web/CrashControllerTests.java index 825e58f9803..f67395bbf26 100644 --- a/src/test/java/org/springframework/samples/petclinic/web/CrashControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/web/CrashControllerTests.java @@ -1,17 +1,5 @@ package org.springframework.samples.petclinic.web; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver; - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; - /** * Test class for {@link CrashController} * From 760c5e7f3ab42e2af81026172d1e5838da3a164d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20=20Parejo?= Date: Wed, 20 Oct 2021 10:52:03 +0200 Subject: [PATCH 21/27] Improved DB initialization script by including column names in vet inserts --- src/main/resources/db/hsqldb/data.sql | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/resources/db/hsqldb/data.sql b/src/main/resources/db/hsqldb/data.sql index 164773b3fa8..69ad34e8c2e 100644 --- a/src/main/resources/db/hsqldb/data.sql +++ b/src/main/resources/db/hsqldb/data.sql @@ -8,12 +8,12 @@ INSERT INTO authorities(id,username,authority) VALUES (2,'owner1','owner'); INSERT INTO users(username,password,enabled) VALUES ('vet1','v3t',TRUE); INSERT INTO authorities(id,username,authority) VALUES (3,'vet1','veterinarian'); -INSERT INTO vets VALUES (1, 'James', 'Carter'); -INSERT INTO vets VALUES (2, 'Helen', 'Leary'); -INSERT INTO vets VALUES (3, 'Linda', 'Douglas'); -INSERT INTO vets VALUES (4, 'Rafael', 'Ortega'); -INSERT INTO vets VALUES (5, 'Henry', 'Stevens'); -INSERT INTO vets VALUES (6, 'Sharon', 'Jenkins'); +INSERT INTO vets(id, first_name,last_name) VALUES (1, 'James', 'Carter'); +INSERT INTO vets(id, first_name,last_name) VALUES (2, 'Helen', 'Leary'); +INSERT INTO vets(id, first_name,last_name) VALUES (3, 'Linda', 'Douglas'); +INSERT INTO vets(id, first_name,last_name) VALUES (4, 'Rafael', 'Ortega'); +INSERT INTO vets(id, first_name,last_name) VALUES (5, 'Henry', 'Stevens'); +INSERT INTO vets(id, first_name,last_name) VALUES (6, 'Sharon', 'Jenkins'); INSERT INTO specialties VALUES (1, 'radiology'); INSERT INTO specialties VALUES (2, 'surgery'); From 9d8720eba931530d9dee79e094e09ec8061cc3c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20=20Parejo?= Date: Wed, 20 Oct 2021 10:52:55 +0200 Subject: [PATCH 22/27] Ignored VSCode configuration files --- .gitignore | 2 ++ .vscode/launch.json | 26 -------------------------- .vscode/settings.json | 3 --- .vscode/tasks.json | 19 ------------------- 4 files changed, 2 insertions(+), 48 deletions(-) delete mode 100644 .vscode/launch.json delete mode 100644 .vscode/settings.json delete mode 100644 .vscode/tasks.json diff --git a/.gitignore b/.gitignore index ae4ccb21131..55150cad71c 100644 --- a/.gitignore +++ b/.gitignore @@ -66,3 +66,5 @@ _site/ .metadata/.plugins/org.eclipse.ui.intro/introstate .metadata/.plugins/org.eclipse.ui.workbench/dialog_settings.xml .metadata/.plugins/org.eclipse.ui.workbench/workingsets.xml + +.vscode/ diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 559c538055f..00000000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "type": "java", - "name": "Debug (Launch)-PetClinicApplication", - "request": "launch", - "cwd": "${workspaceFolder}", - "console": "internalConsole", - "stopOnEntry": false, - "mainClass": "org.springframework.samples.petclinic.PetClinicApplication", - "projectName": "spring-petclinic", - "args": "" - }, - { - "type": "java", - "name": "Debug (Attach)", - "request": "attach", - "hostName": "localhost", - "port": 0 - } - ] -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index c5f3f6b9c75..00000000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "java.configuration.updateBuildConfiguration": "interactive" -} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json deleted file mode 100644 index fabd5c41693..00000000000 --- a/.vscode/tasks.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - // See https://go.microsoft.com/fwlink/?LinkId=733558 - // for the documentation about the tasks.json format - "version": "2.0.0", - "tasks": [ - { - "label": "verify", - "type": "shell", - "command": "mvn -B verify", - "group": "build" - }, - { - "label": "test", - "type": "shell", - "command": "mvn -B test", - "group": "test" - } - ] -} From 9806b136f69eeb4b47b07136d03446a7f6e2fe96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20=20Parejo?= Date: Wed, 20 Oct 2021 11:01:51 +0200 Subject: [PATCH 23/27] =?UTF-8?q?Accents=20and=20=C3=B1=20working?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I have enabled UTF-8 encoding in application.properties --- src/main/resources/application.properties | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 5ccc852ef78..01538322437 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -5,6 +5,8 @@ spring.datasource.data=classpath*:db/${database}/data.sql spring.h2.console.enabled=true # Web spring.thymeleaf.mode=HTML +spring.http.encoding.charset=UTF-8 +spring.http.encoding.enabled=true # JPA spring.jpa.hibernate.ddl-auto=create-drop From 1a7581c937b7b904b07c969bb51a7929a39a9e54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Parejo=20Maestre?= Date: Thu, 25 Nov 2021 13:47:21 +0100 Subject: [PATCH 24/27] Added dices on session controller and improved PetService. In orded to illustrate the use of sesion context we have created the DicesOnSessionController. Additionally we have improved the implementation of savePet (in PetService), and added a new PetType (turtles!) --- .../configuration/SecurityConfiguration.java | 1 + .../samples/petclinic/pet/PetService.java | 13 +++++--- .../web/DicesOnSessionController.java | 32 +++++++++++++++++++ src/main/resources/db/hsqldb/data.sql | 1 + 4 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/springframework/samples/petclinic/web/DicesOnSessionController.java diff --git a/src/main/java/org/springframework/samples/petclinic/configuration/SecurityConfiguration.java b/src/main/java/org/springframework/samples/petclinic/configuration/SecurityConfiguration.java index 04ba8db1c76..4ac8f0844c6 100644 --- a/src/main/java/org/springframework/samples/petclinic/configuration/SecurityConfiguration.java +++ b/src/main/java/org/springframework/samples/petclinic/configuration/SecurityConfiguration.java @@ -36,6 +36,7 @@ protected void configure(HttpSecurity http) throws Exception { .antMatchers("/resources/**","/webjars/**","/h2-console/**").permitAll() .antMatchers(HttpMethod.GET, "/","/oups").permitAll() .antMatchers("/users/new").permitAll() + .antMatchers("/session/**").permitAll() .antMatchers("/admin/**").hasAnyAuthority("admin") .antMatchers("/owners/**").hasAnyAuthority("owner","admin") .antMatchers("/vets/**").authenticated() diff --git a/src/main/java/org/springframework/samples/petclinic/pet/PetService.java b/src/main/java/org/springframework/samples/petclinic/pet/PetService.java index 09331bb7857..ce0002fb1b8 100644 --- a/src/main/java/org/springframework/samples/petclinic/pet/PetService.java +++ b/src/main/java/org/springframework/samples/petclinic/pet/PetService.java @@ -62,11 +62,14 @@ public Pet findPetById(int id) throws DataAccessException { @Transactional(rollbackFor = DuplicatedPetNameException.class) public void savePet(Pet pet) throws DataAccessException, DuplicatedPetNameException { - Pet otherPet=pet.getOwner().getPetwithIdDifferent(pet.getName(), pet.getId()); - if (StringUtils.hasLength(pet.getName()) && (otherPet!= null && otherPet.getId()!=pet.getId())) { - throw new DuplicatedPetNameException(); - }else - petRepository.save(pet); + if(pet.getOwner()!=null){ + Pet otherPet=pet.getOwner().getPetwithIdDifferent(pet.getName(), pet.getId()); + if (StringUtils.hasLength(pet.getName()) && (otherPet!= null && otherPet.getId()!=pet.getId())) { + throw new DuplicatedPetNameException(); + }else + petRepository.save(pet); + }else + petRepository.save(pet); } diff --git a/src/main/java/org/springframework/samples/petclinic/web/DicesOnSessionController.java b/src/main/java/org/springframework/samples/petclinic/web/DicesOnSessionController.java new file mode 100644 index 00000000000..2a8522b0ad7 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/web/DicesOnSessionController.java @@ -0,0 +1,32 @@ +package org.springframework.samples.petclinic.web; + +import javax.servlet.http.HttpSession; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class DicesOnSessionController { + + public static int NUM_DICES=5; + public static int NUM_DICES_SIDES=6; + + @GetMapping("/session/rolldices") + public @ResponseBody Integer[] rollDices(HttpSession session){ + Integer[] dices=new Integer[NUM_DICES]; + for(int i=0;i Date: Tue, 20 Sep 2022 21:05:30 +0200 Subject: [PATCH 25/27] Updated lombok version for JDKs higher than 11 --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index d2f022b4765..968fdca6d0a 100644 --- a/pom.xml +++ b/pom.xml @@ -139,6 +139,7 @@ org.projectlombok lombok + 1.18.20 org.springframework.boot From 6c68f05b7db0ce70daccb8872e410ce7e12abbea Mon Sep 17 00:00:00 2001 From: josemgarcia Date: Wed, 21 Sep 2022 12:00:23 +0200 Subject: [PATCH 26/27] Rename the project --- pom.xml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index 968fdca6d0a..3b9f2497082 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 org.springframework.samples - spring-petclinic + spring-petclinic-josemgarcia 2.2.0.BUILD-SNAPSHOT @@ -12,7 +12,7 @@ spring-boot-starter-parent 2.2.4.RELEASE - petclinic + petclinic-josemgarcia @@ -50,16 +50,16 @@ org.springframework.boot spring-boot-starter-security - - org.springframework.security - spring-security-taglibs + + org.springframework.security + spring-security-taglibs + com.h2database h2 @@ -69,7 +69,7 @@ mysql-connector-java runtime - build-info @@ -191,7 +191,7 @@ - pl.project13.maven From 50ff3cbe8d52919975501dd4f9cd6aee6363c6c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Mar=C3=ADa=20Garc=C3=ADa?= Date: Wed, 21 Sep 2022 12:17:48 +0200 Subject: [PATCH 27/27] Update pom.xml --- pom.xml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pom.xml b/pom.xml index 3b9f2497082..f1eef71c6d8 100644 --- a/pom.xml +++ b/pom.xml @@ -31,6 +31,21 @@ 0.0.19 + + + + japarejo + Jose Antonio Parejo + japarejo@us.es + http://www.us.es + + leader + + + https://pbs.twimg.com/media/Eh4n9GhUMAAdO8x?format=jpg + + +