webpiecesxxxxxpackage.base.PopulateDatabase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of WEBPIECESxAPPNAME Show documentation
Show all versions of WEBPIECESxAPPNAME Show documentation
Someone forgot to fill this in. See http://stackoverflow.com/questions/38272550/how-to-fail-the-gradle-build-if-subproject-is-missing-a-property
package webpiecesxxxxxpackage.base;
import java.util.List;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import org.webpieces.router.api.extensions.Startable;
import webpiecesxxxxxpackage.db.EducationEnum;
import webpiecesxxxxxpackage.db.RoleEnum;
import webpiecesxxxxxpackage.db.UserDbo;
import webpiecesxxxxxpackage.db.UserRole;
public class PopulateDatabase implements Startable {
//private static final Logger log = LoggerFactory.getLogger(PopulateDatabase.class);
private EntityManagerFactory factory;
@Inject
public PopulateDatabase(EntityManagerFactory factory) {
this.factory = factory;
}
@Override
public void start() {
createSomeData();
}
private void createSomeData() {
EntityManager mgr = factory.createEntityManager();
List users = UserDbo.findAll(mgr);
if(users.size() > 0)
return; //This database has users, exit immediately to not screw up existing data
EntityTransaction tx = mgr.getTransaction();
tx.begin();
UserDbo user1 = new UserDbo();
user1.setEmail("[email protected]");
user1.setName("SomeName");
user1.setFirstName("Dean");
user1.setLastName("Hill");
UserDbo user2 = new UserDbo();
user2.setEmail("[email protected]");
user2.setName("Bob'sName");
user2.setFirstName("Bob");
user2.setLastName("LastBob");
user2.setLevelOfEducation(EducationEnum.MIDDLE_SCHOOL);
UserRole role1 = new UserRole(user2, RoleEnum.DELINQUINT);
UserRole role2 = new UserRole(user2, RoleEnum.BADASS);
mgr.persist(user1);
mgr.persist(user2);
mgr.persist(role1);
mgr.persist(role2);
mgr.flush();
tx.commit();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy