3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.io
.pesi
.out
;
13 import java
.sql
.Connection
;
14 import java
.sql
.PreparedStatement
;
15 import java
.sql
.SQLException
;
16 import java
.util
.List
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.springframework
.stereotype
.Component
;
21 import org
.springframework
.transaction
.TransactionStatus
;
23 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
24 import eu
.etaxonomy
.cdm
.io
.common
.IExportConfigurator
.DO_REFERENCES
;
25 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.out
.IdMapper
;
26 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
27 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
28 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
29 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
30 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
31 import eu
.etaxonomy
.cdm
.model
.media
.MediaRepresentation
;
32 import eu
.etaxonomy
.cdm
.model
.media
.MediaRepresentationPart
;
33 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
38 * The export class for Images.
39 * Inserts into DataWarehouse database table <code>Image</code>.
45 @SuppressWarnings("unchecked")
46 public class PesiImageExport
extends PesiExportBase
{
47 private static final Logger logger
= Logger
.getLogger(PesiImageExport
.class);
48 private static final Class
<?
extends CdmBase
> standardMethodParameter
= Reference
.class;
50 private static int modCount
= 1000;
51 private static final String dbTableName
= "Image";
52 private static final String pluralString
= "DescriptionElements";
53 private static final String parentPluralString
= "Taxa";
55 public PesiImageExport() {
60 * @see eu.etaxonomy.cdm.io.common.DbExportBase#getStandardMethodParameter()
63 public Class
<?
extends CdmBase
> getStandardMethodParameter() {
64 return standardMethodParameter
;
68 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
71 protected boolean doCheck(PesiExportState state
) {
72 boolean result
= true;
77 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
80 protected void doInvoke(PesiExportState state
) {
82 logger
.error("*** Started Making " + pluralString
+ " ...");
84 // Get the limit for objects to save within a single transaction.
85 // int limit = state.getConfig().getLimitSave();
88 // PESI: Clear the database table Image.
91 // PESI: Create the Images
95 TransactionStatus txStatus
= null;
96 List
<TaxonBase
> list
= null;
98 Connection connection
= state
.getConfig().getDestination().getConnection();
100 txStatus
= startTransaction(true);
101 logger
.error("Started new transaction. Fetching some " + parentPluralString
+ " first (max: " + limit
+ ") ...");
102 while ((list
= getTaxonService().list(null, limit
, taxonCount
, null, null)).size() > 0) {
104 taxonCount
+= list
.size();
105 logger
.error("Fetched " + list
.size() + " " + parentPluralString
+ ".");
107 logger
.error("Check for Images...");
108 for (TaxonBase taxonBase
: list
) {
110 if (taxonBase
.isInstanceOf(Taxon
.class)) {
112 Taxon taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
114 // Determine the TaxonDescriptions
115 Set
<TaxonDescription
> taxonDescriptions
= taxon
.getDescriptions();
117 // Determine the DescriptionElements (Citations) for the current Taxon
118 for (TaxonDescription taxonDescription
: taxonDescriptions
) {
120 // Check whether this TaxonDescription contains images
121 if (taxonDescription
.isImageGallery()) {
123 Set
<DescriptionElementBase
> descriptionElements
= taxonDescription
.getElements();
125 for (DescriptionElementBase descriptionElement
: descriptionElements
) {
126 if (descriptionElement
.isInstanceOf(TextData
.class)) {
127 List
<Media
> media
= descriptionElement
.getMedia();
129 for (Media image
: media
) {
130 Set
<MediaRepresentation
> representations
= image
.getRepresentations();
132 for (MediaRepresentation representation
: representations
) {
133 List
<MediaRepresentationPart
> representationParts
= representation
.getParts();
135 for (MediaRepresentationPart representationPart
: representationParts
) {
136 URI mediaUri
= representationPart
.getUri();
140 Integer taxonFk
= state
.getDbId(taxonBase
.getName());
142 if (taxonFk
!= null && mediaUri
!= null) {
143 doCount(count
++, modCount
, pluralString
);
144 invokeImages(thumb
, mediaUri
, taxonFk
, connection
);
159 logger
.error("Exported " + (count
- pastCount
) + " " + pluralString
+ ".");
161 // Commit transaction
162 commitTransaction(txStatus
);
163 logger
.error("Committed transaction.");
164 logger
.error("Exported " + (count
- pastCount
) + " " + pluralString
+ ". Total: " + count
);
168 txStatus
= startTransaction(true);
169 logger
.error("Started new transaction. Fetching some " + parentPluralString
+ " first (max: " + limit
+ ") ...");
171 if (list
.size() == 0) {
172 logger
.error("No " + pluralString
+ " left to fetch.");
174 // Commit transaction
175 commitTransaction(txStatus
);
176 logger
.error("Committed transaction.");
178 logger
.error("*** Finished Making " + pluralString
+ " ..." + getSuccessString(true));
184 * Inserts image data into the Image datawarehouse table.
190 private void invokeImages(String thumb
, URI url
, Integer taxonFk
, Connection connection
) {
191 String imagesSql
= "INSERT INTO Image (taxonFk, img_thumb, img_url) VALUES" +
194 PreparedStatement imagesStmt
= connection
.prepareStatement(imagesSql
);
196 if (taxonFk
!= null) {
197 imagesStmt
.setInt(1, taxonFk
);
199 imagesStmt
.setObject(1, null);
203 imagesStmt
.setString(2, thumb
);
205 imagesStmt
.setObject(2, null);
209 imagesStmt
.setString(3, url
.toString());
211 imagesStmt
.setObject(3, null);
214 imagesStmt
.executeUpdate();
215 } catch (SQLException e
) {
216 logger
.error("Image could not be created. TaxonFk: " + taxonFk
+ ", Thumb: " + thumb
+ ", URL: " + url
);
223 * Deletes all entries of database tables related to <code>AdditionalTaxonSource</code>.
224 * @param state The {@link PesiExportState PesiExportState}.
225 * @return Whether the delete operation was successful or not.
227 protected boolean doDelete(PesiExportState state
) {
228 PesiExportConfigurator pesiConfig
= (PesiExportConfigurator
) state
.getConfig();
231 Source destination
= pesiConfig
.getDestination();
233 // Clear AdditionalTaxonSource
234 sql
= "DELETE FROM " + dbTableName
;
235 destination
.setQuery(sql
);
236 destination
.update(sql
);
241 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
244 protected boolean isIgnore(PesiExportState state
) {
245 return ! ( state
.getConfig().isDoImages());