Project

General

Profile

« Previous | Next » 

Revision 788f4a81

Added by Andreas Müller over 4 years ago

ref #8509 rename old PESI factual data exports

View differences:

cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/old/PesiImageExport.java
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
}
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/old/PesiImageExport_Old.java
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
}
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/old/PesiNoteExport.java
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.sql.Connection;
12
import java.sql.PreparedStatement;
13
import java.sql.SQLException;
14
import java.util.Arrays;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.Set;
18

  
19
import org.apache.log4j.Logger;
20
import org.joda.time.DateTime;
21
import org.springframework.stereotype.Component;
22
import org.springframework.transaction.TransactionStatus;
23

  
24
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
25
import eu.etaxonomy.cdm.io.common.Source;
26
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
27
import eu.etaxonomy.cdm.io.common.mapping.out.IdMapper;
28
import eu.etaxonomy.cdm.io.common.mapping.out.MethodMapper;
29
import eu.etaxonomy.cdm.io.pesi.out.PesiExportBase;
30
import eu.etaxonomy.cdm.io.pesi.out.PesiExportConfigurator;
31
import eu.etaxonomy.cdm.io.pesi.out.PesiExportMapping;
32
import eu.etaxonomy.cdm.io.pesi.out.PesiExportState;
33
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
34
import eu.etaxonomy.cdm.model.common.CdmBase;
35
import eu.etaxonomy.cdm.model.common.Extension;
36
import eu.etaxonomy.cdm.model.common.ExtensionType;
37
import eu.etaxonomy.cdm.model.common.Language;
38
import eu.etaxonomy.cdm.model.common.LanguageString;
39
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
40
import eu.etaxonomy.cdm.model.description.DescriptionBase;
41
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
42
import eu.etaxonomy.cdm.model.description.Distribution;
43
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
44
import eu.etaxonomy.cdm.model.description.TaxonDescription;
45
import eu.etaxonomy.cdm.model.description.TaxonInteraction;
46
import eu.etaxonomy.cdm.model.description.TextData;
47
import eu.etaxonomy.cdm.model.location.NamedArea;
48
import eu.etaxonomy.cdm.model.name.TaxonName;
49
import eu.etaxonomy.cdm.model.taxon.Taxon;
50
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
51

  
52
/**
53
 * The export class for {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase DescriptionElements}.<p>
54
 * Inserts into DataWarehouse database table <code>Note</code>.<p>
55
 * It is divided into two phases:<ul>
56
 * <li>Phase 1:	Export of DescriptionElements as Notes.
57
 * <li>Phase 2:	Export of TaxonName extensions <code>taxComment</code>, <code>fauComment</code> and <code>fauExtraCodes</code> as Notes.</ul>
58
 * @author e.-m.lee
59
 * @since 23.02.2010
60
 *
61
 */
