1 package org.bitrepository.integrityservice.cache.database;
2
3 import static org.bitrepository.integrityservice.cache.database.DatabaseConstants.COLLECTIONS_TABLE;
4 import static org.bitrepository.integrityservice.cache.database.DatabaseConstants.COLLECTION_ID;
5 import static org.bitrepository.integrityservice.cache.database.DatabaseConstants.COLLECTION_KEY;
6 import static org.bitrepository.integrityservice.cache.database.DatabaseConstants.FILE_INFO_TABLE;
7 import static org.bitrepository.integrityservice.cache.database.DatabaseConstants.FILES_TABLE;
8 import static org.bitrepository.integrityservice.cache.database.DatabaseConstants.FI_FILE_KEY;
9 import static org.bitrepository.integrityservice.cache.database.DatabaseConstants.FILES_KEY;
10 import static org.bitrepository.integrityservice.cache.database.DatabaseConstants.COLLECTION_STATS_TABLE;
11 import static org.bitrepository.integrityservice.cache.database.DatabaseConstants.PILLAR_STATS_TABLE;
12 import static org.bitrepository.integrityservice.cache.database.DatabaseConstants.STATS_TABLE;
13 import static org.bitrepository.integrityservice.cache.database.DatabaseConstants.STATS_KEY;
14
15 import java.util.List;
16
17 import org.bitrepository.service.database.DBConnector;
18 import org.bitrepository.service.database.DatabaseUtils;
19
20 public class IntegrityDBTools extends IntegrityDAOUtils {
21
22 public IntegrityDBTools(DBConnector dbConnector) {
23 super(dbConnector);
24 }
25
26
27
28
29
30
31 public void addCollection(String collectionID) throws IntegrityDBStateException {
32 List<String> existingCollections = retrieveCollectionsInDatabase();
33 if(existingCollections.contains(collectionID)) {
34 throw new IntegrityDBStateException("Collection '" + collectionID +"' already exists in integrityDB, can't add.");
35 }
36
37 String sql = "INSERT INTO " + COLLECTIONS_TABLE + " ( " + COLLECTION_ID + " ) VALUES ( ? )";
38 DatabaseUtils.executeStatement(dbConnector, sql, collectionID);
39
40 }
41
42
43
44
45
46
47
48 public void removeCollection(String collectionID) throws IntegrityDBStateException {
49 List<String> existingCollections = retrieveCollectionsInDatabase();
50 if(!existingCollections.contains(collectionID)) {
51 throw new IntegrityDBStateException("Collection '" + collectionID +"' is not present in collection, can't remove.");
52 }
53
54 Long collectionKey = retrieveCollectionKey(collectionID);
55
56 String purgeFileInfoForCollectionSql = "DELETE FROM " + FILE_INFO_TABLE
57 + " WHERE " + FI_FILE_KEY + " = ANY( SELECT " + FILES_KEY + " FROM " + FILES_TABLE
58 + " WHERE " + COLLECTION_KEY + " = ? )";
59 DatabaseUtils.executeStatement(dbConnector, purgeFileInfoForCollectionSql, collectionKey);
60
61 String purgeFilesForCollectionSql = "DELETE FROM files WHERE collection_key = ?";
62 DatabaseUtils.executeStatement(dbConnector, purgeFilesForCollectionSql, collectionKey);
63
64 String purgeCollectionStatsSql = "DELETE FROM " + COLLECTION_STATS_TABLE
65 + " WHERE " + STATS_KEY + " = ANY(SELECT " + STATS_KEY + " FROM " + STATS_TABLE
66 + " WHERE " + COLLECTION_KEY + " = ?)";
67 DatabaseUtils.executeStatement(dbConnector, purgeCollectionStatsSql, collectionKey);
68
69 String purgePillarStatsSql = "DELETE FROM " + PILLAR_STATS_TABLE
70 + " WHERE " + STATS_KEY + " = ANY(SELECT " + STATS_KEY + " FROM " + STATS_TABLE
71 + " WHERE " + COLLECTION_KEY + " = ?)";
72 DatabaseUtils.executeStatement(dbConnector, purgePillarStatsSql, collectionKey);
73
74 String purgeStatsSql = "DELETE FROM " + STATS_TABLE + " WHERE " + COLLECTION_KEY + " = ?";
75 DatabaseUtils.executeStatement(dbConnector, purgeStatsSql, collectionKey);
76
77 String removeCollectionIDSql = "DELETE FROM " + COLLECTIONS_TABLE + " WHERE " + COLLECTION_ID + " = ?";
78 DatabaseUtils.executeStatement(dbConnector, removeCollectionIDSql, collectionID);;
79
80 }
81
82 }