You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SampleDB.java 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.demo.reservation;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11. import java.util.Calendar;
  12. import java.util.Collection;
  13. import java.util.Date;
  14. import java.util.Iterator;
  15. import java.util.List;
  16. import com.itmill.toolkit.data.Container;
  17. import com.itmill.toolkit.data.Item;
  18. import com.itmill.toolkit.data.util.QueryContainer;
  19. public class SampleDB {
  20. public class User {
  21. public static final String TABLE = "USER";
  22. public static final String PROPERTY_ID_ID = TABLE + "_ID";
  23. public static final String PROPERTY_ID_FULLNAME = TABLE + "_FULLNAME";
  24. public static final String PROPERTY_ID_EMAIL = TABLE + "_EMAIL";
  25. public static final String PROPERTY_ID_PASSWORD = TABLE + "_PASSWORD";
  26. public static final String PROPERTY_ID_DELETED = TABLE + "_DELETED";
  27. }
  28. public class Resource {
  29. public static final String TABLE = "RESOURCE";
  30. public static final String PROPERTY_ID_ID = TABLE + "_ID";
  31. public static final String PROPERTY_ID_STYLENAME = TABLE + "_STYLENAME";
  32. public static final String PROPERTY_ID_NAME = TABLE + "_NAME";
  33. public static final String PROPERTY_ID_DESCRIPTION = TABLE
  34. + "_DESCRIPTION";
  35. public static final String PROPERTY_ID_LOCATIONX = TABLE
  36. + "_LOCATION_X";
  37. public static final String PROPERTY_ID_LOCATIONY = TABLE
  38. + "_LOCATION_Y";
  39. public static final String PROPERTY_ID_CATEGORY = TABLE + "_CATEGORY";
  40. public static final String PROPERTY_ID_DELETED = TABLE + "_DELETED";
  41. }
  42. public class Reservation {
  43. public static final String TABLE = "RESERVATION";
  44. public static final String PROPERTY_ID_ID = TABLE + "_ID";
  45. public static final String PROPERTY_ID_DESCRIPTION = TABLE
  46. + "_DESCRIPTION";
  47. public static final String PROPERTY_ID_RESOURCE_ID = TABLE
  48. + "_RESOURCE_ID";
  49. public static final String PROPERTY_ID_RESERVED_BY_ID = TABLE
  50. + "_RESERVED_BY_USER_ID";
  51. public static final String PROPERTY_ID_RESERVED_FROM = TABLE
  52. + "_RESERVED_FROM";
  53. public static final String PROPERTY_ID_RESERVED_TO = TABLE
  54. + "_RESERVED_TO";
  55. }
  56. // TODO -> param
  57. private static final String DB_URL = "jdbc:hsqldb:file:reservation.db";
  58. private static final String CREATE_TABLE_USER = "CREATE TABLE "
  59. + User.TABLE + " (" + " " + User.PROPERTY_ID_ID
  60. + " INTEGER IDENTITY" + ", " + User.PROPERTY_ID_FULLNAME
  61. + " VARCHAR(100) NOT NULL" + ", " + User.PROPERTY_ID_EMAIL
  62. + " VARCHAR(50) NOT NULL" + ", " + User.PROPERTY_ID_PASSWORD
  63. + " VARCHAR(20) NOT NULL" + ", " + User.PROPERTY_ID_DELETED
  64. + " BOOLEAN DEFAULT false NOT NULL" + ", UNIQUE("
  65. + User.PROPERTY_ID_FULLNAME + "), UNIQUE(" + User.PROPERTY_ID_EMAIL
  66. + ") )";
  67. private static final String CREATE_TABLE_RESOURCE = "CREATE TABLE "
  68. + Resource.TABLE + " (" + " " + Resource.PROPERTY_ID_ID
  69. + " INTEGER IDENTITY" + ", " + Resource.PROPERTY_ID_STYLENAME
  70. + " VARCHAR(20) NOT NULL" + ", " + Resource.PROPERTY_ID_NAME
  71. + " VARCHAR(30) NOT NULL" + ", " + Resource.PROPERTY_ID_DESCRIPTION
  72. + " VARCHAR(100)" + ", " + Resource.PROPERTY_ID_LOCATIONX
  73. + " DOUBLE" + ", " + Resource.PROPERTY_ID_LOCATIONY + " DOUBLE"
  74. + ", " + Resource.PROPERTY_ID_CATEGORY + " VARCHAR(30)" + ", "
  75. + Resource.PROPERTY_ID_DELETED + " BOOLEAN DEFAULT false NOT NULL"
  76. + ", UNIQUE(" + Resource.PROPERTY_ID_NAME + "))";
  77. private static final String CREATE_TABLE_RESERVATION = "CREATE TABLE "
  78. + Reservation.TABLE + " (" + " " + Reservation.PROPERTY_ID_ID
  79. + " INTEGER IDENTITY" + ", " + Reservation.PROPERTY_ID_RESOURCE_ID
  80. + " INTEGER" + ", " + Reservation.PROPERTY_ID_RESERVED_BY_ID
  81. + " INTEGER" + ", " + Reservation.PROPERTY_ID_RESERVED_FROM
  82. + " TIMESTAMP NOT NULL" + ", "
  83. + Reservation.PROPERTY_ID_RESERVED_TO + " TIMESTAMP NOT NULL"
  84. + ", " + Reservation.PROPERTY_ID_DESCRIPTION + " VARCHAR(100)"
  85. + ", FOREIGN KEY (" + Reservation.PROPERTY_ID_RESOURCE_ID
  86. + ") REFERENCES " + Resource.TABLE + "(" + Resource.PROPERTY_ID_ID
  87. + "), FOREIGN KEY (" + Reservation.PROPERTY_ID_RESERVED_BY_ID
  88. + ") REFERENCES " + User.TABLE + "(" + User.PROPERTY_ID_ID + "))";
  89. private Connection connection = null;
  90. /**
  91. * Create database.
  92. */
  93. public SampleDB() {
  94. this(false);
  95. }
  96. public SampleDB(boolean recreate) {
  97. // connect to SQL database
  98. connect();
  99. if (recreate) {
  100. dropTables();
  101. }
  102. // initialize SQL database
  103. createTables();
  104. // test by executing sample JDBC query
  105. testDatabase();
  106. }
  107. private void dropTables() {
  108. try {
  109. update("DROP TABLE " + Reservation.TABLE);
  110. } catch (final SQLException IGNORED) {
  111. // IGNORED, assuming it was not there
  112. }
  113. try {
  114. update("DROP TABLE " + Resource.TABLE);
  115. } catch (final SQLException IGNORED) {
  116. // IGNORED, assuming it was not there
  117. }
  118. try {
  119. update("DROP TABLE " + User.TABLE);
  120. } catch (final SQLException IGNORED) {
  121. // IGNORED, assuming it was not there
  122. }
  123. }
  124. /**
  125. * Connect to SQL database. In this sample we use HSQLDB and an toolkit
  126. * named database in implicitly created into system memory.
  127. *
  128. */
  129. private void connect() {
  130. try {
  131. Class.forName("org.hsqldb.jdbcDriver").newInstance();
  132. connection = DriverManager.getConnection(DB_URL);
  133. } catch (final Exception e) {
  134. throw new RuntimeException(e);
  135. }
  136. }
  137. /**
  138. * use for SQL commands CREATE, DROP, INSERT and UPDATE
  139. *
  140. * @param expression
  141. * @throws SQLException
  142. */
  143. private void update(String expression) throws SQLException {
  144. Statement st = null;
  145. st = connection.createStatement();
  146. final int i = st.executeUpdate(expression);
  147. if (i == -1) {
  148. System.out.println("SampleDatabase error : " + expression);
  149. }
  150. st.close();
  151. }
  152. /**
  153. * Create test table and few rows. Issue note: using capitalized column
  154. * names as HSQLDB returns column names in capitalized form with this demo.
  155. *
  156. */
  157. private void createTables() {
  158. try {
  159. String stmt = null;
  160. stmt = CREATE_TABLE_RESOURCE;
  161. update(stmt);
  162. } catch (final SQLException e) {
  163. if (e.toString().indexOf("Table already exists") == -1) {
  164. throw new RuntimeException(e);
  165. }
  166. }
  167. try {
  168. String stmt = null;
  169. stmt = CREATE_TABLE_USER;
  170. update(stmt);
  171. } catch (final SQLException e) {
  172. if (e.toString().indexOf("Table already exists") == -1) {
  173. throw new RuntimeException(e);
  174. }
  175. }
  176. try {
  177. String stmt = null;
  178. stmt = CREATE_TABLE_RESERVATION;
  179. update(stmt);
  180. } catch (final SQLException e) {
  181. if (e.toString().indexOf("Table already exists") == -1) {
  182. throw new RuntimeException(e);
  183. }
  184. }
  185. }
  186. /**
  187. * Test database connection with simple SELECT command.
  188. *
  189. */
  190. private String testDatabase() {
  191. String result = null;
  192. try {
  193. final Statement stmt = connection.createStatement(
  194. ResultSet.TYPE_SCROLL_INSENSITIVE,
  195. ResultSet.CONCUR_UPDATABLE);
  196. final ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM "
  197. + Resource.TABLE);
  198. rs.next();
  199. result = "rowcount for table test is " + rs.getObject(1).toString();
  200. stmt.close();
  201. } catch (final SQLException e) {
  202. throw new RuntimeException(e);
  203. }
  204. return result;
  205. }
  206. public Connection getConnection() {
  207. return connection;
  208. }
  209. public Container getCategories() {
  210. // TODO where deleted=?
  211. final String q = "SELECT DISTINCT(" + Resource.PROPERTY_ID_CATEGORY
  212. + ") FROM " + Resource.TABLE + " ORDER BY "
  213. + Resource.PROPERTY_ID_CATEGORY;
  214. try {
  215. return new QueryContainer(q, connection,
  216. ResultSet.TYPE_SCROLL_INSENSITIVE,
  217. ResultSet.CONCUR_READ_ONLY);
  218. } catch (final SQLException e) {
  219. throw new RuntimeException(e);
  220. }
  221. }
  222. public Container getResources(String category) {
  223. // TODO where deleted=?
  224. String q = "SELECT * FROM " + Resource.TABLE;
  225. if (category != null) {
  226. q += " WHERE " + Resource.PROPERTY_ID_CATEGORY + "='" + category
  227. + "'"; // FIXME ->
  228. // PreparedStatement!
  229. }
  230. try {
  231. return new QueryContainer(q, connection,
  232. ResultSet.TYPE_SCROLL_INSENSITIVE,
  233. ResultSet.CONCUR_READ_ONLY);
  234. } catch (final SQLException e) {
  235. throw new RuntimeException(e);
  236. }
  237. }
  238. public Container getReservations(List resources) {
  239. // TODO where reserved_by=?
  240. // TODO where from=?
  241. // TODO where to=?
  242. // TODO where deleted=?
  243. String q = "SELECT * FROM " + Reservation.TABLE + "," + Resource.TABLE;
  244. q += " WHERE " + Reservation.PROPERTY_ID_RESOURCE_ID + "="
  245. + Resource.PROPERTY_ID_ID;
  246. if (resources != null && resources.size() > 0) {
  247. final StringBuffer s = new StringBuffer();
  248. for (final Iterator it = resources.iterator(); it.hasNext();) {
  249. if (s.length() > 0) {
  250. s.append(",");
  251. }
  252. s.append(((Item) it.next())
  253. .getItemProperty(Resource.PROPERTY_ID_ID));
  254. }
  255. q += " HAVING " + Reservation.PROPERTY_ID_RESOURCE_ID + " IN (" + s
  256. + ")";
  257. }
  258. q += " ORDER BY " + Reservation.PROPERTY_ID_RESERVED_FROM;
  259. try {
  260. final QueryContainer qc = new QueryContainer(q, connection,
  261. ResultSet.TYPE_SCROLL_INSENSITIVE,
  262. ResultSet.CONCUR_READ_ONLY);
  263. if (qc.size() < 1) {
  264. return null;
  265. } else {
  266. return qc;
  267. }
  268. } catch (final SQLException e) {
  269. throw new RuntimeException(e);
  270. }
  271. }
  272. public void addReservation(Item resource, int reservedById,
  273. Date reservedFrom, Date reservedTo, String description) {
  274. if (reservedFrom.after(reservedTo)) {
  275. final Date tmp = reservedTo;
  276. reservedTo = reservedFrom;
  277. reservedFrom = tmp;
  278. }
  279. final int resourceId = ((Integer) resource.getItemProperty(
  280. Resource.PROPERTY_ID_ID).getValue()).intValue();
  281. final String q = "INSERT INTO " + Reservation.TABLE + " ("
  282. + Reservation.PROPERTY_ID_RESOURCE_ID + ","
  283. + Reservation.PROPERTY_ID_RESERVED_BY_ID + ","
  284. + Reservation.PROPERTY_ID_RESERVED_FROM + ","
  285. + Reservation.PROPERTY_ID_RESERVED_TO + ","
  286. + Reservation.PROPERTY_ID_DESCRIPTION + ")"
  287. + "VALUES (?,?,?,?,?)";
  288. synchronized (DB_URL) {
  289. try {
  290. if (!isAvailableResource(resourceId, reservedFrom, reservedTo)) {
  291. throw new ResourceNotAvailableException(
  292. "The resource is not available at that time.");
  293. }
  294. final PreparedStatement p = connection.prepareStatement(q);
  295. p.setInt(1, resourceId);
  296. p.setInt(2, reservedById);
  297. p.setTimestamp(3,
  298. new java.sql.Timestamp(reservedFrom.getTime()));
  299. p.setTimestamp(4, new java.sql.Timestamp(reservedTo.getTime()));
  300. p.setString(5, description);
  301. p.execute();
  302. } catch (final Exception e) {
  303. throw new RuntimeException(e);
  304. }
  305. }
  306. }
  307. public boolean isAvailableResource(int resourceId, Date reservedFrom,
  308. Date reservedTo) {
  309. // TODO where deleted=?
  310. if (reservedFrom.after(reservedTo)) {
  311. final Date tmp = reservedTo;
  312. reservedTo = reservedFrom;
  313. reservedFrom = tmp;
  314. }
  315. final String checkQ = "SELECT count(*) FROM " + Reservation.TABLE
  316. + " WHERE " + Reservation.PROPERTY_ID_RESOURCE_ID + "=? AND (("
  317. + Reservation.PROPERTY_ID_RESERVED_FROM + ">=? AND "
  318. + Reservation.PROPERTY_ID_RESERVED_FROM + "<?) OR ("
  319. + Reservation.PROPERTY_ID_RESERVED_TO + ">? AND "
  320. + Reservation.PROPERTY_ID_RESERVED_TO + "<=?) OR ("
  321. + Reservation.PROPERTY_ID_RESERVED_FROM + "<=? AND "
  322. + Reservation.PROPERTY_ID_RESERVED_TO + ">=?)" + ")";
  323. try {
  324. final PreparedStatement p = connection.prepareStatement(checkQ);
  325. p.setInt(1, resourceId);
  326. p.setTimestamp(2, new java.sql.Timestamp(reservedFrom.getTime()));
  327. p.setTimestamp(3, new java.sql.Timestamp(reservedTo.getTime()));
  328. p.setTimestamp(4, new java.sql.Timestamp(reservedFrom.getTime()));
  329. p.setTimestamp(5, new java.sql.Timestamp(reservedTo.getTime()));
  330. p.setTimestamp(6, new java.sql.Timestamp(reservedFrom.getTime()));
  331. p.setTimestamp(7, new java.sql.Timestamp(reservedTo.getTime()));
  332. p.execute();
  333. final ResultSet rs = p.getResultSet();
  334. if (rs.next() && rs.getInt(1) > 0) {
  335. return false;
  336. }
  337. } catch (final Exception e) {
  338. throw new RuntimeException(e);
  339. }
  340. return true;
  341. }
  342. public Container getUsers() {
  343. // TODO where deleted=?
  344. final String q = "SELECT * FROM " + User.TABLE + " ORDER BY "
  345. + User.PROPERTY_ID_FULLNAME;
  346. try {
  347. final QueryContainer qc = new QueryContainer(q, connection,
  348. ResultSet.TYPE_SCROLL_INSENSITIVE,
  349. ResultSet.CONCUR_READ_ONLY);
  350. return qc;
  351. } catch (final SQLException e) {
  352. throw new RuntimeException(e);
  353. }
  354. }
  355. public void generateReservations() {
  356. final int days = 30;
  357. final String descriptions[] = { "Picking up guests from airport",
  358. "Sightseeing with the guests",
  359. "Moving new servers from A to B", "Shopping",
  360. "Customer meeting", "Guests arriving at harbour",
  361. "Moving furniture", "Taking guests to see town" };
  362. final Container cat = getCategories();
  363. final Collection cIds = cat.getItemIds();
  364. for (final Iterator it = cIds.iterator(); it.hasNext();) {
  365. final Object id = it.next();
  366. final Item ci = cat.getItem(id);
  367. final String c = (String) ci.getItemProperty(
  368. Resource.PROPERTY_ID_CATEGORY).getValue();
  369. final Container resources = getResources(c);
  370. final Collection rIds = resources.getItemIds();
  371. final Calendar cal = Calendar.getInstance();
  372. cal.set(Calendar.MINUTE, 0);
  373. cal.set(Calendar.SECOND, 0);
  374. cal.set(Calendar.MILLISECOND, 0);
  375. final int hourNow = new Date().getHours();
  376. // cal.add(Calendar.DAY_OF_MONTH, -days);
  377. for (int i = 0; i < days; i++) {
  378. int r = 3;
  379. for (final Iterator rit = rIds.iterator(); rit.hasNext()
  380. && r > 0; r--) {
  381. final Object rid = rit.next();
  382. final Item resource = resources.getItem(rid);
  383. final int s = hourNow - 6
  384. + (int) Math.round(Math.random() * 6.0);
  385. final int e = s + 1 + (int) Math.round(Math.random() * 4.0);
  386. final Date start = new Date(cal.getTimeInMillis());
  387. start.setHours(s);
  388. final Date end = new Date(cal.getTimeInMillis());
  389. end.setHours(e);
  390. addReservation(resource, 0, start, end,
  391. descriptions[(int) Math.floor(Math.random()
  392. * descriptions.length)]);
  393. }
  394. cal.add(Calendar.DATE, 1);
  395. }
  396. }
  397. }
  398. public void generateResources() {
  399. final Object[][] resources = {
  400. // Turku
  401. { "01", "01 Ford Mondeo", "w/ company logo", "Turku",
  402. new Double(60.510857), new Double(22.275424) },
  403. { "02", "02 Citroen Jumper",
  404. "w/ company logo. 12m3 storage space.", "Turku",
  405. new Double(60.452171), new Double(22.2995) },
  406. { "03", "03 Saab 93", "Cabriolet. Keys from the rental desk.",
  407. "Turku", new Double(60.4507), new Double(22.295551) },
  408. { "04", "04 Volvo S60", "Key from the rental desk.", "Turku",
  409. new Double(60.434722), new Double(22.224398) },
  410. { "05", "05 Smart fourtwo", "Cabrio. Keys from infodesk.",
  411. "Turku", new Double(60.508970), new Double(22.264790) },
  412. // Helsinki
  413. { "06", "06 Smart fourtwo", "Cabrio. Keys from infodesk.",
  414. "Helsinki", new Double(60.17175), new Double(24.939029) },
  415. { "07", "07 Smart fourtwo", "Cabrio. Keys from infodesk.",
  416. "Helsinki", new Double(60.17175), new Double(24.939029) },
  417. { "08", "08 Smart fourtwo", "Cabrio. Keys from infodesk.",
  418. "Helsinki", new Double(60.166579),
  419. new Double(24.953899) },
  420. { "09", "09 Volvo S60", "Keys from infodesk.", "Helsinki",
  421. new Double(60.317832), new Double(24.967289) },
  422. { "10", "10 Saab 93", "Keys from infodesk.", "Helsinki",
  423. new Double(60.249193), new Double(25.045921) },
  424. // Silicon Valley
  425. { "11", "11 Ford Mustang", "Keys from Acme clerk.",
  426. "Silicon Valley", new Double(37.615853),
  427. new Double(-122.386384) },
  428. { "12", "12 Ford Fusion", "Keys from infodesk.",
  429. "Silicon Valley", new Double(37.365028),
  430. new Double(-121.922654) },
  431. { "13", "13 Land Rover", "Keys from infodesk.",
  432. "Silicon Valley", new Double(37.365028),
  433. new Double(-121.922654) },
  434. { "14", "14 Land Rover", "Keys from infodesk.",
  435. "Silicon Valley", new Double(37.365028),
  436. new Double(-121.922654) },
  437. { "15", "15 Ford Mustang", "GT Cal Special. Keys from guard.",
  438. "Silicon Valley", new Double(37.403812),
  439. new Double(-121.977425) },
  440. { "16", "16 Ford Focus", "Keys from guard.", "Silicon Valley",
  441. new Double(37.403812), new Double(-121.977425) },
  442. // Paris
  443. { "17", "17 Peugeot 308", "Keys from infodesk.", "Paris",
  444. new Double(48.844756), new Double(2.372784) },
  445. { "18", "18 Citroen C6", "Keys from rental desk.", "Paris",
  446. new Double(49.007253), new Double(2.545025) },
  447. { "19", "19 Citroen C6", "Keys from infodesk.", "Paris",
  448. new Double(48.729061), new Double(2.368087) },
  449. { "20", "20 Peugeot 308", "Keys from ticket sales.", "Paris",
  450. new Double(48.880931), new Double(2.356988) },
  451. { "21", "21 Peugeot 308", "Keys from ticket sales.", "Paris",
  452. new Double(48.876479), new Double(2.358161) },
  453. // STHLM
  454. { "22", "22 Volvo S60", "Keys from infodesk.", "Stockholm",
  455. new Double(59.350414), new Double(18.106574) },
  456. { "23", "23 Saab 93", "Keys from infodesk.", "Stockholm",
  457. new Double(59.355905), new Double(17.946784) },
  458. { "24", "24 Smart fourtwo", "Keys from infodesk.", "Stockholm",
  459. new Double(59.315939), new Double(18.095904) },
  460. { "25", "25 Smart fourtwo", "Keys from infodesk.", "Stockholm",
  461. new Double(59.330716), new Double(18.058702) },
  462. // Boston
  463. /*
  464. * { "26", "26 Ford Mustang", "Keys from infodesk.", "Boston", new
  465. * Double(42.366588), new Double(-71.020955) }, { "27", "27 Smart
  466. * fourtwo", "Keys from infodesk.", "Boston", new Double(42.365419), new
  467. * Double(-71.061748) }, { "28", "28 Volvo S60", "Keys from Seaport
  468. * Hotel reception.", "Boston", new Double(42.34811), new
  469. * Double(-71.041127) }, { "29", "29 Smart fourtwo", "Keys from Seaport
  470. * Hotel reception.", "Boston", new Double(42.348072), new
  471. * Double(-71.041315) },
  472. */
  473. };
  474. final String q = "INSERT INTO " + Resource.TABLE + "("
  475. + Resource.PROPERTY_ID_STYLENAME + ","
  476. + Resource.PROPERTY_ID_NAME + ","
  477. + Resource.PROPERTY_ID_DESCRIPTION + ","
  478. + Resource.PROPERTY_ID_CATEGORY + ","
  479. + Resource.PROPERTY_ID_LOCATIONX + ","
  480. + Resource.PROPERTY_ID_LOCATIONY + ")"
  481. + " VALUES (?,?,?,?,?,?)";
  482. try {
  483. final PreparedStatement stmt = connection.prepareStatement(q);
  484. for (int i = 0; i < resources.length; i++) {
  485. int j = 0;
  486. stmt.setString(j + 1, (String) resources[i][j++]);
  487. stmt.setString(j + 1, (String) resources[i][j++]);
  488. stmt.setString(j + 1, (String) resources[i][j++]);
  489. stmt.setString(j + 1, (String) resources[i][j++]);
  490. stmt.setDouble(j + 1, ((Double) resources[i][j++])
  491. .doubleValue());
  492. stmt.setDouble(j + 1, ((Double) resources[i][j++])
  493. .doubleValue());
  494. stmt.execute();
  495. }
  496. } catch (final SQLException e) {
  497. throw new RuntimeException(e);
  498. }
  499. }
  500. public void generateDemoUser() {
  501. final String q = "INSERT INTO USER (" + User.PROPERTY_ID_FULLNAME + ","
  502. + User.PROPERTY_ID_EMAIL + "," + User.PROPERTY_ID_PASSWORD
  503. + ") VALUES (?,?,?)";
  504. try {
  505. final PreparedStatement stmt = connection.prepareStatement(q);
  506. stmt.setString(1, "Demo User");
  507. stmt.setString(2, "demo.user@itmill.com");
  508. stmt.setString(3, "demo");
  509. stmt.execute();
  510. } catch (final SQLException e) {
  511. throw new RuntimeException(e);
  512. }
  513. }
  514. }