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
.mapping
.out
.IdMapper
;
25 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
26 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
27 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
28 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
29 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
30 import eu
.etaxonomy
.cdm
.model
.media
.MediaRepresentation
;
31 import eu
.etaxonomy
.cdm
.model
.media
.MediaRepresentationPart
;
32 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
37 * The export class for Images.
38 * Inserts into DataWarehouse database table <code>Image</code>.
44 @SuppressWarnings("unchecked")
45 public class PesiImageExport
extends PesiExportBase
{
46 private static final Logger logger
= Logger
.getLogger(PesiImageExport
.class);
47 private static final Class
<?
extends CdmBase
> standardMethodParameter
= Reference
.class;
49 private static int modCount
= 1000;
50 private static final String dbTableName
= "Image";
51 private static final String pluralString
= "DescriptionElements";
52 private static final String parentPluralString
= "Taxa";
54 public PesiImageExport() {
59 * @see eu.etaxonomy.cdm.io.common.DbExportBase#getStandardMethodParameter()
62 public Class
<?
extends CdmBase
> getStandardMethodParameter() {
63 return standardMethodParameter
;
67 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
70 protected boolean doCheck(PesiExportState state
) {
71 boolean result
= true;
76 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
79 protected void doInvoke(PesiExportState state
) {
81 logger
.error("*** Started Making " + pluralString
+ " ...");
83 // Get the limit for objects to save within a single transaction.
84 // int limit = state.getConfig().getLimitSave();
87 // PESI: Clear the database table Image.
90 // PESI: Create the Images
94 TransactionStatus txStatus
= null;
95 List
<TaxonBase
> list
= null;
97 Connection connection
= state
.getConfig().getDestination().getConnection();
99 txStatus
= startTransaction(true);
100 logger
.error("Started new transaction. Fetching some " + parentPluralString
+ " first (max: " + limit
+ ") ...");
101 while ((list
= getTaxonService().list(null, limit
, taxonCount
, null, null)).size() > 0) {
103 taxonCount
+= list
.size();
104 logger
.error("Fetched " + list
.size() + " " + parentPluralString
+ ".");
106 logger
.error("Check for Images...");
107 for (TaxonBase taxonBase
: list
) {
109 if (taxonBase
.isInstanceOf(Taxon
.class)) {
111 Taxon taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
113 // Determine the TaxonDescriptions
114 Set
<TaxonDescription
> taxonDescriptions
= taxon
.getDescriptions();
116 // Determine the DescriptionElements (Citations) for the current Taxon
117 for (TaxonDescription taxonDescription
: taxonDescriptions
) {
119 // Check whether this TaxonDescription contains images
120 if (taxonDescription
.isImageGallery()) {
122 Set
<DescriptionElementBase
> descriptionElements
= taxonDescription
.getElements();
124 for (DescriptionElementBase descriptionElement
: descriptionElements
) {
125 if (descriptionElement
.isInstanceOf(TextData
.class)) {
126 List
<Media
> media
= descriptionElement
.getMedia();
128 for (Media image
: media
) {
129 Set
<MediaRepresentation
> representations
= image
.getRepresentations();
131 for (MediaRepresentation representation
: representations
) {
132 List
<MediaRepresentationPart
> representationParts
= representation
.getParts();
134 for (MediaRepresentationPart representationPart
: representationParts
) {
135 URI mediaUri
= representationPart
.getUri();
139 Integer taxonFk
= state
.getDbId(taxonBase
.getName());
141 if (taxonFk
!= null && mediaUri
!= null) {
142 doCount(count
++, modCount
, pluralString
);
143 invokeImages(thumb
, mediaUri
, taxonFk
, connection
);
158 logger
.error("Exported " + (count
- pastCount
) + " " + pluralString
+ ".");
160 // Commit transaction
161 commitTransaction(txStatus
);
162 logger
.error("Committed transaction.");
163 logger
.error("Exported " + (count
- pastCount
) + " " + pluralString
+ ". Total: " + count
);
167 txStatus
= startTransaction(true);
168 logger
.error("Started new transaction. Fetching some " + parentPluralString
+ " first (max: " + limit
+ ") ...");
170 if (list
.size() == 0) {
171 logger
.error("No " + pluralString
+ " left to fetch.");
173 // Commit transaction
174 commitTransaction(txStatus
);
175 logger
.error("Committed transaction.");
177 logger
.error("*** Finished Making " + pluralString
+ " ..." + getSuccessString(true));
183 * Inserts image data into the Image datawarehouse table.
189 private void invokeImages(String thumb
, URI url
, Integer taxonFk
, Connection connection
) {
190 String imagesSql
= "INSERT INTO Image (taxonFk, img_thumb, img_url) VALUES" +
193 PreparedStatement imagesStmt
= connection
.prepareStatement(imagesSql
);
195 if (taxonFk
!= null) {
196 imagesStmt
.setInt(1, taxonFk
);
198 imagesStmt
.setObject(1, null);
202 imagesStmt
.setString(2, thumb
);
204 imagesStmt
.setObject(2, null);
208 imagesStmt
.setString(3, url
.toString());
210 imagesStmt
.setObject(3, null);
213 imagesStmt
.executeUpdate();
214 } catch (SQLException e
) {
215 logger
.error("Image could not be created. TaxonFk: " + taxonFk
+ ", Thumb: " + thumb
+ ", URL: " + url
);
222 * Deletes all entries of database tables related to <code>AdditionalTaxonSource</code>.
223 * @param state The {@link PesiExportState PesiExportState}.
224 * @return Whether the delete operation was successful or not.
226 protected boolean doDelete(PesiExportState state
) {
227 PesiExportConfigurator pesiConfig
= (PesiExportConfigurator
) state
.getConfig();
230 Source destination
= pesiConfig
.getDestination();
232 // Clear AdditionalTaxonSource
233 sql
= "DELETE FROM " + dbTableName
;
234 destination
.setQuery(sql
);
235 destination
.update(sql
);
240 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
243 protected boolean isIgnore(PesiExportState state
) {
245 // return state.getConfig().isDoAdditionalTaxonSource()