Project

General

Profile

Download (7.88 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
 */
44
@Component
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;
48

    
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";
53
	
54
	public PesiImageExport() {
55
		super();
56
	}
57

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

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

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

    
81
		logger.error("*** Started Making " + pluralString + " ...");
82

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

    
87
		// PESI: Clear the database table Image.
88
		doDelete(state);
89

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

    
97
		Connection connection = state.getConfig().getDestination().getConnection();
98
		// Start transaction
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) {
102

    
103
			taxonCount += list.size();
104
			logger.error("Fetched " + list.size() + " " + parentPluralString + ".");
105
			
106
			logger.error("Check for Images...");
107
			for (TaxonBase taxonBase : list) {
108
				
109
				if (taxonBase.isInstanceOf(Taxon.class)) {
110
					
111
					Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
112

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

    
116
					// Determine the DescriptionElements (Citations) for the current Taxon
117
					for (TaxonDescription taxonDescription : taxonDescriptions) {
118
						
119
						// Check whether this TaxonDescription contains images
120
						if (taxonDescription.isImageGallery()) {
121
							
122
							Set<DescriptionElementBase> descriptionElements = taxonDescription.getElements();
123
							
124
							for (DescriptionElementBase descriptionElement : descriptionElements) {
125
								if (descriptionElement.isInstanceOf(TextData.class)) {
126
									List<Media> media = descriptionElement.getMedia();
127
									
128
									for (Media image : media) {
129
										Set<MediaRepresentation> representations = image.getRepresentations();
130
										
131
										for (MediaRepresentation representation : representations) {
132
											List<MediaRepresentationPart> representationParts = representation.getParts();
133
											
134
											for (MediaRepresentationPart representationPart : representationParts) {
135
												URI mediaUri = representationPart.getUri();
136
												
137
												// Add image data
138
												String thumb = null;
139
												Integer taxonFk = state.getDbId(taxonBase.getName());
140
												
141
												if (taxonFk != null && mediaUri != null) {
142
													doCount(count++, modCount, pluralString);
143
													invokeImages(thumb, mediaUri, taxonFk, connection);
144
												}
145
											}
146
										}
147
										
148
									}
149
								}
150
							}
151
						
152
						}
153
						
154
					}
155
				}
156
				
157
			}
158
			logger.error("Exported " + (count - pastCount) + " " + pluralString + ".");
159

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

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

    
177
		logger.error("*** Finished Making " + pluralString + " ..." + getSuccessString(true));
178
		
179
		return;
180
	}
181

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

    
201
			if (thumb != null) {
202
				imagesStmt.setString(2, thumb);
203
			} else {
204
				imagesStmt.setObject(2, null);
205
			}
206
			
207
			if (url != null) {
208
				imagesStmt.setString(3, url.toString());
209
			} else {
210
				imagesStmt.setObject(3, null);
211
			}
212
			
213
			imagesStmt.executeUpdate();
214
		} catch (SQLException e) {
215
			logger.error("Image could not be created. TaxonFk: " + taxonFk + ", Thumb: " + thumb + ", URL: " + url);
216
			e.printStackTrace();
217
		}
218

    
219
	}
220

    
221
	/**
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.
225
	 */
226
	protected boolean doDelete(PesiExportState state) {
227
		PesiExportConfigurator pesiConfig = (PesiExportConfigurator) state.getConfig();
228
		
229
		String sql;
230
		Source destination =  pesiConfig.getDestination();
231

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

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

    
247
}
(2-2/6)