2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.io
.algaterra
;
12 import java
.sql
.ResultSet
;
13 import java
.sql
.SQLException
;
14 import java
.util
.HashMap
;
15 import java
.util
.HashSet
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.springframework
.stereotype
.Component
;
22 import eu
.etaxonomy
.cdm
.io
.algaterra
.validation
.AlgaTerraCollectionImportValidator
;
23 import eu
.etaxonomy
.cdm
.io
.berlinModel
.in
.BerlinModelImportBase
;
24 import eu
.etaxonomy
.cdm
.io
.berlinModel
.in
.BerlinModelImportConfigurator
;
25 import eu
.etaxonomy
.cdm
.io
.berlinModel
.in
.BerlinModelImportState
;
26 import eu
.etaxonomy
.cdm
.io
.common
.IOValidator
;
27 import eu
.etaxonomy
.cdm
.io
.common
.ResultSetPartitioner
;
28 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
29 import eu
.etaxonomy
.cdm
.model
.common
.OriginalSourceType
;
30 import eu
.etaxonomy
.cdm
.model
.occurrence
.Collection
;
31 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
39 public class AlgaTerraCollectionImport
extends BerlinModelImportBase
{
40 private static final Logger logger
= Logger
.getLogger(AlgaTerraCollectionImport
.class);
43 private static int modCount
= 5000;
44 private static final String pluralString
= "collections";
45 private static final String dbTableName
= "Collection"; //??
47 public static final String NAMESPACE_COLLECTION
= "Collection";
48 public static final String NAMESPACE_SUBCOLLECTION
= "Collection (Subcollection)";
51 public AlgaTerraCollectionImport(){
52 super(dbTableName
, pluralString
);
57 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getIdQuery()
60 protected String
getIdQuery(BerlinModelImportState state
) {
61 String result
= " SELECT CollectionId " +
63 + " ORDER BY partOfFk, c.CollectionId ";
68 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
71 protected String
getRecordQuery(BerlinModelImportConfigurator config
) {
73 " SELECT CollectionId, Name, Town, IHCode, Subcollection, partOfFk, TDWGGazetteerFk, Address, CultCollFlag, " +
74 " Created_When, Updated_When, Created_Who, Updated_Who, Notes " +
75 " FROM Collection c " +
76 " WHERE c.CollectionId IN (" + ID_LIST_TOKEN
+ ") "
77 + " ORDER BY partOfFk, c.CollectionId "
83 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#doPartition(eu.etaxonomy.cdm.io.berlinModel.in.ResultSetPartitioner, eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState)
86 public boolean doPartition(ResultSetPartitioner partitioner
, BerlinModelImportState bmState
) {
87 boolean success
= true;
89 AlgaTerraImportState state
= (AlgaTerraImportState
)bmState
;
90 Set
<Collection
> collectionsToSave
= new HashSet
<Collection
>();
93 Map
<String
, Collection
> collectionMap
= partitioner
.getObjectMap(NAMESPACE_COLLECTION
);
96 ResultSet rs
= partitioner
.getResultSet();
105 if ((i
++ % modCount
) == 0 && i
!= 1 ){ logger
.info(pluralString
+ " handled: " + (i
-1));}
107 int collectionId
= rs
.getInt("CollectionId");
108 String name
= rs
.getString("Name");
109 String town
= rs
.getString("Town");
110 String ihCode
= rs
.getString("IHCode");
111 String subCollectionStr
= rs
.getString("Subcollection");
112 Integer partOfFk
= nullSafeInt(rs
, "PartOfFk");
114 // Integer tdwgArea = nullSafeInt(rs, "TDWGGazetteerFk"); //somehow redundant with town
115 // String address = rs.getString("Address"); //only available for BGBM
116 // Boolean cultCollFlag = rs.getBoolean("CultCollFlag"); //?? not really needed according to Henning
118 //TODO createdUpdates, NOtes
123 Reference sourceRef
= state
.getTransactionalSourceReference();
127 Collection collection
;
128 if (partOfFk
== null){
129 collection
= makeCollection(collectionsToSave
,
130 collectionId
, name
, town
, ihCode
, sourceRef
, NAMESPACE_COLLECTION
, collectionMap
);
132 collection
= collectionMap
.get(String
.valueOf(partOfFk
));
133 if (collection
== null){
134 logger
.warn("PartOf collection not found");
140 if (isNotBlank(subCollectionStr
)){
141 Collection subCollection
= makeCollection(collectionsToSave
, collectionId
, subCollectionStr
, town
, ihCode
, sourceRef
, NAMESPACE_SUBCOLLECTION
, collectionMap
);
142 subCollection
.setSuperCollection(collection
);
146 //TODO movedToFk (extension ??)
149 } catch (Exception e
) {
150 logger
.warn("Exception in collection: CollectionId " + collectionId
+ ". " + e
.getMessage());
151 // e.printStackTrace();
156 logger
.warn(pluralString
+ " to save: " + collectionsToSave
.size());
157 getCollectionService().save(collectionsToSave
);
160 } catch (SQLException e
) {
161 logger
.error("SQLException:" + e
);
169 * @param collectionsToSave
170 * @param collectionId
175 * @param collectionMap
178 private Collection
makeCollection(Set
<Collection
> collectionsToSave
, int collectionId
,
179 String name
, String town
, String ihCode
, Reference sourceRef
, String namespace
, Map
<String
, Collection
> collectionMap
) {
180 Collection collection
= Collection
.NewInstance();
181 collection
.setName(name
);
182 if (isNotBlank(ihCode
) && ! "--".equals(ihCode
)){
183 collection
.setCode(ihCode
);
184 collection
.setCodeStandard("Index Herbariorum");
187 collection
.setTownOrLocation(town
);
188 collection
.addSource(OriginalSourceType
.Import
, String
.valueOf(collectionId
), namespace
, sourceRef
, null);
190 collectionMap
.put(String
.valueOf(collectionId
), collection
);
191 collectionsToSave
.add(collection
); //or subcollection ?
199 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
, BerlinModelImportState state
) {
203 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<Object
, Map
<String
, ?
extends CdmBase
>>();
206 Set
<String
> collectionIdSet
= new HashSet
<String
>();
209 handleForeignKey(rs
, collectionIdSet
, "partOfFk");
213 nameSpace
= NAMESPACE_COLLECTION
;
214 cdmClass
= Collection
.class;
215 idSet
= collectionIdSet
;
216 Map
<String
, Collection
> collectionMap
= (Map
<String
, Collection
>)getCommonService().getSourcedObjectsByIdInSource(cdmClass
, idSet
, nameSpace
);
217 result
.put(nameSpace
, collectionMap
);
220 } catch (SQLException e
) {
221 throw new RuntimeException(e
);
229 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
232 protected boolean doCheck(BerlinModelImportState state
){
233 IOValidator
<BerlinModelImportState
> validator
= new AlgaTerraCollectionImportValidator();
234 return validator
.validate(state
);
238 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
241 protected boolean isIgnore(BerlinModelImportState bmState
){
242 AlgaTerraImportState state
= (AlgaTerraImportState
)bmState
;
243 return ! ( state
.getAlgaTerraConfigurator().isDoEcoFacts() || state
.getAlgaTerraConfigurator().isDoTypes() );