Create sample data programattically

This commit is contained in:
Art O Cathain 2017-06-02 11:27:11 +01:00
parent 101c9dc690
commit 1911f805e6
7 changed files with 190 additions and 112 deletions

View file

@ -16,20 +16,31 @@
package org.springframework.samples.petclinic; package org.springframework.samples.petclinic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.samples.petclinic.system.SampleData;
/** /**
* PetClinic Spring Boot Application. * PetClinic Spring Boot Application.
* *
* @author Dave Syer * @author Dave Syer
*
*/ */
@SpringBootApplication @SpringBootApplication
public class PetClinicApplication { public class PetClinicApplication implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
private SampleData sampleData;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
SpringApplication.run(PetClinicApplication.class, args); SpringApplication.run(PetClinicApplication.class, args);
} }
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
sampleData.create();
}
} }

View file

@ -0,0 +1,157 @@
package org.springframework.samples.petclinic.system;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.owner.Owner;
import org.springframework.samples.petclinic.owner.OwnerRepository;
import org.springframework.samples.petclinic.owner.Pet;
import org.springframework.samples.petclinic.owner.PetType;
import org.springframework.samples.petclinic.vet.Specialty;
import org.springframework.samples.petclinic.vet.Vet;
import org.springframework.samples.petclinic.visit.Visit;
import org.springframework.samples.petclinic.visit.VisitRepository;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import java.time.Instant;
import java.util.Date;
import java.util.stream.Stream;
@Component
public class SampleData {
@Autowired
private OwnerRepository ownerRepository;
@Autowired
private VisitRepository visitRepository;
@Autowired
private EntityManager entityManager;
@Transactional
public void deleteAll() {
Stream.of("Visit", "Pet", "Owner", "PetType", "Vet", "Specialty")
.forEach(entityName -> entityManager.createQuery("DELETE from " + entityName).executeUpdate());
}
@Transactional
public void create() {
Specialty radiology = saveSpeciality("radiology");
Specialty surgery = saveSpeciality("surgery");
Specialty dentistry = saveSpeciality("dentistry");
saveVet("James", "Carter");
saveVet("Helen", "Leary", radiology);
saveVet("Linda", "Douglas", surgery, dentistry);
saveVet("Rafael", "Ortega", surgery);
saveVet("Henry", "Stevens", radiology);
saveVet("Sharon", "Jenkins");
PetType cat = savePetType("cat");
PetType dog = savePetType("dog");
PetType lizard = savePetType("lizard");
PetType snake = savePetType("snake");
PetType bird = savePetType("bird");
PetType hamster = savePetType("hamster");
Pet leo = createPet("Leo", "2010-09-07", cat);
Pet basil = createPet("Basil", "2012-08-06", hamster);
Pet rosy = createPet("Rosy", "2011-04-17", dog);
Pet jewel = createPet("Jewel", "2010-03-07", dog);
Pet iggy = createPet("Iggy", "2010-11-30", lizard);
Pet george = createPet("George", "2010-01-20", snake);
Pet samantha = createPet("Samantha", "2012-09-04", cat);
Pet max = createPet("Max", "2012-09-04", cat);
Pet lucky1 = createPet("Lucky", "2011-08-06", bird);
Pet mulligan = createPet("Mulligan", "2007-02-24", dog);
Pet freddy = createPet("Freddy", "2010-03-09", bird);
Pet lucky2 = createPet("Lucky", "2010-06-24", dog);
Pet sly = createPet("Sly", "2012-06-08", cat);
saveOwner("George", "Franklin", "110 W. Liberty St.", "Madison", "6085551023", leo);
saveOwner("Betty", "Davis", "638 Cardinal Ave.", "Sun Prairie", "6085551749", basil);
saveOwner("Eduardo", "Rodriquez", "2693 Commerce St.", "McFarland", "6085558763", rosy, jewel);
saveOwner("Harold", "Davis", "563 Friendly St.", "Windsor", "6085553198", iggy);
saveOwner("Peter", "McTavish", "2387 S. Fair Way", "Madison", "6085552765", george);
saveOwner("Jean", "Coleman", "105 N. Lake St.", "Monona", "6085552654", samantha, max);
saveOwner("Jeff", "Black", "1450 Oak Blvd.", "Monona", "6085555387", lucky1);
saveOwner("Maria", "Escobito", "345 Maple St.", "Madison", "6085557683", mulligan);
saveOwner("David", "Schroeder", "2749 Blackhawk Trail", "Madison", "6085559435", freddy);
saveOwner("Carlos", "Estaban", "2335 Independence La.", "Waunakee", "6085555487", lucky2, sly);
saveVisit(samantha, "rabies shot", "2013-01-01");
saveVisit(max, "rabies shot", "2013-01-02");
saveVisit(max, "neutered", "2013-01-03");
saveVisit(jewel, "spayed", "2013-01-04");
}
private void saveVisit(Pet pet, String description, String visitDate) {
Visit visit = new Visit();
visit.setDescription(description);
visit.setPetId(pet.getId());
visit.setDate(fromString(visitDate));
visitRepository.save(visit);
}
private Pet createPet(String name, String birthDate, PetType petType) {
Pet pet = new Pet();
pet.setName(name);
pet.setBirthDate(fromString(birthDate));
pet.setType(petType);
return pet;
}
private void saveOwner(String firstName, String lastName, String address, String city, String telephone, Pet... pets) {
Owner owner = new Owner();
owner.setFirstName(firstName);
owner.setLastName(lastName);
owner.setAddress(address);
owner.setCity(city);
owner.setTelephone(telephone);
Stream.of(pets).forEach(owner::addPet);
ownerRepository.save(owner);
}
private PetType savePetType(String typeName) {
PetType petType = new PetType();
petType.setName(typeName);
entityManager.persist(petType);
return petType;
}
private void saveVet(String firstName, String lastName, Specialty... specialties) {
Vet vet = new Vet();
vet.setFirstName(firstName);
vet.setLastName(lastName);
Stream.of(specialties).forEach(vet::addSpecialty);
entityManager.persist(vet);
}
private Specialty saveSpeciality(String name) {
Specialty radiology = new Specialty();
radiology.setName(name);
entityManager.persist(radiology);
return radiology;
}
private Date fromString(String dateShortIso) {
return Date.from(Instant.parse(dateShortIso + "T00:00:00Z"));
}
@Transactional
public void reset() {
deleteAll();
create();
}
}

