latest updates to PESI export
[cdmlib-apps.git] / cdm-pesi / src / main / java / eu / etaxonomy / cdm / io / pesi / out / PesiImageExport.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10 package eu.etaxonomy.cdm.io.pesi.out;
11
12 import java.net.URI;
13 import java.sql.Connection;
14 import java.sql.PreparedStatement;
15 import java.sql.SQLException;
16 import java.util.List;
17 import java.util.Set;
18
19 import org.apache.log4j.Logger;
20 import org.springframework.stereotype.Component;
21 import org.springframework.transaction.TransactionStatus;
22
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;
36
37 /**
38 * The export class for Images.
39 * Inserts into DataWarehouse database table <code>Image</code>.
40 * @author e.-m.lee
41 * @date 18.08.2010
42 *
43 */
44 @Component
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;
49
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";
54
55 public PesiImageExport() {
56 super();
57 }
58
59 /* (non-Javadoc)
60 * @see eu.etaxonomy.cdm.io.common.DbExportBase#getStandardMethodParameter()
61 */
62 @Override
63 public Class<? extends CdmBase> getStandardMethodParameter() {
64 return standardMethodParameter;
65 }
66
67 /* (non-Javadoc)
68 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
69 */
70 @Override
71 protected boolean doCheck(PesiExportState state) {
72 boolean result = true;
73 return result;
74 }
75
76 /* (non-Javadoc)
77 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
78 */
79 @Override
80 protected void doInvoke(PesiExportState state) {
81
82 logger.error("*** Started Making " + pluralString + " ...");
83
84 // Get the limit for objects to save within a single transaction.
85 // int limit = state.getConfig().getLimitSave();
86 int limit = 1000;
87
88 // PESI: Clear the database table Image.
89 doDelete(state);
90
91 // PESI: Create the Images
92 int count = 0;
93 int taxonCount = 0;
94 int pastCount = 0;
95 TransactionStatus txStatus = null;
96 List<TaxonBase> list = null;
97
98 Connection connection = state.getConfig().getDestination().getConnection();
99 // Start transaction
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) {
103
104 taxonCount += list.size();
105 logger.error("Fetched " + list.size() + " " + parentPluralString + ".");
106
107 logger.error("Check for Images...");
108 for (TaxonBase taxonBase : list) {
109
110 if (taxonBase.isInstanceOf(Taxon.class)) {
111
112 Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
113
114 // Determine the TaxonDescriptions
115 Set<TaxonDescription> taxonDescriptions = taxon.getDescriptions();
116
117 // Determine the DescriptionElements (Citations) for the current Taxon
118 for (TaxonDescription taxonDescription : taxonDescriptions) {
119
120 // Check whether this TaxonDescription contains images
121 if (taxonDescription.isImageGallery()) {
122
123 Set<DescriptionElementBase> descriptionElements = taxonDescription.getElements();
124
125 for (DescriptionElementBase descriptionElement : descriptionElements) {
126 if (descriptionElement.isInstanceOf(TextData.class)) {
127 List<Media> media = descriptionElement.getMedia();
128
129 for (Media image : media) {
130 Set<MediaRepresentation> representations = image.getRepresentations();
131
132 for (MediaRepresentation representation : representations) {
133 List<MediaRepresentationPart> representationParts = representation.getParts();
134
135 for (MediaRepresentationPart representationPart : representationParts) {
136 URI mediaUri = representationPart.getUri();
137
138 // Add image data
139 String thumb = null;
140 Integer taxonFk = state.getDbId(taxonBase.getName());
141
142 if (taxonFk != null && mediaUri != null) {
143 doCount(count++, modCount, pluralString);
144 invokeImages(thumb, mediaUri, taxonFk, connection);
145 }
146 }
147 }
148
149 }
150 }
151 }
152
153 }
154
155 }
156 }
157
158 }
159 logger.error("Exported " + (count - pastCount) + " " + pluralString + ".");
160
161 // Commit transaction
162 commitTransaction(txStatus);
163 logger.error("Committed transaction.");
164 logger.error("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
165 pastCount = count;
166
167 // Start transaction
168 txStatus = startTransaction(true);
169 logger.error("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
170 }
171 if (list.size() == 0) {
172 logger.error("No " + pluralString + " left to fetch.");
173 }
174 // Commit transaction
175 commitTransaction(txStatus);
176 logger.error("Committed transaction.");
177
178 logger.error("*** Finished Making " + pluralString + " ..." + getSuccessString(true));
179
180 return;
181 }
182
183 /**
184 * Inserts image data into the Image datawarehouse table.
185 * @param thumb
186 * @param url
187 * @param taxonFk
188 * @param connection
189 */
190 private void invokeImages(String thumb, URI url, Integer taxonFk, Connection connection) {
191 String imagesSql = "INSERT INTO Image (taxonFk, img_thumb, img_url) VALUES" +
192 " (?, ?, ?)";
193 try {
194 PreparedStatement imagesStmt = connection.prepareStatement(imagesSql);
195
196 if (taxonFk != null) {
197 imagesStmt.setInt(1, taxonFk);
198 } else {
199 imagesStmt.setObject(1, null);
200 }
201
202 if (thumb != null) {
203 imagesStmt.setString(2, thumb);
204 } else {
205 imagesStmt.setObject(2, null);
206 }
207
208 if (url != null) {
209 imagesStmt.setString(3, url.toString());
210 } else {
211 imagesStmt.setObject(3, null);
212 }
213
214 imagesStmt.executeUpdate();
215 } catch (SQLException e) {
216 logger.error("Image could not be created. TaxonFk: " + taxonFk + ", Thumb: " + thumb + ", URL: " + url);
217 e.printStackTrace();
218 }
219
220 }
221
222 /**
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.
226 */
227 protected boolean doDelete(PesiExportState state) {
228 PesiExportConfigurator pesiConfig = (PesiExportConfigurator) state.getConfig();
229
230 String sql;
231 Source destination = pesiConfig.getDestination();
232
233 // Clear AdditionalTaxonSource
234 sql = "DELETE FROM " + dbTableName;
235 destination.setQuery(sql);
236 destination.update(sql);
237 return true;
238 }
239
240 /* (non-Javadoc)
241 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
242 */
243 @Override
244 protected boolean isIgnore(PesiExportState state) {
245 return ! ( state.getConfig().isDoImages());
246 }
247
248 }