2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.algaterra
;
11 import java
.sql
.ResultSet
;
12 import java
.sql
.SQLException
;
13 import java
.util
.HashMap
;
14 import java
.util
.HashSet
;
18 import org
.apache
.logging
.log4j
.LogManager
;
19 import org
.apache
.logging
.log4j
.Logger
;
20 import org
.springframework
.stereotype
.Component
;
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
;
37 public class AlgaTerraSiteImagesImport
extends AlgaTerraImageImportBase
{
39 private static final long serialVersionUID
= 1L;
40 private static final Logger logger
= LogManager
.getLogger();
42 private static int modCount
= 5000;
43 private static final String pluralString
= "site images";
44 private static final String dbTableName
= "SiteImages"; //??
46 public AlgaTerraSiteImagesImport(){
47 super(dbTableName
, pluralString
);
52 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getIdQuery()
55 protected String
getIdQuery(BerlinModelImportState state
) {
56 String result
= " SELECT SiteId "
58 + " ORDER BY EcoFactFk ";
63 protected String
getRecordQuery(BerlinModelImportConfigurator config
) {
66 " SELECT si.*, si.Comment as FigurePhrase, si.Picture as fileName, si.Path as filePath " +
67 " FROM SiteImages si "
68 + " WHERE (si.SiteID IN (" + ID_LIST_TOKEN
+ ") )"
69 + " ORDER BY EcoFactFk ";
75 public boolean doPartition(ResultSetPartitioner partitioner
, BerlinModelImportState bmState
) {
76 boolean success
= true;
78 AlgaTerraImportState state
= (AlgaTerraImportState
)bmState
;
80 Set
<SpecimenOrObservationBase
> unitsToSave
= new HashSet
<SpecimenOrObservationBase
>();
82 Map
<String
, FieldUnit
> ecoFactFieldObservationMap
= partitioner
.getObjectMap(AlgaTerraSpecimenImportBase
.ECO_FACT_FIELD_OBSERVATION_NAMESPACE
);
84 ResultSet rs
= partitioner
.getResultSet();
93 if ((i
++ % modCount
) == 0 && i
!= 1 ){ logger
.info(pluralString
+ " handled: " + (i
-1));}
95 int figureId
= rs
.getInt("SiteId");
96 int ecoFactFk
= rs
.getInt("EcoFactFk");
99 //TODO etc. Created, Notes, Copyright, TermsOfUse etc.
103 //TODO use deduplicated ecofact
104 FieldUnit fieldObservation
= ecoFactFieldObservationMap
.get(String
.valueOf(ecoFactFk
));
106 if (fieldObservation
== null){
107 logger
.warn("Could not find eco fact field observation (" + ecoFactFk
+") for site image " + figureId
);
113 Media media
= handleSingleImage(rs
, fieldObservation
, state
, partitioner
, PathType
.Site
);
115 handleSiteImageSpecificFields(rs
, media
, state
);
117 unitsToSave
.add(fieldObservation
);
120 } catch (Exception e
) {
121 logger
.warn("Exception in " + getTableName() + ": SiteId " + figureId
+ ". " + e
.getMessage());
127 // logger.warn("Specimen: " + countSpecimen + ", Descriptions: " + countDescriptions );
129 logger
.warn(pluralString
+ " to save: " + unitsToSave
.size());
130 getOccurrenceService().saveOrUpdate(unitsToSave
);
133 } catch (SQLException e
) {
134 logger
.error("SQLException:" + e
);
139 private void handleSiteImageSpecificFields(ResultSet rs
, Media media
, AlgaTerraImportState state
) throws SQLException
{
144 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
, BerlinModelImportState state
) {
148 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
151 Set
<String
> ecoFactIdSet
= new HashSet
<>();
154 handleForeignKey(rs
, ecoFactIdSet
, "EcoFactFk");
157 //field observation map
158 nameSpace
= AlgaTerraSpecimenImportBase
.ECO_FACT_FIELD_OBSERVATION_NAMESPACE
;
159 idSet
= ecoFactIdSet
;
160 Map
<String
, FieldUnit
> fieldObservationMap
= getCommonService().getSourcedObjectsByIdInSourceC(FieldUnit
.class, idSet
, nameSpace
);
161 result
.put(nameSpace
, fieldObservationMap
);
164 } catch (SQLException e
) {
165 throw new RuntimeException(e
);
171 protected boolean doCheck(BerlinModelImportState state
){
172 IOValidator
<BerlinModelImportState
> validator
= new AlgaTerraTypeImportValidator();
173 return validator
.validate(state
);
177 protected boolean isIgnore(BerlinModelImportState bmState
){
178 AlgaTerraImportConfigurator config
= ((AlgaTerraImportState
) bmState
).getAlgaTerraConfigurator();
179 return ! ( config
.isDoEcoFacts() && config
.isDoImages()) ;