1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.io.algaterra;
|
11
|
|
12
|
import java.sql.ResultSet;
|
13
|
import java.sql.SQLException;
|
14
|
import java.util.HashMap;
|
15
|
import java.util.HashSet;
|
16
|
import java.util.Map;
|
17
|
import java.util.Set;
|
18
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
import org.springframework.stereotype.Component;
|
21
|
|
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;
|
32
|
|
33
|
|
34
|
/**
|
35
|
* @author a.mueller
|
36
|
* @created 01.09.2012
|
37
|
*/
|
38
|
@Component
|
39
|
public class AlgaTerraCollectionImport extends BerlinModelImportBase {
|
40
|
private static final Logger logger = Logger.getLogger(AlgaTerraCollectionImport.class);
|
41
|
|
42
|
|
43
|
private static int modCount = 5000;
|
44
|
private static final String pluralString = "collections";
|
45
|
private static final String dbTableName = "Collection"; //??
|
46
|
|
47
|
public static final String NAMESPACE_COLLECTION = "Collection";
|
48
|
public static final String NAMESPACE_SUBCOLLECTION = "Collection (Subcollection)";
|
49
|
|
50
|
|
51
|
public AlgaTerraCollectionImport(){
|
52
|
super(dbTableName, pluralString);
|
53
|
}
|
54
|
|
55
|
|
56
|
/* (non-Javadoc)
|
57
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getIdQuery()
|
58
|
*/
|
59
|
@Override
|
60
|
protected String getIdQuery(BerlinModelImportState state) {
|
61
|
String result = " SELECT CollectionId " +
|
62
|
" FROM Collection c "
|
63
|
+ " ORDER BY partOfFk, c.CollectionId ";
|
64
|
return result;
|
65
|
}
|
66
|
|
67
|
/* (non-Javadoc)
|
68
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
|
69
|
*/
|
70
|
@Override
|
71
|
protected String getRecordQuery(BerlinModelImportConfigurator config) {
|
72
|
String strQuery =
|
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 "
|
78
|
;
|
79
|
return strQuery;
|
80
|
}
|
81
|
|
82
|
/* (non-Javadoc)
|
83
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#doPartition(eu.etaxonomy.cdm.io.berlinModel.in.ResultSetPartitioner, eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState)
|
84
|
*/
|
85
|
@Override
|
86
|
public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState bmState) {
|
87
|
boolean success = true;
|
88
|
|
89
|
AlgaTerraImportState state = (AlgaTerraImportState)bmState;
|
90
|
Set<Collection> collectionsToSave = new HashSet<Collection>();
|
91
|
|
92
|
|
93
|
Map<String, Collection> collectionMap = partitioner.getObjectMap(NAMESPACE_COLLECTION);
|
94
|
|
95
|
|
96
|
ResultSet rs = partitioner.getResultSet();
|
97
|
|
98
|
try {
|
99
|
|
100
|
int i = 0;
|
101
|
|
102
|
//for each reference
|
103
|
while (rs.next()){
|
104
|
|
105
|
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
|
106
|
|
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");
|
113
|
|
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
|
117
|
|
118
|
//TODO createdUpdates, NOtes
|
119
|
|
120
|
try {
|
121
|
|
122
|
//source ref
|
123
|
Reference sourceRef = state.getTransactionalSourceReference();
|
124
|
|
125
|
|
126
|
//collection
|
127
|
Collection collection;
|
128
|
if (partOfFk == null){
|
129
|
collection = makeCollection(collectionsToSave,
|
130
|
collectionId, name, town, ihCode, sourceRef, NAMESPACE_COLLECTION, collectionMap);
|
131
|
}else{
|
132
|
collection = collectionMap.get(String.valueOf(partOfFk));
|
133
|
if (collection == null){
|
134
|
logger.warn("PartOf collection not found");
|
135
|
}
|
136
|
}
|
137
|
|
138
|
|
139
|
//subcollection
|
140
|
if (isNotBlank(subCollectionStr)){
|
141
|
Collection subCollection = makeCollection(collectionsToSave, collectionId, subCollectionStr, town, ihCode, sourceRef, NAMESPACE_SUBCOLLECTION, collectionMap);
|
142
|
subCollection.setSuperCollection(collection);
|
143
|
}
|
144
|
|
145
|
|
146
|
//TODO movedToFk (extension ??)
|
147
|
|
148
|
|
149
|
} catch (Exception e) {
|
150
|
logger.warn("Exception in collection: CollectionId " + collectionId + ". " + e.getMessage());
|
151
|
// e.printStackTrace();
|
152
|
}
|
153
|
|
154
|
}
|
155
|
|
156
|
logger.warn(pluralString + " to save: " + collectionsToSave.size());
|
157
|
getCollectionService().save(collectionsToSave);
|
158
|
|
159
|
return success;
|
160
|
} catch (SQLException e) {
|
161
|
logger.error("SQLException:" + e);
|
162
|
return false;
|
163
|
}
|
164
|
}
|
165
|
|
166
|
|
167
|
|
168
|
/**
|
169
|
* @param collectionsToSave
|
170
|
* @param collectionId
|
171
|
* @param name
|
172
|
* @param town
|
173
|
* @param ihCode
|
174
|
* @param sourceRef
|
175
|
* @param collectionMap
|
176
|
* @return
|
177
|
*/
|
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");
|
185
|
}
|
186
|
|
187
|
collection.setTownOrLocation(town);
|
188
|
collection.addSource(OriginalSourceType.Import, String.valueOf(collectionId), namespace, sourceRef, null);
|
189
|
|
190
|
collectionMap.put(String.valueOf(collectionId), collection);
|
191
|
collectionsToSave.add(collection); //or subcollection ?
|
192
|
return collection;
|
193
|
}
|
194
|
|
195
|
|
196
|
|
197
|
|
198
|
@Override
|
199
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
|
200
|
String nameSpace;
|
201
|
Class cdmClass;
|
202
|
Set<String> idSet;
|
203
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
204
|
|
205
|
try{
|
206
|
Set<String> collectionIdSet = new HashSet<String>();
|
207
|
|
208
|
while (rs.next()){
|
209
|
handleForeignKey(rs, collectionIdSet, "partOfFk");
|
210
|
}
|
211
|
|
212
|
//type specimen map
|
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);
|
218
|
|
219
|
|
220
|
} catch (SQLException e) {
|
221
|
throw new RuntimeException(e);
|
222
|
}
|
223
|
return result;
|
224
|
|
225
|
}
|
226
|
|
227
|
|
228
|
/* (non-Javadoc)
|
229
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
|
230
|
*/
|
231
|
@Override
|
232
|
protected boolean doCheck(BerlinModelImportState state){
|
233
|
IOValidator<BerlinModelImportState> validator = new AlgaTerraCollectionImportValidator();
|
234
|
return validator.validate(state);
|
235
|
}
|
236
|
|
237
|
/* (non-Javadoc)
|
238
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
239
|
*/
|
240
|
@Override
|
241
|
protected boolean isIgnore(BerlinModelImportState bmState){
|
242
|
AlgaTerraImportState state = (AlgaTerraImportState)bmState;
|
243
|
return ! ( state.getAlgaTerraConfigurator().isDoEcoFacts() || state.getAlgaTerraConfigurator().isDoTypes() );
|
244
|
}
|
245
|
|
246
|
}
|