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.FieldUnit;
|
30
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
31
|
|
32
|
|
33
|
/**
|
34
|
* @author a.mueller
|
35
|
* @since 20.03.2008
|
36
|
*/
|
37
|
@Component
|
38
|
public class AlgaTerraSiteImagesImport extends AlgaTerraImageImportBase {
|
39
|
private static final Logger logger = Logger.getLogger(AlgaTerraSiteImagesImport.class);
|
40
|
|
41
|
|
42
|
private static int modCount = 5000;
|
43
|
private static final String pluralString = "site images";
|
44
|
private static final String dbTableName = "SiteImages"; //??
|
45
|
|
46
|
public AlgaTerraSiteImagesImport(){
|
47
|
super(dbTableName, pluralString);
|
48
|
}
|
49
|
|
50
|
|
51
|
/* (non-Javadoc)
|
52
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getIdQuery()
|
53
|
*/
|
54
|
@Override
|
55
|
protected String getIdQuery(BerlinModelImportState state) {
|
56
|
String result = " SELECT SiteId "
|
57
|
+ " FROM SiteImages "
|
58
|
+ " ORDER BY EcoFactFk ";
|
59
|
return result;
|
60
|
}
|
61
|
|
62
|
/* (non-Javadoc)
|
63
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
|
64
|
*/
|
65
|
@Override
|
66
|
protected String getRecordQuery(BerlinModelImportConfigurator config) {
|
67
|
String strQuery =
|
68
|
|
69
|
" SELECT si.*, si.Comment as FigurePhrase, si.Picture as fileName, si.Path as filePath " +
|
70
|
" FROM SiteImages si "
|
71
|
+ " WHERE (si.SiteID IN (" + ID_LIST_TOKEN + ") )"
|
72
|
+ " ORDER BY EcoFactFk ";
|
73
|
;
|
74
|
return strQuery;
|
75
|
}
|
76
|
|
77
|
/* (non-Javadoc)
|
78
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#doPartition(eu.etaxonomy.cdm.io.berlinModel.in.ResultSetPartitioner, eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState)
|
79
|
*/
|
80
|
@Override
|
81
|
public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState bmState) {
|
82
|
boolean success = true;
|
83
|
|
84
|
AlgaTerraImportState state = (AlgaTerraImportState)bmState;
|
85
|
|
86
|
Set<SpecimenOrObservationBase> unitsToSave = new HashSet<SpecimenOrObservationBase>();
|
87
|
|
88
|
Map<String, FieldUnit> ecoFactFieldObservationMap = partitioner.getObjectMap(AlgaTerraSpecimenImportBase.ECO_FACT_FIELD_OBSERVATION_NAMESPACE);
|
89
|
|
90
|
ResultSet rs = partitioner.getResultSet();
|
91
|
|
92
|
try {
|
93
|
|
94
|
int i = 0;
|
95
|
|
96
|
//for each reference
|
97
|
while (rs.next()){
|
98
|
|
99
|
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
|
100
|
|
101
|
int figureId = rs.getInt("SiteId");
|
102
|
int ecoFactFk = rs.getInt("EcoFactFk");
|
103
|
|
104
|
|
105
|
//TODO etc. Created, Notes, Copyright, TermsOfUse etc.
|
106
|
|
107
|
try {
|
108
|
|
109
|
//TODO use deduplicated ecofact
|
110
|
FieldUnit fieldObservation = ecoFactFieldObservationMap.get(String.valueOf(ecoFactFk));
|
111
|
|
112
|
if (fieldObservation == null){
|
113
|
logger.warn("Could not find eco fact field observation (" + ecoFactFk +") for site image " + figureId);
|
114
|
}else{
|
115
|
|
116
|
}
|
117
|
|
118
|
//field observation
|
119
|
Media media = handleSingleImage(rs, fieldObservation, state, partitioner, PathType.Site);
|
120
|
|
121
|
handleSiteImageSpecificFields(rs, media, state);
|
122
|
|
123
|
unitsToSave.add(fieldObservation);
|
124
|
|
125
|
|
126
|
} catch (Exception e) {
|
127
|
logger.warn("Exception in " + getTableName() + ": SiteId " + 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 handleSiteImageSpecificFields(ResultSet rs, Media media, AlgaTerraImportState state) throws SQLException {
|
148
|
//TODO
|
149
|
|
150
|
}
|
151
|
|
152
|
|
153
|
@Override
|
154
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
|
155
|
|
156
|
String nameSpace;
|
157
|
Set<String> idSet;
|
158
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
|
159
|
|
160
|
try{
|
161
|
Set<String> ecoFactIdSet = new HashSet<>();
|
162
|
|
163
|
while (rs.next()){
|
164
|
handleForeignKey(rs, ecoFactIdSet, "EcoFactFk");
|
165
|
}
|
166
|
|
167
|
//field observation map
|
168
|
nameSpace = AlgaTerraSpecimenImportBase.ECO_FACT_FIELD_OBSERVATION_NAMESPACE;
|
169
|
idSet = ecoFactIdSet;
|
170
|
Map<String, FieldUnit> fieldObservationMap = getCommonService().getSourcedObjectsByIdInSourceC(FieldUnit.class, idSet, nameSpace);
|
171
|
result.put(nameSpace, fieldObservationMap);
|
172
|
|
173
|
|
174
|
} catch (SQLException e) {
|
175
|
throw new RuntimeException(e);
|
176
|
}
|
177
|
return result;
|
178
|
}
|
179
|
|
180
|
@Override
|
181
|
protected boolean doCheck(BerlinModelImportState state){
|
182
|
IOValidator<BerlinModelImportState> validator = new AlgaTerraTypeImportValidator();
|
183
|
return validator.validate(state);
|
184
|
}
|
185
|
|
186
|
@Override
|
187
|
protected boolean isIgnore(BerlinModelImportState bmState){
|
188
|
AlgaTerraImportConfigurator config = ((AlgaTerraImportState) bmState).getAlgaTerraConfigurator();
|
189
|
return ! ( config.isDoEcoFacts() && config.isDoImages()) ;
|
190
|
}
|
191
|
|
192
|
}
|