comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java @ 984:2b05c4a0c6fd

Datacage: clear database before initial scan flys-artifacts/trunk@2416 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 28 Jul 2011 14:26:01 +0000
parents 4ea5f5a2284e
children 70545233f8ee
comparison
equal deleted inserted replaced
983:4ea5f5a2284e 984:2b05c4a0c6fd
1 package de.intevation.flys.artifacts.datacage; 1 package de.intevation.flys.artifacts.datacage;
2
3 import java.sql.SQLException;
2 4
3 import de.intevation.artifacts.GlobalContext; 5 import de.intevation.artifacts.GlobalContext;
4 6
5 import de.intevation.artifactdatabase.db.SQL; 7 import de.intevation.artifactdatabase.db.SQL;
6 import de.intevation.artifactdatabase.db.SQLExecutor; 8 import de.intevation.artifactdatabase.db.SQLExecutor;
7 9
8 import de.intevation.artifactdatabase.LifetimeListener; 10 import de.intevation.artifactdatabase.LifetimeListener;
11
12 import de.intevation.artifacts.Artifact;
13 import de.intevation.artifacts.ArtifactDatabase;
14 import de.intevation.artifacts.ArtifactDatabaseException;
15
16 import de.intevation.flys.artifacts.FLYSArtifact;
17
18 import de.intevation.artifacts.common.utils.LRUCache;
9 19
10 import org.apache.log4j.Logger; 20 import org.apache.log4j.Logger;
11 21
12 import org.w3c.dom.Document; 22 import org.w3c.dom.Document;
13 23
14 public class Datacage 24 public class Datacage
15 implements LifetimeListener 25 implements LifetimeListener
16 { 26 {
17 private static Logger log = Logger.getLogger(Datacage.class); 27 private static Logger log = Logger.getLogger(Datacage.class);
18 28
29 public static final String ARTEFACT_DATABASE_KEY =
30 "global.artifact.database";
31
32 private String SQL_DELETE_ALL_USERS = "delete.all.users";
33 private String SQL_INSERT_USER = "insert.user";
34
19 protected SQLExecutor sqlExecutor; 35 protected SQLExecutor sqlExecutor;
36
37 public class InitialScan
38 implements ArtifactDatabase.ArtifactLoadedCallback
39 {
40 protected LRUCache<String, Integer> users;
41
42 public InitialScan() {
43 }
44
45 @Override
46 public void artifactLoaded(
47 String userId,
48 String collectionId,
49 String artifactId,
50 Artifact artifact
51 ) {
52 if (!(artifact instanceof FLYSArtifact)) {
53 log.warn("ignoring none FLYS artifacts");
54 return;
55 }
56
57 FLYSArtifact flysArtifact = (FLYSArtifact)artifact;
58 // TODO: implement me!
59 }
60
61 public boolean scan(ArtifactDatabase adb) {
62 try {
63 adb.loadAllArtifacts(this);
64 }
65 catch (ArtifactDatabaseException ade) {
66 log.error(ade);
67 return false;
68 }
69 return true;
70 }
71 } // class InitialScan
72
20 73
21 public Datacage() { 74 public Datacage() {
22 } 75 }
23 76
24 @Override 77 @Override
28 setupSQL(config.getSQL()); 81 setupSQL(config.getSQL());
29 sqlExecutor = new SQLExecutor(config.getDBConnection()); 82 sqlExecutor = new SQLExecutor(config.getDBConnection());
30 } 83 }
31 84
32 protected void setupSQL(SQL sql) { 85 protected void setupSQL(SQL sql) {
33 log.debug("implement me!"); 86 SQL_DELETE_ALL_USERS = sql.get(SQL_DELETE_ALL_USERS);
87 SQL_INSERT_USER = sql.get(SQL_INSERT_USER);
34 } 88 }
35 89
36 @Override 90 @Override
37 public void systemUp(GlobalContext context) { 91 public void systemUp(GlobalContext context) {
38 log.debug("systemUp"); 92 log.debug("systemUp");
93 initialScan(context);
39 } 94 }
95
96 protected void initialScan(GlobalContext context) {
97 log.debug("initialScan");
98
99 Object adbObject = context.get(ARTEFACT_DATABASE_KEY);
100
101 if (!(adbObject instanceof ArtifactDatabase)) {
102 log.error("missing artefact database. Cannot scan");
103 return;
104 }
105
106 ArtifactDatabase adb = (ArtifactDatabase)adbObject;
107
108 if (!cleanDatabase()) {
109 log.error("cleaning database failed");
110 return;
111 }
112
113 InitialScan is = new InitialScan();
114
115 if (!is.scan(adb)) {
116 log.error("initial scan failed");
117 return;
118 }
119
120 }
121
122 protected boolean cleanDatabase() {
123
124 return sqlExecutor.new Instance() {
125 @Override
126 public boolean doIt() throws SQLException {
127 prepareStatement(SQL_DELETE_ALL_USERS);
128 stmnt.execute();
129 return true;
130 }
131 }.runWrite();
132 }
133
40 134
41 @Override 135 @Override
42 public void systemDown(GlobalContext context) { 136 public void systemDown(GlobalContext context) {
43 log.debug("systemDown"); 137 log.debug("systemDown");
44 } 138 }

http://dive4elements.wald.intevation.org