comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java @ 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 2b05c4a0c6fd
children 82ef338fee91
comparison
equal deleted inserted replaced
985:8094ba4ab873 986:70545233f8ee
28 28
29 public static final String ARTEFACT_DATABASE_KEY = 29 public static final String ARTEFACT_DATABASE_KEY =
30 "global.artifact.database"; 30 "global.artifact.database";
31 31
32 private String SQL_DELETE_ALL_USERS = "delete.all.users"; 32 private String SQL_DELETE_ALL_USERS = "delete.all.users";
33 private String SQL_USER_ID_NEXTVAL = "user.id.nextval";
34 private String SQL_USER_BY_GID = "user.by.gid";
33 private String SQL_INSERT_USER = "insert.user"; 35 private String SQL_INSERT_USER = "insert.user";
34 36
35 protected SQLExecutor sqlExecutor; 37 protected SQLExecutor sqlExecutor;
36 38
37 public class InitialScan 39 public class InitialScan
38 implements ArtifactDatabase.ArtifactLoadedCallback 40 implements ArtifactDatabase.ArtifactLoadedCallback
39 { 41 {
40 protected LRUCache<String, Integer> users; 42 protected LRUCache<String, Integer> users;
43 protected LRUCache<String, Integer> collections;
41 44
42 public InitialScan() { 45 public InitialScan() {
46 users = new LRUCache<String, Integer>();
47 collections = new LRUCache<String, Integer>();
43 } 48 }
44 49
45 @Override 50 @Override
46 public void artifactLoaded( 51 public void artifactLoaded(
47 String userId, 52 String userId,
53 log.warn("ignoring none FLYS artifacts"); 58 log.warn("ignoring none FLYS artifacts");
54 return; 59 return;
55 } 60 }
56 61
57 FLYSArtifact flysArtifact = (FLYSArtifact)artifact; 62 FLYSArtifact flysArtifact = (FLYSArtifact)artifact;
63
64 Integer uId = getUserId(userId);
58 // TODO: implement me! 65 // TODO: implement me!
66 }
67
68 protected Integer getUserId(final String userId) {
69 Integer u = users.get(userId);
70 if (u != null) {
71 return u;
72 }
73
74 final Integer [] res = new Integer[1];
75
76 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
77 @Override
78 public boolean doIt() throws SQLException {
79 prepareStatement(SQL_USER_BY_GID);
80 stmnt.setString(1, userId);
81 result = stmnt.executeQuery();
82 if (!result.next()) {
83 return false;
84 }
85 res[0] = result.getInt(1);
86 return true;
87 }
88 };
89
90 if (exec.runRead()) {
91 users.put(userId, res[0]);
92 return res[0];
93 }
94
95 exec = sqlExecutor.new Instance() {
96 @Override
97 public boolean doIt() throws SQLException {
98 prepareStatement(SQL_USER_ID_NEXTVAL);
99 result = stmnt.executeQuery();
100 if (!result.next()) {
101 return false;
102 }
103 res[0] = result.getInt(1);
104 reset();
105 prepareStatement(SQL_INSERT_USER);
106 stmnt.setInt (1, res[0]);
107 stmnt.setString(2, userId);
108 stmnt.execute();
109 return true;
110 }
111 };
112
113 if (exec.runWrite()) {
114 users.put(userId, res[0]);
115 return res[0];
116 }
117
118 return null;
59 } 119 }
60 120
61 public boolean scan(ArtifactDatabase adb) { 121 public boolean scan(ArtifactDatabase adb) {
62 try { 122 try {
63 adb.loadAllArtifacts(this); 123 adb.loadAllArtifacts(this);
82 sqlExecutor = new SQLExecutor(config.getDBConnection()); 142 sqlExecutor = new SQLExecutor(config.getDBConnection());
83 } 143 }
84 144
85 protected void setupSQL(SQL sql) { 145 protected void setupSQL(SQL sql) {
86 SQL_DELETE_ALL_USERS = sql.get(SQL_DELETE_ALL_USERS); 146 SQL_DELETE_ALL_USERS = sql.get(SQL_DELETE_ALL_USERS);
147 SQL_USER_ID_NEXTVAL = sql.get(SQL_USER_ID_NEXTVAL);
148 SQL_USER_BY_GID = sql.get(SQL_USER_BY_GID);
87 SQL_INSERT_USER = sql.get(SQL_INSERT_USER); 149 SQL_INSERT_USER = sql.get(SQL_INSERT_USER);
88 } 150 }
89 151
90 @Override 152 @Override
91 public void systemUp(GlobalContext context) { 153 public void systemUp(GlobalContext context) {

http://dive4elements.wald.intevation.org