Project

General

Profile

Download (8.71 KB) Statistics
| Branch: | Tag: | Revision:
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.SQLException;
13
import java.util.List;
14

    
15
import org.apache.log4j.Logger;
16
import org.springframework.stereotype.Component;
17
import org.springframework.transaction.TransactionStatus;
18

    
19
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.MethodMapper;
20
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
21
import eu.etaxonomy.cdm.io.common.Source;
22
import eu.etaxonomy.cdm.model.common.CdmBase;
23
import eu.etaxonomy.cdm.model.description.DescriptionBase;
24
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
25
import eu.etaxonomy.cdm.model.description.TaxonDescription;
26
import eu.etaxonomy.cdm.model.taxon.Taxon;
27

    
28
/**
29
 * @author e.-m.lee
30
 * @date 03.03.2010
31
 *
32
 */
33
@Component
34
@SuppressWarnings("unchecked")
35
public class PesiNoteSourceExport extends PesiExportBase {
36
	private static final Logger logger = Logger.getLogger(PesiNoteSourceExport.class);
37
	private static final Class<? extends CdmBase> standardMethodParameter = DescriptionElementBase.class;
38

    
39
	private static int modCount = 1000;
40
	private static final String dbTableName = "NoteSource";
41
	private static final String pluralString = "NoteSources";
42

    
43
	public PesiNoteSourceExport() {
44
		super();
45
	}
46

    
47
	/* (non-Javadoc)
48
	 * @see eu.etaxonomy.cdm.io.common.DbExportBase#getStandardMethodParameter()
49
	 */
50
	@Override
51
	public Class<? extends CdmBase> getStandardMethodParameter() {
52
		return standardMethodParameter;
53
	}
54

    
55
	/* (non-Javadoc)
56
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
57
	 */
58
	@Override
59
	protected boolean doCheck(PesiExportState state) {
60
		boolean result = true;
61
		return result;
62
	}
63

    
64
	/* (non-Javadoc)
65
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
66
	 */
67
	@Override
68
	protected boolean doInvoke(PesiExportState state) {
69
		try {
70
			logger.error("*** Started Making " + pluralString + " ...");
71
	
72
			// Get the limit for objects to save within a single transaction.
73
//			int pageSize = state.getConfig().getLimitSave();
74
			int pageSize = 1000;
75

    
76
			// Calculate the pageNumber
77
			int maxCount = 0;
78
			int pageNumber = 1;
79

    
80
			// Stores whether this invoke was successful or not.
81
			boolean success = true;
82

    
83
			// PESI: Clear the database table NoteSource.
84
			doDelete(state);
85
	
86
			// Get specific mappings: (CDM) ? -> (PESI) NoteSource
87
			PesiExportMapping mapping = getMapping();
88

    
89
			// Initialize the db mapper
90
			mapping.initialize(state);
91

    
92
			// PESI: Create the NoteSource
93
			int count = 0;
94
			int pastCount = 0;
95
			TransactionStatus txStatus = null;
96
			List<DescriptionElementBase> list = null;
97
			
98
			// Start transaction
99
			txStatus = startTransaction(true);
100
			logger.error("Started new transaction. Fetching some " + pluralString + " (max: " + pageSize + ") ...");
101
			while ((list = getDescriptionService().listDescriptionElements(null, null, null, pageSize, pageNumber, null)).size() > 0) {
102

    
103
				logger.error("Fetched " + list.size() + " " + pluralString + ". Exporting...");
104
				for (DescriptionElementBase descriptionElement : list) {
105
					
106
					if (getNoteCategoryFk(descriptionElement) != null) {
107
						doCount(count++, modCount, pluralString);
108
						success &= mapping.invoke(descriptionElement);
109
					}
110
				}
111

    
112
				// Commit transaction
113
				commitTransaction(txStatus);
114
				logger.error("Committed transaction.");
115
				logger.error("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
116
				pastCount = count;
117
	
118
				// Start transaction
119
				txStatus = startTransaction(true);
120
				logger.error("Started new transaction. Fetching some " + pluralString + " (max: " + pageSize + ") ...");
121

    
122
				// Increment pageNumber
123
				pageNumber++;
124
			}
125
			if (list.size() == 0) {
126
				logger.error("No " + pluralString + " left to fetch.");
127
			}
128
			// Commit transaction
129
			commitTransaction(txStatus);
130
			logger.error("Committed transaction.");
131
	
132
			logger.error("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
133
			
134
			return success;
135
		} catch (SQLException e) {
136
			e.printStackTrace();
137
			logger.error(e.getMessage());
138
			return false;
139
		}
140
	}
141

    
142
	/**
143
	 * Deletes all entries of database tables related to <code>NoteSource</code>.
144
	 * @param state The {@link PesiExportState PesiExportState}.
145
	 * @return Whether the delete operation was successful or not.
146
	 */
147
	protected boolean doDelete(PesiExportState state) {
148
		PesiExportConfigurator pesiConfig = (PesiExportConfigurator) state.getConfig();
149
		
150
		String sql;
151
		Source destination =  pesiConfig.getDestination();
152

    
153
		// Clear NoteSource
154
		sql = "DELETE FROM " + dbTableName;
155
		destination.setQuery(sql);
156
		destination.update(sql);
157
		return true;
158
	}
159

    
160
	/* (non-Javadoc)
161
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
162
	 */
163
	@Override
164
	protected boolean isIgnore(PesiExportState state) {
165
		// TODO
166
//		return ! state.getConfig().isDoNoteSource();
167
		return false;
168
	}
169

    
170
	/**
171
	 * Returns the <code>NoteCategoryFk</code> attribute.
172
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
173
	 * @return The <code>NoteCategoryFk</code> attribute.
174
	 */
175
	private static Integer getNoteCategoryFk(DescriptionElementBase descriptionElement) {
176
		Integer result = null;
177
		result = PesiTransformer.textData2NodeCategoryFk(descriptionElement.getFeature());
178
		return result;
179
	}
180

    
181
	/**
182
	 * Returns the <code>NoteFk</code> attribute.
183
	 * @param description The {@link TaxonDescription TaxonDescription}.
184
	 * @param state The {@link PesiExportState PesiExportState}.
185
	 * @return The <code>NoteFk</code> attribute.
186
	 * @see MethodMapper
187
	 */
188
	@SuppressWarnings("unused")
189
	private static Integer getNoteFk(DescriptionElementBase descriptionElement, PesiExportState state) {
190
		Integer result = state.getDbId(descriptionElement);
191
		return result;
192
	}
193
	
194
	/**
195
	 * Returns the <code>SourceFk</code> attribute.
196
	 * @param description The {@link TaxonDescription TaxonDescription}.
197
	 * @param state The {@link DbExportStateBase DbExportState}.
198
	 * @return The <code>SourceFk</code> attribute.
199
	 * @see MethodMapper
200
	 */
201
	@SuppressWarnings("unused")
202
	private static Integer getSourceFk(DescriptionElementBase descriptionElement, DbExportStateBase<?> state) {
203
		Integer result = state.getDbId(descriptionElement);
204
//		DescriptionBase description = descriptionElement.getInDescription();
205
//		if (description.isInstanceOf(TaxonDescription.class)) {
206
//			TaxonDescription taxonDescription = CdmBase.deproxy(description, TaxonDescription.class);
207
//			Taxon taxon = taxonDescription.getTaxon();
208
//			result = state.getDbId(taxon.getSec());
209
//		}
210
		return result;
211
	}
212
	
213
	/**
214
	 * Returns the <code>SourceNameCache</code> attribute.
215
	 * @param description The {@link TaxonDescription TaxonDescription}.
216
	 * @return The <code>SourceNameCache</code> attribute.
217
	 * @see MethodMapper
218
	 */
219
	@SuppressWarnings("unused")
220
	private static String getSourceNameCache(DescriptionElementBase descriptionElement) {
221
		String result = null;
222
		DescriptionBase description = descriptionElement.getInDescription();
223
		if (description.isInstanceOf(TaxonDescription.class)) {
224
			TaxonDescription taxonDescription = CdmBase.deproxy(description, TaxonDescription.class);
225
			Taxon taxon = taxonDescription.getTaxon();
226
			result = taxon.getSec().getTitleCache();
227
		}
228
		return result;
229
	}
230
	
231
	/**
232
	 * Returns the <code>SourceDetail</code> attribute.
233
	 * @param description The {@link TaxonDescription TaxonDescription}.
234
	 * @return The <code>SourceDetail</code> attribute.
235
	 * @see MethodMapper
236
	 */
237
	@SuppressWarnings("unused")
238
	private static String getSourceDetail(DescriptionElementBase descriptionElement) {
239
		return descriptionElement.getCitationMicroReference(); // TODO: What else could be used?
240
	}
241

    
242
	/**
243
	 * Returns the CDM to PESI specific export mappings.
244
	 * @return The {@link PesiExportMapping PesiExportMapping}.
245
	 */
246
	private PesiExportMapping getMapping() {
247
		PesiExportMapping mapping = new PesiExportMapping(dbTableName);
248

    
249
		mapping.addMapper(MethodMapper.NewInstance("NoteFk", this.getClass(), "getNoteFk", standardMethodParameter, PesiExportState.class));
250
		mapping.addMapper(MethodMapper.NewInstance("SourceFk", this.getClass(), "getSourceFk", standardMethodParameter, DbExportStateBase.class));
251
		mapping.addMapper(MethodMapper.NewInstance("SourceNameCache", this));
252
		mapping.addMapper(MethodMapper.NewInstance("SourceDetail", this));
253

    
254
		return mapping;
255
	}
256

    
257
}
(8-8/14)