62
@Component
63
public class PesiNoteExport extends PesiExportBase {
64

  
65
    private static final long serialVersionUID = 2113856079824112001L;
66
    private static final Logger logger = Logger.getLogger(PesiNoteExport.class);
67

  
68
    private static final Class<? extends CdmBase> standardMethodParameter = DescriptionElementBase.class;
69

  
70
	private static int modCount = 1000;
71
	private static final String dbTableName = "Note";
72
	private static final String pluralString = "Notes";
73
	private static final String parentPluralString = "Taxa";
74

  
75
	public PesiNoteExport() {
76
		super();
77
	}
78

  
79
	@Override
80
	public Class<? extends CdmBase> getStandardMethodParameter() {
81
		return standardMethodParameter;
82
	}
83

  
84
	@Override
85
	protected boolean doCheck(PesiExportState state) {
86
		boolean result = true;
87
		return result;
88
	}
89

  
90
	@Override
91
	protected void doInvoke(PesiExportState state) {
92
		try {
93
			logger.info("*** Started Making " + pluralString + " ...");
94

  
95
			// Get the limit for objects to save within a single transaction.
96
			int limit = state.getConfig().getLimitSave();
97

  
98
			// Stores whether this invoke was successful or not.
99
			boolean success = true;
100

  
101
			// PESI: Clear the database table Note.
102
			doDelete(state);
103

  
104
			// Start transaction
105
			success &= doPhase01(state);
106

  
107
			logger.info("PHASE 2...");
108
			doPhase02(state, limit);
109

  
110
			logger.info("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
111

  
112
			if (!success){
113
			    state.getResult().addError("An error occurred in PesiNoteExport");
114
			}
115
			return;
116
		} catch (SQLException e) {
117
			e.printStackTrace();
118
			logger.error(e.getMessage());
119
			state.getResult().addException(e);
120
		}
121
	}
122

  
123
	//PHASE 01: Description Elements
124
	private boolean doPhase01(PesiExportState state) throws SQLException {
125
		logger.info("PHASE 1...");
126
		int count = 0;
127
		int pastCount = 0;
128
		 boolean success = true;
129

  
130
		 // Calculate the pageNumber
131
		int pageNumber = 1;
132
		int pageSize = 1000;
133

  
134

  
135
		// Get specific mappings: (CDM) DescriptionElement -> (PESI) Note
136
		PesiExportMapping mapping = getMapping();
137

  
138
		// Initialize the db mapper
139
		mapping.initialize(state);
140

  
141

  
142
		List<DescriptionElementBase> list = null;
143

  
144
		TransactionStatus txStatus = startTransaction(true);
145
		logger.info("Started new transaction. Fetching some " + pluralString + " (max: " + pageSize + ") ...");
146
		List<String> propPath = Arrays.asList(new String[]{"inDescription.taxon"});
147
		while ((list = getDescriptionService().listDescriptionElements(null, null, null, pageSize, pageNumber, propPath)).size() > 0) {
148

  
149
			logger.info("Fetched " + list.size() + " " + pluralString + ". Exporting...");
150
			for (DescriptionElementBase descriptionElement : list) {
151
				if (getNoteCategoryFk(descriptionElement) != null) {
152
					doCount(count++, modCount, pluralString);
153
					success &= mapping.invoke(descriptionElement);
154
				}
155
			}
156

  
157
			// Commit transaction
158
			commitTransaction(txStatus);
159
			logger.debug("Committed transaction.");
160
			logger.info("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
161
			pastCount = count;
162

  
163
			// Start transaction
164
			txStatus = startTransaction(true);
165
			logger.info("Started new transaction. Fetching some " + pluralString + " (max: " + pageSize + ") ...");
166

  
167
			// Increment pageNumber
168
			pageNumber++;
169
		}
170
		if (list.size() == 0) {
171
			logger.info("No " + pluralString + " left to fetch.");
172
		}
173
		// Commit transaction
174
		commitTransaction(txStatus);
175
		logger.info("Committed transaction.");
176
		return success;
177
	}
178

  
179
	//PHASE 02: Taxa extensions
180
	private void doPhase02(PesiExportState state, int limit) {
181
		TransactionStatus txStatus;
182
		txStatus = startTransaction(true);
183
		ExtensionType taxCommentExtensionType = (ExtensionType)getTermService().find(PesiTransformer.uuidExtTaxComment);
184
		ExtensionType fauCommentExtensionType = (ExtensionType)getTermService().find(PesiTransformer.uuidExtFauComment);
185
		ExtensionType fauExtraCodesExtensionType = (ExtensionType)getTermService().find(PesiTransformer.uuidExtFauExtraCodes);
186
		List<TaxonBase> taxonBaseList = null;
187

  
188
		int count = 0;
189
		int pastCount = 0;
190
		Connection connection = state.getConfig().getDestination().getConnection();
191
		logger.info("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
192
		//logger.warn("TODO handle extensions on taxon level, not name level (");
193
		while ((taxonBaseList = getTaxonService().list(null, limit, count, null, null)).size() > 0) {
194

  
195
			logger.info("Fetched " + taxonBaseList.size() + " names. Exporting...");
196
			for (TaxonBase<?> taxon : taxonBaseList) {
197
				Set<Extension> extensions = taxon.getExtensions();
198
				for (Extension extension : extensions) {
199
					if (extension.getType().equals(taxCommentExtensionType)) {
200
						String taxComment = extension.getValue();
201
						invokeNotes(taxComment,
202
								PesiTransformer.getNoteCategoryFk(PesiTransformer.uuidExtTaxComment),
203
								PesiTransformer.getNoteCategoryCache(PesiTransformer.uuidExtTaxComment),
204
								null, null, getTaxonFk(taxon.getName(), state),connection);
205
					} else if (extension.getType().equals(fauCommentExtensionType)) {
206
						String fauComment = extension.getValue();
207
						invokeNotes(fauComment,
208
								PesiTransformer.getNoteCategoryFk(PesiTransformer.uuidExtFauComment),
209
								PesiTransformer.getNoteCategoryCache(PesiTransformer.uuidExtFauComment),
210
								null, null, getTaxonFk(taxon.getName(), state),connection);
211
					} else if (extension.getType().equals(fauExtraCodesExtensionType)) {
212
						String fauExtraCodes = extension.getValue();
213
						invokeNotes(fauExtraCodes,
214
								PesiTransformer.getNoteCategoryFk(PesiTransformer.uuidExtFauExtraCodes),
215
								PesiTransformer.getNoteCategoryCache(PesiTransformer.uuidExtFauExtraCodes),
216
								null, null, getTaxonFk(taxon.getName(), state),connection);
217
					}
218
				}
219

  
220
				doCount(count++, modCount, pluralString);
221
			}
222

  
223
			// Commit transaction
224
			commitTransaction(txStatus);
225
			logger.debug("Committed transaction.");
226
			logger.info("Exported " + (count - pastCount) + " names. Total: " + count);
227
			pastCount = count;
228

  
229
			// Start transaction
230
			txStatus = startTransaction(true);
231
			logger.info("Started new transaction. Fetching some taxa first (max: " + limit + ") ...");
232
		}
233
		if (taxonBaseList.size() == 0) {
234
			logger.info("No taxa left to fetch.");
235
		}
236
		// Commit transaction
237
		commitTransaction(txStatus);
238
		logger.debug("Committed transaction.");
239
	}
240

  
241
	/**
242
	 * @param taxComment
243
	 * @param noteCategoryFk
244
	 * @param noteCategoryCache
245
	 * @param object
246
	 * @param object2
247
	 */
248
	private void invokeNotes(String note, Integer noteCategoryFk,
249
			String noteCategoryCache, Integer languageFk, String languageCache,
250
			Integer taxonFk, Connection connection) {
251
		String notesSql = "UPDATE Note SET Note_1 = ?, NoteCategoryFk = ?, NoteCategoryCache = ?, LanguageFk = ?, LanguageCache = ? WHERE TaxonFk = ?";
252
		try {
253
			PreparedStatement notesStmt = connection.prepareStatement(notesSql);
254

  
255
			if (note != null) {
256
				notesStmt.setString(1, note);
257
			} else {
258
				notesStmt.setObject(1, null);
259
			}
260

  
261
			if (noteCategoryFk != null) {
262
				notesStmt.setInt(2, noteCategoryFk);
263
			} else {
264
				notesStmt.setObject(2, null);
265
			}
266

  
267
			if (noteCategoryCache != null) {
268
				notesStmt.setString(3, noteCategoryCache);
269
			} else {
270
				notesStmt.setObject(3, null);
271
			}
272

  
273
			if (languageFk != null) {
274
				notesStmt.setInt(4, languageFk);
275
			} else {
276
				notesStmt.setObject(4, null);
277
			}
278

  
279
			if (languageCache != null) {
280
				notesStmt.setString(5, languageCache);
281
			} else {
282
				notesStmt.setObject(5, null);
283
			}
284

  
285
			if (taxonFk != null) {
286
				notesStmt.setInt(6, taxonFk);
287
			} else {
288
				notesStmt.setObject(6, null);
289
			}
290

  
291
			notesStmt.executeUpdate();
292
		} catch (SQLException e) {
293
			logger.error("Note could not be created: " + note);
294
			e.printStackTrace();
295
		}
296

  
297

  
298
	}
299

  
300
	/**
301
	 * Deletes all entries of database tables related to <code>Note</code>.
302
	 * @param state The PesiExportState
303
	 * @return Whether the delete operation was successful or not.
304
	 */
305
	protected boolean doDelete(PesiExportState state) {
306
		PesiExportConfigurator pesiConfig = state.getConfig();
307

  
308
		String sql;
309
		Source destination =  pesiConfig.getDestination();
310

  
311
		// Clear NoteSource
312
		sql = "DELETE FROM NoteSource";
313
		destination.setQuery(sql);
314
		destination.update(sql);
315

  
316
		// Clear Note
317
		sql = "DELETE FROM " + dbTableName;
318
		destination.setQuery(sql);
319
		destination.update(sql);
320
		return true;
321
	}
322

  
323
	@Override
324
	protected boolean isIgnore(PesiExportState state) {
325
		return ! state.getConfig().isDoNotes();
326
	}
327

  
328
	/**
329
	 * Returns the <code>Note_1</code> attribute.
330
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
331
	 * @return The <code>Note_1</code> attribute.
332
	 * @see MethodMapper
333
	 */
334
	@SuppressWarnings("unused")
335
	private static String getNote_1(DescriptionElementBase descriptionElement) {
336
		String result = null;
337

  
338
		if (descriptionElement.isInstanceOf(TextData.class)) {
339
			TextData textData = CdmBase.deproxy(descriptionElement, TextData.class);
340
			result = textData.getText(Language.DEFAULT());
341
		}
342

  
343
		return result;
344
	}
345

  
346
	/**
347
	 * Returns the <code>Note_2</code> attribute.
348
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
349
	 * @return The <code>Note_2</code> attribute.
350
	 * @see MethodMapper
351
	 */
352
	@SuppressWarnings("unused")
353
	private static String getNote_2(DescriptionElementBase descriptionElement) {
354
		logger.warn("Not yet implemented");
355
		return null;
356
	}
357

  
358
	/**
359
	 * Returns the <code>NoteCategoryFk</code> attribute.
360
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
361
	 * @return The <code>NoteCategoryFk</code> attribute.
362
	 * @see MethodMapper
363
	 */
364
	private static Integer getNoteCategoryFk(DescriptionElementBase descriptionElement) {
365
		Integer result = null;
366
		result = PesiTransformer.feature2NoteCategoryFk(descriptionElement.getFeature());
367
		return result;
368
	}
369

  
370
	/**
371
	 * Returns the <code>NoteCategoryCache</code> attribute.
372
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
373
	 * @return The <code>NoteCategoryCache</code> attribute.
374
	 * @see MethodMapper
375
	 */
376
	@SuppressWarnings("unused")
377
	private static String getNoteCategoryCache(DescriptionElementBase descriptionElement, PesiExportState state) {
378
		return state.getTransformer().getCacheByFeature(descriptionElement.getFeature());
379
	}
380

  
381
	/**
382
	 * Returns the <code>LanguageFk</code> attribute.
383
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
384
	 * @return The <code>LanguageFk</code> attribute.
385
	 * @see MethodMapper
386
	 */
387
	@SuppressWarnings("unused")
388
	private static Integer getLanguageFk(DescriptionElementBase descriptionElement) {
389
		Language language = getLanguage(descriptionElement);
390

  
391
		return PesiTransformer.language2LanguageId(language);
392
	}
393

  
394
	/**
395
	 * Returns the <code>LanguageCache</code> attribute.
396
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
397
	 * @return The <code>LanguageCache</code> attribute.
398
	 * @throws UndefinedTransformerMethodException
399
	 * @see MethodMapper
400
	 */
401
	@SuppressWarnings("unused")
402
	private static String getLanguageCache(DescriptionElementBase descriptionElement, PesiExportState state) throws UndefinedTransformerMethodException {
403
		Language language = getLanguage(descriptionElement);
404
		return state.getTransformer().getCacheByLanguage(language);
405
	}
406

  
407
	private static Language getLanguage(DescriptionElementBase descriptionElement) {
408
		Language language = null;
409

  
410
		Map<Language, LanguageString> multilanguageText = null;
411
		if (descriptionElement.isInstanceOf(CommonTaxonName.class)) {
412
			CommonTaxonName commonTaxonName = CdmBase.deproxy(descriptionElement, CommonTaxonName.class);
413
			language = commonTaxonName.getLanguage();
414
		} else if (descriptionElement.isInstanceOf(TextData.class)) {
415
			TextData textData = CdmBase.deproxy(descriptionElement, TextData.class);
416
			multilanguageText = textData.getMultilanguageText();
417
		} else if (descriptionElement.isInstanceOf(IndividualsAssociation.class)) {
418
			IndividualsAssociation individualsAssociation = CdmBase.deproxy(descriptionElement, IndividualsAssociation.class);
419
			multilanguageText = individualsAssociation.getDescription();
420
		} else if (descriptionElement.isInstanceOf(TaxonInteraction.class)) {
421
			TaxonInteraction taxonInteraction = CdmBase.deproxy(descriptionElement, TaxonInteraction.class);
422
			multilanguageText = taxonInteraction.getDescription();
423
		} else {
424
			logger.debug("Given descriptionElement does not support languages. Hence LanguageCache could not be determined: " + descriptionElement.getUuid());
425
		}
426

  
427
		if (multilanguageText != null) {
428
			Set<Language> languages = multilanguageText.keySet();
429

  
430
			// TODO: Think of something more sophisticated than this
431
			if (languages.size() > 0) {
432
				language = languages.iterator().next();
433
			}
434
			if (languages.size() > 1){
435
				logger.warn("There is more than 1 language for a given description (" + descriptionElement.getClass().getSimpleName() + "):" + descriptionElement.getUuid());
436
			}
437
		}
438
		return language;
439
	}
440

  
441
	/**
442
	 * Returns the <code>Region</code> attribute.
443
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
444
	 * @return The <code>Region</code> attribute.
445
	 * @see MethodMapper
446
	 */
447
	@SuppressWarnings("unused")
448
	private static String getRegion(DescriptionElementBase descriptionElement) {
449
		String result = null;
450
		DescriptionBase<?> inDescription = descriptionElement.getInDescription();
451

  
452
		try {
453
			// Area information are associated to TaxonDescriptions and Distributions.
454
			if (descriptionElement.isInstanceOf(Distribution.class)) {
455
				Distribution distribution = CdmBase.deproxy(descriptionElement, Distribution.class);
456
				//TODO not working any more after transformer refactoring
457
				result = new PesiTransformer(null).getCacheByNamedArea(distribution.getArea());
458
			} else if (inDescription != null && inDescription.isInstanceOf(TaxonDescription.class)) {
459
				TaxonDescription taxonDescription = CdmBase.deproxy(inDescription, TaxonDescription.class);
460
				Set<NamedArea> namedAreas = taxonDescription.getGeoScopes();
461
				if (namedAreas.size() == 1) {
462
					result = new PesiTransformer(null).getCacheByNamedArea(namedAreas.iterator().next());
463
				} else if (namedAreas.size() > 1) {
464
					logger.warn("This TaxonDescription contains more than one NamedArea: " + taxonDescription.getTitleCache());
465
				}
466
			}
467
		} catch (ClassCastException e) {
468
			// TODO Auto-generated catch block
469
			e.printStackTrace();
470
		} catch (UndefinedTransformerMethodException e) {
471
			// TODO Auto-generated catch block
472
			e.printStackTrace();
473
		}
474
		return result;
475
	}
476

  
477
	/**
478
	 * Returns the <code>TaxonFk</code> attribute.
479
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
480
	 * @param state The {@link PesiExportState PesiExportState}.
481
	 * @return The <code>TaxonFk</code> attribute.
482
	 * @see MethodMapper
483
	 */
484
	@SuppressWarnings("unused")
485
	private static Integer getTaxonFk(DescriptionElementBase descriptionElement, DbExportStateBase<?, PesiTransformer> state) {
486
		Integer result = null;
487
		DescriptionBase<?> inDescription = descriptionElement.getInDescription();
488
		if (inDescription != null && inDescription.isInstanceOf(TaxonDescription.class)) {
489
			TaxonDescription taxonDescription = CdmBase.deproxy(inDescription, TaxonDescription.class);
490
			Taxon taxon = taxonDescription.getTaxon();
491
			result = state.getDbId(taxon.getName());
492
		}
493
		return result;
494
	}
495

  
496
	/**
497
	 * Returns the TaxonFk for a given TaxonName.
498
	 * @param taxonName The {@link TaxonNameBase TaxonName}.
499
	 * @param state The {@link DbExportStateBase DbExportState}.
500
	 * @return
501
	 */
502
	private static Integer getTaxonFk(TaxonName taxonName, DbExportStateBase<?, PesiTransformer> state) {
503
		return state.getDbId(taxonName);
504
	}
505

  
506
	/**
507
	 * Returns the <code>LastAction</code> attribute.
508
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
509
	 * @return The <code>LastAction</code> attribute.
510
	 * @see MethodMapper
511
	 */
512
	@SuppressWarnings("unused")
513
	private static String getLastAction(DescriptionElementBase descriptionElement) {
514
		// TODO
515
		return null;
516
	}
517

  
518
	/**
519
	 * Returns the <code>LastActionDate</code> attribute.
520
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
521
	 * @return The <code>LastActionDate</code> attribute.
522
	 * @see MethodMapper
523
	 */
524
	@SuppressWarnings("unused")
525
	private static DateTime getLastActionDate(DescriptionElementBase descriptionElement) {
526
		DateTime result = null;
527
		return result;
528
	}
529

  
530
	/**
531
	 * Returns the CDM to PESI specific export mappings.
532
	 * @return The {@link PesiExportMapping PesiExportMapping}.
533
	 */
534
	private PesiExportMapping getMapping() {
535
		PesiExportMapping mapping = new PesiExportMapping(dbTableName);
536

  
537
		mapping.addMapper(IdMapper.NewInstance("NoteId"));
538
		mapping.addMapper(MethodMapper.NewInstance("Note_1", this));
539
		mapping.addMapper(MethodMapper.NewInstance("Note_2", this));
540
		mapping.addMapper(MethodMapper.NewInstance("NoteCategoryFk", this));
541
		mapping.addMapper(MethodMapper.NewInstance("NoteCategoryCache", this));
542
		mapping.addMapper(MethodMapper.NewInstance("LanguageFk", this));
543
		mapping.addMapper(MethodMapper.NewInstance("LanguageCache", this));
544
		mapping.addMapper(MethodMapper.NewInstance("Region", this));
545
		mapping.addMapper(MethodMapper.NewInstance("TaxonFk", this.getClass(), "getTaxonFk", standardMethodParameter, DbExportStateBase.class));
546
		mapping.addMapper(MethodMapper.NewInstance("LastAction", this));
547
		mapping.addMapper(MethodMapper.NewInstance("LastActionDate", this));
548

  
549
		return mapping;
550
	}
551

  
552
}
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/old/PesiNoteExport_Old.java
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.sql.Connection;
12
import java.sql.PreparedStatement;
13
import java.sql.SQLException;
14
import java.util.Arrays;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.Set;
18

  
19
import org.apache.log4j.Logger;
20
import org.joda.time.DateTime;
21
import org.springframework.stereotype.Component;
22
import org.springframework.transaction.TransactionStatus;
23

  
24
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
25
import eu.etaxonomy.cdm.io.common.Source;
26
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
27
import eu.etaxonomy.cdm.io.common.mapping.out.IdMapper;
28
import eu.etaxonomy.cdm.io.common.mapping.out.MethodMapper;
29
import eu.etaxonomy.cdm.io.pesi.out.PesiExportBase;
30
import eu.etaxonomy.cdm.io.pesi.out.PesiExportConfigurator;
31
import eu.etaxonomy.cdm.io.pesi.out.PesiExportMapping;
32
import eu.etaxonomy.cdm.io.pesi.out.PesiExportState;
33
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
34
import eu.etaxonomy.cdm.model.common.CdmBase;
35
import eu.etaxonomy.cdm.model.common.Extension;
36
import eu.etaxonomy.cdm.model.common.ExtensionType;
37
import eu.etaxonomy.cdm.model.common.Language;
38
import eu.etaxonomy.cdm.model.common.LanguageString;
39
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
40
import eu.etaxonomy.cdm.model.description.DescriptionBase;
41
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
42
import eu.etaxonomy.cdm.model.description.Distribution;
43
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
44
import eu.etaxonomy.cdm.model.description.TaxonDescription;
45
import eu.etaxonomy.cdm.model.description.TaxonInteraction;
46
import eu.etaxonomy.cdm.model.description.TextData;
47
import eu.etaxonomy.cdm.model.location.NamedArea;
48
import eu.etaxonomy.cdm.model.name.TaxonName;
49
import eu.etaxonomy.cdm.model.taxon.Taxon;
50
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
51

  
52
/**
53
 * The export class for {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase DescriptionElements}.<p>
54
 * Inserts into DataWarehouse database table <code>Note</code>.<p>
55
 * It is divided into two phases:<ul>
56
 * <li>Phase 1:	Export of DescriptionElements as Notes.
57
 * <li>Phase 2:	Export of TaxonName extensions <code>taxComment</code>, <code>fauComment</code> and <code>fauExtraCodes</code> as Notes.</ul>
58
 * @author e.-m.lee
59
 * @since 23.02.2010
60
 */
61
@Component
62
public class PesiNoteExport_Old extends PesiExportBase {
63

  
64
    private static final long serialVersionUID = 2113856079824112001L;
65
    private static final Logger logger = Logger.getLogger(PesiNoteExport_Old.class);
66

  
67
    private static final Class<? extends CdmBase> standardMethodParameter = DescriptionElementBase.class;
68

  
69
	private static int modCount = 1000;
70
	private static final String dbTableName = "Note";
71
	private static final String pluralString = "Notes";
72
	private static final String parentPluralString = "Taxa";
73

  
74
	public PesiNoteExport_Old() {
75
		super();
76
	}
77

  
78
	@Override
79
	public Class<? extends CdmBase> getStandardMethodParameter() {
80
		return standardMethodParameter;
81
	}
82

  
83
	@Override
84
	protected boolean doCheck(PesiExportState state) {
85
		boolean result = true;
86
		return result;
87
	}
88

  
89
	@Override
90
	protected void doInvoke(PesiExportState state) {
91
		try {
92
			logger.info("*** Started Making " + pluralString + " ...");
93

  
94
			// Get the limit for objects to save within a single transaction.
95
			int limit = state.getConfig().getLimitSave();
96

  
97
			// Stores whether this invoke was successful or not.
98
			boolean success = true;
99

  
100
			// PESI: Clear the database table Note.
101
			doDelete(state);
102

  
103
			// Start transaction
104
			success &= doPhase01(state);
105

  
106
			logger.info("PHASE 2...");
107
			doPhase02(state, limit);
108

  
109
			logger.info("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
110

  
111
			if (!success){
112
			    state.getResult().addError("An error occurred in PesiNoteExport");
113
			}
114
			return;
115
		} catch (SQLException e) {
116
			e.printStackTrace();
117
			logger.error(e.getMessage());
118
			state.getResult().addException(e);
119
		}
120
	}
121

  
122
	//PHASE 01: Description Elements
123
	private boolean doPhase01(PesiExportState state) throws SQLException {
124
		logger.info("PHASE 1...");
125
		int count = 0;
126
		int pastCount = 0;
127
		 boolean success = true;
128

  
129
		 // Calculate the pageNumber
130
		int pageNumber = 1;
131
		int pageSize = 1000;
132

  
133

  
134
		// Get specific mappings: (CDM) DescriptionElement -> (PESI) Note
135
		PesiExportMapping mapping = getMapping();
136

  
137
		// Initialize the db mapper
138
		mapping.initialize(state);
139

  
140

  
141
		List<DescriptionElementBase> list = null;
142

  
143
		TransactionStatus txStatus = startTransaction(true);
144
		logger.info("Started new transaction. Fetching some " + pluralString + " (max: " + pageSize + ") ...");
145
		List<String> propPath = Arrays.asList(new String[]{"inDescription.taxon"});
146
		while ((list = getDescriptionService().listDescriptionElements(null, null, null, pageSize, pageNumber, propPath)).size() > 0) {
147

  
148
			logger.info("Fetched " + list.size() + " " + pluralString + ". Exporting...");
149
			for (DescriptionElementBase descriptionElement : list) {
150
				if (getNoteCategoryFk(descriptionElement) != null) {
151
					doCount(count++, modCount, pluralString);
152
					success &= mapping.invoke(descriptionElement);
153
				}
154
			}
155

  
156
			// Commit transaction
157
			commitTransaction(txStatus);
158
			logger.debug("Committed transaction.");
159
			logger.info("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
160
			pastCount = count;
161

  
162
			// Start transaction
163
			txStatus = startTransaction(true);
164
			logger.info("Started new transaction. Fetching some " + pluralString + " (max: " + pageSize + ") ...");
165

  
166
			// Increment pageNumber
167
			pageNumber++;
168
		}
169
		if (list.size() == 0) {
170
			logger.info("No " + pluralString + " left to fetch.");
171
		}
172
		// Commit transaction
173
		commitTransaction(txStatus);
174
		logger.info("Committed transaction.");
175
		return success;
176
	}
177

  
178
	//PHASE 02: Taxa extensions
179
	private void doPhase02(PesiExportState state, int limit) {
180
		TransactionStatus txStatus;
181
		txStatus = startTransaction(true);
182
		ExtensionType taxCommentExtensionType = (ExtensionType)getTermService().find(PesiTransformer.uuidExtTaxComment);
183
		ExtensionType fauCommentExtensionType = (ExtensionType)getTermService().find(PesiTransformer.uuidExtFauComment);
184
		ExtensionType fauExtraCodesExtensionType = (ExtensionType)getTermService().find(PesiTransformer.uuidExtFauExtraCodes);
185
		List<TaxonBase> taxonBaseList = null;
186

  
187
		int count = 0;
188
		int pastCount = 0;
189
		Connection connection = state.getConfig().getDestination().getConnection();
190
		logger.info("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
191
		//logger.warn("TODO handle extensions on taxon level, not name level (");
192
		while ((taxonBaseList = getTaxonService().list(null, limit, count, null, null)).size() > 0) {
193

  
194
			logger.info("Fetched " + taxonBaseList.size() + " names. Exporting...");
195
			for (TaxonBase<?> taxon : taxonBaseList) {
196
				Set<Extension> extensions = taxon.getExtensions();
197
				for (Extension extension : extensions) {
198
					if (extension.getType().equals(taxCommentExtensionType)) {
199
						String taxComment = extension.getValue();
200
						invokeNotes(taxComment,
201
								PesiTransformer.getNoteCategoryFk(PesiTransformer.uuidExtTaxComment),
202
								PesiTransformer.getNoteCategoryCache(PesiTransformer.uuidExtTaxComment),
203
								null, null, getTaxonFk(taxon.getName(), state),connection);
204
					} else if (extension.getType().equals(fauCommentExtensionType)) {
205
						String fauComment = extension.getValue();
206
						invokeNotes(fauComment,
207
								PesiTransformer.getNoteCategoryFk(PesiTransformer.uuidExtFauComment),
208
								PesiTransformer.getNoteCategoryCache(PesiTransformer.uuidExtFauComment),
209
								null, null, getTaxonFk(taxon.getName(), state),connection);
210
					} else if (extension.getType().equals(fauExtraCodesExtensionType)) {
211
						String fauExtraCodes = extension.getValue();
212
						invokeNotes(fauExtraCodes,
213
								PesiTransformer.getNoteCategoryFk(PesiTransformer.uuidExtFauExtraCodes),
214
								PesiTransformer.getNoteCategoryCache(PesiTransformer.uuidExtFauExtraCodes),
215
								null, null, getTaxonFk(taxon.getName(), state),connection);
216
					}
217
				}
218

  
219
				doCount(count++, modCount, pluralString);
220
			}
221

  
222
			// Commit transaction
223
			commitTransaction(txStatus);
224
			logger.debug("Committed transaction.");
225
			logger.info("Exported " + (count - pastCount) + " names. Total: " + count);
226
			pastCount = count;
227

  
228
			// Start transaction
229
			txStatus = startTransaction(true);
230
			logger.info("Started new transaction. Fetching some taxa first (max: " + limit + ") ...");
231
		}
232
		if (taxonBaseList.size() == 0) {
233
			logger.info("No taxa left to fetch.");
234
		}
235
		// Commit transaction
236
		commitTransaction(txStatus);
237
		logger.debug("Committed transaction.");
238
	}
239

  
240
	/**
241
	 * @param taxComment
242
	 * @param noteCategoryFk
243
	 * @param noteCategoryCache
244
	 * @param object
245
	 * @param object2
246
	 */
247
	private void invokeNotes(String note, Integer noteCategoryFk,
248
			String noteCategoryCache, Integer languageFk, String languageCache,
249
			Integer taxonFk, Connection connection) {
250
		String notesSql = "UPDATE Note SET Note_1 = ?, NoteCategoryFk = ?, NoteCategoryCache = ?, LanguageFk = ?, LanguageCache = ? WHERE TaxonFk = ?";
251
		try {
252
			PreparedStatement notesStmt = connection.prepareStatement(notesSql);
253

  
254
			if (note != null) {
255
				notesStmt.setString(1, note);
256
			} else {
257
				notesStmt.setObject(1, null);
258
			}
259

  
260
			if (noteCategoryFk != null) {
261
				notesStmt.setInt(2, noteCategoryFk);
262
			} else {
263
				notesStmt.setObject(2, null);
264
			}
265

  
266
			if (noteCategoryCache != null) {
267
				notesStmt.setString(3, noteCategoryCache);
268
			} else {
269
				notesStmt.setObject(3, null);
270
			}
271

  
272
			if (languageFk != null) {
273
				notesStmt.setInt(4, languageFk);
274
			} else {
275
				notesStmt.setObject(4, null);
276
			}
277

  
278
			if (languageCache != null) {
279
				notesStmt.setString(5, languageCache);
280
			} else {
281
				notesStmt.setObject(5, null);
282
			}
283

  
284
			if (taxonFk != null) {
285
				notesStmt.setInt(6, taxonFk);
286
			} else {
287
				notesStmt.setObject(6, null);
288
			}
289

  
290
			notesStmt.executeUpdate();
291
		} catch (SQLException e) {
292
			logger.error("Note could not be created: " + note);
293
			e.printStackTrace();
294
		}
295

  
296

  
297
	}
298

  
299
	/**
300
	 * Deletes all entries of database tables related to <code>Note</code>.
301
	 * @param state The PesiExportState
302
	 * @return Whether the delete operation was successful or not.
303
	 */
304
	protected boolean doDelete(PesiExportState state) {
305
		PesiExportConfigurator pesiConfig = state.getConfig();
306

  
307
		String sql;
308
		Source destination =  pesiConfig.getDestination();
309

  
310
		// Clear NoteSource
311
		sql = "DELETE FROM NoteSource";
312
		destination.setQuery(sql);
313
		destination.update(sql);
314

  
315
		// Clear Note
316
		sql = "DELETE FROM " + dbTableName;
317
		destination.setQuery(sql);
318
		destination.update(sql);
319
		return true;
320
	}
321

  
322
	@Override
323
	protected boolean isIgnore(PesiExportState state) {
324
		return ! state.getConfig().isDoNotes();
325
	}
326

  
327
	/**
328
	 * Returns the <code>Note_1</code> attribute.
329
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
330
	 * @return The <code>Note_1</code> attribute.
331
	 * @see MethodMapper
332
	 */
333
	@SuppressWarnings("unused")
334
	private static String getNote_1(DescriptionElementBase descriptionElement) {
335
		String result = null;
336

  
337
		if (descriptionElement.isInstanceOf(TextData.class)) {
338
			TextData textData = CdmBase.deproxy(descriptionElement, TextData.class);
339
			result = textData.getText(Language.DEFAULT());
340
		}
341

  
342
		return result;
343
	}
344

  
345
	/**
346
	 * Returns the <code>Note_2</code> attribute.
347
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
348
	 * @return The <code>Note_2</code> attribute.
349
	 * @see MethodMapper
350
	 */
351
	@SuppressWarnings("unused")
352
	private static String getNote_2(DescriptionElementBase descriptionElement) {
353
		logger.warn("Not yet implemented");
354
		return null;
355
	}
356

  
357
	/**
358
	 * Returns the <code>NoteCategoryFk</code> attribute.
359
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
360
	 * @return The <code>NoteCategoryFk</code> attribute.
361
	 * @see MethodMapper
362
	 */
363
	private static Integer getNoteCategoryFk(DescriptionElementBase descriptionElement) {
364
		Integer result = null;
365
		result = PesiTransformer.feature2NoteCategoryFk(descriptionElement.getFeature());
366
		return result;
367
	}
368

  
369
	/**
370
	 * Returns the <code>NoteCategoryCache</code> attribute.
371
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
372
	 * @return The <code>NoteCategoryCache</code> attribute.
373
	 * @see MethodMapper
374
	 */
375
	@SuppressWarnings("unused")
376
	private static String getNoteCategoryCache(DescriptionElementBase descriptionElement, PesiExportState state) {
377
		return state.getTransformer().getCacheByFeature(descriptionElement.getFeature());
378
	}
379

  
380
	/**
381
	 * Returns the <code>LanguageFk</code> attribute.
382
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
383
	 * @return The <code>LanguageFk</code> attribute.
384
	 * @see MethodMapper
385
	 */
386
	@SuppressWarnings("unused")
387
	private static Integer getLanguageFk(DescriptionElementBase descriptionElement) {
388
		Language language = getLanguage(descriptionElement);
389

  
390
		return PesiTransformer.language2LanguageId(language);
391
	}
392

  
393
	/**
394
	 * Returns the <code>LanguageCache</code> attribute.
395
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
396
	 * @return The <code>LanguageCache</code> attribute.
397
	 * @throws UndefinedTransformerMethodException
398
	 * @see MethodMapper
399
	 */
400
	@SuppressWarnings("unused")
401
	private static String getLanguageCache(DescriptionElementBase descriptionElement, PesiExportState state) throws UndefinedTransformerMethodException {
402
		Language language = getLanguage(descriptionElement);
403
		return state.getTransformer().getCacheByLanguage(language);
404
	}
405

  
406
	private static Language getLanguage(DescriptionElementBase descriptionElement) {
407
		Language language = null;
408

  
409
		Map<Language, LanguageString> multilanguageText = null;
410
		if (descriptionElement.isInstanceOf(CommonTaxonName.class)) {
411
			CommonTaxonName commonTaxonName = CdmBase.deproxy(descriptionElement, CommonTaxonName.class);
412
			language = commonTaxonName.getLanguage();
413
		} else if (descriptionElement.isInstanceOf(TextData.class)) {
414
			TextData textData = CdmBase.deproxy(descriptionElement, TextData.class);
415
			multilanguageText = textData.getMultilanguageText();
416
		} else if (descriptionElement.isInstanceOf(IndividualsAssociation.class)) {
417
			IndividualsAssociation individualsAssociation = CdmBase.deproxy(descriptionElement, IndividualsAssociation.class);
418
			multilanguageText = individualsAssociation.getDescription();
419
		} else if (descriptionElement.isInstanceOf(TaxonInteraction.class)) {
420
			TaxonInteraction taxonInteraction = CdmBase.deproxy(descriptionElement, TaxonInteraction.class);
421
			multilanguageText = taxonInteraction.getDescription();
422
		} else {
423
			logger.debug("Given descriptionElement does not support languages. Hence LanguageCache could not be determined: " + descriptionElement.getUuid());
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff