comparison artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.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 190aa68ae7a8
children d96bcb40dbf9
comparison
equal deleted inserted replaced
304:40b64b4aafce 305:f33401ea2a6c
17 import de.intevation.artifacts.User; 17 import de.intevation.artifacts.User;
18 import de.intevation.artifacts.UserFactory; 18 import de.intevation.artifacts.UserFactory;
19 19
20 import de.intevation.artifacts.common.utils.StringUtils; 20 import de.intevation.artifacts.common.utils.StringUtils;
21 import de.intevation.artifacts.common.utils.XMLUtils; 21 import de.intevation.artifacts.common.utils.XMLUtils;
22
23 import de.intevation.artifactdatabase.db.SQLExecutor;
24 import de.intevation.artifactdatabase.db.SQL;
22 25
23 import java.sql.SQLException; 26 import java.sql.SQLException;
24 import java.sql.Timestamp; 27 import java.sql.Timestamp;
25 import java.sql.Types; 28 import java.sql.Types;
26 29
46 private static Logger logger = Logger.getLogger(Backend.class); 49 private static Logger logger = Logger.getLogger(Backend.class);
47 50
48 /** 51 /**
49 * The SQL statement to create new artifact id inside the database. 52 * The SQL statement to create new artifact id inside the database.
50 */ 53 */
51 public static final String SQL_NEXT_ID = 54 public String SQL_NEXT_ID;
52 SQL.get("artifacts.id.nextval");
53 55
54 /** 56 /**
55 * The SQL statement to insert an artifact into the database. 57 * The SQL statement to insert an artifact into the database.
56 */ 58 */
57 public static final String SQL_INSERT = 59 public String SQL_INSERT;
58 SQL.get("artifacts.insert");
59 60
60 /** 61 /**
61 * The SQL statement to update some columns of an existing 62 * The SQL statement to update some columns of an existing
62 * artifact in the database. 63 * artifact in the database.
63 */ 64 */
64 public static final String SQL_UPDATE = 65 public String SQL_UPDATE;
65 SQL.get("artifacts.update");
66 66
67 /** 67 /**
68 * The SQL statement to touch the access time of an 68 * The SQL statement to touch the access time of an
69 * artifact inside the database. 69 * artifact inside the database.
70 */ 70 */
71 public static final String SQL_TOUCH = 71 public String SQL_TOUCH;
72 SQL.get("artifacts.touch");
73 72
74 /** 73 /**
75 * The SQL statement to load an artifact by a given 74 * The SQL statement to load an artifact by a given
76 * identifier from the database. 75 * identifier from the database.
77 */ 76 */
78 public static final String SQL_LOAD_BY_GID = 77 public String SQL_LOAD_BY_GID;
79 SQL.get("artifacts.select.gid");
80 78
81 /** 79 /**
82 * The SQL statement to get the database id of an artifact 80 * The SQL statement to get the database id of an artifact
83 * identified by the identifier. 81 * identified by the identifier.
84 */ 82 */
85 public static final String SQL_GET_ID = 83 public String SQL_GET_ID;
86 SQL.get("artifacts.get.id");
87 84
88 /** 85 /**
89 * The SQL statement to replace the content of an 86 * The SQL statement to replace the content of an
90 * existing artifact inside the database. 87 * existing artifact inside the database.
91 */ 88 */
92 public static final String SQL_REPLACE = 89 public String SQL_REPLACE;
93 SQL.get("artifacts.replace");
94 90
95 // USER SQL 91 // USER SQL
96 92
97 public static final String SQL_USERS_NEXT_ID = 93 public String SQL_USERS_NEXT_ID;
98 SQL.get("users.id.nextval"); 94 public String SQL_USERS_INSERT;
99 95 public String SQL_USERS_SELECT_ID_BY_GID;
100 public static final String SQL_USERS_INSERT = 96 public String SQL_USERS_SELECT_GID;
101 SQL.get("users.insert"); 97 public String SQL_USERS_DELETE_ID;
102 98 public String SQL_USERS_DELETE_COLLECTIONS;
103 public static final String SQL_USERS_SELECT_ID_BY_GID = 99 public String SQL_USERS_SELECT_ALL;
104 SQL.get("users.select.id.by.gid"); 100 public String SQL_USERS_COLLECTIONS;
105 101 public String SQL_USERS_COLLECTION_IDS;
106 public static final String SQL_USERS_SELECT_GID = 102 public String SQL_USERS_DELETE_ALL_COLLECTIONS;
107 SQL.get("users.select.gid"); 103 public String SQL_ARTIFACTS_IN_ONLY_COLLECTION_ONLY;
108 104 public String SQL_OUTDATE_ARTIFACTS_COLLECTION;
109 public static final String SQL_USERS_DELETE_ID = 105 public String SQL_UPDATE_COLLECTION_TTL;
110 SQL.get("users.delete.id"); 106 public String SQL_UPDATE_COLLECTION_NAME;
111 107 public String SQL_OUTDATE_ARTIFACTS_USER;
112 public static final String SQL_USERS_DELETE_COLLECTIONS = 108 public String SQL_DELETE_USER_COLLECTION_ITEMS;
113 SQL.get("users.delete.collections"); 109 public String SQL_COLLECTIONS_NEXT_ID;
114 110 public String SQL_COLLECTIONS_INSERT;
115 public static final String SQL_USERS_SELECT_ALL = 111 public String SQL_COLLECTIONS_SELECT_USER;
116 SQL.get("users.select.all"); 112 public String SQL_COLLECTIONS_SELECT_ALL;
117 113 public String SQL_COLLECTIONS_SELECT_GID;
118 public static final String SQL_USERS_COLLECTIONS = 114 public String SQL_COLLECTIONS_CREATION_TIME;
119 SQL.get("users.collections"); 115 public String SQL_COLLECTIONS_ID_BY_GID;
120 116 public String SQL_DELETE_COLLECTION_ITEMS;
121 public static final String SQL_USERS_COLLECTION_IDS = 117 public String SQL_DELETE_COLLECTION;
122 SQL.get("users.collection.ids"); 118 public String SQL_COLLECTION_CHECK_ARTIFACT;
123 119 public String SQL_COLLECTION_ITEMS_ID_NEXTVAL;
124 public static final String SQL_USERS_DELETE_ALL_COLLECTIONS = 120 public String SQL_COLLECTION_ITEMS_INSERT;
125 SQL.get("users.delete.all.collections"); 121 public String SQL_COLLECTION_GET_ATTRIBUTE;
126 122 public String SQL_COLLECTION_SET_ATTRIBUTE;
127 public static final String SQL_ARTIFACTS_IN_ONLY_COLLECTION_ONLY = 123 public String SQL_COLLECTION_ITEM_GET_ATTRIBUTE;
128 SQL.get("artifacts.in.one.collection.only"); 124 public String SQL_COLLECTION_ITEM_SET_ATTRIBUTE;
129 125 public String SQL_COLLECTIONS_TOUCH_BY_GID;
130 public static final String SQL_OUTDATE_ARTIFACTS_COLLECTION = 126 public String SQL_COLLECTION_ITEM_ID_CID_AID;
131 SQL.get("outdate.artifacts.collection"); 127 public String SQL_COLLECTION_ITEM_OUTDATE_ARTIFACT;
132 128 public String SQL_COLLECTION_ITEM_DELETE;
133 public static final String SQL_UPDATE_COLLECTION_TTL = 129 public String SQL_COLLECTIONS_TOUCH_BY_ID;
134 SQL.get("collections.update.ttl"); 130 public String SQL_COLLECTION_ITEMS_LIST_GID;
135 131 public String SQL_ALL_ARTIFACTS;
136 public static final String SQL_UPDATE_COLLECTION_NAME =
137 SQL.get("collections.update.name");
138
139 public static final String SQL_OUTDATE_ARTIFACTS_USER =
140 SQL.get("outdate.artifacts.user");
141
142 public static final String SQL_DELETE_USER_COLLECTION_ITEMS =
143 SQL.get("delete.user.collection.items");
144
145 public static final String SQL_COLLECTIONS_NEXT_ID =
146 SQL.get("collections.id.nextval");
147
148 public static final String SQL_COLLECTIONS_INSERT =
149 SQL.get("collections.insert");
150
151 public static final String SQL_COLLECTIONS_SELECT_USER =
152 SQL.get("collections.select.user");
153
154 public static final String SQL_COLLECTIONS_SELECT_ALL =
155 SQL.get("collections.select.all");
156
157 public static final String SQL_COLLECTIONS_SELECT_GID =
158 SQL.get("collections.select.by.gid");
159
160 public static final String SQL_COLLECTIONS_CREATION_TIME =
161 SQL.get("collection.creation.time");
162
163 public static final String SQL_COLLECTIONS_ID_BY_GID =
164 SQL.get("collections.id.by.gid");
165
166 public static final String SQL_DELETE_COLLECTION_ITEMS =
167 SQL.get("delete.collection.items");
168
169 public static final String SQL_DELETE_COLLECTION =
170 SQL.get("delete.collection");
171
172 public static final String SQL_COLLECTION_CHECK_ARTIFACT =
173 SQL.get("collection.check.artifact");
174
175 public static final String SQL_COLLECTION_ITEMS_ID_NEXTVAL =
176 SQL.get("collection.items.id.nextval");
177
178 public static final String SQL_COLLECTION_ITEMS_INSERT =
179 SQL.get("collection.items.insert");
180
181 public static final String SQL_COLLECTION_GET_ATTRIBUTE =
182 SQL.get("collection.get.attribute");
183
184 public static final String SQL_COLLECTION_SET_ATTRIBUTE =
185 SQL.get("collection.set.attribute");
186
187 public static final String SQL_COLLECTION_ITEM_GET_ATTRIBUTE =
188 SQL.get("collection.item.get.attribute");
189
190 public static final String SQL_COLLECTION_ITEM_SET_ATTRIBUTE =
191 SQL.get("collection.item.set.attribute");
192
193 public static final String SQL_COLLECTIONS_TOUCH_BY_GID =
194 SQL.get("collections.touch.by.gid");
195
196 public static final String SQL_COLLECTION_ITEM_ID_CID_AID =
197 SQL.get("collection.item.id.cid.aid");
198
199 public static final String SQL_COLLECTION_ITEM_OUTDATE_ARTIFACT =
200 SQL.get("collection.item.outdate.artifact");
201
202 public static final String SQL_COLLECTION_ITEM_DELETE =
203 SQL.get("collection.item.delete");
204
205 public static final String SQL_COLLECTIONS_TOUCH_BY_ID =
206 SQL.get("collections.touch.by.id");
207
208 public static final String SQL_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");
213 132
214 /** The singleton.*/ 133 /** The singleton.*/
215 protected static Backend instance; 134 protected static Backend instance;
135
136 protected SQLExecutor sqlExecutor;
137
138 protected DBConfig config;
216 139
217 /** 140 /**
218 * The database cleaner. Reference is stored here because 141 * The database cleaner. Reference is stored here because
219 * the cleaner is woken up if the backend finds an outdated 142 * the cleaner is woken up if the backend finds an outdated
220 * artifact. This artifact should be removed as soon as 143 * artifact. This artifact should be removed as soon as
334 * Default constructor 257 * Default constructor
335 */ 258 */
336 public Backend() { 259 public Backend() {
337 } 260 }
338 261
262 public Backend(DBConfig config) {
263 this.config = config;
264 sqlExecutor = new SQLExecutor(config.getDBConnection());
265 setupSQL(config.getSQL());
266 }
267
339 /** 268 /**
340 * Constructor to create a backend with a link to the database cleaner. 269 * Constructor to create a backend with a link to the database cleaner.
341 * @param cleaner The clean which periodically removes outdated 270 * @param cleaner The clean which periodically removes outdated
342 * artifacts from the database. 271 * artifacts from the database.
343 */ 272 */
344 public Backend(DatabaseCleaner cleaner) { 273 public Backend(DBConfig config, DatabaseCleaner cleaner) {
274 this(config);
345 this.cleaner = cleaner; 275 this.cleaner = cleaner;
346 } 276 }
347 277
278 public DBConfig getConfig() {
279 return config;
280 }
348 281
349 /** 282 /**
350 * Returns the singleton of this Backend. 283 * Returns the singleton of this Backend.
351 * 284 *
352 * @return the backend. 285 * @return the backend.
353 */ 286 */
354 public static synchronized Backend getInstance() { 287 public static synchronized Backend getInstance() {
355 if (instance == null) { 288 if (instance == null) {
356 instance = new Backend(); 289 instance = new Backend(DBConfig.getInstance());
357 } 290 }
358 291
359 return instance; 292 return instance;
293 }
294
295 protected void setupSQL(SQL sql) {
296 SQL_NEXT_ID = sql.get("artifacts.id.nextval");
297 SQL_INSERT = sql.get("artifacts.insert");
298 SQL_UPDATE = sql.get("artifacts.update");
299 SQL_TOUCH = sql.get("artifacts.touch");
300 SQL_LOAD_BY_GID = sql.get("artifacts.select.gid");
301 SQL_GET_ID = sql.get("artifacts.get.id");
302 SQL_REPLACE = sql.get("artifacts.replace");
303 SQL_USERS_NEXT_ID = sql.get("users.id.nextval");
304 SQL_USERS_INSERT = sql.get("users.insert");
305 SQL_USERS_SELECT_ID_BY_GID = sql.get("users.select.id.by.gid");
306 SQL_USERS_SELECT_GID = sql.get("users.select.gid");
307 SQL_USERS_DELETE_ID = sql.get("users.delete.id");
308 SQL_USERS_DELETE_COLLECTIONS = sql.get("users.delete.collections");
309 SQL_USERS_SELECT_ALL = sql.get("users.select.all");
310 SQL_USERS_COLLECTIONS = sql.get("users.collections");
311 SQL_USERS_COLLECTION_IDS = sql.get("users.collection.ids");
312 SQL_USERS_DELETE_ALL_COLLECTIONS =
313 sql.get("users.delete.all.collections");
314 SQL_ARTIFACTS_IN_ONLY_COLLECTION_ONLY =
315 sql.get("artifacts.in.one.collection.only");
316 SQL_OUTDATE_ARTIFACTS_COLLECTION =
317 sql.get("outdate.artifacts.collection");
318 SQL_UPDATE_COLLECTION_TTL = sql.get("collections.update.ttl");
319 SQL_UPDATE_COLLECTION_NAME = sql.get("collections.update.name");
320 SQL_OUTDATE_ARTIFACTS_USER = sql.get("outdate.artifacts.user");
321 SQL_DELETE_USER_COLLECTION_ITEMS =
322 sql.get("delete.user.collection.items");
323 SQL_COLLECTIONS_NEXT_ID = sql.get("collections.id.nextval");
324 SQL_COLLECTIONS_INSERT = sql.get("collections.insert");
325 SQL_COLLECTIONS_SELECT_USER = sql.get("collections.select.user");
326 SQL_COLLECTIONS_SELECT_ALL = sql.get("collections.select.all");
327 SQL_COLLECTIONS_SELECT_GID = sql.get("collections.select.by.gid");
328 SQL_COLLECTIONS_CREATION_TIME = sql.get("collection.creation.time");
329 SQL_COLLECTIONS_ID_BY_GID = sql.get("collections.id.by.gid");
330 SQL_DELETE_COLLECTION_ITEMS = sql.get("delete.collection.items");
331 SQL_DELETE_COLLECTION = sql.get("delete.collection");
332 SQL_COLLECTION_CHECK_ARTIFACT = sql.get("collection.check.artifact");
333 SQL_COLLECTION_ITEMS_ID_NEXTVAL =
334 sql.get("collection.items.id.nextval");
335 SQL_COLLECTION_ITEMS_INSERT = sql.get("collection.items.insert");
336 SQL_COLLECTION_GET_ATTRIBUTE = sql.get("collection.get.attribute");
337 SQL_COLLECTION_SET_ATTRIBUTE = sql.get("collection.set.attribute");
338 SQL_COLLECTION_ITEM_GET_ATTRIBUTE =
339 sql.get("collection.item.get.attribute");
340 SQL_COLLECTION_ITEM_SET_ATTRIBUTE =
341 sql.get("collection.item.set.attribute");
342 SQL_COLLECTIONS_TOUCH_BY_GID = sql.get("collections.touch.by.gid");
343 SQL_COLLECTION_ITEM_ID_CID_AID = sql.get("collection.item.id.cid.aid");
344 SQL_COLLECTION_ITEM_OUTDATE_ARTIFACT =
345 sql.get("collection.item.outdate.artifact");
346 SQL_COLLECTION_ITEM_DELETE = sql.get("collection.item.delete");
347 SQL_COLLECTIONS_TOUCH_BY_ID = sql.get("collections.touch.by.id");
348 SQL_COLLECTION_ITEMS_LIST_GID = sql.get("collection.items.list.gid");
349 SQL_ALL_ARTIFACTS = sql.get("all.artifacts");
360 } 350 }
361 351
362 /** 352 /**
363 * Sets the factory lookup mechanism to decouple ArtifactDatabase 353 * Sets the factory lookup mechanism to decouple ArtifactDatabase
364 * and Backend. 354 * and Backend.
509 return false; 499 return false;
510 } 500 }
511 501
512 final Object [] loaded = new Object[1]; 502 final Object [] loaded = new Object[1];
513 503
514 SQLExecutor exec = new SQLExecutor() { 504 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
515 public boolean doIt() throws SQLException { 505 public boolean doIt() throws SQLException {
516 prepareStatement(SQL_LOAD_BY_GID); 506 prepareStatement(SQL_LOAD_BY_GID);
517 stmnt.setString(1, identifer); 507 stmnt.setString(1, identifer);
518 508
519 result = stmnt.executeQuery(); 509 result = stmnt.executeQuery();
599 throw new RuntimeException("No valid UUID"); 589 throw new RuntimeException("No valid UUID");
600 } 590 }
601 591
602 final int [] id = new int[1]; 592 final int [] id = new int[1];
603 593
604 SQLExecutor exec = new SQLExecutor() { 594 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
605 public boolean doIt() throws SQLException { 595 public boolean doIt() throws SQLException {
606 596
607 prepareStatement(SQL_GET_ID); 597 prepareStatement(SQL_GET_ID);
608 stmnt.setString(1, uuid); 598 stmnt.setString(1, uuid);
609 result = stmnt.executeQuery(); 599 result = stmnt.executeQuery();
685 final ArtifactFactory factory, 675 final ArtifactFactory factory,
686 final Long ttl 676 final Long ttl
687 ) { 677 ) {
688 final int [] id = new int[1]; 678 final int [] id = new int[1];
689 679
690 SQLExecutor exec = new SQLExecutor() { 680 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
691 public boolean doIt() throws SQLException { 681 public boolean doIt() throws SQLException {
692 prepareStatement(SQL_NEXT_ID); 682 prepareStatement(SQL_NEXT_ID);
693 result = stmnt.executeQuery(); 683 result = stmnt.executeQuery();
694 684
695 if (!result.next()) { 685 if (!result.next()) {
736 * Touches the access timestamp of a given artifact to prevent 726 * Touches the access timestamp of a given artifact to prevent
737 * that it will be removed from the database by the database cleaner. 727 * that it will be removed from the database by the database cleaner.
738 * @param artifact The persistent wrapper around the living artifact. 728 * @param artifact The persistent wrapper around the living artifact.
739 */ 729 */
740 public void touch(final PersistentArtifact artifact) { 730 public void touch(final PersistentArtifact artifact) {
741 new SQLExecutor() { 731 sqlExecutor.new Instance() {
742 public boolean doIt() throws SQLException { 732 public boolean doIt() throws SQLException {
743 prepareStatement(SQL_TOUCH); 733 prepareStatement(SQL_TOUCH);
744 stmnt.setInt(1, artifact.getId()); 734 stmnt.setInt(1, artifact.getId());
745 stmnt.execute(); 735 stmnt.execute();
746 conn.commit(); 736 conn.commit();
753 * Writes modification of an artifact back to the database. 743 * Writes modification of an artifact back to the database.
754 * @param artifact The persistent wrapper around a living 744 * @param artifact The persistent wrapper around a living
755 * artifact. 745 * artifact.
756 */ 746 */
757 public void store(final PersistentArtifact artifact) { 747 public void store(final PersistentArtifact artifact) {
758 new SQLExecutor() { 748 sqlExecutor.new Instance() {
759 public boolean doIt() throws SQLException { 749 public boolean doIt() throws SQLException {
760 prepareStatement(SQL_UPDATE); 750 prepareStatement(SQL_UPDATE);
761 stmnt.setInt(2, artifact.getId()); 751 stmnt.setInt(2, artifact.getId());
762 752
763 byte [] bytes = artifact 753 byte [] bytes = artifact
780 ) { 770 ) {
781 final User [] user = new User[1]; 771 final User [] user = new User[1];
782 772
783 final byte [] roleData = XMLUtils.toByteArray(role, true); 773 final byte [] roleData = XMLUtils.toByteArray(role, true);
784 774
785 SQLExecutor exec = new SQLExecutor() { 775 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
786 public boolean doIt() throws SQLException { 776 public boolean doIt() throws SQLException {
787 777
788 prepareStatement(SQL_USERS_NEXT_ID); 778 prepareStatement(SQL_USERS_NEXT_ID);
789 result = stmnt.executeQuery(); 779 result = stmnt.executeQuery();
790 780
827 817
828 if (!isValidIdentifier(identifier)) { 818 if (!isValidIdentifier(identifier)) {
829 return false; 819 return false;
830 } 820 }
831 821
832 SQLExecutor exec = new SQLExecutor() { 822 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
833 public boolean doIt() throws SQLException { 823 public boolean doIt() throws SQLException {
834 prepareStatement(SQL_USERS_SELECT_ID_BY_GID); 824 prepareStatement(SQL_USERS_SELECT_ID_BY_GID);
835 825
836 stmnt.setString(1, identifier); 826 stmnt.setString(1, identifier);
837 result = stmnt.executeQuery(); 827 result = stmnt.executeQuery();
893 return null; 883 return null;
894 } 884 }
895 885
896 final User [] user = new User[1]; 886 final User [] user = new User[1];
897 887
898 SQLExecutor exec = new SQLExecutor() { 888 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
899 public boolean doIt() throws SQLException { 889 public boolean doIt() throws SQLException {
900 prepareStatement(SQL_USERS_SELECT_GID); 890 prepareStatement(SQL_USERS_SELECT_GID);
901 stmnt.setString(1, identifier); 891 stmnt.setString(1, identifier);
902 result = stmnt.executeQuery(); 892 result = stmnt.executeQuery();
903 if (!result.next()) { // no such user 893 if (!result.next()) { // no such user
922 final UserFactory factory, 912 final UserFactory factory,
923 final Object context 913 final Object context
924 ) { 914 ) {
925 final ArrayList<User> users = new ArrayList<User>(); 915 final ArrayList<User> users = new ArrayList<User>();
926 916
927 SQLExecutor exec = new SQLExecutor() { 917 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
928 public boolean doIt() throws SQLException { 918 public boolean doIt() throws SQLException {
929 prepareStatement(SQL_USERS_SELECT_ALL); 919 prepareStatement(SQL_USERS_SELECT_ALL);
930 result = stmnt.executeQuery(); 920 result = stmnt.executeQuery();
931 921
932 while (result.next()) { 922 while (result.next()) {
968 958
969 final ArtifactCollection [] collection = new ArtifactCollection[1]; 959 final ArtifactCollection [] collection = new ArtifactCollection[1];
970 960
971 final byte [] data = XMLUtils.toByteArray(attribute, true); 961 final byte [] data = XMLUtils.toByteArray(attribute, true);
972 962
973 SQLExecutor exec = new SQLExecutor() { 963 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
974 public boolean doIt() throws SQLException { 964 public boolean doIt() throws SQLException {
975 // fetch owner id 965 // fetch owner id
976 prepareStatement(SQL_USERS_SELECT_ID_BY_GID); 966 prepareStatement(SQL_USERS_SELECT_ID_BY_GID);
977 stmnt.setString(1, ownerIdentifier); 967 stmnt.setString(1, ownerIdentifier);
978 result = stmnt.executeQuery(); 968 result = stmnt.executeQuery();
1063 return null; 1053 return null;
1064 } 1054 }
1065 1055
1066 final ArtifactCollection[] ac = new ArtifactCollection[1]; 1056 final ArtifactCollection[] ac = new ArtifactCollection[1];
1067 1057
1068 SQLExecutor exec = new SQLExecutor() { 1058 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
1069 public boolean doIt() throws SQLException { 1059 public boolean doIt() throws SQLException {
1070 1060
1071 prepareStatement(SQL_COLLECTIONS_SELECT_GID); 1061 prepareStatement(SQL_COLLECTIONS_SELECT_GID);
1072 stmnt.setString(1, collectionId); 1062 stmnt.setString(1, collectionId);
1073 1063
1124 } 1114 }
1125 1115
1126 final ArrayList<ArtifactCollection> collections = 1116 final ArrayList<ArtifactCollection> collections =
1127 new ArrayList<ArtifactCollection>(); 1117 new ArrayList<ArtifactCollection>();
1128 1118
1129 SQLExecutor exec = new SQLExecutor() { 1119 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
1130 1120
1131 public boolean doIt() throws SQLException { 1121 public boolean doIt() throws SQLException {
1132 1122
1133 if (ownerIdentifier != null) { 1123 if (ownerIdentifier != null) {
1134 prepareStatement(SQL_COLLECTIONS_SELECT_USER); 1124 prepareStatement(SQL_COLLECTIONS_SELECT_USER);
1186 public boolean deleteCollection(final String collectionId) { 1176 public boolean deleteCollection(final String collectionId) {
1187 if (!isValidIdentifier(collectionId)) { 1177 if (!isValidIdentifier(collectionId)) {
1188 logger.debug("Invalid collection id: '" + collectionId + "'"); 1178 logger.debug("Invalid collection id: '" + collectionId + "'");
1189 return false; 1179 return false;
1190 } 1180 }
1191 SQLExecutor exec = new SQLExecutor() { 1181 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
1192 public boolean doIt() throws SQLException { 1182 public boolean doIt() throws SQLException {
1193 // fetch collection id 1183 // fetch collection id
1194 prepareStatement(SQL_COLLECTIONS_ID_BY_GID); 1184 prepareStatement(SQL_COLLECTIONS_ID_BY_GID);
1195 stmnt.setString(1, collectionId); 1185 stmnt.setString(1, collectionId);
1196 result = stmnt.executeQuery(); 1186 result = stmnt.executeQuery();
1230 logger.debug("collection id is not valid: " + collectionId); 1220 logger.debug("collection id is not valid: " + collectionId);
1231 } 1221 }
1232 1222
1233 final byte[][] data = new byte[1][1]; 1223 final byte[][] data = new byte[1][1];
1234 1224
1235 SQLExecutor exec = new SQLExecutor() { 1225 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
1236 public boolean doIt() throws SQLException { 1226 public boolean doIt() throws SQLException {
1237 prepareStatement(SQL_COLLECTION_GET_ATTRIBUTE); 1227 prepareStatement(SQL_COLLECTION_GET_ATTRIBUTE);
1238 stmnt.setString(1, collectionId); 1228 stmnt.setString(1, collectionId);
1239 result = stmnt.executeQuery(); 1229 result = stmnt.executeQuery();
1240 if (!result.next()) { 1230 if (!result.next()) {
1261 return false; 1251 return false;
1262 } 1252 }
1263 1253
1264 final byte [] data = XMLUtils.toByteArray(attribute, true); 1254 final byte [] data = XMLUtils.toByteArray(attribute, true);
1265 1255
1266 return new SQLExecutor() { 1256 return sqlExecutor.new Instance() {
1267 public boolean doIt() throws SQLException { 1257 public boolean doIt() throws SQLException {
1268 1258
1269 // set the column in collection items 1259 // set the column in collection items
1270 prepareStatement(SQL_COLLECTION_SET_ATTRIBUTE); 1260 prepareStatement(SQL_COLLECTION_SET_ATTRIBUTE);
1271 if (data == null) { 1261 if (data == null) {
1302 return null; 1292 return null;
1303 } 1293 }
1304 1294
1305 final byte [][] data = new byte[1][1]; 1295 final byte [][] data = new byte[1][1];
1306 1296
1307 SQLExecutor exec = new SQLExecutor() { 1297 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
1308 public boolean doIt() throws SQLException { 1298 public boolean doIt() throws SQLException {
1309 prepareStatement(SQL_COLLECTION_ITEM_GET_ATTRIBUTE); 1299 prepareStatement(SQL_COLLECTION_ITEM_GET_ATTRIBUTE);
1310 stmnt.setString(1, collectionId); 1300 stmnt.setString(1, collectionId);
1311 stmnt.setString(2, artifactId); 1301 stmnt.setString(2, artifactId);
1312 result = stmnt.executeQuery(); 1302 result = stmnt.executeQuery();
1338 return false; 1328 return false;
1339 } 1329 }
1340 1330
1341 final byte [] data = XMLUtils.toByteArray(attribute, true); 1331 final byte [] data = XMLUtils.toByteArray(attribute, true);
1342 1332
1343 return new SQLExecutor() { 1333 return sqlExecutor.new Instance() {
1344 public boolean doIt() throws SQLException { 1334 public boolean doIt() throws SQLException {
1345 1335
1346 // set the column in collection items 1336 // set the column in collection items
1347 prepareStatement(SQL_COLLECTION_ITEM_SET_ATTRIBUTE); 1337 prepareStatement(SQL_COLLECTION_ITEM_SET_ATTRIBUTE);
1348 if (data == null) { 1338 if (data == null) {
1382 return false; 1372 return false;
1383 } 1373 }
1384 1374
1385 final byte [] data = XMLUtils.toByteArray(attribute, true); 1375 final byte [] data = XMLUtils.toByteArray(attribute, true);
1386 1376
1387 SQLExecutor exec = new SQLExecutor() { 1377 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
1388 public boolean doIt() throws SQLException { 1378 public boolean doIt() throws SQLException {
1389 // fetch artifact id 1379 // fetch artifact id
1390 prepareStatement(SQL_GET_ID); 1380 prepareStatement(SQL_GET_ID);
1391 stmnt.setString(1, artifactId); 1381 stmnt.setString(1, artifactId);
1392 result = stmnt.executeQuery(); 1382 result = stmnt.executeQuery();
1455 ) { 1445 ) {
1456 if (!isValidIdentifier(collectionId)) { 1446 if (!isValidIdentifier(collectionId)) {
1457 logger.debug("Invalid collection id: '" + collectionId + "'"); 1447 logger.debug("Invalid collection id: '" + collectionId + "'");
1458 return false; 1448 return false;
1459 } 1449 }
1460 return new SQLExecutor() { 1450 return sqlExecutor.new Instance() {
1461 public boolean doIt() throws SQLException { 1451 public boolean doIt() throws SQLException {
1462 1452
1463 // fetch id, collection id and artitfact id 1453 // fetch id, collection id and artitfact id
1464 prepareStatement(SQL_COLLECTION_ITEM_ID_CID_AID); 1454 prepareStatement(SQL_COLLECTION_ITEM_ID_CID_AID);
1465 stmnt.setString(1, collectionId); 1455 stmnt.setString(1, collectionId);
1508 } 1498 }
1509 1499
1510 final ArrayList<CollectionItem> collectionItems = 1500 final ArrayList<CollectionItem> collectionItems =
1511 new ArrayList<CollectionItem>(); 1501 new ArrayList<CollectionItem>();
1512 1502
1513 SQLExecutor exec = new SQLExecutor() { 1503 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
1514 public boolean doIt() throws SQLException { 1504 public boolean doIt() throws SQLException {
1515 prepareStatement(SQL_COLLECTION_ITEMS_LIST_GID); 1505 prepareStatement(SQL_COLLECTION_ITEMS_LIST_GID);
1516 stmnt.setString(1, collectionId); 1506 stmnt.setString(1, collectionId);
1517 result = stmnt.executeQuery(); 1507 result = stmnt.executeQuery();
1518 while (result.next()) { 1508 while (result.next()) {
1536 if (!isValidIdentifier(uuid)) { 1526 if (!isValidIdentifier(uuid)) {
1537 logger.debug("Invalid collection id: '" + uuid + "'"); 1527 logger.debug("Invalid collection id: '" + uuid + "'");
1538 return false; 1528 return false;
1539 } 1529 }
1540 1530
1541 return new SQLExecutor() { 1531 return sqlExecutor.new Instance() {
1542 public boolean doIt() throws SQLException { 1532 public boolean doIt() throws SQLException {
1543 prepareStatement(SQL_UPDATE_COLLECTION_TTL); 1533 prepareStatement(SQL_UPDATE_COLLECTION_TTL);
1544 if (ttl == null) { 1534 if (ttl == null) {
1545 stmnt.setNull(1, Types.BIGINT); 1535 stmnt.setNull(1, Types.BIGINT);
1546 } 1536 }
1561 if (!isValidIdentifier(uuid)) { 1551 if (!isValidIdentifier(uuid)) {
1562 logger.debug("Invalid collection id: '" + uuid + "'"); 1552 logger.debug("Invalid collection id: '" + uuid + "'");
1563 return false; 1553 return false;
1564 } 1554 }
1565 1555
1566 return new SQLExecutor() { 1556 return sqlExecutor.new Instance() {
1567 public boolean doIt() throws SQLException { 1557 public boolean doIt() throws SQLException {
1568 prepareStatement(SQL_UPDATE_COLLECTION_NAME); 1558 prepareStatement(SQL_UPDATE_COLLECTION_NAME);
1569 stmnt.setString(1, name); 1559 stmnt.setString(1, name);
1570 stmnt.setString(2, uuid); 1560 stmnt.setString(2, uuid);
1571 stmnt.execute(); 1561 stmnt.execute();
1581 if (factoryLookup == null) { 1571 if (factoryLookup == null) {
1582 logger.error("factory lookup == null"); 1572 logger.error("factory lookup == null");
1583 return false; 1573 return false;
1584 } 1574 }
1585 1575
1586 return new SQLExecutor() { 1576 return sqlExecutor.new Instance() {
1587 public boolean doIt() throws SQLException { 1577 public boolean doIt() throws SQLException {
1588 1578
1589 // a little cache to avoid too much deserializations. 1579 // a little cache to avoid too much deserializations.
1590 Map<String, Artifact> alreadyLoaded = 1580 Map<String, Artifact> alreadyLoaded =
1591 new LinkedHashMap<String, Artifact>() { 1581 new LinkedHashMap<String, Artifact>() {

http://dive4elements.wald.intevation.org