// create a new site
Site newSite = SiteService.get().newSite();
newSite.setName("Site_" + user.getUsername()); // make this unique
// create a new period
Period newPeriod = SiteService.get().newPeriod();
newPeriod.setSite(newSite);
newPeriod.setName("Class_" + user.getUsername()); // make this unique
// add the period to the user and the site
SortedSet<Period> periodSet = new TreeSet<Period>();
periodSet.add(newPeriod);
user.getPeriods().add(newPeriod);
newSite.getPeriods().add(newPeriod);
// a default student must be added to the new period
User studentUser = UserService.get().newUser();
studentUser.setRole(Role.STUDENT);
studentUser.setFirstName("Student");
studentUser.setLastName(newPeriod.getName());
studentUser.setUsername(newPeriod.getName());
studentUser.getPeriods().add(newPeriod);
studentUser.setValid(true);
studentUser.setCreateDate(new Date());
// create a random number for the password
Random randomNumber = new Random();
Integer studentPasswordInteger = randomNumber.nextInt();
studentPassword = studentPasswordInteger.toString().substring(0, 6);
log.debug("this is the student password : {}", studentPassword);
studentUser.setPassword(studentPassword);
// add the teacher and the default student to the new default class
userSet.add(studentUser);
newPeriod.setUsers(userSet);
Databinder.getHibernateSession().save(newSite);
Databinder.getHibernateSession().save(newPeriod);
Databinder.getHibernateSession().save(studentUser);
}