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.AlgaTerraTypeImportValidator;
|
23
|
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator;
|
24
|
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState;
|
25
|
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelTaxonImport;
|
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.media.Media;
|
30
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
31
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
32
|
|
33
|
|
34
|
/**
|
35
|
* Import for AlgaTerra images from table
|
36
|
* @author a.mueller
|
37
|
* @created 18.01.2013
|
38
|
*/
|
39
|
@Component
|
40
|
public class AlgaTerraPictureImport extends AlgaTerraImageImportBase {
|
41
|
private static final Logger logger = Logger.getLogger(AlgaTerraPictureImport.class);
|
42
|
|
43
|
|
44
|
private static int modCount = 5000;
|
45
|
private static final String pluralString = "pictures";
|
46
|
private static final String dbTableName = "Picture"; //??
|
47
|
|
48
|
public AlgaTerraPictureImport(){
|
49
|
super(dbTableName, pluralString);
|
50
|
}
|
51
|
|
52
|
|
53
|
/* (non-Javadoc)
|
54
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getIdQuery()
|
55
|
*/
|
56
|
@Override
|
57
|
protected String getIdQuery(BerlinModelImportState state) {
|
58
|
String result = " SELECT p.PictureId "
|
59
|
+ " FROM Picture p INNER JOIN Fact f ON p.PictureId = f.ExtensionFk LEFT OUTER JOIN PTaxon pt ON f.PTNameFk = pt.PTNameFk AND f.PTRefFk = pt.PTRefFk "
|
60
|
+ " WHERE f.FactCategoryFk = 205 "
|
61
|
+ " ORDER BY p.PictureId ";
|
62
|
return result;
|
63
|
}
|
64
|
|
65
|
/* (non-Javadoc)
|
66
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
|
67
|
*/
|
68
|
@Override
|
69
|
protected String getRecordQuery(BerlinModelImportConfigurator config) {
|
70
|
String strQuery =
|
71
|
|
72
|
" SELECT p.*, f.*, pt.RIdentifier, p.PicturePhrase as FigurePhrase, p.PictureFile as fileName, p.PicturePath as filePath " +
|
73
|
" FROM Picture p INNER JOIN Fact f ON p.PictureId = f.ExtensionFk LEFT OUTER JOIN PTaxon pt ON f.PTNameFk = pt.PTNameFk AND f.PTRefFk = pt.PTRefFk "
|
74
|
+ " WHERE f.FactCategoryFk = 205 AND ( p.PictureID IN (" + ID_LIST_TOKEN + ") )"
|
75
|
+ " ORDER BY p.PictureId, f.factId, pt.RIdentifier ";
|
76
|
;
|
77
|
return strQuery;
|
78
|
}
|
79
|
|
80
|
/* (non-Javadoc)
|
81
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#doPartition(eu.etaxonomy.cdm.io.berlinModel.in.ResultSetPartitioner, eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState)
|
82
|
*/
|
83
|
public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState bmState) {
|
84
|
boolean success = true;
|
85
|
|
86
|
AlgaTerraImportState state = (AlgaTerraImportState)bmState;
|
87
|
|
88
|
// Set<SpecimenOrObservationBase> specimenToSave = new HashSet<SpecimenOrObservationBase>();
|
89
|
Set<TaxonBase> taxaToSave = new HashSet<TaxonBase>();
|
90
|
|
91
|
// Map<String, DerivedUnitBase> ecoFactMap = (Map<String, DerivedUnitBase>) partitioner.getObjectMap(AlgaTerraSpecimenImportBase.ECO_FACT_DERIVED_UNIT_NAMESPACE);
|
92
|
Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>) partitioner.getObjectMap(BerlinModelTaxonImport.NAMESPACE);
|
93
|
|
94
|
ResultSet rs = partitioner.getResultSet();
|
95
|
|
96
|
try {
|
97
|
|
98
|
int i = 0;
|
99
|
|
100
|
//for each reference
|
101
|
while (rs.next()){
|
102
|
|
103
|
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
|
104
|
|
105
|
int pictureId = rs.getInt("PictureID");
|
106
|
int taxonId = rs.getInt("RIdentifier");
|
107
|
int factId = rs.getInt("FactId");
|
108
|
|
109
|
|
110
|
//TODO etc. Created, Notes, Copyright, TermsOfUse etc.
|
111
|
|
112
|
try {
|
113
|
|
114
|
TaxonBase<?> taxonBase = taxonMap.get(String.valueOf(taxonId));
|
115
|
if (taxonBase == null){
|
116
|
logger.warn("Could not find taxon (" + taxonId +") for picture fact " + factId);
|
117
|
}else if (! taxonBase.isInstanceOf(Taxon.class)){
|
118
|
logger.warn("Taxon is not of class Taxon but " + taxonBase.getClass() + ". RIdentifier: " + taxonId + " PictureId: " + pictureId + ", FactId: "+ factId);
|
119
|
|
120
|
}else{
|
121
|
Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
|
122
|
|
123
|
Media media = handleSingleImage(rs, taxon, state, partitioner);
|
124
|
|
125
|
handlePictureSpecificFields(rs, media, state);
|
126
|
|
127
|
taxaToSave.add(taxon);
|
128
|
}
|
129
|
|
130
|
|
131
|
} catch (Exception e) {
|
132
|
logger.warn("Exception in " + getTableName() + ": PictureId " + pictureId + ". " + e.getMessage());
|
133
|
e.printStackTrace();
|
134
|
}
|
135
|
|
136
|
}
|
137
|
|
138
|
// logger.warn("Specimen: " + countSpecimen + ", Descriptions: " + countDescriptions );
|
139
|
|
140
|
logger.warn(pluralString + " to save: " + taxaToSave.size());
|
141
|
// getOccurrenceService().save(taxaToSave);
|
142
|
getTaxonService().save(taxaToSave);
|
143
|
|
144
|
return success;
|
145
|
} catch (SQLException e) {
|
146
|
logger.error("SQLException:" + e);
|
147
|
return false;
|
148
|
}
|
149
|
}
|
150
|
|
151
|
|
152
|
|
153
|
private void handlePictureSpecificFields(ResultSet rs, Media media, AlgaTerraImportState state) throws SQLException {
|
154
|
//TODO
|
155
|
|
156
|
}
|
157
|
|
158
|
|
159
|
/* (non-Javadoc)
|
160
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
|
161
|
*/
|
162
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
|
163
|
String nameSpace;
|
164
|
Class cdmClass;
|
165
|
Set<String> idSet;
|
166
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
167
|
|
168
|
try{
|
169
|
Set<String> taxonIdSet = new HashSet<String>();
|
170
|
|
171
|
while (rs.next()){
|
172
|
handleForeignKey(rs, taxonIdSet, "RIdentifier");
|
173
|
}
|
174
|
|
175
|
//taxon map
|
176
|
nameSpace = BerlinModelTaxonImport.NAMESPACE;
|
177
|
cdmClass = TaxonBase.class;
|
178
|
idSet = taxonIdSet;
|
179
|
Map<String, TaxonBase> taxonMap = (Map<String,TaxonBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
180
|
result.put(nameSpace, taxonMap);
|
181
|
|
182
|
|
183
|
} catch (SQLException e) {
|
184
|
throw new RuntimeException(e);
|
185
|
}
|
186
|
return result;
|
187
|
}
|
188
|
|
189
|
|
190
|
|
191
|
|
192
|
/* (non-Javadoc)
|
193
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
|
194
|
*/
|
195
|
@Override
|
196
|
protected boolean doCheck(BerlinModelImportState state){
|
197
|
IOValidator<BerlinModelImportState> validator = new AlgaTerraTypeImportValidator();
|
198
|
return validator.validate(state);
|
199
|
}
|
200
|
|
201
|
/* (non-Javadoc)
|
202
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
203
|
*/
|
204
|
protected boolean isIgnore(BerlinModelImportState bmState){
|
205
|
AlgaTerraImportConfigurator config = ((AlgaTerraImportState) bmState).getAlgaTerraConfigurator();
|
206
|
return ! ( config.isDoTypes() && config.isDoImages()) ;
|
207
|
}
|
208
|
|
209
|
}
|