Mercurial > dive4elements > river
changeset 986:70545233f8ee
Datacage: Add users at initial scan.
flys-artifacts/trunk@2418 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 28 Jul 2011 15:07:20 +0000 |
parents | 8094ba4ab873 |
children | 82ef338fee91 |
files | flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties |
diffstat | 3 files changed, 71 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- 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 <sascha.teichmann@intevation.de> + + * 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 <sascha.teichmann@intevation.de> * doc/conf/datacage.sql: Using sequences for id generation now
--- 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<String, Integer> users; + protected LRUCache<String, Integer> collections; public InitialScan() { + users = new LRUCache<String, Integer>(); + collections = new LRUCache<String, Integer>(); } @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); }
--- 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 (?, ?)