Project

General

Profile

Download (9.78 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.sql.SQLException;
12
import java.util.List;
13

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

    
18
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
19
import eu.etaxonomy.cdm.io.common.IExportConfigurator.DO_REFERENCES;
20
import eu.etaxonomy.cdm.io.common.Source;
21
import eu.etaxonomy.cdm.io.common.mapping.out.MethodMapper;
22
import eu.etaxonomy.cdm.io.pesi.out.PesiExportBase;
23
import eu.etaxonomy.cdm.io.pesi.out.PesiExportConfigurator;
24
import eu.etaxonomy.cdm.io.pesi.out.PesiExportMapping;
25
import eu.etaxonomy.cdm.io.pesi.out.PesiExportState;
26
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.description.DescriptionBase;
29
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
30
import eu.etaxonomy.cdm.model.description.TaxonDescription;
31
import eu.etaxonomy.cdm.model.taxon.Taxon;
32

    
33
/**
34
 * The export class for NoteSources.
35
 * Inserts into DataWarehouse database table <code>NoteSource</code>.
36
 * @author e.-m.lee
37
 * @since 03.03.2010
38
 */
39
@Component
40
public class PesiNoteSourceExport_Old extends PesiExportBase {
41

    
42
    private static final long serialVersionUID = 6918935733864341726L;
43
    private static final Logger logger = Logger.getLogger(PesiNoteSourceExport_Old.class);
44

    
45
    private static final Class<? extends CdmBase> standardMethodParameter = DescriptionElementBase.class;
46

    
47
	private static int modCount = 1000;
48
	private static final String dbTableName = "NoteSource";
49
	private static final String pluralString = "NoteSources";
50

    
51
	public PesiNoteSourceExport_Old() {
52
		super();
53
	}
54

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

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

    
72
	/* (non-Javadoc)
73
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
74
	 */
75
	@Override
76
	protected void doInvoke(PesiExportState state) {
77
		try {
78
			logger.error("*** Started Making " + pluralString + " ...");
79

    
80
			// Get the limit for objects to save within a single transaction.
81
//			int pageSize = state.getConfig().getLimitSave();
82
			int pageSize = 1000;
83

    
84
			// pageNumber
85
			int pageNumber = 1;
86

    
87
			// Stores whether this invoke was successful or not.
88
			boolean success = true;
89

    
90
			// PESI: Clear the database table NoteSource.
91
			doDelete(state);
92

    
93
			// Get specific mappings: (CDM) ? -> (PESI) NoteSource
94
			PesiExportMapping mapping = getMapping();
95

    
96
			// Initialize the db mapper
97
			mapping.initialize(state);
98

    
99
			// PESI: Create the NoteSource
100
			int count = 0;
101
			int pastCount = 0;
102
			TransactionStatus txStatus = null;
103
			List<DescriptionElementBase> list = null;
104

    
105
			// Start transaction
106
			txStatus = startTransaction(true);
107
			logger.info("Started new transaction. Fetching some " + pluralString + " (max: " + pageSize + ") ...");
108
			while ((list = getDescriptionService().listDescriptionElements(null, null, null, pageSize, pageNumber, null)).size() > 0) {
109

    
110
				logger.info("Fetched " + list.size() + " " + pluralString + ". Exporting...");
111
				for (DescriptionElementBase descriptionElement : list) {
112

    
113
					if (getNoteCategoryFk(descriptionElement) != null && neededValuesNotNull(descriptionElement, state)) {
114
						doCount(count++, modCount, pluralString);
115
						success &= mapping.invoke(descriptionElement);
116
					}
117
				}
118

    
119
				// Commit transaction
120
				commitTransaction(txStatus);
121
				logger.debug("Committed transaction.");
122
				logger.info("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
123
				pastCount = count;
124

    
125
				// Start transaction
126
				txStatus = startTransaction(true);
127
				logger.info("Started new transaction. Fetching some " + pluralString + " (max: " + pageSize + ") ...");
128

    
129
				// Increment pageNumber
130
				pageNumber++;
131
			}
132
			if (list.size() == 0) {
133
				logger.info("No " + pluralString + " left to fetch.");
134
			}
135

    
136
			list = null;
137
			// Commit transaction
138
			commitTransaction(txStatus);
139
			logger.debug("Committed transaction.");
140

    
141
			logger.info("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
142

    
143
			if (!success){
144
			    state.getResult().addError("An error occurred in PesiNoteSourceExport");
145
			}
146
			return;
147
		} catch (SQLException e) {
148
			e.printStackTrace();
149
			logger.error(e.getMessage());
150
			state.getResult().addException(e);
151
			return;
152
		}
153
	}
154

    
155
	/**
156
	 * Checks whether needed values for an entity are NULL.
157
	 * @return
158
	 */
159
	private boolean neededValuesNotNull(DescriptionElementBase descriptionElement, PesiExportState state) {
160
		boolean result = true;
161
		if (getSourceFk(descriptionElement, state) == null) {
162
			logger.error("SourceFk is NULL, but is not allowed to be. Therefore no record was written to export database for this descriptionElement: " + descriptionElement.getUuid());
163
			result = false;
164
		}
165
		return result;
166
	}
167

    
168
	/**
169
	 * Deletes all entries of database tables related to <code>NoteSource</code>.
170
	 * @param state The {@link PesiExportState PesiExportState}.
171
	 * @return Whether the delete operation was successful or not.
172
	 */
173
	protected boolean doDelete(PesiExportState state) {
174
		PesiExportConfigurator pesiConfig = state.getConfig();
175

    
176
		String sql;
177
		Source destination =  pesiConfig.getDestination();
178

    
179
		// Clear NoteSource
180
		sql = "DELETE FROM " + dbTableName;
181
		destination.setQuery(sql);
182
		destination.update(sql);
183
		return true;
184
	}
185

    
186
	/* (non-Javadoc)
187
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
188
	 */
189
	@Override
190
	protected boolean isIgnore(PesiExportState state) {
191
		return ! ( state.getConfig().isDoNoteSources() && state.getConfig().isDoNotes() && state.getConfig().getDoReferences().equals(DO_REFERENCES.ALL));
192
	}
193

    
194
	/**
195
	 * Returns the <code>NoteCategoryFk</code> attribute.
196
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
197
	 * @return The <code>NoteCategoryFk</code> attribute.
198
	 */
199
	private static Integer getNoteCategoryFk(DescriptionElementBase descriptionElement) {
200
		Integer result = null;
201
		result = PesiTransformer.feature2NoteCategoryFk(descriptionElement.getFeature());
202
		return result;
203
	}
204

    
205
	/**
206
	 * Returns the <code>NoteFk</code> attribute.
207
	 * @param description The {@link TaxonDescription TaxonDescription}.
208
	 * @param state The {@link PesiExportState PesiExportState}.
209
	 * @return The <code>NoteFk</code> attribute.
210
	 * @see MethodMapper
211
	 */
212
	@SuppressWarnings("unused")
213
	private static Integer getNoteFk(DescriptionElementBase descriptionElement, PesiExportState state) {
214
		Integer result = null;
215
		result = state.getDbId(descriptionElement);
216
		return result;
217
	}
218

    
219
	/**
220
	 * Returns the <code>SourceFk</code> attribute.
221
	 * @param description The {@link TaxonDescription TaxonDescription}.
222
	 * @param state The {@link DbExportStateBase DbExportState}.
223
	 * @return The <code>SourceFk</code> attribute.
224
	 * @see MethodMapper
225
	 */
226
	private static Integer getSourceFk(DescriptionElementBase descriptionElement, PesiExportState state) {
227
		Integer result = null;
228
		result = state.getDbId(descriptionElement);
229
		return result;
230
	}
231

    
232
	/**
233
	 * Returns the <code>SourceNameCache</code> attribute.
234
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
235
	 * @return The <code>SourceNameCache</code> attribute.
236
	 * @see MethodMapper
237
	 */
238
	@SuppressWarnings("unused")
239
	private static String getSourceNameCache(DescriptionElementBase descriptionElement) {
240
		String result = null;
241

    
242
		DescriptionBase<?> inDescription = descriptionElement.getInDescription();
243
		if (inDescription != null && inDescription.isInstanceOf(TaxonDescription.class)) {
244
			TaxonDescription taxonDescription = CdmBase.deproxy(inDescription, TaxonDescription.class);
245
			Taxon taxon = taxonDescription.getTaxon();
246
			result = taxon.getSec().getTitleCache();
247
		}
248

    
249
		return result;
250
	}
251

    
252
	/**
253
	 * Returns the <code>SourceDetail</code> attribute.
254
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
255
	 * @return The <code>SourceDetail</code> attribute.
256
	 * @see MethodMapper
257
	 */
258
	@SuppressWarnings("unused")
259
	private static String getSourceDetail(DescriptionElementBase descriptionElement) {
260
		//FIXME this is a replacement for the deprecated descriptionElement.getCitationMicroReference()
261
		//it needs to be checked what should be done when multiple sources exist
262
		if (descriptionElement.getSources().size() < 1){
263
			return null;
264
		}else{
265
			if (descriptionElement.getSources().size() > 1){
266
				logger.warn("Multiple sources exist");
267
			}
268
			return descriptionElement.getSources().iterator().next().getCitationMicroReference();
269
		}
270
//		return descriptionElement.getCitationMicroReference(); // TODO: What should be used instead?
271
	}
272

    
273
	/**
274
	 * Returns the CDM to PESI specific export mappings.
275
	 * @return The {@link PesiExportMapping PesiExportMapping}.
276
	 */
277
	private PesiExportMapping getMapping() {
278
		PesiExportMapping mapping = new PesiExportMapping(dbTableName);
279

    
280
		mapping.addMapper(MethodMapper.NewInstance("NoteFk", this.getClass(), "getNoteFk", standardMethodParameter, PesiExportState.class));
281
		mapping.addMapper(MethodMapper.NewInstance("SourceFk", this.getClass(), "getSourceFk", standardMethodParameter, PesiExportState.class));
282
		mapping.addMapper(MethodMapper.NewInstance("SourceNameCache", this));
283
		mapping.addMapper(MethodMapper.NewInstance("SourceDetail", this));
284

    
285
		return mapping;
286
	}
287

    
288
}
(4-4/6)