Project

General

Profile

Download (7.58 KB) Statistics
| Branch: | Revision:
1
/**
2
* Copyright (C) 2009 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
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.
8
*/
9
package eu.etaxonomy.cdm.io.pesi.out.old;
10

    
11
import java.net.URI;
12
import java.sql.Connection;
13
import java.sql.PreparedStatement;
14
import java.sql.SQLException;
15
import java.util.List;
16
import java.util.Set;
17

    
18
import org.apache.log4j.Logger;
19
import org.springframework.stereotype.Component;
20
import org.springframework.transaction.TransactionStatus;
21

    
22
import eu.etaxonomy.cdm.io.common.Source;
23
import eu.etaxonomy.cdm.io.pesi.out.PesiExportBase;
24
import eu.etaxonomy.cdm.io.pesi.out.PesiExportConfigurator;
25
import eu.etaxonomy.cdm.io.pesi.out.PesiExportState;
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
 * @since 18.08.2010
42
 */
43
@Component
44
public class PesiImageExport_Old extends PesiExportBase {
45

    
46
    private static final long serialVersionUID = 5644950520054486619L;
47
    private static final Logger logger = Logger.getLogger(PesiImageExport_Old.class);
48

    
49
    private static final Class<? extends CdmBase> standardMethodParameter = Reference.class;
50

    
51
	private static int modCount = 1000;
52
	private static final String dbTableName = "Image";
53
	private static final String pluralString = "DescriptionElements";
54
	private static final String parentPluralString = "Taxa";
55

    
56
	public PesiImageExport_Old() {
57
		super();
58
	}
59

    
60
	/* (non-Javadoc)
61
	 * @see eu.etaxonomy.cdm.io.common.DbExportBase#getStandardMethodParameter()
62
	 */
63
	@Override
64
	public Class<? extends CdmBase> getStandardMethodParameter() {
65
		return standardMethodParameter;
66
	}
67

    
68
	/* (non-Javadoc)
69
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
70
	 */
71
	@Override
72
	protected boolean doCheck(PesiExportState state) {
73
		boolean result = true;
74
		return result;
75
	}
76

    
77
	/* (non-Javadoc)
78
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
79
	 */
80
	@Override
81
	protected void doInvoke(PesiExportState state) {
82

    
83
		logger.error("*** Started Making " + pluralString + " ...");
84

    
85
		// Get the limit for objects to save within a single transaction.
86
//			int limit = state.getConfig().getLimitSave();
87
		int limit = 1000;
88

    
89
		// PESI: Clear the database table Image.
90
		doDelete(state);
91

    
92
		// PESI: Create the Images
93
		int count = 0;
94
		int taxonCount = 0;
95
		int pastCount = 0;
96
		TransactionStatus txStatus = null;
97
		List<TaxonBase> list = null;
98

    
99
		Connection connection = state.getConfig().getDestination().getConnection();
100
		// Start transaction
101
		txStatus = startTransaction(true);
102
		logger.error("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
103
		while ((list = getTaxonService().list(null, limit, taxonCount, null, null)).size() > 0) {
104

    
105
			taxonCount += list.size();
106
			logger.error("Fetched " + list.size() + " " + parentPluralString + ".");
107

    
108
			logger.error("Check for Images...");
109
			for (TaxonBase taxonBase : list) {
110

    
111
				if (taxonBase.isInstanceOf(Taxon.class)) {
112

    
113
					Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
114

    
115
					// Determine the TaxonDescriptions
116
					Set<TaxonDescription> taxonDescriptions = taxon.getDescriptions();
117

    
118
					// Determine the DescriptionElements (Citations) for the current Taxon
119
					for (TaxonDescription taxonDescription : taxonDescriptions) {
120

    
121
						// Check whether this TaxonDescription contains images
122
						if (taxonDescription.isImageGallery()) {
123

    
124
							Set<DescriptionElementBase> descriptionElements = taxonDescription.getElements();
125

    
126
							for (DescriptionElementBase descriptionElement : descriptionElements) {
127
								if (descriptionElement.isInstanceOf(TextData.class)) {
128
									List<Media> media = descriptionElement.getMedia();
129

    
130
									for (Media image : media) {
131
										Set<MediaRepresentation> representations = image.getRepresentations();
132

    
133
										for (MediaRepresentation representation : representations) {
134
											List<MediaRepresentationPart> representationParts = representation.getParts();
135

    
136
											for (MediaRepresentationPart representationPart : representationParts) {
137
												URI mediaUri = representationPart.getUri();
138

    
139
												// Add image data
140
												String thumb = null;
141
												Integer taxonFk = state.getDbId(taxonBase.getName());
142

    
143
												if (taxonFk != null && mediaUri != null) {
144
													doCount(count++, modCount, pluralString);
145
													invokeImages(thumb, mediaUri, taxonFk, connection);
146
												}
147
											}
148
										}
149

    
150
									}
151
								}
152
							}
153

    
154
						}
155

    
156
					}
157
				}
158

    
159
			}
160
			logger.error("Exported " + (count - pastCount) + " " + pluralString + ".");
161

    
162
			// Commit transaction
163
			commitTransaction(txStatus);
164
			logger.error("Committed transaction.");
165
			logger.error("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
166
			pastCount = count;
167

    
168
			// Start transaction
169
			txStatus = startTransaction(true);
170
			logger.error("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
171
		}
172
		if (list.size() == 0) {
173
			logger.error("No " + pluralString + " left to fetch.");
174
		}
175
		// Commit transaction
176
		commitTransaction(txStatus);
177
		logger.error("Committed transaction.");
178

    
179
		logger.error("*** Finished Making " + pluralString + " ..." + getSuccessString(true));
180

    
181
		return;
182
	}
183

    
184
	/**
185
	 * Inserts image data into the Image datawarehouse table.
186
	 * @param thumb
187
	 * @param url
188
	 * @param taxonFk
189
	 * @param connection
190
	 */
191
	private void invokeImages(String thumb, URI url, Integer taxonFk, Connection connection) {
192
		String imagesSql = "INSERT INTO Image (taxonFk, img_thumb, img_url) VALUES" +
193
				" (?, ?, ?)";
194
		try {
195
			PreparedStatement imagesStmt = connection.prepareStatement(imagesSql);
196

    
197
			if (taxonFk != null) {
198
				imagesStmt.setInt(1, taxonFk);
199
			} else {
200
				imagesStmt.setObject(1, null);
201
			}
202

    
203
			if (thumb != null) {
204
				imagesStmt.setString(2, thumb);
205
			} else {
206
				imagesStmt.setObject(2, null);
207
			}
208

    
209
			if (url != null) {
210
				imagesStmt.setString(3, url.toString());
211
			} else {
212
				imagesStmt.setObject(3, null);
213
			}
214

    
215
			imagesStmt.executeUpdate();
216
		} catch (SQLException e) {
217
			logger.error("Image could not be created. TaxonFk: " + taxonFk + ", Thumb: " + thumb + ", URL: " + url);
218
			e.printStackTrace();
219
		}
220

    
221
	}
222

    
223
	/**
224
	 * Deletes all entries of database tables related to <code>AdditionalTaxonSource</code>.
225
	 * @param state The {@link PesiExportState PesiExportState}.
226
	 * @return Whether the delete operation was successful or not.
227
	 */
228
	protected boolean doDelete(PesiExportState state) {
229
		PesiExportConfigurator pesiConfig = state.getConfig();
230

    
231
		String sql;
232
		Source destination =  pesiConfig.getDestination();
233

    
234
		// Clear AdditionalTaxonSource
235
		sql = "DELETE FROM " + dbTableName;
236
		destination.setQuery(sql);
237
		destination.update(sql);
238
		return true;
239
	}
240

    
241
	/* (non-Javadoc)
242
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
243
	 */
244
	@Override
245
	protected boolean isIgnore(PesiExportState state) {
246
		return ! ( state.getConfig().isDoImages());
247
	}
248

    
249
}
(2-2/6)