comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java @ 987:82ef338fee91

Datacage: Add collections at initial scan. flys-artifacts/trunk@2419 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 28 Jul 2011 15:34:26 +0000
parents 70545233f8ee
children dbe39e1fb5e7
comparison
equal deleted inserted replaced
986:70545233f8ee 987:82ef338fee91
27 private static Logger log = Logger.getLogger(Datacage.class); 27 private static Logger log = Logger.getLogger(Datacage.class);
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"; 33 private String SQL_USER_ID_NEXTVAL = "user.id.nextval";
34 private String SQL_USER_BY_GID = "user.by.gid"; 34 private String SQL_USER_BY_GID = "user.by.gid";
35 private String SQL_INSERT_USER = "insert.user"; 35 private String SQL_INSERT_USER = "insert.user";
36 private String SQL_COLLECTION_BY_GID = "collection.by.gid";
37 private String SQL_COLLECTION_ID_NEXTVAL = "collection.id.nextval";
38 private String SQL_INSERT_COLLECTION = "insert.collection";
36 39
37 protected SQLExecutor sqlExecutor; 40 protected SQLExecutor sqlExecutor;
38 41
39 public class InitialScan 42 public class InitialScan
40 implements ArtifactDatabase.ArtifactLoadedCallback 43 implements ArtifactDatabase.ArtifactLoadedCallback
60 } 63 }
61 64
62 FLYSArtifact flysArtifact = (FLYSArtifact)artifact; 65 FLYSArtifact flysArtifact = (FLYSArtifact)artifact;
63 66
64 Integer uId = getUserId(userId); 67 Integer uId = getUserId(userId);
68 // TODO: We need the name of the collection
69 Integer cId = getCollectionId(collectionId, uId, "XXX");
70
65 // TODO: implement me! 71 // TODO: implement me!
66 } 72 }
67 73
68 protected Integer getUserId(final String userId) { 74 protected Integer getId(
69 Integer u = users.get(userId); 75 LRUCache<String, Integer> cache,
70 if (u != null) { 76 final String idString,
71 return u; 77 final String selectById
78 ) {
79 Integer id = cache.get(idString);
80 if (id != null) {
81 return id;
72 } 82 }
73 83
74 final Integer [] res = new Integer[1]; 84 final Integer [] res = new Integer[1];
75 85
76 SQLExecutor.Instance exec = sqlExecutor.new Instance() { 86 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
77 @Override 87 @Override
78 public boolean doIt() throws SQLException { 88 public boolean doIt() throws SQLException {
79 prepareStatement(SQL_USER_BY_GID); 89 prepareStatement(selectById);
80 stmnt.setString(1, userId); 90 stmnt.setString(1, idString);
81 result = stmnt.executeQuery(); 91 result = stmnt.executeQuery();
82 if (!result.next()) { 92 if (!result.next()) {
83 return false; 93 return false;
84 } 94 }
85 res[0] = result.getInt(1); 95 res[0] = result.getInt(1);
86 return true; 96 return true;
87 } 97 }
88 }; 98 };
89 99
90 if (exec.runRead()) { 100 if (exec.runRead()) {
91 users.put(userId, res[0]); 101 cache.put(idString, res[0]);
92 return res[0]; 102 return res[0];
93 } 103 }
94 104
95 exec = sqlExecutor.new Instance() { 105 return null;
106 }
107
108
109 protected Integer getCollectionId(
110 final String collectionId,
111 final Integer ownerId,
112 final String collectionName
113 ) {
114 Integer c = getId(collections, collectionId, SQL_COLLECTION_BY_GID);
115
116 if (c != null) {
117 return c;
118 }
119
120 final Integer [] res = new Integer[1];
121
122 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
123 @Override
124 public boolean doIt() throws SQLException {
125 prepareStatement(SQL_COLLECTION_ID_NEXTVAL);
126 result = stmnt.executeQuery();
127 if (!result.next()) {
128 return false;
129 }
130 res[0] = result.getInt(1);
131 reset();
132 prepareStatement(SQL_INSERT_COLLECTION);
133 stmnt.setInt (1, res[0]);
134 stmnt.setString(2, collectionId);
135 stmnt.setInt (3, ownerId);
136 stmnt.setString(4, collectionName);
137 stmnt.execute();
138 return true;
139 }
140 };
141
142 if (exec.runWrite()) {
143 collections.put(collectionId, res[0]);
144 return res[0];
145 }
146
147 return null;
148 }
149
150 protected Integer getUserId(final String userId) {
151
152 Integer u = getId(users, userId, SQL_USER_BY_GID);
153
154 if (u != null) {
155 return u;
156 }
157
158 final Integer [] res = new Integer[1];
159
160 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
96 @Override 161 @Override
97 public boolean doIt() throws SQLException { 162 public boolean doIt() throws SQLException {
98 prepareStatement(SQL_USER_ID_NEXTVAL); 163 prepareStatement(SQL_USER_ID_NEXTVAL);
99 result = stmnt.executeQuery(); 164 result = stmnt.executeQuery();
100 if (!result.next()) { 165 if (!result.next()) {
141 setupSQL(config.getSQL()); 206 setupSQL(config.getSQL());
142 sqlExecutor = new SQLExecutor(config.getDBConnection()); 207 sqlExecutor = new SQLExecutor(config.getDBConnection());
143 } 208 }
144 209
145 protected void setupSQL(SQL sql) { 210 protected void setupSQL(SQL sql) {
146 SQL_DELETE_ALL_USERS = sql.get(SQL_DELETE_ALL_USERS); 211 SQL_DELETE_ALL_USERS = sql.get(SQL_DELETE_ALL_USERS);
147 SQL_USER_ID_NEXTVAL = sql.get(SQL_USER_ID_NEXTVAL); 212 SQL_USER_ID_NEXTVAL = sql.get(SQL_USER_ID_NEXTVAL);
148 SQL_USER_BY_GID = sql.get(SQL_USER_BY_GID); 213 SQL_USER_BY_GID = sql.get(SQL_USER_BY_GID);
149 SQL_INSERT_USER = sql.get(SQL_INSERT_USER); 214 SQL_INSERT_USER = sql.get(SQL_INSERT_USER);
215 SQL_COLLECTION_BY_GID = sql.get(SQL_COLLECTION_BY_GID);
216 SQL_COLLECTION_ID_NEXTVAL = sql.get(SQL_COLLECTION_ID_NEXTVAL);
217 SQL_INSERT_COLLECTION = sql.get(SQL_INSERT_COLLECTION);
150 } 218 }
151 219
152 @Override 220 @Override
153 public void systemUp(GlobalContext context) { 221 public void systemUp(GlobalContext context) {
154 log.debug("systemUp"); 222 log.debug("systemUp");

http://dive4elements.wald.intevation.org