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
|
import java.util.UUID;
|
19
|
|
20
|
import org.apache.commons.lang.StringUtils;
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.springframework.stereotype.Component;
|
23
|
|
24
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
|
25
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade.DerivedUnitType;
|
26
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
|
27
|
import eu.etaxonomy.cdm.io.algaterra.validation.AlgaTerraCollectionImportValidator;
|
28
|
import eu.etaxonomy.cdm.io.algaterra.validation.AlgaTerraSpecimenImportValidator;
|
29
|
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator;
|
30
|
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState;
|
31
|
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelTaxonImport;
|
32
|
import eu.etaxonomy.cdm.io.common.IOValidator;
|
33
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
34
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
35
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
36
|
import eu.etaxonomy.cdm.model.common.Language;
|
37
|
import eu.etaxonomy.cdm.model.common.Marker;
|
38
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
39
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
40
|
import eu.etaxonomy.cdm.model.description.CategoricalData;
|
41
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
42
|
import eu.etaxonomy.cdm.model.description.Feature;
|
43
|
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
|
44
|
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
|
45
|
import eu.etaxonomy.cdm.model.description.Modifier;
|
46
|
import eu.etaxonomy.cdm.model.description.QuantitativeData;
|
47
|
import eu.etaxonomy.cdm.model.description.State;
|
48
|
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
|
49
|
import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
|
50
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
51
|
import eu.etaxonomy.cdm.model.description.TextData;
|
52
|
import eu.etaxonomy.cdm.model.name.BotanicalName;
|
53
|
import eu.etaxonomy.cdm.model.name.Rank;
|
54
|
import eu.etaxonomy.cdm.model.occurrence.Collection;
|
55
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
56
|
import eu.etaxonomy.cdm.model.occurrence.FieldObservation;
|
57
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
58
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
59
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
60
|
|
61
|
|
62
|
/**
|
63
|
* @author a.mueller
|
64
|
* @created 01.09.2012
|
65
|
*/
|
66
|
@Component
|
67
|
public class AlgaTerraCollectionImport extends AlgaTerraSpecimenImportBase {
|
68
|
private static final Logger logger = Logger.getLogger(AlgaTerraCollectionImport.class);
|
69
|
|
70
|
|
71
|
private static int modCount = 5000;
|
72
|
private static final String pluralString = "collections";
|
73
|
private static final String dbTableName = "Collection"; //??
|
74
|
|
75
|
public static final String NAMESPACE_COLLECTION = "Collection";
|
76
|
public static final String NAMESPACE_SUBCOLLECTION = "Collection (Subcollection)";
|
77
|
|
78
|
|
79
|
public AlgaTerraCollectionImport(){
|
80
|
super();
|
81
|
}
|
82
|
|
83
|
|
84
|
|
85
|
/* (non-Javadoc)
|
86
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getIdQuery()
|
87
|
*/
|
88
|
@Override
|
89
|
protected String getIdQuery(BerlinModelImportState state) {
|
90
|
String result = " SELECT CollectionId " +
|
91
|
" FROM Collection c "
|
92
|
+ " ORDER BY c.CollectionId ";
|
93
|
return result;
|
94
|
}
|
95
|
|
96
|
/* (non-Javadoc)
|
97
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
|
98
|
*/
|
99
|
@Override
|
100
|
protected String getRecordQuery(BerlinModelImportConfigurator config) {
|
101
|
String strQuery =
|
102
|
" SELECT CollectionId, Name, Town, IHCode, Subcollection, TDWGGazetteerFk, Address, CultCollFlag, " +
|
103
|
" Created_When, Updated_When, Created_Who, Updated_Who, Notes " +
|
104
|
" FROM Collection c " +
|
105
|
" WHERE c.CollectionId IN (" + ID_LIST_TOKEN + ") "
|
106
|
+ " ORDER BY c.CollectionId "
|
107
|
;
|
108
|
return strQuery;
|
109
|
}
|
110
|
|
111
|
/* (non-Javadoc)
|
112
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#doPartition(eu.etaxonomy.cdm.io.berlinModel.in.ResultSetPartitioner, eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState)
|
113
|
*/
|
114
|
public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState bmState) {
|
115
|
boolean success = true;
|
116
|
|
117
|
AlgaTerraImportState state = (AlgaTerraImportState)bmState;
|
118
|
Set<Collection> collectionsToSave = new HashSet<Collection>();
|
119
|
|
120
|
ResultSet rs = partitioner.getResultSet();
|
121
|
|
122
|
try {
|
123
|
|
124
|
int i = 0;
|
125
|
|
126
|
//for each reference
|
127
|
while (rs.next()){
|
128
|
|
129
|
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
|
130
|
|
131
|
int collectionId = rs.getInt("CollectionId");
|
132
|
String name = rs.getString("Name");
|
133
|
String town = rs.getString("Town");
|
134
|
String ihCode = rs.getString("IHCode");
|
135
|
String subCollectionStr = rs.getString("Subcollection");
|
136
|
Integer tdwgArea = nullSafeInt(rs, "TDWGGazetteerFk"); //somehow redundant with town
|
137
|
String address = rs.getString("Address"); //only available for BGBM
|
138
|
Boolean cultCollFlag = rs.getBoolean("CultCollFlag"); //?? not really needed according to Henning
|
139
|
//TODO createdUpdates, NOtes
|
140
|
|
141
|
try {
|
142
|
|
143
|
//source ref
|
144
|
Reference<?> sourceRef = state.getTransactionalSourceReference();
|
145
|
|
146
|
|
147
|
Collection collection = Collection.NewInstance();
|
148
|
collection.setName(name);
|
149
|
if (isNotBlank("ihCode")){
|
150
|
collection.setCode(ihCode);
|
151
|
collection.setCodeStandard("Index Herbariorum");
|
152
|
}
|
153
|
|
154
|
collection.setTownOrLocation(town);
|
155
|
collection.addSource(String.valueOf(collectionId), NAMESPACE_COLLECTION, sourceRef, null);
|
156
|
|
157
|
collectionsToSave.add(collection); //or subcollection ?
|
158
|
|
159
|
//subcollection
|
160
|
if (isNotBlank(subCollectionStr)){
|
161
|
Collection subCollection = Collection.NewInstance();
|
162
|
subCollection.setName(subCollectionStr);
|
163
|
subCollection.setSuperCollection(collection);
|
164
|
collectionsToSave.add(subCollection); //or subcollection ?
|
165
|
collection.addSource(String.valueOf(collectionId), NAMESPACE_SUBCOLLECTION, sourceRef, null);
|
166
|
}
|
167
|
|
168
|
|
169
|
} catch (Exception e) {
|
170
|
logger.warn("Exception in collection: CollectionId " + collectionId + ". " + e.getMessage());
|
171
|
// e.printStackTrace();
|
172
|
}
|
173
|
|
174
|
}
|
175
|
|
176
|
logger.warn(pluralString + " to save: " + collectionsToSave.size());
|
177
|
getCollectionService().save(collectionsToSave);
|
178
|
|
179
|
return success;
|
180
|
} catch (SQLException e) {
|
181
|
logger.error("SQLException:" + e);
|
182
|
return false;
|
183
|
}
|
184
|
}
|
185
|
|
186
|
|
187
|
|
188
|
/* (non-Javadoc)
|
189
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
|
190
|
*/
|
191
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
|
192
|
//no related objects are needed
|
193
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
194
|
|
195
|
return result;
|
196
|
|
197
|
}
|
198
|
|
199
|
|
200
|
/* (non-Javadoc)
|
201
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
|
202
|
*/
|
203
|
@Override
|
204
|
protected boolean doCheck(BerlinModelImportState state){
|
205
|
IOValidator<BerlinModelImportState> validator = new AlgaTerraCollectionImportValidator();
|
206
|
return validator.validate(state);
|
207
|
}
|
208
|
|
209
|
/* (non-Javadoc)
|
210
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getTableName()
|
211
|
*/
|
212
|
@Override
|
213
|
protected String getTableName() {
|
214
|
return dbTableName;
|
215
|
}
|
216
|
|
217
|
/* (non-Javadoc)
|
218
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getPluralString()
|
219
|
*/
|
220
|
@Override
|
221
|
public String getPluralString() {
|
222
|
return pluralString;
|
223
|
}
|
224
|
|
225
|
/* (non-Javadoc)
|
226
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
227
|
*/
|
228
|
protected boolean isIgnore(BerlinModelImportState bmState){
|
229
|
AlgaTerraImportState state = (AlgaTerraImportState)bmState;
|
230
|
return ! ( state.getAlgaTerraConfigurator().isDoSpecimen() || state.getAlgaTerraConfigurator().isDoTypes() );
|
231
|
}
|
232
|
|
233
|
}
|