comparison artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 80:8447467cef86

Implementation to import artifacts from incoming xml documents (applied patch from issue208 by SLT). artifacts/trunk@799 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 19 Mar 2010 09:34:40 +0000
parents f69e5b87f05f
children 72e2dd4feb31
comparison
equal deleted inserted replaced
79:f69e5b87f05f 80:8447467cef86
34 public static final String SQL_TOUCH = 34 public static final String SQL_TOUCH =
35 SQL.get("artifacts.touch"); 35 SQL.get("artifacts.touch");
36 36
37 public static final String SQL_LOAD_BY_GID = 37 public static final String SQL_LOAD_BY_GID =
38 SQL.get("artifacts.select.gid"); 38 SQL.get("artifacts.select.gid");
39
40 public static final String SQL_GET_ID =
41 SQL.get("artifacts.get.id");
42
43 public static final String SQL_REPLACE =
44 SQL.get("artifacts.replace");
39 45
40 protected DatabaseCleaner cleaner; 46 protected DatabaseCleaner cleaner;
41 47
42 protected FactoryLookup factoryLookup; 48 protected FactoryLookup factoryLookup;
43 49
117 artifact, 123 artifact,
118 factory.getSerializer(), 124 factory.getSerializer(),
119 insertDatabase(artifact, factory, ttl)); 125 insertDatabase(artifact, factory, ttl));
120 } 126 }
121 127
128 public PersistentArtifact storeOrReplace(
129 Artifact artifact,
130 ArtifactFactory factory,
131 Long ttl
132 )
133 throws Exception
134 {
135 return new PersistentArtifact(
136 artifact,
137 factory.getSerializer(),
138 storeOrReplaceDatabase(artifact, factory, ttl));
139 }
140
122 public interface ArtifactLoader { 141 public interface ArtifactLoader {
123 142
124 Object load(ArtifactFactory factory, byte [] bytes, int id); 143 Object load(ArtifactFactory factory, byte [] bytes, int id);
125 144
126 } // interface ArtifactLoader 145 } // interface ArtifactLoader
127
128 146
129 public PersistentArtifact getArtifact(String identifer) { 147 public PersistentArtifact getArtifact(String identifer) {
130 148
131 return (PersistentArtifact)loadArtifact( 149 return (PersistentArtifact)loadArtifact(
132 identifer, 150 identifer,
146 : new PersistentArtifact(artifact, serializer, id); 164 : new PersistentArtifact(artifact, serializer, id);
147 } 165 }
148 }); 166 });
149 } 167 }
150 168
151
152
153 public Object loadArtifact(String identifer, ArtifactLoader loader) { 169 public Object loadArtifact(String identifer, ArtifactLoader loader) {
154 170
155 if (!StringUtils.checkUUID(identifer)) { 171 if (!StringUtils.checkUUID(identifer)) {
156 return null; 172 return null;
157 } 173 }
245 } 261 }
246 262
247 ArtifactSerializer serializer = factory.getSerializer(); 263 ArtifactSerializer serializer = factory.getSerializer();
248 264
249 return serializer.fromBytes(bytes); 265 return serializer.fromBytes(bytes);
266 }
267
268 protected int storeOrReplaceDatabase(
269 Artifact artifact,
270 ArtifactFactory factory,
271 Long ttl
272 ) {
273 String uuid = artifact.identifier();
274
275 if (!StringUtils.checkUUID(uuid)) {
276 throw new RuntimeException("No valid UUID");
277 }
278
279 Connection connection = null;
280 PreparedStatement stmnt = null;
281 ResultSet result = null;
282
283 DataSource dataSource = DBConnection.getDataSource();
284 try {
285 connection = dataSource.getConnection();
286 try {
287 connection.setAutoCommit(false);
288
289 stmnt = connection.prepareStatement(SQL_GET_ID);
290
291 stmnt.setString(1, uuid);
292 result = stmnt.executeQuery();
293
294 Integer ID = result.next()
295 ? Integer.valueOf(result.getInt(1))
296 : null;
297
298 result.close(); result = null;
299 stmnt.close(); stmnt = null;
300
301 if (ID != null) { // already in database
302 int id = ID.intValue();
303
304 stmnt = connection.prepareStatement(SQL_REPLACE);
305
306 if (ttl == null) {
307 stmnt.setNull(1, Types.BIGINT);
308 }
309 else {
310 stmnt.setLong(1, ttl.longValue());
311 }
312
313 stmnt.setString(2, factory.getName());
314 stmnt.setBytes(
315 3,
316 factory.getSerializer().toBytes(artifact));
317 stmnt.setInt(4, id);
318
319 stmnt.execute();
320 connection.commit();
321 return id;
322 }
323
324 stmnt = connection.prepareStatement(SQL_NEXT_ID);
325 result = stmnt.executeQuery();
326
327 if (!result.next()) {
328 throw new RuntimeException("No id generated");
329 }
330
331 int id = result.getInt(1);
332
333 result.close(); result = null;
334 stmnt.close(); stmnt = null;
335
336 stmnt = connection.prepareStatement(SQL_INSERT);
337
338 stmnt.setInt(1, id);
339 stmnt.setString(2, uuid);
340 if (ttl == null) {
341 stmnt.setNull(3, Types.BIGINT);
342 }
343 else {
344 stmnt.setLong(3, ttl.longValue());
345 }
346
347 stmnt.setString(4, factory.getName());
348
349 stmnt.setBytes(
350 5,
351 factory.getSerializer().toBytes(artifact));
352
353 stmnt.execute();
354 connection.commit();
355 return id;
356 }
357 catch (SQLException sqle) {
358 connection.rollback();
359 throw sqle;
360 }
361 }
362 catch (SQLException sqle) {
363 logger.error(sqle.getLocalizedMessage(), sqle);
364 }
365 finally {
366 if (result != null) {
367 try { result.close(); }
368 catch (SQLException sqle) {}
369 }
370 if (stmnt != null) {
371 try { stmnt.close(); }
372 catch (SQLException sqle) {}
373 }
374 if (connection != null) {
375 try { connection.close(); }
376 catch (SQLException sqle) {}
377 }
378 }
379 throw new RuntimeException("failed insert artifact into database");
250 } 380 }
251 381
252 protected int insertDatabase( 382 protected int insertDatabase(
253 Artifact artifact, 383 Artifact artifact,
254 ArtifactFactory factory, 384 ArtifactFactory factory,

http://dive4elements.wald.intevation.org