@Test public void testTableCreationAndPersist() throws SQLException, ClassNotFoundException { Class.forName("org.h2.Driver"); String databaseUrl = "jdbc:h2:res\\test\\db\\ormLiteDb"; ConnectionSource cs = new JdbcConnectionSource(databaseUrl); TableUtils.createTableIfNotExists(cs, Event.class); TableUtils.clearTable(cs, Event.class); int count = 10; String namePrefix = "Testinimi"; Dao<Event, Integer> eventDao = DaoManager.createDao(cs, Event.class); for (int i = 0; i < count; i++) { Event event = new Event(); event.setTitle(namePrefix + i); eventDao.create(event); } assertTrue(eventDao.countOf() == count); for (Iterator<Event> it = eventDao.iterator(); it.hasNext(); ) { Event event = it.next(); assertTrue(event.getTitle().startsWith(namePrefix)); } ; cs.close(); }
public void addClientUser(TwitterSession twitterSession, CreateCompletedListener addedListener) { ConnectionSource connectionSource = null; try { DataBaseHelper helper = new DataBaseHelper(Global.getInstance().getApplicationContext()); connectionSource = helper.getConnectionSource(); TableUtils.createTableIfNotExists(connectionSource, ClientUserTable.class); Dao<ClientUserTable, String> dao = helper.getDao(ClientUserTable.class); ClientUserTable table1 = dao.queryForId("" + twitterSession.getUserId()); if (table1 == null) { ClientUserTable table = new ClientUserTable(twitterSession); dao.createOrUpdate(table); new ClientUser( clientUsers.size(), twitterSession, clientUser -> { clientUsers.add(clientUser); addedListener.completed(clientUser); }, TwitterException::printStackTrace); } else { addedListener.completed(null); } } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } finally { if (connectionSource != null) { try { connectionSource.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
@DefaultHandler public Resolution defaultResolution() throws AjaxException { log.info("FiltersDataAction"); try { conn = EntityManager.getConnection(); Dao<Filter, String> dao = DaoManager.createDao(conn, Filter.class); String string = new ObjectMapper() .configure(Feature.DEFAULT_VIEW_INCLUSION, false) .writerWithView(JsonViews.Filters.class) .writeValueAsString(dao.queryForEq("views_id", getView().getId())); log.info(string); return new StreamingResolution("text/json", string); } catch (Exception e) { e.printStackTrace(); getContext() .getValidationErrors() .add("entity", new SimpleError("unknown exception [" + e.getMessage() + "]")); throw new AjaxException(this, "filters", "data error"); } finally { try { conn.close(); } catch (SQLException e) { } } }
public void show() throws SQLException { Dao<Account, String> accountDao = connect(); List<Account> list = accountDao.queryForAll(); for (Account ball : list) System.out.println(ball.getText() + " " + ball.getPrioritet()); connectionSource.close(); }
public void closeConnection() { if (connectionSource != null) { try { connectionSource.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public void remove() throws SQLException { Scanner sc = new Scanner(System.in); String id = sc.next(); Dao<Account, String> accountDao = connect(); accountDao.delete(accountDao.queryForId(id)); System.out.println("Enter some string to continue"); sc.next(); connectionSource.close(); }
public static void loadUsers(LoadCompletedListener loadCompletedListener) { if (!isLoaded) { isLoaded = true; ConnectionSource connectionSource = null; try { DataBaseHelper helper = new DataBaseHelper(Global.getInstance().getApplicationContext()); connectionSource = helper.getConnectionSource(); TableUtils.createTableIfNotExists(connectionSource, ClientUserTable.class); Dao<ClientUserTable, String> dao = helper.getDao(ClientUserTable.class); List<ClientUserTable> tables = dao.queryForAll(); int i = 0; for (ClientUserTable table : tables) { TwitterAuthToken twitterAuthToken = new TwitterAuthToken(table.getAuthToken(), table.getAuthTokenSecret()); TwitterSession session = new TwitterSession(twitterAuthToken, table.getUserId(), table.getUserName()); new ClientUser( i, session, user -> { clientUsers.add(user); confirmationLoadComplete(clientUsers, tables, loadCompletedListener); }, e -> { clientUsers.add(null); MainActivity mainActivity = ((MainActivity) Global.getInstance().getMainActivityContext()); if (mainActivity != null && mainActivity.isDestroyed()) mainActivity.sendToast(e.getMessage()); confirmationLoadComplete(clientUsers, tables, loadCompletedListener); }); i++; } } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } finally { if (connectionSource != null) { try { connectionSource.close(); } catch (SQLException e) { e.printStackTrace(); } } } } else { loadCompletedListener.completed(clientUsers); } }
public void write() throws SQLException { Scanner sc = new Scanner(System.in); System.out.println("Text"); String text = sc.nextLine(); System.out.println("Prioritet"); int prioritet = sc.nextInt(); Account account = new Account(); account.setText(text); account.setPrioritet(prioritet); Dao<Account, String> accountDao = connect(); accountDao.create(account); System.out.println("Enter some string to continue"); sc.next(); connectionSource.close(); }
public int size() { ConnectionSource connectionSource = null; try { DataBaseHelper helper = new DataBaseHelper(Global.getInstance().getApplicationContext()); connectionSource = helper.getConnectionSource(); TableUtils.createTableIfNotExists(connectionSource, ClientUserTable.class); Dao<ClientUserTable, String> dao = helper.getDao(ClientUserTable.class); List<ClientUserTable> tables = dao.queryForAll(); return tables.size(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } finally { if (connectionSource != null) { try { connectionSource.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
public Resolution register() throws AjaxException, RegisterException { log.info("RegisterAction.register() - Creating new user / userview"); // Object nextPage = getContext().getRequest().getSession().getAttribute("nextPage"); ConnectionSource conn = null; try { conn = EntityManager.getConnection(); /** Check that the view allows creating users */ Dao<View, String> vDao = DaoManager.createDao(conn, View.class); View view = vDao.queryForId(getView().getId().toString()); if (!view.getCreateUser()) { log.info(" View does not allow creating new users"); throw new AjaxException(this, "userView", "Create User Disabled"); } Dao<UserView, String> uvDao = DaoManager.createDao(conn, UserView.class); Dao<User, String> uDao = DaoManager.createDao(conn, User.class); /** * Try to create a user and userview If this fails there is likely already a user with the * same name and account */ try { log.info(" Creating new user in system: " + getAccount().getAlias()); User user = new User(); user.setAccount(getAccount()); user.setEmail(getEmail()); user.setName(getUsername()); user.setPassword(getPassword()); uDao.create(user); log.info(" Creating new userview in for: " + getView().getAlias()); UserView userView = new UserView(); userView.setUser(user); userView.setView(getView()); uvDao.create(userView); getContext().setUser(user); getContext().setAccessLevel(MyActionBeanContext.ACCESS_LEVEL_PASSWORD); /** User exists but nextPage was not specified. Redirect to the manager page */ if (getContext().getUser() != null && nextPage == null) { log.info(" User exists but nextpage is null - redirect to ManagerAction"); RedirectResolution resolution = new RedirectResolution(ManagerAction.class).includeRequestParameters(true); return resolution; } log.info(" Login successful - redirect to " + nextPage); RedirectResolution resolution = new RedirectResolution(getNextPage()); return resolution; } catch (SQLException x) { log.error("RegisterException", x); return new ErrorResolution( HttpServletResponse.SC_PRECONDITION_FAILED, "{\"result\":\"error\",\"message\":\"" + x.getMessage() + "\"}"); } } catch (Exception x) { x.printStackTrace(); log.error("RegisterException", x); getContext().getValidationErrors().add("entity", new SimpleError("create user error")); throw new RegisterException(this, getAccount(), getView(), "Exception encountered"); } finally { try { conn.close(); } catch (SQLException e) { } } }