Mercurial > dive4elements > framework
comparison artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 310:63122b9dee1d
Added interface to listen events in the backend.
artifacts/trunk@2435 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 01 Aug 2011 09:41:40 +0000 |
parents | 86dd32b45d87 |
children | 1d517e051e95 |
comparison
equal
deleted
inserted
replaced
309:86dd32b45d87 | 310:63122b9dee1d |
---|---|
26 | 26 |
27 import java.sql.SQLException; | 27 import java.sql.SQLException; |
28 import java.sql.Timestamp; | 28 import java.sql.Timestamp; |
29 import java.sql.Types; | 29 import java.sql.Types; |
30 | 30 |
31 import java.util.List; | |
31 import java.util.ArrayList; | 32 import java.util.ArrayList; |
32 import java.util.Date; | 33 import java.util.Date; |
33 import java.util.HashMap; | 34 import java.util.HashMap; |
35 | |
36 import java.util.concurrent.CopyOnWriteArrayList; | |
34 | 37 |
35 import org.apache.log4j.Logger; | 38 import org.apache.log4j.Logger; |
36 | 39 |
37 import org.w3c.dom.Document; | 40 import org.w3c.dom.Document; |
38 | 41 |
132 /** The singleton.*/ | 135 /** The singleton.*/ |
133 protected static Backend instance; | 136 protected static Backend instance; |
134 | 137 |
135 protected SQLExecutor sqlExecutor; | 138 protected SQLExecutor sqlExecutor; |
136 | 139 |
140 protected List<BackendListener> listeners; | |
141 | |
137 protected DBConfig config; | 142 protected DBConfig config; |
138 | 143 |
139 /** | 144 /** |
140 * The database cleaner. Reference is stored here because | 145 * The database cleaner. Reference is stored here because |
141 * the cleaner is woken up if the backend finds an outdated | 146 * the cleaner is woken up if the backend finds an outdated |
254 | 259 |
255 /** | 260 /** |
256 * Default constructor | 261 * Default constructor |
257 */ | 262 */ |
258 public Backend() { | 263 public Backend() { |
264 listeners = new CopyOnWriteArrayList<BackendListener>(); | |
259 } | 265 } |
260 | 266 |
261 public Backend(DBConfig config) { | 267 public Backend(DBConfig config) { |
262 this.config = config; | 268 this.config = config; |
263 sqlExecutor = new SQLExecutor(config.getDBConnection()); | 269 sqlExecutor = new SQLExecutor(config.getDBConnection()); |
346 SQL_COLLECTIONS_TOUCH_BY_ID = sql.get("collections.touch.by.id"); | 352 SQL_COLLECTIONS_TOUCH_BY_ID = sql.get("collections.touch.by.id"); |
347 SQL_COLLECTION_ITEMS_LIST_GID = sql.get("collection.items.list.gid"); | 353 SQL_COLLECTION_ITEMS_LIST_GID = sql.get("collection.items.list.gid"); |
348 SQL_ALL_ARTIFACTS = sql.get("all.artifacts"); | 354 SQL_ALL_ARTIFACTS = sql.get("all.artifacts"); |
349 } | 355 } |
350 | 356 |
357 public void addListener(BackendListener listener) { | |
358 listeners.add(listener); | |
359 } | |
360 | |
351 /** | 361 /** |
352 * Sets the factory lookup mechanism to decouple ArtifactDatabase | 362 * Sets the factory lookup mechanism to decouple ArtifactDatabase |
353 * and Backend. | 363 * and Backend. |
354 * @param factoryLookup | 364 * @param factoryLookup |
355 */ | 365 */ |
716 | 726 |
717 if (!exec.runWrite()) { | 727 if (!exec.runWrite()) { |
718 throw new RuntimeException("failed insert artifact into database"); | 728 throw new RuntimeException("failed insert artifact into database"); |
719 } | 729 } |
720 | 730 |
731 fireArtifactStored(artifact); | |
732 | |
721 return id[0]; | 733 return id[0]; |
734 } | |
735 | |
736 protected void fireArtifactStored(Artifact artifact) { | |
737 for (BackendListener listener: listeners) { | |
738 listener.artifactStored(artifact, this); | |
739 } | |
722 } | 740 } |
723 | 741 |
724 /** | 742 /** |
725 * Touches the access timestamp of a given artifact to prevent | 743 * Touches the access timestamp of a given artifact to prevent |
726 * that it will be removed from the database by the database cleaner. | 744 * that it will be removed from the database by the database cleaner. |
742 * Writes modification of an artifact back to the database. | 760 * Writes modification of an artifact back to the database. |
743 * @param artifact The persistent wrapper around a living | 761 * @param artifact The persistent wrapper around a living |
744 * artifact. | 762 * artifact. |
745 */ | 763 */ |
746 public void store(final PersistentArtifact artifact) { | 764 public void store(final PersistentArtifact artifact) { |
747 sqlExecutor.new Instance() { | 765 boolean success = sqlExecutor.new Instance() { |
748 public boolean doIt() throws SQLException { | 766 public boolean doIt() throws SQLException { |
749 prepareStatement(SQL_UPDATE); | 767 prepareStatement(SQL_UPDATE); |
750 stmnt.setInt(2, artifact.getId()); | 768 stmnt.setInt(2, artifact.getId()); |
751 | 769 |
752 byte [] bytes = artifact | 770 byte [] bytes = artifact |
757 stmnt.execute(); | 775 stmnt.execute(); |
758 conn.commit(); | 776 conn.commit(); |
759 return true; | 777 return true; |
760 } | 778 } |
761 }.runWrite(); | 779 }.runWrite(); |
780 | |
781 if (success) { | |
782 fireArtifactStored(artifact.getArtifact()); | |
783 } | |
762 } | 784 } |
763 | 785 |
764 public User createUser( | 786 public User createUser( |
765 final String name, | 787 final String name, |
766 final Document role, | 788 final Document role, |