comparison artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 303:190aa68ae7a8

Added method to artifact database to load all artifacts. artifacts/trunk@2408 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 27 Jul 2011 09:32:26 +0000
parents b0a949d3fe09
children f33401ea2a6c
comparison
equal deleted inserted replaced
302:c066343c63b0 303:190aa68ae7a8
8 package de.intevation.artifactdatabase; 8 package de.intevation.artifactdatabase;
9 9
10 import de.intevation.artifacts.Artifact; 10 import de.intevation.artifacts.Artifact;
11 import de.intevation.artifacts.ArtifactCollection; 11 import de.intevation.artifacts.ArtifactCollection;
12 import de.intevation.artifacts.ArtifactCollectionFactory; 12 import de.intevation.artifacts.ArtifactCollectionFactory;
13 import de.intevation.artifacts.ArtifactDatabase.ArtifactLoadedCallback;
13 import de.intevation.artifacts.ArtifactFactory; 14 import de.intevation.artifacts.ArtifactFactory;
14 import de.intevation.artifacts.ArtifactSerializer; 15 import de.intevation.artifacts.ArtifactSerializer;
15 import de.intevation.artifacts.CollectionItem; 16 import de.intevation.artifacts.CollectionItem;
16 import de.intevation.artifacts.User; 17 import de.intevation.artifacts.User;
17 import de.intevation.artifacts.UserFactory; 18 import de.intevation.artifacts.UserFactory;
23 import java.sql.Timestamp; 24 import java.sql.Timestamp;
24 import java.sql.Types; 25 import java.sql.Types;
25 26
26 import java.util.ArrayList; 27 import java.util.ArrayList;
27 import java.util.Date; 28 import java.util.Date;
29 import java.util.Map;
30 import java.util.LinkedHashMap;
28 import java.util.HashMap; 31 import java.util.HashMap;
29 32
30 import org.apache.log4j.Logger; 33 import org.apache.log4j.Logger;
31 34
32 import org.w3c.dom.Document; 35 import org.w3c.dom.Document;
202 public static final String SQL_COLLECTIONS_TOUCH_BY_ID = 205 public static final String SQL_COLLECTIONS_TOUCH_BY_ID =
203 SQL.get("collections.touch.by.id"); 206 SQL.get("collections.touch.by.id");
204 207
205 public static final String SQL_COLLECTION_ITEMS_LIST_GID = 208 public static final String SQL_COLLECTION_ITEMS_LIST_GID =
206 SQL.get("collection.items.list.gid"); 209 SQL.get("collection.items.list.gid");
210
211 public static final String SQL_ALL_ARTIFACTS =
212 SQL.get("all.artifacts");
207 213
208 /** The singleton.*/ 214 /** The singleton.*/
209 protected static Backend instance; 215 protected static Backend instance;
210 216
211 /** 217 /**
496 ) { 502 ) {
497 if (!isValidIdentifier(identifer)) { 503 if (!isValidIdentifier(identifer)) {
498 return null; 504 return null;
499 } 505 }
500 506
507 if (factoryLookup == null) {
508 logger.error("factory lookup == null");
509 return false;
510 }
511
501 final Object [] loaded = new Object[1]; 512 final Object [] loaded = new Object[1];
502 513
503 SQLExecutor exec = new SQLExecutor() { 514 SQLExecutor exec = new SQLExecutor() {
504 public boolean doIt() throws SQLException { 515 public boolean doIt() throws SQLException {
505 prepareStatement(SQL_LOAD_BY_GID); 516 prepareStatement(SQL_LOAD_BY_GID);
514 int id = result.getInt(1); 525 int id = result.getInt(1);
515 long ttlX = result.getLong(2); 526 long ttlX = result.getLong(2);
516 Long ttl = result.wasNull() ? null : ttlX; 527 Long ttl = result.wasNull() ? null : ttlX;
517 528
518 String factoryName = result.getString(3); 529 String factoryName = result.getString(3);
519
520 if (factoryLookup == null) {
521 logger.error("factory lookup == null");
522 return false;
523 }
524 530
525 ArtifactFactory factory = factoryLookup 531 ArtifactFactory factory = factoryLookup
526 .getArtifactFactory(factoryName); 532 .getArtifactFactory(factoryName);
527 533
528 if (factory == null) { 534 if (factory == null) {
1567 1573
1568 return true; 1574 return true;
1569 } 1575 }
1570 }.runWrite(); 1576 }.runWrite();
1571 } 1577 }
1578
1579 public boolean loadAllArtifacts(final ArtifactLoadedCallback alc) {
1580
1581 if (factoryLookup == null) {
1582 logger.error("factory lookup == null");
1583 return false;
1584 }
1585
1586 return new SQLExecutor() {
1587 public boolean doIt() throws SQLException {
1588
1589 // a little cache to avoid too much deserializations.
1590 Map<String, Artifact> alreadyLoaded =
1591 new LinkedHashMap<String, Artifact>() {
1592 @Override
1593 protected boolean removeEldestEntry(Map.Entry eldest) {
1594 // store only the last 200 to avoid memory flooding
1595 return size() > 200;
1596 }
1597 };
1598
1599 prepareStatement(SQL_ALL_ARTIFACTS);
1600 result = stmnt.executeQuery();
1601 while (result.next()) {
1602 String userId = result.getString(1);
1603 String collectionId = result.getString(2);
1604 String artifactId = result.getString(3);
1605 String factoryName = result.getString(4);
1606
1607 Artifact artifact = alreadyLoaded.get(artifactId);
1608
1609 if (artifact != null) {
1610 alc.artifactLoaded(
1611 userId, collectionId, artifactId, artifact);
1612 continue;
1613 }
1614
1615 ArtifactFactory factory = factoryLookup
1616 .getArtifactFactory(factoryName);
1617
1618 if (factory == null) {
1619 logger.error("factory '" + factoryName + "' not found");
1620 continue;
1621 }
1622
1623 byte [] bytes = result.getBytes(5);
1624
1625 artifact = factory.getSerializer().fromBytes(bytes);
1626
1627 if (artifact != null) {
1628 alc.artifactLoaded(
1629 userId, collectionId, artifactId, artifact);
1630 }
1631
1632 alreadyLoaded.put(artifactId, artifact);
1633 }
1634 return true;
1635 }
1636 }.runRead();
1637 }
1572 } 1638 }
1573 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 1639 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org