diff --git a/readme.md b/readme.md index 1db67921a143424db1f9e953be6918381b44124f..7ae1da37c01e46d41c168b7d92552aa37fee1e2d 100644 --- a/readme.md +++ b/readme.md @@ -3,6 +3,11 @@ ## Understanding the Spring Petclinic application with a few diagrams <a href="https://speakerdeck.com/michaelisvy/spring-petclinic-sample-application">See the presentation here</a> +## Implementation of Domain Driven Desing based on following samples +* https://github.com/VaughnVernon/IDDD_Samples +* https://github.com/citerus/dddsample-core +* https://github.com/BottegaIT/ddd-leaven-v2 + ## Running petclinic locally ``` git clone https://github.com/spring-projects/spring-petclinic.git diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java index c9b8e6977ae4a3caf3d7ef8abcfa3e09fdec17d4..b9475fef33e5f90044e0299879781a594fbeaaea 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java @@ -3,13 +3,7 @@ package org.springframework.samples.petclinic; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.annotation.Bean; -import org.springframework.samples.petclinic.config.PetclinicProperties; -import org.springframework.ui.ModelMap; -import org.springframework.web.context.request.WebRequest; -import org.springframework.web.context.request.WebRequestInterceptor; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.samples.petclinic.infrastructure.config.PetclinicProperties; @SpringBootApplication @EnableConfigurationProperties(PetclinicProperties.class) diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/web/OwnerResource.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/owner/OwnerResource.java similarity index 58% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/web/OwnerResource.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/owner/OwnerResource.java index e37ac3505035d19453da21830e8028f986d6f270..c0fe34ac140ddb78357cd79f6be187f313ba8cd4 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/web/OwnerResource.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/owner/OwnerResource.java @@ -13,25 +13,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.web; - -import java.util.Collection; +package org.springframework.samples.petclinic.boundary.web.owner; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.service.ClinicService; +import org.springframework.samples.petclinic.domain.model.owner.Owner; +import org.springframework.samples.petclinic.domain.model.owner.OwnerService; +import org.springframework.samples.petclinic.support.web.AbstractResourceController; 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.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import javax.validation.Valid; +import java.util.Collection; /** * @author Juergen Hoeller @@ -42,21 +35,20 @@ import javax.validation.Valid; @RestController public class OwnerResource extends AbstractResourceController { - private final ClinicService clinicService; - + private final OwnerService ownerService; @Autowired - public OwnerResource(ClinicService clinicService) { - this.clinicService = clinicService; + public OwnerResource(OwnerService ownerService) { + this.ownerService = ownerService; } @InitBinder public void setAllowedFields(WebDataBinder dataBinder) { dataBinder.setDisallowedFields("id"); } - + private Owner retrieveOwner(int ownerId) { - return this.clinicService.findOwnerById(ownerId); + return this.ownerService.findOwnerById(ownerId); } /** @@ -65,9 +57,9 @@ public class OwnerResource extends AbstractResourceController { @RequestMapping(value = "/owner", method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) public void createOwner(@Valid @RequestBody Owner owner) { - this.clinicService.saveOwner(owner); + this.ownerService.saveOwner(owner); } - + /** * Read single Owner */ @@ -75,31 +67,30 @@ public class OwnerResource extends AbstractResourceController { public Owner findOwner(@PathVariable("ownerId") int ownerId) { return retrieveOwner(ownerId); } - + /** * Read List of Owners */ @GetMapping("/owner/list") public Collection<Owner> findAll() { - return clinicService.findAll(); + return ownerService.findAll(); } - + /** * Update Owner */ @RequestMapping(value = "/owner/{ownerId}", method = RequestMethod.PUT) public Owner updateOwner(@PathVariable("ownerId") int ownerId, @Valid @RequestBody Owner ownerRequest) { - Owner ownerModel = retrieveOwner(ownerId); - // This is done by hand for simplicity purpose. In a real life use-case we should consider using MapStruct. - ownerModel.setFirstName(ownerRequest.getFirstName()); - ownerModel.setLastName(ownerRequest.getLastName()); - ownerModel.setCity(ownerRequest.getCity()); - ownerModel.setAddress(ownerRequest.getAddress()); - ownerModel.setTelephone(ownerRequest.getTelephone()); - this.clinicService.saveOwner(ownerModel); + Owner ownerModel = retrieveOwner(ownerId); + // This is done by hand for simplicity purpose. In a real life use-case we should consider using MapStruct. + ownerModel.setFirstName(ownerRequest.getFirstName()); + ownerModel.setLastName(ownerRequest.getLastName()); + ownerModel.setCity(ownerRequest.getCity()); + ownerModel.setAddress(ownerRequest.getAddress()); + ownerModel.setTelephone(ownerRequest.getTelephone()); + this.ownerService.saveOwner(ownerModel); return ownerModel; } - } diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/web/PetResource.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/pet/PetResource.java similarity index 78% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/web/PetResource.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/pet/PetResource.java index 192bbfb261ae9623d1d88a6378a0bbdc1d8bf525..a1d61178c67282d9190220878a438d60403b0af5 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/web/PetResource.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/pet/PetResource.java @@ -13,16 +13,18 @@ * 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.boundary.web.pet; import com.fasterxml.jackson.annotation.JsonFormat; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.http.HttpStatus; -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.ClinicService; +import org.springframework.samples.petclinic.domain.model.owner.Owner; +import org.springframework.samples.petclinic.domain.model.owner.OwnerService; +import org.springframework.samples.petclinic.domain.model.pet.Pet; +import org.springframework.samples.petclinic.domain.model.pet.PetService; +import org.springframework.samples.petclinic.domain.model.pet.PetType; +import org.springframework.samples.petclinic.support.web.AbstractResourceController; import org.springframework.web.bind.annotation.*; import javax.validation.constraints.Size; @@ -37,21 +39,24 @@ import java.util.Map; @RestController public class PetResource extends AbstractResourceController { - private final ClinicService clinicService; + private final PetService petService; + + private final OwnerService ownerService; @Autowired - public PetResource(ClinicService clinicService) { - this.clinicService = clinicService; + public PetResource(PetService petService, OwnerService ownerService) { + this.petService = petService; + this.ownerService = ownerService; } @GetMapping("/petTypes") Object getPetTypes() { - return clinicService.findPetTypes(); + return petService.findPetTypes(); } @GetMapping("/owners/{ownerId}/pets/new") public String initCreationForm(@PathVariable("ownerId") int ownerId, Map<String, Object> model) { - Owner owner = this.clinicService.findOwnerById(ownerId); + Owner owner = this.ownerService.findOwnerById(ownerId); Pet pet = new Pet(); owner.addPet(pet); model.put("pet", pet); @@ -65,7 +70,7 @@ public class PetResource extends AbstractResourceController { @PathVariable("ownerId") int ownerId) { Pet pet = new Pet(); - Owner owner = this.clinicService.findOwnerById(ownerId); + Owner owner = this.ownerService.findOwnerById(ownerId); owner.addPet(pet); save(pet, petRequest); @@ -74,7 +79,7 @@ public class PetResource extends AbstractResourceController { @PutMapping("/owners/{ownerId}/pets/{petId}") @ResponseStatus(HttpStatus.NO_CONTENT) public void processUpdateForm(@RequestBody PetRequest petRequest) { - save(clinicService.findPetById(petRequest.getId()), petRequest); + save(petService.findPetById(petRequest.getId()), petRequest); } private void save(Pet pet, PetRequest petRequest) { @@ -82,18 +87,18 @@ public class PetResource extends AbstractResourceController { pet.setName(petRequest.getName()); pet.setBirthDate(petRequest.getBirthDate()); - for (PetType petType : clinicService.findPetTypes()) { + for (PetType petType : petService.findPetTypes()) { if (petType.getId() == petRequest.getTypeId()) { pet.setType(petType); } } - clinicService.savePet(pet); + petService.savePet(pet); } @GetMapping("/owner/*/pet/{petId}") public PetDetails findPet(@PathVariable("petId") int petId) { - Pet pet = this.clinicService.findPetById(petId); + Pet pet = this.petService.findPetById(petId); return new PetDetails(pet); } diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/pet/PetValidator.java similarity index 92% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/pet/PetValidator.java index fbb7dd5be2c7c255c2e7ec70dd2583e74d6336da..78fa4efffda3c090f13b273d3de783e0e15df190 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/pet/PetValidator.java @@ -13,9 +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.boundary.web.pet; -import org.springframework.samples.petclinic.model.Pet; +import org.springframework.samples.petclinic.domain.model.pet.Pet; import org.springframework.util.StringUtils; import org.springframework.validation.Errors; diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/web/VetResource.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/vet/VetResource.java similarity index 67% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/web/VetResource.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/vet/VetResource.java index b476a43cd3254b64860b666eeb88b4dcade16b36..38254dde3a0b282e32379e249a71f31399f19058 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/web/VetResource.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/vet/VetResource.java @@ -13,19 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.web; - -import java.util.Collection; +package org.springframework.samples.petclinic.boundary.web.vet; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; -import org.springframework.samples.petclinic.model.Vet; -import org.springframework.samples.petclinic.service.ClinicService; +import org.springframework.samples.petclinic.domain.model.vet.Vet; +import org.springframework.samples.petclinic.domain.model.vet.VetService; +import org.springframework.samples.petclinic.support.web.AbstractResourceController; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; +import java.util.Collection; + /** * @author Juergen Hoeller * @author Mark Fisher @@ -35,15 +33,15 @@ import org.springframework.web.bind.annotation.RestController; @RestController public class VetResource extends AbstractResourceController { - private final ClinicService clinicService; + private final VetService vetService; @Autowired - public VetResource(ClinicService clinicService) { - this.clinicService = clinicService; + public VetResource(VetService vetService) { + this.vetService = vetService; } - + @GetMapping("/vets") public Collection<Vet> showResourcesVetList() { - return this.clinicService.findVets(); + return this.vetService.findVets(); } } diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/web/VisitResource.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/visit/VisitResource.java similarity index 60% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/web/VisitResource.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/visit/VisitResource.java index 12db83c71b9eb45a63c230c26af0af0973225e2d..be857685f588853bd158227dada18fe17a57fbdb 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/web/VisitResource.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/visit/VisitResource.java @@ -13,18 +13,15 @@ * 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.boundary.web.visit; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; -import org.springframework.samples.petclinic.model.Visit; -import org.springframework.samples.petclinic.service.ClinicService; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.samples.petclinic.domain.model.pet.PetService; +import org.springframework.samples.petclinic.domain.model.visit.Visit; +import org.springframework.samples.petclinic.domain.model.visit.VisitService; +import org.springframework.samples.petclinic.support.web.AbstractResourceController; +import org.springframework.web.bind.annotation.*; import javax.validation.Valid; @@ -37,11 +34,14 @@ import javax.validation.Valid; @RestController public class VisitResource extends AbstractResourceController { - private final ClinicService clinicService; + private final VisitService visitService; + + private final PetService petService; @Autowired - public VisitResource(ClinicService clinicService) { - this.clinicService = clinicService; + public VisitResource(VisitService visitService, PetService petService) { + this.visitService = visitService; + this.petService = petService; } @PostMapping("/owners/{ownerId}/pets/{petId}/visits") @@ -50,12 +50,12 @@ public class VisitResource extends AbstractResourceController { @Valid @RequestBody Visit visit, @PathVariable("petId") int petId) { - clinicService.findPetById(petId).addVisit(visit); - clinicService.saveVisit(visit); + petService.findPetById(petId).addVisit(visit); + visitService.saveVisit(visit); } @GetMapping("/owners/{ownerId}/pets/{petId}/visits") public Object visits(@PathVariable("petId") int petId) { - return clinicService.findPetById(petId).getVisits(); + return petService.findPetById(petId).getVisits(); } } diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Owner.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/owner/Owner.java similarity index 91% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Owner.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/owner/Owner.java index 840a965ed4a897a9c4a52aeab5bd8122ba264cfb..560ee262abef5a3da9e1b7a89a83b6f506b16183 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Owner.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/owner/Owner.java @@ -13,25 +13,18 @@ * 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.OneToMany; -import javax.persistence.Table; -import javax.validation.constraints.Digits; +package org.springframework.samples.petclinic.domain.model.owner; import org.hibernate.validator.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.domain.model.pet.Pet; +import org.springframework.samples.petclinic.domain.shared.Person; + +import javax.persistence.*; +import javax.validation.constraints.Digits; +import java.util.*; /** * Simple JavaBean domain object representing an owner. diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/owner/OwnerRepository.java similarity index 73% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/owner/OwnerRepository.java index 4e0c7c4afa223c2b3b30c79185a7ffd96a94f80b..8c25b893e66582cf83aa137ee6b318c42f9ca468 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/owner/OwnerRepository.java @@ -13,17 +13,9 @@ * 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.domain.model.owner; -import java.util.Collection; - -import org.springframework.dao.DataAccessException; import org.springframework.data.jpa.repository.JpaRepository; -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; /** * Repository class for <code>Owner</code> domain objects All method names are compliant with Spring Data naming diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/owner/OwnerService.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/owner/OwnerService.java new file mode 100644 index 0000000000000000000000000000000000000000..ca66e8b882dfc3478d7ee334138ff3690341d153 --- /dev/null +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/owner/OwnerService.java @@ -0,0 +1,38 @@ +package org.springframework.samples.petclinic.domain.model.owner; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataAccessException; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Collection; + +/** + * @author mszarlinski on 2016-10-30. + */ +@Service +public class OwnerService { + + private final OwnerRepository ownerRepository; + + @Autowired + public OwnerService(OwnerRepository ownerRepository) { + this.ownerRepository = ownerRepository; + } + + @Transactional(readOnly = true) + public Owner findOwnerById(int id) throws DataAccessException { + return ownerRepository.findOne(id); + } + + @Transactional(readOnly = true) + public Collection<Owner> findAll() throws DataAccessException { + return ownerRepository.findAll(); + } + + @Transactional + public void saveOwner(Owner owner) throws DataAccessException { + ownerRepository.save(owner); + } + +} diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Pet.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/pet/Pet.java similarity index 78% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Pet.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/pet/Pet.java index a121e7b81ec29626547a8514037875b42e1a8ca2..7e5501f89e8f724bb23383f0e3f1370a7453a4c0 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Pet.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/pet/Pet.java @@ -13,28 +13,17 @@ * 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.domain.model.pet; import com.fasterxml.jackson.annotation.JsonIgnore; import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.PropertyComparator; +import org.springframework.samples.petclinic.domain.model.owner.Owner; +import org.springframework.samples.petclinic.domain.model.visit.Visit; +import org.springframework.samples.petclinic.support.jpa.NamedEntity; -import javax.persistence.CascadeType; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; -import javax.persistence.OneToMany; -import javax.persistence.Table; -import javax.persistence.Temporal; -import javax.persistence.TemporalType; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import javax.persistence.*; +import java.util.*; /** * Simple business object representing a pet. @@ -80,7 +69,7 @@ public class Pet extends NamedEntity { return this.type; } - protected void setOwner(Owner owner) { + public void setOwner(Owner owner) { this.owner = owner; } @@ -94,7 +83,7 @@ public class Pet extends NamedEntity { protected Set<Visit> getVisitsInternal() { if (this.visits == null) { - this.visits = new HashSet<Visit>(); + this.visits = new HashSet<>(); } return this.visits; } diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/pet/PetRepository.java similarity index 86% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/pet/PetRepository.java index 58e73d7f67a3823b922cc6ef387528dbe99d62c2..d9893772b0b1a0a2556ee7ae240dcaaa533a7383 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/pet/PetRepository.java @@ -13,16 +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.domain.model.pet; -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; + +import java.util.List; /** * Repository class for <code>Pet</code> domain objects All method names are compliant with Spring Data naming diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/pet/PetService.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/pet/PetService.java new file mode 100644 index 0000000000000000000000000000000000000000..2a733a010055404717b55ea356576ac4f89c1314 --- /dev/null +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/pet/PetService.java @@ -0,0 +1,37 @@ +package org.springframework.samples.petclinic.domain.model.pet; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataAccessException; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Collection; + +/** + * @author mszarlinski on 2016-10-30. + */ +@Service +public class PetService { + + private final PetRepository petRepository; + + @Autowired + public PetService(PetRepository petRepository) { + this.petRepository = petRepository; + } + + @Transactional(readOnly = true) + public Pet findPetById(int id) throws DataAccessException { + return petRepository.findById(id); + } + + @Transactional + public void savePet(Pet pet) throws DataAccessException { + petRepository.save(pet); + } + + @Transactional(readOnly = true) + public Collection<PetType> findPetTypes() throws DataAccessException { + return petRepository.findPetTypes(); + } +} diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/PetType.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/pet/PetType.java similarity index 86% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/PetType.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/pet/PetType.java index 99adf75917e3c4c900a88f47bdfea62e6efba541..7692e8e76d1297bd99e4997182f499f9d06775c7 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/PetType.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/pet/PetType.java @@ -13,7 +13,9 @@ * 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.domain.model.pet; + +import org.springframework.samples.petclinic.support.jpa.NamedEntity; import javax.persistence.Entity; import javax.persistence.Table; diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Specialty.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/vet/Specialty.java similarity index 86% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Specialty.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/vet/Specialty.java index 6ea209cd45ac5fdaa287aabe9b3bfd7d71682102..3d57d43adbbb49a2c88f204c17bd078c00385374 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Specialty.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/vet/Specialty.java @@ -13,7 +13,9 @@ * 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.domain.model.vet; + +import org.springframework.samples.petclinic.support.jpa.NamedEntity; import javax.persistence.Entity; import javax.persistence.Table; diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Vet.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/vet/Vet.java similarity index 94% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Vet.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/vet/Vet.java index c58bd85b2a14d6453da980aa6041b2d6184425cc..2e6fb04a50f1e78c5cacadb31f40a5e0e40e5a38 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Vet.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/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.domain.model.vet; import java.util.ArrayList; import java.util.Collections; @@ -31,6 +31,7 @@ import javax.xml.bind.annotation.XmlElement; import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.PropertyComparator; +import org.springframework.samples.petclinic.domain.shared.Person; /** * Simple JavaBean domain object representing a veterinarian. diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/vet/VetRepository.java similarity index 92% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/vet/VetRepository.java index aabdfd65d9b04c6054a7268802f329b634e7fb46..ecb986e81fa27d1f530edf6294dcf54aad2fe380 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/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; - -import java.util.Collection; +package org.springframework.samples.petclinic.domain.model.vet; import org.springframework.dao.DataAccessException; import org.springframework.data.repository.Repository; -import org.springframework.samples.petclinic.model.Vet; + +import java.util.Collection; /** * Repository class for <code>Vet</code> domain objects All method names are compliant with Spring Data naming diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/vet/VetService.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/vet/VetService.java new file mode 100644 index 0000000000000000000000000000000000000000..9c4b9740084f3ae2edd8533f5d0ca69e02ee00aa --- /dev/null +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/vet/VetService.java @@ -0,0 +1,29 @@ +package org.springframework.samples.petclinic.domain.model.vet; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataAccessException; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.cache.annotation.CacheResult; +import java.util.Collection; + +/** + * @author mszarlinski on 2016-10-30. + */ +@Service +public class VetService { + + private final VetRepository vetRepository; + + @Autowired + public VetService(VetRepository vetRepository) { + this.vetRepository = vetRepository; + } + + @Transactional(readOnly = true) + @CacheResult(cacheName = "vets") + public Collection<Vet> findVets() throws DataAccessException { + return vetRepository.findAll(); + } +} diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Vets.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/vet/Vets.java similarity index 91% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Vets.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/vet/Vets.java index e8f44a7bcee01982c88d3c6e990ccf8a0463f99b..3a52aec6f5dcfd19c7742c9556488445b9eaa8c5 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Vets.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/vet/Vets.java @@ -14,7 +14,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.domain.model.vet; import java.util.ArrayList; import java.util.List; @@ -36,7 +36,7 @@ public class Vets { @XmlElement public List<Vet> getVetList() { if (vets == null) { - vets = new ArrayList<Vet>(); + vets = new ArrayList<>(); } return vets; } diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Visit.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/visit/Visit.java similarity index 89% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Visit.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/visit/Visit.java index 4440c6f5c41089d083c31eac4b473c93437e89f7..d2a90dc3007718e3b1aa04f1fff294a7eef97095 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Visit.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/visit/Visit.java @@ -13,18 +13,14 @@ * 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.domain.model.visit; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnore; +import org.springframework.samples.petclinic.domain.model.pet.Pet; +import org.springframework.samples.petclinic.support.jpa.BaseEntity; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; -import javax.persistence.Table; -import javax.persistence.Temporal; -import javax.persistence.TemporalType; +import javax.persistence.*; import javax.validation.constraints.Size; import java.util.Date; diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/visit/VisitRepository.java similarity index 88% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/visit/VisitRepository.java index e734f3275c5c251de07c0c9cc2fdc98117e47db2..6e8a91a7a2f4e48a82ca72e817f6c5943789bc7d 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/visit/VisitRepository.java @@ -13,14 +13,14 @@ * 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.domain.model.visit; 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; +import org.springframework.samples.petclinic.support.jpa.BaseEntity; +import org.springframework.samples.petclinic.domain.model.visit.Visit; /** * Repository class for <code>Visit</code> domain objects All method names are compliant with Spring Data naming diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/visit/VisitService.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/visit/VisitService.java new file mode 100644 index 0000000000000000000000000000000000000000..997534c1b97adb1708eee13a715f82fff6a83c6d --- /dev/null +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/visit/VisitService.java @@ -0,0 +1,25 @@ +package org.springframework.samples.petclinic.domain.model.visit; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataAccessException; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * @author mszarlinski on 2016-10-30. + */ +@Service +public class VisitService { + + private final VisitRepository visitRepository; + + @Autowired + public VisitService(VisitRepository visitRepository) { + this.visitRepository = visitRepository; + } + + @Transactional + public void saveVisit(Visit visit) throws DataAccessException { + visitRepository.save(visit); + } +} diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Person.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/shared/Person.java similarity index 91% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Person.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/shared/Person.java index d3e03c0ddf61a9c3bf2209a13b88cc11f1a5c897..631cd03f3d1a5ae211fb777e19a421c4d13002a1 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/Person.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/shared/Person.java @@ -13,12 +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.domain.shared; import javax.persistence.Column; import javax.persistence.MappedSuperclass; import org.hibernate.validator.constraints.NotEmpty; +import org.springframework.samples.petclinic.support.jpa.BaseEntity; /** * Simple JavaBean domain object representing an person. diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/util/CallMonitoringAspect.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/infrastructure/CallMonitoringAspect.java similarity index 97% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/util/CallMonitoringAspect.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/infrastructure/CallMonitoringAspect.java index 2ddcf5f6c0ddef9f280758c60f95d3da849733ca..c09e2214953015714bf3a035c7afb757f5fcc2d7 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/util/CallMonitoringAspect.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/infrastructure/CallMonitoringAspect.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.util; +package org.springframework.samples.petclinic.infrastructure; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/config/CacheConfig.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/infrastructure/config/CacheConfig.java similarity index 93% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/config/CacheConfig.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/infrastructure/config/CacheConfig.java index 512254b2d9fca5da3f6e5ce4b228db57e09cd634..9e4786a0df3f68c36e7db18206dd49d4d83ab69f 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/config/CacheConfig.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/infrastructure/config/CacheConfig.java @@ -1,4 +1,4 @@ -package org.springframework.samples.petclinic.config; +package org.springframework.samples.petclinic.infrastructure.config; import org.ehcache.config.CacheConfiguration; import org.ehcache.config.builders.CacheConfigurationBuilder; @@ -11,7 +11,6 @@ import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Profile; import javax.cache.CacheManager; import java.util.concurrent.TimeUnit; diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/config/PetclinicProperties.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/infrastructure/config/PetclinicProperties.java similarity index 94% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/config/PetclinicProperties.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/infrastructure/config/PetclinicProperties.java index 137529aaa90b804957225ce945126654a3dbaab4..0397aff79b14dd0240f5235d004079ac7ea7198d 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/config/PetclinicProperties.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/infrastructure/config/PetclinicProperties.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.config; +package org.springframework.samples.petclinic.infrastructure.config; import org.springframework.boot.context.properties.ConfigurationProperties; diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/config/WebConfig.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/infrastructure/config/WebConfig.java similarity index 86% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/config/WebConfig.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/infrastructure/config/WebConfig.java index ac20774957f6d5ac33bd2f48e273974cf221e17a..2d87a9c3c63fefb0c0b26e4ddd1feee74e096e14 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/config/WebConfig.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/infrastructure/config/WebConfig.java @@ -1,4 +1,4 @@ -package org.springframework.samples.petclinic.config; +package org.springframework.samples.petclinic.infrastructure.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/package-info.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/package-info.java deleted file mode 100644 index 2723dc9aba4ebb31d4bc4c7954c23ef7d3aaabc5..0000000000000000000000000000000000000000 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ - -/** - * - * The classes in this package represent PetClinic's business layer. - * - */ -package org.springframework.samples.petclinic.model; - diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java deleted file mode 100644 index d35e26bdffb1388b8587ab60d801147d80103136..0000000000000000000000000000000000000000 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java +++ /dev/null @@ -1,50 +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.service; - -import java.util.Collection; - -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; - - -/** - * Mostly used as a facade so all controllers have a single point of entry - * - * @author Michael Isvy - */ -public interface ClinicService { - - Collection<PetType> findPetTypes(); - - Owner findOwnerById(int id); - - Pet findPetById(int id); - - void savePet(Pet pet); - - void saveVisit(Visit visit); - - Collection<Vet> findVets(); - - void saveOwner(Owner owner); - - Collection<Owner> findAll(); - -} diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java deleted file mode 100644 index 4c7218c2143f602499475045a9c2f26f4c6add00..0000000000000000000000000000000000000000 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java +++ /dev/null @@ -1,104 +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.service; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.model.*; -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.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import javax.cache.annotation.CacheResult; -import java.util.Collection; - -/** - * Mostly used as a facade for all Petclinic controllers - * Also a placeholder for @Transactional and @Cacheable annotations - * - * @author Michael Isvy - */ -@Service -public class ClinicServiceImpl implements ClinicService { - - private PetRepository petRepository; - private VetRepository vetRepository; - private OwnerRepository ownerRepository; - private VisitRepository visitRepository; - - @Autowired - public ClinicServiceImpl(PetRepository petRepository, VetRepository vetRepository, OwnerRepository ownerRepository, VisitRepository visitRepository) { - this.petRepository = petRepository; - this.vetRepository = vetRepository; - this.ownerRepository = ownerRepository; - this.visitRepository = visitRepository; - } - - @Override - @Transactional(readOnly = true) - public Collection<PetType> findPetTypes() throws DataAccessException { - return petRepository.findPetTypes(); - } - - @Override - @Transactional(readOnly = true) - public Owner findOwnerById(int id) throws DataAccessException { - return ownerRepository.findOne(id); - } - - @Transactional(readOnly = true) - public Collection<Owner> findAll() throws DataAccessException { - return ownerRepository.findAll(); - } - - @Override - @Transactional - public void saveOwner(Owner owner) throws DataAccessException { - ownerRepository.save(owner); - } - - - @Override - @Transactional - public void saveVisit(Visit visit) throws DataAccessException { - visitRepository.save(visit); - } - - - @Override - @Transactional(readOnly = true) - public Pet findPetById(int id) throws DataAccessException { - return petRepository.findById(id); - } - - @Override - @Transactional - public void savePet(Pet pet) throws DataAccessException { - petRepository.save(pet); - } - - @Override - @Transactional(readOnly = true) - @CacheResult(cacheName = "vets") - public Collection<Vet> findVets() throws DataAccessException { - return vetRepository.findAll(); - } - - -} diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/support/jpa/BaseEntity.java similarity index 95% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/support/jpa/BaseEntity.java index 8dfbfe90b4310b667c83b9d38e6e3619970b2218..72bdac1fdf238cfc2556c67eee2dea50edf925fa 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/support/jpa/BaseEntity.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.support.jpa; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/support/jpa/NamedEntity.java similarity index 95% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/support/jpa/NamedEntity.java index cb36a6267a88372a99982fe1e6e566415f6e41ca..f724405869db4ecafd5479cea000aa0c707d2148 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/support/jpa/NamedEntity.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.support.jpa; import javax.persistence.Column; import javax.persistence.MappedSuperclass; diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/web/AbstractResourceController.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/support/web/AbstractResourceController.java similarity index 86% rename from springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/web/AbstractResourceController.java rename to springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/support/web/AbstractResourceController.java index 60fe88ca2f00af1a498ccfa25d997a1fb22f2966..d77808a5b5c038d33870768274f8edf24f4fb311 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/web/AbstractResourceController.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/support/web/AbstractResourceController.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.support.web; import org.springframework.web.bind.annotation.CrossOrigin; @@ -21,5 +21,5 @@ import org.springframework.web.bind.annotation.CrossOrigin; * @author Antoine Rey */ @CrossOrigin -abstract class AbstractResourceController { +public abstract class AbstractResourceController { } diff --git a/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/web/PetResourceTests.java b/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/boundary/web/pet/PetResourceTests.java similarity index 55% rename from springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/web/PetResourceTests.java rename to springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/boundary/web/pet/PetResourceTests.java index 32a2bc7ba1423becbf8aa513d08359a3e4295c85..7c6e1db51c1abd7106c6db48c945015ec6d03fe8 100644 --- a/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/web/PetResourceTests.java +++ b/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/boundary/web/pet/PetResourceTests.java @@ -1,4 +1,4 @@ -package org.springframework.samples.petclinic.web; +package org.springframework.samples.petclinic.boundary.web.pet; import org.junit.Test; import org.junit.runner.RunWith; @@ -6,18 +6,18 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; -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.ClinicService; +import org.springframework.samples.petclinic.boundary.web.pet.PetResource; +import org.springframework.samples.petclinic.domain.model.owner.Owner; +import org.springframework.samples.petclinic.domain.model.owner.OwnerService; +import org.springframework.samples.petclinic.domain.model.pet.Pet; +import org.springframework.samples.petclinic.domain.model.pet.PetService; +import org.springframework.samples.petclinic.domain.model.pet.PetType; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; 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.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @RunWith(SpringRunner.class) @WebMvcTest(PetResource.class) @@ -27,25 +27,29 @@ public class PetResourceTests { private MockMvc mvc; @MockBean - ClinicService clinicService; + PetService petService; + + @MockBean + OwnerService ownerService; @Test public void shouldGetAPetInJSonFormat() throws Exception { Pet pet = setupPet(); - given(clinicService.findPetById(2)).willReturn(pet); + given(petService.findPetById(2)).willReturn(pet); mvc.perform(get("/owner/2/pet/2.json").accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) - .andExpect(jsonPath("$.id").value(2)) - .andExpect(jsonPath("$.name").value("Basil")) - .andExpect(jsonPath("$.type.id").value(6)); + .andExpect(status().isOk()) + .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(jsonPath("$.id").value(2)) + .andExpect(jsonPath("$.name").value("Basil")) + .andExpect(jsonPath("$.type.id").value(6)); } - private Pet setupPet() {Owner owner = new Owner(); + private Pet setupPet() { + Owner owner = new Owner(); owner.setFirstName("George"); owner.setLastName("Bush"); diff --git a/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/web/VetResourceTests.java b/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/boundary/web/vet/VetResourceTests.java similarity index 74% rename from springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/web/VetResourceTests.java rename to springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/boundary/web/vet/VetResourceTests.java index 16c0357412a6467e4149e4a527a1ae9cc64628b6..56064f488a9456a2fd89d98a0d05c61e5469b2f0 100644 --- a/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/web/VetResourceTests.java +++ b/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/boundary/web/vet/VetResourceTests.java @@ -1,4 +1,4 @@ -package org.springframework.samples.petclinic.web; +package org.springframework.samples.petclinic.boundary.web.vet; import org.junit.Test; import org.junit.runner.RunWith; @@ -6,13 +6,13 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; -import org.springframework.samples.petclinic.model.Vet; -import org.springframework.samples.petclinic.service.ClinicService; +import org.springframework.samples.petclinic.boundary.web.vet.VetResource; +import org.springframework.samples.petclinic.domain.model.vet.Vet; +import org.springframework.samples.petclinic.domain.model.vet.VetService; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; -import java.util.Arrays; - +import static java.util.Arrays.asList; 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.jsonPath; @@ -26,7 +26,7 @@ public class VetResourceTests { private MockMvc mvc; @MockBean - ClinicService clinicService; + VetService vetService; @Test public void shouldGetAListOfVetsInJSonFormat() throws Exception { @@ -34,7 +34,7 @@ public class VetResourceTests { Vet vet = new Vet(); vet.setId(1); - given(clinicService.findVets()).willReturn(Arrays.asList(vet)); + given(vetService.findVets()).willReturn(asList(vet)); mvc.perform(get("/vets.json").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) diff --git a/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/service/AbstractClinicServiceTests.java b/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/domain/model/AbstractClinicServiceTests.java similarity index 71% rename from springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/service/AbstractClinicServiceTests.java rename to springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/domain/model/AbstractClinicServiceTests.java index 507588290a2ecc0b6404fdc0c00aa93f7fcdf0dc..fb871b910a451cacd74e027918069d8c5b1991f2 100644 --- a/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/service/AbstractClinicServiceTests.java +++ b/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/domain/model/AbstractClinicServiceTests.java @@ -13,15 +13,19 @@ * 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.domain.model; import org.junit.Test; 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.model.Vet; -import org.springframework.samples.petclinic.model.Visit; +import org.springframework.samples.petclinic.domain.model.owner.Owner; +import org.springframework.samples.petclinic.domain.model.owner.OwnerService; +import org.springframework.samples.petclinic.domain.model.pet.Pet; +import org.springframework.samples.petclinic.domain.model.pet.PetService; +import org.springframework.samples.petclinic.domain.model.pet.PetType; +import org.springframework.samples.petclinic.domain.model.vet.Vet; +import org.springframework.samples.petclinic.domain.model.vet.VetService; +import org.springframework.samples.petclinic.domain.model.visit.Visit; +import org.springframework.samples.petclinic.domain.model.visit.VisitService; import org.springframework.test.context.ContextConfiguration; import org.springframework.transaction.annotation.Transactional; @@ -51,67 +55,76 @@ import static org.assertj.core.api.Assertions.assertThat; public abstract class AbstractClinicServiceTests { @Autowired - protected ClinicService clinicService; + protected OwnerService ownerService; + + @Autowired + protected VetService vetService; + + @Autowired + protected PetService petService; + + @Autowired + protected VisitService visitService; @Test public void shouldFindSingleOwnerWithPet() { - Owner owner = this.clinicService.findOwnerById(1); + Owner owner = this.ownerService.findOwnerById(1); assertThat(owner.getLastName()).startsWith("Franklin"); assertThat(owner.getPets().size()).isEqualTo(1); } - + @Test public void shouldReturnAllOwnersInCaseLastNameIsEmpty() { - Collection<Owner> owners = this.clinicService.findAll(); - assertThat(owners).extracting("lastName").contains("Davis", "Franklin"); + Collection<Owner> owners = this.ownerService.findAll(); + assertThat(owners).extracting("lastName").contains("Davis", "Franklin"); } @Test @Transactional public void shouldInsertOwner() { - Collection<Owner> owners = this.clinicService.findAll(); + Collection<Owner> owners = this.ownerService.findAll(); int found = owners.size(); - + Owner owner = new Owner(); owner.setFirstName("Sam"); owner.setLastName("Schultz"); owner.setAddress("4, Evans Street"); owner.setCity("Wollongong"); owner.setTelephone("4444444444"); - this.clinicService.saveOwner(owner); + this.ownerService.saveOwner(owner); assertThat(owner.getId().longValue()).isNotEqualTo(0); - owners = this.clinicService.findAll(); + owners = this.ownerService.findAll(); assertThat(owners.size()).isEqualTo(found + 1); } @Test @Transactional public void shouldUpdateOwner() { - Owner owner = this.clinicService.findOwnerById(1); + Owner owner = this.ownerService.findOwnerById(1); String oldLastName = owner.getLastName(); String newLastName = oldLastName + "X"; - + owner.setLastName(newLastName); - this.clinicService.saveOwner(owner); + this.ownerService.saveOwner(owner); // retrieving new name from database - owner = this.clinicService.findOwnerById(1); + owner = this.ownerService.findOwnerById(1); assertThat(owner.getLastName()).isEqualTo(newLastName); } @Test public void shouldFindPetWithCorrectId() { - Pet pet7 = this.clinicService.findPetById(7); + Pet pet7 = this.petService.findPetById(7); assertThat(pet7.getName()).startsWith("Samantha"); assertThat(pet7.getOwner().getFirstName()).isEqualTo("Jean"); - + } @Test public void shouldFindAllPetTypes() { - Collection<PetType> petTypes = this.clinicService.findPetTypes(); - + Collection<PetType> petTypes = this.petService.findPetTypes(); + PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1); assertThat(petType1.getName()).isEqualTo("cat"); PetType petType4 = EntityUtils.getById(petTypes, PetType.class, 4); @@ -121,21 +134,21 @@ public abstract class AbstractClinicServiceTests { @Test @Transactional public void shouldInsertPetIntoDatabaseAndGenerateId() { - Owner owner6 = this.clinicService.findOwnerById(6); + Owner owner6 = this.ownerService.findOwnerById(6); int found = owner6.getPets().size(); - + Pet pet = new Pet(); pet.setName("bowser"); - Collection<PetType> types = this.clinicService.findPetTypes(); + Collection<PetType> types = this.petService.findPetTypes(); pet.setType(EntityUtils.getById(types, PetType.class, 2)); pet.setBirthDate(new Date()); owner6.addPet(pet); assertThat(owner6.getPets().size()).isEqualTo(found + 1); - - this.clinicService.savePet(pet); - this.clinicService.saveOwner(owner6); - - owner6 = this.clinicService.findOwnerById(6); + + this.petService.savePet(pet); + this.ownerService.saveOwner(owner6); + + owner6 = this.ownerService.findOwnerById(6); assertThat(owner6.getPets().size()).isEqualTo(found + 1); // checks that id has been generated assertThat(pet.getId()).isNotNull(); @@ -144,21 +157,21 @@ public abstract class AbstractClinicServiceTests { @Test @Transactional public void shouldUpdatePetName() throws Exception { - Pet pet7 = this.clinicService.findPetById(7); + Pet pet7 = this.petService.findPetById(7); String oldName = pet7.getName(); - + String newName = oldName + "X"; pet7.setName(newName); - this.clinicService.savePet(pet7); + this.petService.savePet(pet7); - pet7 = this.clinicService.findPetById(7); + pet7 = this.petService.findPetById(7); assertThat(pet7.getName()).isEqualTo(newName); } @Test public void shouldFindVets() { - Collection<Vet> vets = this.clinicService.findVets(); - + Collection<Vet> vets = this.vetService.findVets(); + Vet vet = EntityUtils.getById(vets, Vet.class, 3); assertThat(vet.getLastName()).isEqualTo("Douglas"); assertThat(vet.getNrOfSpecialties()).isEqualTo(2); @@ -169,15 +182,15 @@ public abstract class AbstractClinicServiceTests { @Test @Transactional public void shouldAddNewVisitForPet() { - Pet pet7 = this.clinicService.findPetById(7); + Pet pet7 = this.petService.findPetById(7); int found = pet7.getVisits().size(); Visit visit = new Visit(); pet7.addVisit(visit); - visit.setDescription("test"); - this.clinicService.saveVisit(visit); - this.clinicService.savePet(pet7); + visit.setDescription("test"); + this.visitService.saveVisit(visit); + this.petService.savePet(pet7); - pet7 = this.clinicService.findPetById(7); + pet7 = this.petService.findPetById(7); assertThat(pet7.getVisits().size()).isEqualTo(found + 1); assertThat(visit.getId()).isNotNull(); } diff --git a/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/service/EntityUtils.java b/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/domain/model/EntityUtils.java similarity index 90% rename from springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/service/EntityUtils.java rename to springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/domain/model/EntityUtils.java index 367aff37c36642b1d339d5da267cc0b39038b0a0..a040266e6b004aeeb14255f056a198c88207ed84 100644 --- a/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/service/EntityUtils.java +++ b/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/domain/model/EntityUtils.java @@ -14,12 +14,12 @@ * limitations under the License. */ -package org.springframework.samples.petclinic.service; +package org.springframework.samples.petclinic.domain.model; import java.util.Collection; import org.springframework.orm.ObjectRetrievalFailureException; -import org.springframework.samples.petclinic.model.BaseEntity; +import org.springframework.samples.petclinic.support.jpa.BaseEntity; /** * Utility methods for handling entities. Separate from the BaseEntity class mainly because of dependency on the @@ -27,7 +27,7 @@ import org.springframework.samples.petclinic.model.BaseEntity; * * @author Juergen Hoeller * @author Sam Brannen - * @see org.springframework.samples.petclinic.model.BaseEntity + * @see BaseEntity * @since 29.10.2003 */ public abstract class EntityUtils { diff --git a/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java b/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/infrastructure/ValidatorTests.java similarity index 87% rename from springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java rename to springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/infrastructure/ValidatorTests.java index f8582b5d9fbaea898bcc3a831ba441d3a835a944..ced033fdf0579c15123cd056ac65b54403e2c691 100644 --- a/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java +++ b/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/infrastructure/ValidatorTests.java @@ -1,4 +1,4 @@ -package org.springframework.samples.petclinic.model; +package org.springframework.samples.petclinic.infrastructure; import static org.assertj.core.api.Assertions.assertThat; @@ -10,17 +10,18 @@ import javax.validation.Validator; import org.junit.Test; import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.samples.petclinic.domain.shared.Person; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; /** - * + * * @author Michael Isvy - * Simple test to make sure that Bean Validation is working + * Simple test to make sure that Bean Validation is working * (useful when upgrading to a new version of Hibernate Validator/ Bean Validation) * */ public class ValidatorTests { - + private Validator createValidator() { LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean(); localValidatorFactoryBean.afterPropertiesSet(); @@ -43,5 +44,5 @@ public class ValidatorTests { assertThat(violation.getPropertyPath().toString()).isEqualTo("firstName"); assertThat(violation.getMessage()).isEqualTo("may not be empty"); } - + }