View file

@ -1,14 +1,27 @@
package org.springframework.samples.petclinic.system; package org.springframework.samples.petclinic.system;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller @Controller
class WelcomeController { class WelcomeController {
@RequestMapping("/") @Autowired
private SampleData sampleData;
@RequestMapping(method = RequestMethod.GET, path = "/")
public String welcome() { public String welcome() {
return "welcome"; return "welcome";
} }
@RequestMapping(method = RequestMethod.POST, path = "/")
public String reset() {
LoggerFactory.getLogger(getClass()).info("Resetting");
sampleData.reset();
return "redirect:/";
}
} }

View file

@ -1,7 +1,6 @@
# database init, supports mysql too # database init, supports mysql too
database=hsqldb database=hsqldb
spring.datasource.schema=classpath*:db/${database}/schema.sql spring.datasource.schema=classpath*:db/${database}/schema.sql
spring.datasource.data=classpath*:db/${database}/data.sql
# Web # Web
spring.thymeleaf.mode=HTML spring.thymeleaf.mode=HTML

View file

@ -1,53 +0,0 @@
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 specialties VALUES (1, 'radiology');
INSERT INTO specialties VALUES (2, 'surgery');
INSERT INTO specialties VALUES (3, 'dentistry');
INSERT INTO vet_specialties VALUES (2, 1);
INSERT INTO vet_specialties VALUES (3, 2);
INSERT INTO vet_specialties VALUES (3, 3);
INSERT INTO vet_specialties VALUES (4, 2);
INSERT INTO vet_specialties VALUES (5, 1);
INSERT INTO types VALUES (1, 'cat');
INSERT INTO types VALUES (2, 'dog');
INSERT INTO types VALUES (3, 'lizard');
INSERT INTO types VALUES (4, 'snake');
INSERT INTO types VALUES (5, 'bird');
INSERT INTO types VALUES (6, 'hamster');
INSERT INTO owners VALUES (1, 'George', 'Franklin', '110 W. Liberty St.', 'Madison', '6085551023');
INSERT INTO owners VALUES (2, 'Betty', 'Davis', '638 Cardinal Ave.', 'Sun Prairie', '6085551749');
INSERT INTO owners VALUES (3, 'Eduardo', 'Rodriquez', '2693 Commerce St.', 'McFarland', '6085558763');
INSERT INTO owners VALUES (4, 'Harold', 'Davis', '563 Friendly St.', 'Windsor', '6085553198');
INSERT INTO owners VALUES (5, 'Peter', 'McTavish', '2387 S. Fair Way', 'Madison', '6085552765');
INSERT INTO owners VALUES (6, 'Jean', 'Coleman', '105 N. Lake St.', 'Monona', '6085552654');
INSERT INTO owners VALUES (7, 'Jeff', 'Black', '1450 Oak Blvd.', 'Monona', '6085555387');
INSERT INTO owners VALUES (8, 'Maria', 'Escobito', '345 Maple St.', 'Madison', '6085557683');
INSERT INTO owners VALUES (9, 'David', 'Schroeder', '2749 Blackhawk Trail', 'Madison', '6085559435');
INSERT INTO owners VALUES (10, 'Carlos', 'Estaban', '2335 Independence La.', 'Waunakee', '6085555487');
INSERT INTO pets VALUES (1, 'Leo', '2010-09-07', 1, 1);
INSERT INTO pets VALUES (2, 'Basil', '2012-08-06', 6, 2);
INSERT INTO pets VALUES (3, 'Rosy', '2011-04-17', 2, 3);
INSERT INTO pets VALUES (4, 'Jewel', '2010-03-07', 2, 3);
INSERT INTO pets VALUES (5, 'Iggy', '2010-11-30', 3, 4);
INSERT INTO pets VALUES (6, 'George', '2010-01-20', 4, 5);
INSERT INTO pets VALUES (7, 'Samantha', '2012-09-04', 1, 6);
INSERT INTO pets VALUES (8, 'Max', '2012-09-04', 1, 6);
INSERT INTO pets VALUES (9, 'Lucky', '2011-08-06', 5, 7);
INSERT INTO pets VALUES (10, 'Mulligan', '2007-02-24', 2, 8);
INSERT INTO pets VALUES (11, 'Freddy', '2010-03-09', 5, 9);
INSERT INTO pets VALUES (12, 'Lucky', '2010-06-24', 2, 10);
INSERT INTO pets VALUES (13, 'Sly', '2012-06-08', 1, 10);
INSERT INTO visits VALUES (1, 7, '2013-01-01', 'rabies shot');
INSERT INTO visits VALUES (2, 8, '2013-01-02', 'rabies shot');
INSERT INTO visits VALUES (3, 8, '2013-01-03', 'neutered');
INSERT INTO visits VALUES (4, 7, '2013-01-04', 'spayed');

View file

@ -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');

View file

@ -11,6 +11,10 @@
</div> </div>
</div> </div>
<form method="post">
<button class="btn btn-default" >Reset</button>
</form>
</body> </body>
</html> </html>