Project

General

Profile

Download (7.99 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;
10

    
11
import java.sql.SQLException;
12
import java.util.Arrays;
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.Set;
16

    
17
import org.apache.log4j.Logger;
18
import org.springframework.stereotype.Component;
19
import org.springframework.transaction.TransactionStatus;
20

    
21
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
22
import eu.etaxonomy.cdm.io.common.Source;
23
import eu.etaxonomy.cdm.io.common.mapping.out.DbObjectMapper;
24
import eu.etaxonomy.cdm.io.common.mapping.out.DbStringMapper;
25
import eu.etaxonomy.cdm.io.common.mapping.out.MethodMapper;
26
import eu.etaxonomy.cdm.model.common.CdmBase;
27
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
28
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
29
import eu.etaxonomy.cdm.model.reference.Reference;
30
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
31
import eu.etaxonomy.cdm.profiler.ProfilerController;
32
/**
33
 * The export class for PESI additional sources stored as taxon sources in the CDM (mostly coming from ERMS)<p>
34
 * @author a.mueller
35
 * @since 22.09.2019
36
 */
37
@Component
38
public class PesiAdditionalSourceExport extends PesiExportBase {
39

    
40
    private static final long serialVersionUID = -2567615286288369111L;
41
    private static final Logger logger = Logger.getLogger(PesiAdditionalSourceExport.class);
42

    
43
	private static final Class<? extends CdmBase> standardMethodParameter = IdentifiableSource.class;
44

    
45
	private static int modCount = 1000;
46
	private static final String dbTableName = "AdditionalTaxonSource";
47
	private static final String pluralString = "addtional sources";
48
	private static final String parentPluralString = "Taxa";
49

    
50
	public PesiAdditionalSourceExport() {
51
		super();
52
	}
53

    
54
	int countTaxa = 0;
55
	int countSources = 0;
56

    
57
	@Override
58
	public Class<? extends CdmBase> getStandardMethodParameter() {
59
		return standardMethodParameter;
60
	}
61

    
62
	@Override
63
	protected void doInvoke(PesiExportState state) {
64
		try {
65
			logger.info("*** Started making " + pluralString + " ...");
66

    
67
			// Stores whether this invoke was successful or not.
68
			boolean success = true;
69

    
70
			success &= doDelete(state);
71

    
72
			// Get specific mappings: (CDM) Source -> (PESI) Addtional Taxon Source
73
			PesiExportMapping mapping = getMapping();
74
			mapping.initialize(state);
75

    
76
			// Taxon Sources
77
			success &= doPhase01(state, mapping);
78

    
79
			logger.info("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
80

    
81
			if (!success){
82
				state.getResult().addError("An unknown problem occurred");
83
			}
84
			return;
85
		} catch (SQLException e) {
86
			e.printStackTrace();
87
			logger.error(e.getMessage());
88
			state.getResult().addException(e, e.getMessage());
89
		}
90
	}
91

    
92
	//PHASE 01: Sources
93
	private boolean doPhase01(PesiExportState state, PesiExportMapping mapping) {
94

    
95
//	    System.out.println("PHASE 1 of description import");
96
	    logger.info("PHASE 1...");
97
		int count = 0;
98
		int pastCount = 0;
99
		boolean success = true;
100
		//int limit = state.getConfig().getLimitSave();
101
		int limit = 1000;
102

    
103
		@SuppressWarnings("rawtypes")
104
        List<TaxonBase> taxonList = null;
105

    
106
		TransactionStatus txStatus = startTransaction(true);
107

    
108
		if (logger.isDebugEnabled()){
109
		    logger.info("Started new transaction. Fetching some " + parentPluralString + " (max: " + limit + ") ...");
110
		    logger.debug("Start snapshot, before starting loop");
111
		    ProfilerController.memorySnapshot();
112
		}
113

    
114
		List<String> propPath = Arrays.asList(new String[]{"sources.*"});
115
		int partitionCount = 0;
116
		while ((taxonList = getNextTaxonPartition(TaxonBase.class, limit, partitionCount++, propPath )) != null   ) {
117

    
118
			if (logger.isDebugEnabled()) {
119
                logger.info("Fetched " + taxonList.size() + " " + parentPluralString + ". Exporting...");
120
            }
121

    
122
			for (TaxonBase<?> taxon : taxonList) {
123
				countTaxa++;
124
				doCount(count++, modCount, parentPluralString);
125
				state.setCurrentTaxon(taxon);
126
				if (!taxon.getSources().isEmpty()){
127
					success &= handleSingleTaxon(taxon, mapping);
128
				}
129
			}
130
			taxonList = null;
131
			state.setCurrentTaxon(null);
132

    
133
			// Commit transaction
134
			commitTransaction(txStatus);
135
			logger.info("Exported " + (count - pastCount) + " " + parentPluralString + ". Total taxa: " + count + ". Total sources " + countSources);
136
			pastCount = count;
137
			if (logger.isDebugEnabled()) {
138
                ProfilerController.memorySnapshot();
139
            }
140
			// Start transaction
141
			txStatus = startTransaction(true);
142
			if(logger.isDebugEnabled()) {
143
                logger.info("Started new transaction. Fetching some " + parentPluralString + " (max: " + limit + ") for description import ...");
144
            }
145
		}
146

    
147

    
148
		// Commit transaction
149
		commitTransaction(txStatus);
150
		logger.debug("Committed transaction.");
151
		return success;
152
	}
153

    
154
	private boolean handleSingleTaxon(TaxonBase<?> taxon, PesiExportMapping mapping) {
155

    
156
	    boolean success = true;
157

    
158
		Set<IdentifiableSource> sources = new HashSet<>();
159
		sources.addAll(taxon.getSources());
160

    
161
		for (IdentifiableSource src : sources){
162
			OriginalSourceType type = src.getType();
163
			if (type == OriginalSourceType.Other){
164
			    mapping.invoke(src);
165
			    countSources++;
166
			}
167
		}
168
		if (logger.isDebugEnabled()) {
169
            logger.info("number of handled sources " + countSources);
170
        }
171
		return success;
172
	}
173

    
174
	/**
175
	 * Deletes all entries of database tables related to <code>Note</code>.
176
	 * @param state The PesiExportState
177
	 * @return Whether the delete operation was successful or not.
178
	 */
179
	protected boolean doDelete(PesiExportState state) {
180
	    Source destination = state.getConfig().getDestination();
181

    
182
		// Clear NoteSource
183
		String sql = "DELETE FROM AdditionalTaxonSource";
184
		destination.update(sql);
185

    
186
		return true;
187
	}
188

    
189
	/**
190
	 * Returns the TaxonFk for a given TaxonName or Taxon.
191
	 * @param state The {@link DbExportStateBase DbExportState}.
192
	 * @return
193
	 */
194
	@SuppressWarnings("unused")  //used by mapper
195
	private static Integer getTaxonFk(IdentifiableSource source, PesiExportState state) {
196
		TaxonBase<?> entity = state.getCurrentTaxon();
197
		return state.getDbId(entity);
198
	}
199

    
200
	@SuppressWarnings("unused")  //used by mapper
201
    private static Integer getSourceUseFk(IdentifiableSource source, PesiExportState state) {
202
        return state.getTransformer().getSourceUseKeyCacheByCache(source.getOriginalNameString());
203
    }
204

    
205
    @SuppressWarnings("unused")  //used by mapper
206
    private static String getSourceNameCache(IdentifiableSource source) {
207
        Reference ref = source.getCitation();
208
        return ref == null? null : ref.getTitleCache();
209
    }
210

    
211
    @SuppressWarnings("unused")  //used by mapper
212
    private static Integer getCurrentTaxonFk(PesiExportState state) {
213
        return state.getDbId(state.getCurrentTaxon());
214
    }
215

    
216
//******************************* MAPPINGS ********************************************
217

    
218
	/**
219
	 * Returns the CDM to PESI specific export mappings for PESI additonal sources.
220
	 * @return The {@link PesiExportMapping PesiExportMapping}.
221
	 */
222
	private PesiExportMapping getMapping() {
223
		PesiExportMapping mapping = new PesiExportMapping(dbTableName);
224

    
225
		mapping.addMapper(MethodMapper.NewInstance("TaxonFk", this, IdentifiableSource.class, PesiExportState.class));
226
        mapping.addMapper(DbObjectMapper.NewInstance("Citation", "SourceFk"));
227
		mapping.addMapper(MethodMapper.NewInstance("SourceUseFk", this, IdentifiableSource.class, PesiExportState.class));
228
		mapping.addMapper(DbStringMapper.NewInstance("originalNameString", "SourceUseCache"));
229
        mapping.addMapper(DbObjectMapper.NewInstance("Citation", "SourceNameCache", IS_CACHE));
230
		mapping.addMapper(DbStringMapper.NewInstance("citationMicroReference", "SourceDetail"));
231

    
232
		return mapping;
233
	}
234

    
235
    @Override
236
    protected boolean doCheck(PesiExportState state) {
237
        boolean result = true;
238
        return result;
239
    }
240

    
241
    @Override
242
    protected boolean isIgnore(PesiExportState state) {
243
        return ! state.getConfig().isDoAdditionalTaxonSource();
244
    }
245
}
(2-2/14)