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