Skip to content

Commit

Permalink
Added performance problems
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKunz committed Feb 14, 2020
1 parent 90fe615 commit e11d143
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
</properties>

<dependencies>
<!-- Used to place more data in teh DB -->
<dependency>
<groupId>com.devskiller</groupId>
<artifactId>jfairy</artifactId>
<version>0.6.3</version>
</dependency>


<!-- Spring and Spring Boot dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,24 @@ public String processCreationForm(@Valid Owner owner, BindingResult result) {
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
}
else {
// let's check if an owner with the same name does not exist..
verifyNameUnique(owner.getFirstName(), owner.getLastName());
this.owners.save(owner);
return "redirect:/owners/" + owner.getId();
}
}

private void verifyNameUnique(String firstName, String lastName) {
for (int id : this.owners.getAllIds()) {
Owner other = owners.findById(id);
boolean sameFirstName = other.getFirstName().equals(firstName);
boolean sameLastName = other.getLastName().equals(lastName);
if (sameFirstName && sameLastName) {
throw new IllegalArgumentException("A Owner with the same name already exists!");
}
}
}

@GetMapping("/owners/find")
public String initFindForm(Map<String, Object> model) {
model.put("owner", new Owner());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.samples.petclinic.owner;

import java.util.Collection;
import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
Expand Down Expand Up @@ -55,6 +56,9 @@ public interface OwnerRepository extends Repository<Owner, Integer> {
@Transactional(readOnly = true)
Owner findById(@Param("id") Integer id);

@Query("select o.id from Owner o")
List<Integer> getAllIds();

/**
* Save an {@link Owner} to the data store, either inserting or updating it.
* @param owner the {@link Owner} to save
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult res
if (StringUtils.hasLength(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null) {
result.rejectValue("name", "duplicate", "already exists");
}
if (pet.getName().equalsIgnoreCase("bugsy")) {
weDontLikeBugsyAroundHere();
}
owner.addPet(pet);
if (result.hasErrors()) {
model.put("pet", pet);
Expand All @@ -89,6 +92,14 @@ public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult res
}
}

private void weDontLikeBugsyAroundHere() {
try {
Thread.sleep(5000);
}
catch (InterruptedException e) {
}
}

@GetMapping("/pets/{petId}/edit")
public String initUpdateForm(@PathVariable("petId") int petId, ModelMap model) {
Pet pet = this.pets.findById(petId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.springframework.samples.petclinic.system;

import com.devskiller.jfairy.Bootstrap;
import com.devskiller.jfairy.Fairy;
import com.devskiller.jfairy.producer.person.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.owner.*;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Locale;
import java.util.Random;

@Component
public class DataBasePopulator {

private final int NUM_OWNERS_TO_ADD = 100;

private final int PETS_PER_OWNER = 3;

@Autowired
private OwnerRepository ownerRepo;

@Autowired
private PetRepository petRepo;

@PostConstruct
@Transactional
void addMoreOwnersAndPets() {

Random numberGen = new Random(42);

List<PetType> petTypes = petRepo.findPetTypes();

Fairy ownerGen = Bootstrap.builder().withLocale(Locale.ENGLISH) // english owners
// ...
.withRandomSeed(42).build();
Fairy petGen = Bootstrap.builder().withLocale(Locale.FRENCH) // with french pets
.withRandomSeed(42).build();

for (int i = 0; i < NUM_OWNERS_TO_ADD; i++) {
Person p = ownerGen.person();
Owner newOwner = new Owner();
newOwner.setAddress(p.getAddress().getAddressLine1());
newOwner.setCity(p.getAddress().getCity());
newOwner.setTelephone("1234567890");
newOwner.setFirstName(p.getFirstName());
newOwner.setLastName(p.getLastName());

double petCount = numberGen.nextDouble() * PETS_PER_OWNER;
for (int j = 0; j <= petCount; j++) {
Person pe = petGen.person();
Pet pet = new Pet();
pet.setBirthDate(pe.getDateOfBirth());
pet.setName(pe.getFirstName());
pet.setType(petTypes.get((int) (numberGen.nextDouble() * petTypes.size())));
newOwner.addPet(pet);
}

ownerRepo.save(newOwner);
}
}

}

0 comments on commit e11d143

Please sign in to comment.