View Javadoc

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.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.berlinModel.out.mapper.IdMapper;
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  import eu.etaxonomy.cdm.model.reference.ReferenceBase;
32  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  @SuppressWarnings("unchecked")
44  public class PesiImageExport extends PesiExportBase {
45  	private static final Logger logger = Logger.getLogger(PesiImageExport.class);
46  	private static final Class<? extends CdmBase> standardMethodParameter = ReferenceBase.class;
47  
48  	private static int modCount = 1000;
49  	private static final String dbTableName = "Image";
50  	private static final String pluralString = "DescriptionElements";
51  	private static final String parentPluralString = "Taxa";
52  	
53  	public PesiImageExport() {
54  		super();
55  	}
56  
57  	/* (non-Javadoc)
58  	 * @see eu.etaxonomy.cdm.io.common.DbExportBase#getStandardMethodParameter()
59  	 */
60  	@Override
61  	public Class<? extends CdmBase> getStandardMethodParameter() {
62  		return standardMethodParameter;
63  	}
64  
65  	/* (non-Javadoc)
66  	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
67  	 */
68  	@Override
69  	protected boolean doCheck(PesiExportState state) {
70  		boolean result = true;
71  		return result;
72  	}
73  
74  	/* (non-Javadoc)
75  	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
76  	 */
77  	@Override
78  	protected boolean doInvoke(PesiExportState state) {
79  
80  		logger.error("*** Started Making " + pluralString + " ...");
81  
82  		// Get the limit for objects to save within a single transaction.
83  //			int limit = state.getConfig().getLimitSave();
84  		int limit = 1000;
85  
86  		// Stores whether this invoke was successful or not.
87  		boolean success = true;
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 												String 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(success));
180 		
181 		return success;
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, String 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);
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 = (PesiExportConfigurator) 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 		// TODO
247 //		return state.getConfig().isDoAdditionalTaxonSource()
248 		return false;
249 	}
250 
251 }