public class DatabaseServiceImpl implements DatabaseService { EntityManager em = JPAContainerFactory.createEntityManagerForPersistenceUnit( TwentyFourConstants.PERSISTENCE_UNIT); @Override public void init() { if (isInitiated()) { System.out.println("The database is already initiated."); return; } System.out.println("Initiating database for the first time."); addUsers(); markAsInitiated(); } private boolean isInitiated() { boolean initiated = false; CriteriaQuery<Properties> query = em.getCriteriaBuilder().createQuery(Properties.class); List<Properties> propertiesList = em.createQuery(query.select(query.from(Properties.class))).getResultList(); for (Properties properties : propertiesList) { if (properties.getDb().toString().equals("basic") && properties.getState().toString().equals("Initiated")) { initiated = true; } } return initiated; } private void addUsers() { UserServiceImpl service = new UserServiceImpl(); service.addUser("chgri", "Chris", "Grivas", Role.ADMIN); } private void markAsInitiated() { Properties twentyFourDatabaseState = new Properties(); twentyFourDatabaseState.setDb("basic"); twentyFourDatabaseState.setState("Initiated"); EntityTransaction tx = em.getTransaction(); tx.begin(); em.persist(twentyFourDatabaseState); tx.commit(); } }
@Theme("mytheme") @Widgetset("vist.stat.vaadinjpacontainertest.MyAppWidgetset") public class MyUI extends UI { private final JPAContainer<Reference> jpaPost = JPAContainerFactory.make(Reference.class, "vist.stat_VaadinJPAContainerTest_war_1.0PU"); private final EntityManager em = jpaPost.getEntityProvider().getEntityManager(); @Override protected void init(VaadinRequest vaadinRequest) { final VerticalLayout layout = new VerticalLayout(); layout.setMargin(true); setContent(layout); Button button = new Button("Click Me"); // button.addClickListener(new Button.ClickListener() { // @Override // public void buttonClick(ClickEvent event) { // Query qwr = em.createNamedQuery("Reference.findActual"); // qwr.setParameter("status", RecStatus.ACTIVE); // List<Reference> resultList = qwr.getResultList(); // for (Reference post : resultList) // layout.addComponent(new Label(post.getName())); // } // }); layout.addComponent(showGrid()); layout.addComponent(button); } @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true) @VaadinServletConfiguration(ui = MyUI.class, productionMode = false) public static class MyUIServlet extends VaadinServlet {} private Grid showGrid() { Grid grid = new Grid("Справочник", jpaPost); grid.setSizeFull(); return grid; } }
@BeforeClass public static void init(){ changements = JPAContainerFactory.makeJndi(Changement.class); }