Mercurial > dive4elements > framework
comparison artifact-database/src/main/java/de/intevation/artifactdatabase/DatabaseCleaner.java @ 305:f33401ea2a6c
Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
artifacts/trunk@2412 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 28 Jul 2011 10:19:35 +0000 |
parents | b0a949d3fe09 |
children | 31ee2b3b5a57 |
comparison
equal
deleted
inserted
replaced
304:40b64b4aafce | 305:f33401ea2a6c |
---|---|
10 | 10 |
11 import de.intevation.artifacts.common.utils.Config; | 11 import de.intevation.artifacts.common.utils.Config; |
12 import de.intevation.artifacts.common.utils.StringUtils; | 12 import de.intevation.artifacts.common.utils.StringUtils; |
13 | 13 |
14 import de.intevation.artifacts.Artifact; | 14 import de.intevation.artifacts.Artifact; |
15 | |
16 import de.intevation.artifactdatabase.db.SQL; | |
17 import de.intevation.artifactdatabase.db.DBConnection; | |
15 | 18 |
16 import java.sql.Connection; | 19 import java.sql.Connection; |
17 import java.sql.PreparedStatement; | 20 import java.sql.PreparedStatement; |
18 import java.sql.ResultSet; | 21 import java.sql.ResultSet; |
19 import java.sql.SQLException; | 22 import java.sql.SQLException; |
76 public static final Set<Integer> EMPTY_IDS = Collections.emptySet(); | 79 public static final Set<Integer> EMPTY_IDS = Collections.emptySet(); |
77 | 80 |
78 /** | 81 /** |
79 * The SQL statement to select the outdated artifacts. | 82 * The SQL statement to select the outdated artifacts. |
80 */ | 83 */ |
81 public static final String SQL_OUTDATED = | 84 public String SQL_OUTDATED; |
82 SQL.get("artifacts.outdated"); | 85 |
83 | 86 public String SQL_OUTDATED_COLLECTIONS; |
84 public static final String SQL_OUTDATED_COLLECTIONS = | 87 public String SQL_DELETE_COLLECTION_ITEMS; |
85 SQL.get("collections.outdated"); | 88 public String SQL_DELETE_COLLECTION; |
86 | |
87 public static final String SQL_DELETE_COLLECTION_ITEMS = | |
88 SQL.get("delete.collection.items"); | |
89 | |
90 public static final String SQL_DELETE_COLLECTION = | |
91 SQL.get("delete.collection"); | |
92 | 89 |
93 /** | 90 /** |
94 * The SQL statement to delete some artifacts from the database. | 91 * The SQL statement to delete some artifacts from the database. |
95 */ | 92 */ |
96 public static final String SQL_DELETE_ARTIFACT = | 93 public String SQL_DELETE_ARTIFACT; |
97 SQL.get("artifacts.delete"); | |
98 | 94 |
99 /** | 95 /** |
100 * XPath to figure out how long the cleaner should sleep between | 96 * XPath to figure out how long the cleaner should sleep between |
101 * cleanups. This is stored in the global configuration. | 97 * cleanups. This is stored in the global configuration. |
102 */ | 98 */ |
133 /** | 129 /** |
134 * The reviver used to bring the dead artifact on last | 130 * The reviver used to bring the dead artifact on last |
135 * time back to live to call endOfLife() on them. | 131 * time back to live to call endOfLife() on them. |
136 */ | 132 */ |
137 protected ArtifactReviver reviver; | 133 protected ArtifactReviver reviver; |
134 | |
135 protected DBConnection dbConnection; | |
138 | 136 |
139 /** | 137 /** |
140 * Default constructor. | 138 * Default constructor. |
141 */ | 139 */ |
142 public DatabaseCleaner() { | 140 public DatabaseCleaner() { |
146 * Constructor to create a cleaner with a given global context | 144 * Constructor to create a cleaner with a given global context |
147 * and a given reviver. | 145 * and a given reviver. |
148 * @param context The global context of the artifact database | 146 * @param context The global context of the artifact database |
149 * @param reviver The reviver to awake artifact one last time. | 147 * @param reviver The reviver to awake artifact one last time. |
150 */ | 148 */ |
151 public DatabaseCleaner(Object context, ArtifactReviver reviver) { | 149 public DatabaseCleaner(Object context, ArtifactReviver reviver, DBConfig config) { |
152 setDaemon(true); | 150 setDaemon(true); |
153 sleepTime = getSleepTime(); | 151 sleepTime = getSleepTime(); |
154 this.context = context; | 152 this.context = context; |
155 this.reviver = reviver; | 153 this.reviver = reviver; |
154 this.dbConnection = config.getDBConnection(); | |
155 setupSQL(config.getSQL()); | |
156 } | |
157 | |
158 protected void setupSQL(SQL sql) { | |
159 SQL_OUTDATED = sql.get("artifacts.outdated"); | |
160 SQL_OUTDATED_COLLECTIONS = sql.get("collections.outdated"); | |
161 SQL_DELETE_COLLECTION_ITEMS = sql.get("delete.collection.items"); | |
162 SQL_DELETE_COLLECTION = sql.get("delete.collection"); | |
163 SQL_DELETE_ARTIFACT = sql.get("artifacts.delete"); | |
156 } | 164 } |
157 | 165 |
158 /** | 166 /** |
159 * Sets the filter that prevents deletion of living artifacts. | 167 * Sets the filter that prevents deletion of living artifacts. |
160 * Living artifacts are artifacts which are currently active | 168 * Living artifacts are artifacts which are currently active |
229 ResultSet result = null; | 237 ResultSet result = null; |
230 | 238 |
231 int removedCollections = 0; | 239 int removedCollections = 0; |
232 int removedArtifacts = 0; | 240 int removedArtifacts = 0; |
233 | 241 |
234 DataSource dataSource = DBConnection.getDataSource(); | 242 DataSource dataSource = dbConnection.getDataSource(); |
235 | 243 |
236 Set<Integer> lockedIds = lockedIdsProvider != null | 244 Set<Integer> lockedIds = lockedIdsProvider != null |
237 ? lockedIdsProvider.getLockedIds() | 245 ? lockedIdsProvider.getLockedIds() |
238 : EMPTY_IDS; | 246 : EMPTY_IDS; |
239 | 247 |