Skip to content

Instantly share code, notes, and snippets.

@jeremi
Created July 14, 2010 09:23
Show Gist options
  • Save jeremi/475234 to your computer and use it in GitHub Desktop.
Save jeremi/475234 to your computer and use it in GitHub Desktop.
Class to load random data for the Roo demo
package com.springsource.petclinic;
import com.springsource.petclinic.domain.Owner;
import com.springsource.petclinic.domain.Pet;
import com.springsource.petclinic.domain.Vet;
import com.springsource.petclinic.domain.Visit;
import com.springsource.petclinic.reference.PetType;
import com.springsource.petclinic.reference.Specialty;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
@Component
public class Loader implements ApplicationListener<ContextRefreshedEvent>{
@Override
@Transactional
public void onApplicationEvent(ContextRefreshedEvent event) {
if (event.getApplicationContext().getParent() == null) {
System.out.println(">>>>>>>> Parent started");
System.out.println("before: " + Owner.countOwners());
Vet vet = null;
Random generator = new Random();
for (int i = 0; i < 20; i++) {
if (i % 3 == 0) {
vet = new Vet();
vet.setLastName(UUID.randomUUID().toString().substring(0, 10));
vet.setFirstName(UUID.randomUUID().toString().substring(0, 10));
vet.setEmail("[email protected]");
vet.setBirthDay(new Date());
vet.setAddress("aqqqqqq");
vet.setCity("aqqqqqq");
vet.setTelephone("00000000");
vet.setHomePage("http://www.jeremi.info");
GregorianCalendar employedSince = new GregorianCalendar();
employedSince.set(2009, 10, 21);
vet.setEmployedSince(employedSince);
int randomInt = (Math.abs(generator.nextInt()) % 3);
if (randomInt == 1) {
vet.setSpecialty(Specialty.Cardiology);
} else if (randomInt == 2) {
vet.setSpecialty(Specialty.Nutrition);
} else {
vet.setSpecialty(Specialty.Dentistry);
}
vet.persist();
}
Owner owner = new Owner();
owner.setLastName(UUID.randomUUID().toString().substring(0, 10));
owner.setFirstName(UUID.randomUUID().toString().substring(0, 10));
owner.setEmail("[email protected]");
owner.setBirthDay(new Date());
owner.setAddress("aqqqqqq");
int randomInt = (Math.abs(generator.nextInt()) % 3);
if (randomInt == 1) {
owner.setCity("Paris");
} else if (randomInt == 2) {
owner.setCity("Montreuil");
} else {
owner.setCity("Boulogne");
}
owner.setTelephone("00000000");
owner.setHomePage("http://www.jeremi.info");
owner.persist();
Pet pet = new Pet();
pet.setName(UUID.randomUUID().toString());
pet.setOwner(owner);
pet.setType(PetType.Bird);
pet.setWeight(new Float(42.1));
pet.persist();
for (int j = 0; j < 3; j++) {
Visit visit = new Visit();
visit.setDescription(UUID.randomUUID().toString());
visit.setPet(pet);
visit.setVet(vet);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -(Math.abs(generator.nextInt()) % 31) - 1);
visit.setVisitDate(cal.getTime());
visit.persist();
}
}
System.out.println("after: " + Owner.countOwners());
} else {
System.out.println(">>>>>>>> Child started; ignoring");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment