# HG changeset patch # User Sascha L. Teichmann # Date 1311865640 0 # Node ID 70545233f8ee96ce1a3038a6fcd7804b78152b92 # Parent 8094ba4ab8732c5ad814002f4096b1e3873bd7c0 Datacage: Add users at initial scan. flys-artifacts/trunk@2418 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 8094ba4ab873 -r 70545233f8ee flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Thu Jul 28 14:38:51 2011 +0000 +++ b/flys-artifacts/ChangeLog Thu Jul 28 15:07:20 2011 +0000 @@ -1,3 +1,9 @@ +2011-07-28 Sascha L. Teichmann + + * src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java, + src/main/resources/datacage-sql/org-h2-driver.properties: Add users + at initial scan. + 2011-07-28 Sascha L. Teichmann * doc/conf/datacage.sql: Using sequences for id generation now diff -r 8094ba4ab873 -r 70545233f8ee flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java Thu Jul 28 14:38:51 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java Thu Jul 28 15:07:20 2011 +0000 @@ -30,6 +30,8 @@ "global.artifact.database"; private String SQL_DELETE_ALL_USERS = "delete.all.users"; + private String SQL_USER_ID_NEXTVAL = "user.id.nextval"; + private String SQL_USER_BY_GID = "user.by.gid"; private String SQL_INSERT_USER = "insert.user"; protected SQLExecutor sqlExecutor; @@ -38,8 +40,11 @@ implements ArtifactDatabase.ArtifactLoadedCallback { protected LRUCache users; + protected LRUCache collections; public InitialScan() { + users = new LRUCache(); + collections = new LRUCache(); } @Override @@ -55,9 +60,64 @@ } FLYSArtifact flysArtifact = (FLYSArtifact)artifact; + + Integer uId = getUserId(userId); // TODO: implement me! } + protected Integer getUserId(final String userId) { + Integer u = users.get(userId); + if (u != null) { + return u; + } + + final Integer [] res = new Integer[1]; + + SQLExecutor.Instance exec = sqlExecutor.new Instance() { + @Override + public boolean doIt() throws SQLException { + prepareStatement(SQL_USER_BY_GID); + stmnt.setString(1, userId); + result = stmnt.executeQuery(); + if (!result.next()) { + return false; + } + res[0] = result.getInt(1); + return true; + } + }; + + if (exec.runRead()) { + users.put(userId, res[0]); + return res[0]; + } + + exec = sqlExecutor.new Instance() { + @Override + public boolean doIt() throws SQLException { + prepareStatement(SQL_USER_ID_NEXTVAL); + result = stmnt.executeQuery(); + if (!result.next()) { + return false; + } + res[0] = result.getInt(1); + reset(); + prepareStatement(SQL_INSERT_USER); + stmnt.setInt (1, res[0]); + stmnt.setString(2, userId); + stmnt.execute(); + return true; + } + }; + + if (exec.runWrite()) { + users.put(userId, res[0]); + return res[0]; + } + + return null; + } + public boolean scan(ArtifactDatabase adb) { try { adb.loadAllArtifacts(this); @@ -84,6 +144,8 @@ protected void setupSQL(SQL sql) { SQL_DELETE_ALL_USERS = sql.get(SQL_DELETE_ALL_USERS); + SQL_USER_ID_NEXTVAL = sql.get(SQL_USER_ID_NEXTVAL); + SQL_USER_BY_GID = sql.get(SQL_USER_BY_GID); SQL_INSERT_USER = sql.get(SQL_INSERT_USER); } diff -r 8094ba4ab873 -r 70545233f8ee flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties --- a/flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties Thu Jul 28 14:38:51 2011 +0000 +++ b/flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties Thu Jul 28 15:07:20 2011 +0000 @@ -1,2 +1,4 @@ delete.all.users = DELETE FROM users -insert.user = INSERT INTO users (gid) VALUES (?) +user.id.nextval = SELECT NEXTVAL('USERS_ID_SEQ') +user.by.gid = SELECT id FROM users WHERE gid = ? +insert.user = INSERT INTO users (id, gid) VALUES (?, ?)