comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java @ 988:dbe39e1fb5e7

Datacage: Add collection items at initial scan. flys-artifacts/trunk@2420 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 28 Jul 2011 15:59:38 +0000
parents 82ef338fee91
children f3be883325ce
comparison
equal deleted inserted replaced
987:82ef338fee91 988:dbe39e1fb5e7
34 private String SQL_USER_BY_GID = "user.by.gid"; 34 private String SQL_USER_BY_GID = "user.by.gid";
35 private String SQL_INSERT_USER = "insert.user"; 35 private String SQL_INSERT_USER = "insert.user";
36 private String SQL_COLLECTION_BY_GID = "collection.by.gid"; 36 private String SQL_COLLECTION_BY_GID = "collection.by.gid";
37 private String SQL_COLLECTION_ID_NEXTVAL = "collection.id.nextval"; 37 private String SQL_COLLECTION_ID_NEXTVAL = "collection.id.nextval";
38 private String SQL_INSERT_COLLECTION = "insert.collection"; 38 private String SQL_INSERT_COLLECTION = "insert.collection";
39 private String SQL_ARTIFACT_BY_GID = "artifact.by.gid";
40 private String SQL_COLLECTION_ITEM_ID_NEXTVAL =
41 "collection.item.id.nextval";
42 private String SQL_INSERT_COLLECTION_ITEM = "insert.collection.item";
43
39 44
40 protected SQLExecutor sqlExecutor; 45 protected SQLExecutor sqlExecutor;
41 46
42 public class InitialScan 47 public class InitialScan
43 implements ArtifactDatabase.ArtifactLoadedCallback 48 implements ArtifactDatabase.ArtifactLoadedCallback
44 { 49 {
45 protected LRUCache<String, Integer> users; 50 protected LRUCache<String, Integer> users;
46 protected LRUCache<String, Integer> collections; 51 protected LRUCache<String, Integer> collections;
52 protected LRUCache<String, Integer> artifacts;
47 53
48 public InitialScan() { 54 public InitialScan() {
49 users = new LRUCache<String, Integer>(); 55 users = new LRUCache<String, Integer>();
50 collections = new LRUCache<String, Integer>(); 56 collections = new LRUCache<String, Integer>();
57 artifacts = new LRUCache<String, Integer>();
51 } 58 }
52 59
53 @Override 60 @Override
54 public void artifactLoaded( 61 public void artifactLoaded(
55 String userId, 62 String userId,
66 73
67 Integer uId = getUserId(userId); 74 Integer uId = getUserId(userId);
68 // TODO: We need the name of the collection 75 // TODO: We need the name of the collection
69 Integer cId = getCollectionId(collectionId, uId, "XXX"); 76 Integer cId = getCollectionId(collectionId, uId, "XXX");
70 77
71 // TODO: implement me! 78 storeArtifact(artifactId, cId, flysArtifact);
72 } 79 }
73 80
74 protected Integer getId( 81 protected Integer getId(
75 LRUCache<String, Integer> cache, 82 LRUCache<String, Integer> cache,
76 final String idString, 83 final String idString,
103 } 110 }
104 111
105 return null; 112 return null;
106 } 113 }
107 114
115 protected void storeArtifact(
116 String artifactId,
117 Integer collectionId,
118 FLYSArtifact artifact
119 ) {
120 Integer aId = getId(artifacts, artifactId, SQL_ARTIFACT_BY_GID);
121
122 if (aId != null) {
123 // We've already stored it. Just create the collection item.
124 storeCollectionItem(collectionId, aId);
125 return;
126 }
127 // We need to write it to database
128 // TODO: Implement me!
129 }
130
131 protected void storeCollectionItem(
132 final Integer collectionId,
133 final Integer artifactId
134 ) {
135 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
136 @Override
137 public boolean doIt() throws SQLException {
138 prepareStatement(SQL_COLLECTION_ITEM_ID_NEXTVAL);
139 result = stmnt.executeQuery();
140 if (!result.next()) {
141 return false;
142 }
143 int ciId = result.getInt(1);
144 reset();
145 prepareStatement(SQL_INSERT_COLLECTION_ITEM);
146 stmnt.setInt(1, ciId);
147 stmnt.setInt(2, collectionId);
148 stmnt.setInt(3, artifactId);
149 stmnt.execute();
150 return true;
151 }
152 };
153
154 if (!exec.runWrite()) {
155 log.error("storing of collection item failed.");
156 }
157 }
108 158
109 protected Integer getCollectionId( 159 protected Integer getCollectionId(
110 final String collectionId, 160 final String collectionId,
111 final Integer ownerId, 161 final Integer ownerId,
112 final String collectionName 162 final String collectionName
213 SQL_USER_BY_GID = sql.get(SQL_USER_BY_GID); 263 SQL_USER_BY_GID = sql.get(SQL_USER_BY_GID);
214 SQL_INSERT_USER = sql.get(SQL_INSERT_USER); 264 SQL_INSERT_USER = sql.get(SQL_INSERT_USER);
215 SQL_COLLECTION_BY_GID = sql.get(SQL_COLLECTION_BY_GID); 265 SQL_COLLECTION_BY_GID = sql.get(SQL_COLLECTION_BY_GID);
216 SQL_COLLECTION_ID_NEXTVAL = sql.get(SQL_COLLECTION_ID_NEXTVAL); 266 SQL_COLLECTION_ID_NEXTVAL = sql.get(SQL_COLLECTION_ID_NEXTVAL);
217 SQL_INSERT_COLLECTION = sql.get(SQL_INSERT_COLLECTION); 267 SQL_INSERT_COLLECTION = sql.get(SQL_INSERT_COLLECTION);
268 SQL_ARTIFACT_BY_GID = sql.get(SQL_ARTIFACT_BY_GID);
269 SQL_COLLECTION_ITEM_ID_NEXTVAL =
270 sql.get(SQL_COLLECTION_ITEM_ID_NEXTVAL);
271 SQL_INSERT_COLLECTION_ITEM =
272 sql.get(SQL_INSERT_COLLECTION_ITEM);
218 } 273 }
219 274
220 @Override 275 @Override
221 public void systemUp(GlobalContext context) { 276 public void systemUp(GlobalContext context) {
222 log.debug("systemUp"); 277 log.debug("systemUp");

http://dive4elements.wald.intevation.org