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.common.IOValidator;
|
26
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
27
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
28
|
import eu.etaxonomy.cdm.model.media.Media;
|
29
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
30
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnitBase;
|
31
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
32
|
|
33
|
|
34
|
/**
|
35
|
* @author a.mueller
|
36
|
* @created 20.03.2008
|
37
|
* @version 1.0
|
38
|
*/
|
39
|
@Component
|
40
|
public class AlgaTerraVoucherImagesImport extends AlgaTerraImageImportBase {
|
41
|
private static final Logger logger = Logger.getLogger(AlgaTerraVoucherImagesImport.class);
|
42
|
|
43
|
|
44
|
private static int modCount = 5000;
|
45
|
private static final String pluralString = "voucher images";
|
46
|
private static final String dbTableName = "VoucherImages"; //??
|
47
|
|
48
|
public AlgaTerraVoucherImagesImport(){
|
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 VoucherImageID "
|
59
|
+ " FROM VoucherImages "
|
60
|
+ " ORDER BY EcoFactFk ";
|
61
|
return result;
|
62
|
}
|
63
|
|
64
|
/* (non-Javadoc)
|
65
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
|
66
|
*/
|
67
|
@Override
|
68
|
protected String getRecordQuery(BerlinModelImportConfigurator config) {
|
69
|
String strQuery =
|
70
|
|
71
|
" SELECT vi.*, vi.Comment as FigurePhrase, vi.PictureFile as fileName " +
|
72
|
" FROM VoucherImages vi "
|
73
|
+ " WHERE (vi.VoucherImageID IN (" + ID_LIST_TOKEN + ") )"
|
74
|
+ " ORDER BY EcoFactFk ";
|
75
|
;
|
76
|
return strQuery;
|
77
|
}
|
78
|
|
79
|
/* (non-Javadoc)
|
80
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#doPartition(eu.etaxonomy.cdm.io.berlinModel.in.ResultSetPartitioner, eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState)
|
81
|
*/
|
82
|
public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState bmState) {
|
83
|
boolean success = true;
|
84
|
|
85
|
AlgaTerraImportState state = (AlgaTerraImportState)bmState;
|
86
|
|
87
|
Set<SpecimenOrObservationBase> unitsToSave = new HashSet<SpecimenOrObservationBase>();
|
88
|
|
89
|
Map<String, DerivedUnitBase> ecoFactMap = (Map<String, DerivedUnitBase>) partitioner.getObjectMap(AlgaTerraSpecimenImportBase.ECO_FACT_DERIVED_UNIT_NAMESPACE);
|
90
|
|
91
|
ResultSet rs = partitioner.getResultSet();
|
92
|
|
93
|
try {
|
94
|
|
95
|
int i = 0;
|
96
|
|
97
|
//for each reference
|
98
|
while (rs.next()){
|
99
|
|
100
|
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
|
101
|
|
102
|
int figureId = rs.getInt("VoucherImageID");
|
103
|
int ecoFactFk = rs.getInt("EcoFactFk");
|
104
|
|
105
|
|
106
|
//TODO etc. Created, Notes, Copyright, TermsOfUse etc.
|
107
|
|
108
|
try {
|
109
|
|
110
|
DerivedUnitBase<?> derivedUnit = ecoFactMap.get(ecoFactFk);
|
111
|
|
112
|
if (derivedUnit == null){
|
113
|
logger.warn("Could not find eco fact specimen (" + ecoFactFk +") for voucher image " + figureId);
|
114
|
}else{
|
115
|
|
116
|
}
|
117
|
|
118
|
//field observation
|
119
|
Media media = handleSingleImage(rs, derivedUnit, state, partitioner);
|
120
|
|
121
|
handleTypeImageSpecificFields(rs, media, state);
|
122
|
|
123
|
unitsToSave.add(derivedUnit);
|
124
|
|
125
|
|
126
|
} catch (Exception e) {
|
127
|
logger.warn("Exception in " + getTableName() + ": VoucherImageId " + figureId + ". " + e.getMessage());
|
128
|
e.printStackTrace();
|
129
|
}
|
130
|
|
131
|
}
|
132
|
|
133
|
// logger.warn("Specimen: " + countSpecimen + ", Descriptions: " + countDescriptions );
|
134
|
|
135
|
logger.warn(pluralString + " to save: " + unitsToSave.size());
|
136
|
getOccurrenceService().saveOrUpdate(unitsToSave);
|
137
|
|
138
|
return success;
|
139
|
} catch (SQLException e) {
|
140
|
logger.error("SQLException:" + e);
|
141
|
return false;
|
142
|
}
|
143
|
}
|
144
|
|
145
|
|
146
|
|
147
|
private void handleTypeImageSpecificFields(ResultSet rs, Media media, AlgaTerraImportState state) throws SQLException {
|
148
|
//TODO
|
149
|
|
150
|
}
|
151
|
|
152
|
|
153
|
/* (non-Javadoc)
|
154
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
|
155
|
*/
|
156
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
|
157
|
String nameSpace;
|
158
|
Class cdmClass;
|
159
|
Set<String> idSet;
|
160
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
161
|
|
162
|
try{
|
163
|
Set<String> ecoFactIdSet = new HashSet<String>();
|
164
|
|
165
|
while (rs.next()){
|
166
|
handleForeignKey(rs, ecoFactIdSet, "EcoFactFk");
|
167
|
}
|
168
|
|
169
|
//type specimen map
|
170
|
nameSpace = AlgaTerraSpecimenImportBase.ECO_FACT_DERIVED_UNIT_NAMESPACE;
|
171
|
cdmClass = DerivedUnitBase.class;
|
172
|
idSet = ecoFactIdSet;
|
173
|
Map<String, DerivedUnitBase> typeSpecimenMap = (Map<String,DerivedUnitBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
174
|
result.put(nameSpace, typeSpecimenMap);
|
175
|
|
176
|
|
177
|
} catch (SQLException e) {
|
178
|
throw new RuntimeException(e);
|
179
|
}
|
180
|
return result;
|
181
|
}
|
182
|
|
183
|
/* (non-Javadoc)
|
184
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
|
185
|
*/
|
186
|
@Override
|
187
|
protected boolean doCheck(BerlinModelImportState state){
|
188
|
IOValidator<BerlinModelImportState> validator = new AlgaTerraTypeImportValidator();
|
189
|
return validator.validate(state);
|
190
|
}
|
191
|
|
192
|
/* (non-Javadoc)
|
193
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
194
|
*/
|
195
|
protected boolean isIgnore(BerlinModelImportState bmState){
|
196
|
AlgaTerraImportConfigurator config = ((AlgaTerraImportState) bmState).getAlgaTerraConfigurator();
|
197
|
return ! ( config.isDoEcoFacts() && config.isDoImages()) ;
|
198
|
}
|
199
|
|
200
|
}
|