Mercurial > dive4elements > river
diff 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 |
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java Thu Jul 28 15:34:26 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java Thu Jul 28 15:59:38 2011 +0000 @@ -36,6 +36,11 @@ private String SQL_COLLECTION_BY_GID = "collection.by.gid"; private String SQL_COLLECTION_ID_NEXTVAL = "collection.id.nextval"; private String SQL_INSERT_COLLECTION = "insert.collection"; + private String SQL_ARTIFACT_BY_GID = "artifact.by.gid"; + private String SQL_COLLECTION_ITEM_ID_NEXTVAL = + "collection.item.id.nextval"; + private String SQL_INSERT_COLLECTION_ITEM = "insert.collection.item"; + protected SQLExecutor sqlExecutor; @@ -44,10 +49,12 @@ { protected LRUCache<String, Integer> users; protected LRUCache<String, Integer> collections; + protected LRUCache<String, Integer> artifacts; public InitialScan() { users = new LRUCache<String, Integer>(); collections = new LRUCache<String, Integer>(); + artifacts = new LRUCache<String, Integer>(); } @Override @@ -68,7 +75,7 @@ // TODO: We need the name of the collection Integer cId = getCollectionId(collectionId, uId, "XXX"); - // TODO: implement me! + storeArtifact(artifactId, cId, flysArtifact); } protected Integer getId( @@ -105,6 +112,49 @@ return null; } + protected void storeArtifact( + String artifactId, + Integer collectionId, + FLYSArtifact artifact + ) { + Integer aId = getId(artifacts, artifactId, SQL_ARTIFACT_BY_GID); + + if (aId != null) { + // We've already stored it. Just create the collection item. + storeCollectionItem(collectionId, aId); + return; + } + // We need to write it to database + // TODO: Implement me! + } + + protected void storeCollectionItem( + final Integer collectionId, + final Integer artifactId + ) { + SQLExecutor.Instance exec = sqlExecutor.new Instance() { + @Override + public boolean doIt() throws SQLException { + prepareStatement(SQL_COLLECTION_ITEM_ID_NEXTVAL); + result = stmnt.executeQuery(); + if (!result.next()) { + return false; + } + int ciId = result.getInt(1); + reset(); + prepareStatement(SQL_INSERT_COLLECTION_ITEM); + stmnt.setInt(1, ciId); + stmnt.setInt(2, collectionId); + stmnt.setInt(3, artifactId); + stmnt.execute(); + return true; + } + }; + + if (!exec.runWrite()) { + log.error("storing of collection item failed."); + } + } protected Integer getCollectionId( final String collectionId, @@ -215,6 +265,11 @@ SQL_COLLECTION_BY_GID = sql.get(SQL_COLLECTION_BY_GID); SQL_COLLECTION_ID_NEXTVAL = sql.get(SQL_COLLECTION_ID_NEXTVAL); SQL_INSERT_COLLECTION = sql.get(SQL_INSERT_COLLECTION); + SQL_ARTIFACT_BY_GID = sql.get(SQL_ARTIFACT_BY_GID); + SQL_COLLECTION_ITEM_ID_NEXTVAL = + sql.get(SQL_COLLECTION_ITEM_ID_NEXTVAL); + SQL_INSERT_COLLECTION_ITEM = + sql.get(SQL_INSERT_COLLECTION_ITEM); } @Override