comparison artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 79:f69e5b87f05f

Implementation to export artifacts as xml (applied patch from issue208 by SLT). artifacts/trunk@792 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 16 Mar 2010 16:03:06 +0000
parents 4ae4dc99127d
children 8447467cef86
comparison
equal deleted inserted replaced
78:55eefe63a777 79:f69e5b87f05f
1 package de.intevation.artifactdatabase; 1 package de.intevation.artifactdatabase;
2
3 import java.util.UUID;
4 2
5 import java.sql.Connection; 3 import java.sql.Connection;
6 import java.sql.SQLException; 4 import java.sql.SQLException;
7 import java.sql.PreparedStatement; 5 import java.sql.PreparedStatement;
8 import java.sql.Types; 6 import java.sql.Types;
15 import de.intevation.artifacts.ArtifactSerializer; 13 import de.intevation.artifacts.ArtifactSerializer;
16 14
17 import org.apache.log4j.Logger; 15 import org.apache.log4j.Logger;
18 16
19 /** 17 /**
20 * @author Sascha L. Teichmann 18 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
21 */ 19 */
22 public class Backend 20 public class Backend
23 implements DatabaseCleaner.ArtifactReviver 21 implements DatabaseCleaner.ArtifactReviver
24 { 22 {
25 private static Logger logger = Logger.getLogger(Backend.class); 23 private static Logger logger = Logger.getLogger(Backend.class);
102 public void setCleaner(DatabaseCleaner cleaner) { 100 public void setCleaner(DatabaseCleaner cleaner) {
103 this.cleaner = cleaner; 101 this.cleaner = cleaner;
104 } 102 }
105 103
106 public String newIdentifier() { 104 public String newIdentifier() {
107 UUID uuid = UUID.randomUUID();
108 // TODO: check database for collisions. 105 // TODO: check database for collisions.
109 return uuid.toString(); 106 return StringUtils.newUUID();
110 } 107 }
111 108
112 public PersistentArtifact storeInitially( 109 public PersistentArtifact storeInitially(
113 Artifact artifact, 110 Artifact artifact,
114 ArtifactFactory factory, 111 ArtifactFactory factory,
120 artifact, 117 artifact,
121 factory.getSerializer(), 118 factory.getSerializer(),
122 insertDatabase(artifact, factory, ttl)); 119 insertDatabase(artifact, factory, ttl));
123 } 120 }
124 121
122 public interface ArtifactLoader {
123
124 Object load(ArtifactFactory factory, byte [] bytes, int id);
125
126 } // interface ArtifactLoader
127
128
125 public PersistentArtifact getArtifact(String identifer) { 129 public PersistentArtifact getArtifact(String identifer) {
126 130
127 try { 131 return (PersistentArtifact)loadArtifact(
128 UUID.fromString(identifer); 132 identifer,
129 } 133 new ArtifactLoader() {
130 catch (IllegalArgumentException iae) { 134
131 logger.warn(iae.getLocalizedMessage()); 135 public Object load(
136 ArtifactFactory factory,
137 byte [] bytes,
138 int id
139 ) {
140 ArtifactSerializer serializer = factory.getSerializer();
141
142 Artifact artifact = serializer.fromBytes(bytes);
143
144 return artifact == null
145 ? null
146 : new PersistentArtifact(artifact, serializer, id);
147 }
148 });
149 }
150
151
152
153 public Object loadArtifact(String identifer, ArtifactLoader loader) {
154
155 if (!StringUtils.checkUUID(identifer)) {
132 return null; 156 return null;
133 } 157 }
134 158
135 Connection connection = null; 159 Connection connection = null;
136 PreparedStatement stmnt_load = null; 160 PreparedStatement stmnt_load = null;
172 if (factory == null) { 196 if (factory == null) {
173 logger.error("factory '" + factoryName + "' not found"); 197 logger.error("factory '" + factoryName + "' not found");
174 return null; 198 return null;
175 } 199 }
176 200
177 ArtifactSerializer serializer =
178 factory.getSerializer();
179
180 byte [] bytes = load_result.getBytes(5); 201 byte [] bytes = load_result.getBytes(5);
181 202
182 Artifact artifact = serializer.fromBytes(bytes); 203 return loader.load(factory, bytes, id);
183
184 return artifact == null
185 ? null
186 : new PersistentArtifact(artifact, serializer, id);
187 } 204 }
188 catch (SQLException sqle) { 205 catch (SQLException sqle) {
189 logger.error(sqle.getLocalizedMessage(), sqle); 206 logger.error(sqle.getLocalizedMessage(), sqle);
190 } 207 }
191 finally { 208 finally {
204 } 221 }
205 return null; 222 return null;
206 } 223 }
207 224
208 protected void artifactOutdated(int id) { 225 protected void artifactOutdated(int id) {
209 logger.info("artifactOutdated: id = " + id); 226 if (logger.isDebugEnabled()) {
227 logger.info("artifactOutdated: id = " + id);
228 }
210 if (cleaner != null) { 229 if (cleaner != null) {
211 cleaner.wakeup(); 230 cleaner.wakeup();
212 } 231 }
213 } 232 }
214 233
389 catch (Exception e) { 408 catch (Exception e) {
390 logger.error(e.getLocalizedMessage(), e); 409 logger.error(e.getLocalizedMessage(), e);
391 } 410 }
392 } 411 }
393 } 412 }
394 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: 413 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org