(no commit message)
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / pesi / out / PesiSourceExport.java
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 import java.util.Set;
15
16 import org.apache.log4j.Logger;
17 import org.springframework.stereotype.Component;
18 import org.springframework.transaction.TransactionStatus;
19
20 import eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbStringMapper;
21 import eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbTimePeriodMapper;
22 import eu.etaxonomy.cdm.io.berlinModel.out.mapper.IdMapper;
23 import eu.etaxonomy.cdm.io.berlinModel.out.mapper.MethodMapper;
24 import eu.etaxonomy.cdm.io.common.Source;
25 import eu.etaxonomy.cdm.io.common.IImportConfigurator.DO_REFERENCES;
26 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
27 import eu.etaxonomy.cdm.model.common.CdmBase;
28 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
29 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
30
31 /**
32 * @author a.mueller
33 * @author e.-m.lee
34 * @date 11.02.2010
35 *
36 */
37 @Component
38 @SuppressWarnings("unchecked")
39 public class PesiSourceExport extends PesiExportBase {
40 private static final Logger logger = Logger.getLogger(PesiSourceExport.class);
41 private static final Class<? extends CdmBase> standardMethodParameter = ReferenceBase.class;
42
43 private static int modCount = 1000;
44 private static final String dbTableName = "Source";
45 private static final String pluralString = "Sources";
46
47 public PesiSourceExport() {
48 super();
49 }
50
51 /* (non-Javadoc)
52 * @see eu.etaxonomy.cdm.io.pesi.out.PesiExportBase#getStandardMethodParameter()
53 */
54 @Override
55 public Class<? extends CdmBase> getStandardMethodParameter() {
56 return standardMethodParameter;
57 }
58
59 /* (non-Javadoc)
60 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
61 */
62 @Override
63 protected boolean doCheck(PesiExportState state) {
64 boolean result = true;
65 return result;
66 }
67
68 /* (non-Javadoc)
69 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
70 */
71 @Override
72 protected boolean doInvoke(PesiExportState state) {
73 try{
74 logger.error("*** Started Making " + pluralString + " ...");
75
76 // Get the limit for objects to save within a single transaction.
77 int limit = state.getConfig().getLimitSave();
78
79 // Stores whether this invoke was successful or not.
80 boolean success = true ;
81
82 // PESI: Clear the database table Source.
83 doDelete(state);
84
85 // CDM: Get the number of all available references.
86 // int maxCount = getReferenceService().count(null);
87 // logger.error("Total amount of " + maxCount + " " + pluralString + " will be exported.");
88
89 // Get specific mappings: (CDM) Reference -> (PESI) Source
90 PesiExportMapping mapping = getMapping();
91
92 // Initialize the db mapper
93 mapping.initialize(state);
94
95 // PESI: Create the Sources
96 // TODO: Store CDM2PESI identifier pairs for later use in other export classes - PesiExportState
97 int count = 0;
98 int pastCount = 0;
99 TransactionStatus txStatus = null;
100 List<ReferenceBase> list = null;
101
102 // Start transaction
103 txStatus = startTransaction(true);
104 logger.error("Started new transaction. Fetching some " + pluralString + " (max: " + limit + ") ...");
105 while ((list = getReferenceService().list(null, limit, count, null, null)).size() > 0) {
106
107 logger.error("Fetched " + list.size() + " " + pluralString + ". Exporting...");
108 for (ReferenceBase<?> reference : list) {
109 doCount(count++, modCount, pluralString);
110 success &= mapping.invoke(reference);
111 }
112
113 // Commit transaction
114 commitTransaction(txStatus);
115 logger.error("Committed transaction.");
116 logger.error("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
117 pastCount = count;
118
119 // Start transaction
120 txStatus = startTransaction(true);
121 logger.error("Started new transaction. Fetching some " + pluralString + " (max: " + limit + ") ...");
122 }
123 if (list.size() == 0) {
124 logger.error("No " + pluralString + " left to fetch.");
125 }
126 // Commit transaction
127 commitTransaction(txStatus);
128 logger.error("Committed transaction.");
129
130 logger.error("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
131
132 return success;
133 } catch (SQLException e) {
134 e.printStackTrace();
135 logger.error(e.getMessage());
136 return false;
137 }
138 }
139
140 /**
141 * Deletes all entries of database tables related to <code>Source</code>.
142 * @param state The {@link PesiExportState PesiExportState}.
143 * @return Whether the delete operation was successful or not.
144 */
145 protected boolean doDelete(PesiExportState state) {
146 PesiExportConfigurator pesiConfig = (PesiExportConfigurator) state.getConfig();
147
148 String sql;
149 Source destination = pesiConfig.getDestination();
150
151 // Clear Occurrences
152 sql = "DELETE FROM Occurrence";
153 destination.setQuery(sql);
154 destination.update(sql);
155
156 // Clear Taxa
157 sql = "DELETE FROM Taxon";
158 destination.setQuery(sql);
159 destination.update(sql);
160
161 // Clear Sources
162 sql = "DELETE FROM " + dbTableName;
163 destination.setQuery(sql);
164 destination.update(sql);
165
166 return true;
167 }
168
169 /**
170 * Returns the <code>IMIS_Id</code> attribute.
171 * @param reference The {@link ReferenceBase Reference}.
172 * @return The <code>IMIS_Id</code> attribute.
173 * @see MethodMapper
174 */
175 @SuppressWarnings("unused")
176 private static Integer getIMIS_Id(ReferenceBase<?> reference) {
177 // TODO
178 // Where is the IMIS_Id from an ERMS import stored in CDM?
179 return null;
180 }
181
182 /**
183 * Returns the <code>SourceCategoryFK</code> attribute.
184 * @param reference The {@link ReferenceBase Reference}.
185 * @return The <code>SourceCategoryFK</code> attribute.
186 * @see MethodMapper
187 */
188 @SuppressWarnings("unused")
189 private static Integer getSourceCategoryFK(ReferenceBase<?> reference) {
190 return PesiTransformer.reference2SourceCategoryFK(reference);
191 }
192
193 /**
194 * Returns the <code>SourceCategoryCache</code> attribute.
195 * @param reference The {@link ReferenceBase Reference}.
196 * @return The <code>SourceCategoryCache</code> attribute.
197 * @see MethodMapper
198 */
199 @SuppressWarnings("unused")
200 private static String getSourceCategoryCache(ReferenceBase<?> reference) {
201 return PesiTransformer.getSourceCategoryCache(reference);
202 }
203
204 /**
205 * Returns the <code>Name</code> attribute. The corresponding CDM attribute is <code>title</code>.
206 * @param reference The {@link ReferenceBase Reference}.
207 * @return The <code>Name</code> attribute.
208 * @see MethodMapper
209 */
210 @SuppressWarnings("unused")
211 private static String getName(ReferenceBase<?> reference) {
212 if (reference != null) {
213 return reference.getTitleCache(); // was getTitle()
214 } else {
215 return null;
216 }
217 }
218
219 /**
220 * Returns the <code>AuthorString</code> attribute. The corresponding CDM attribute is the <code>titleCache</code> of an <code>authorTeam</code>.
221 * @param reference The {@link ReferenceBase Reference}.
222 * @return The <code>AuthorString</code> attribute.
223 * @see MethodMapper
224 */
225 @SuppressWarnings("unused")
226 private static String getAuthorString(ReferenceBase<?> reference) {
227 String result = null;
228
229 if (reference != null) {
230 TeamOrPersonBase team = reference.getAuthorTeam();
231 if (team != null) {
232 // result = team.getTitleCache();
233 result = team.getNomenclaturalTitle();
234 } else {
235 result = null;
236 }
237 }
238
239 return result;
240 }
241
242 /**
243 * Returns the <code>NomRefCache</code> attribute. The corresponding CDM attribute is <code>titleCache</code>.
244 * @param reference The {@link ReferenceBase Reference}.
245 * @return The <code>NomRefCache</code> attribute.
246 * @see MethodMapper
247 */
248 @SuppressWarnings("unused")
249 private static String getNomRefCache(ReferenceBase<?> reference) {
250 if (reference != null) {
251 return reference.getTitleCache();
252 } else {
253 return null;
254 }
255 }
256
257 /**
258 * Returns the <code>Notes</code> attribute.
259 * @param reference The {@link ReferenceBase Reference}.
260 * @return The <code>Notes</code> attribute.
261 * @see MethodMapper
262 */
263 @SuppressWarnings("unused")
264 private static String getNotes(ReferenceBase<?> reference) {
265 // TODO
266 return null;
267 }
268
269 /**
270 * Returns the <code>RefIdInSource</code> attribute.
271 * @param reference The {@link ReferenceBase Reference}.
272 * @return The <code>RefIdInSource</code> attribute.
273 * @see MethodMapper
274 */
275 @SuppressWarnings("unused")
276 private static String getRefIdInSource(ReferenceBase<?> reference) {
277 String result = null;
278
279 if (reference != null) {
280 Set<IdentifiableSource> sources = reference.getSources();
281 if (sources.size() == 1) {
282 result = sources.iterator().next().getIdInSource();
283 } else if (sources.size() > 1) {
284 logger.warn("Reference has multiple IdentifiableSources: " + reference.getUuid() + " (" + reference.getTitleCache() + ")");
285 int count = 1;
286 for (IdentifiableSource source : sources) {
287 result += source.getIdInSource();
288 if (count < sources.size()) {
289 result += "; ";
290 }
291 count++;
292 }
293 }
294 }
295
296 return result;
297 }
298
299 /**
300 * Returns the <code>OriginalDB</code> attribute. The corresponding CDM attribute is the <code>titleCache</code> of a <code>citation</code>.
301 * @param reference The {@link ReferenceBase Reference}.
302 * @return The <code>OriginalDB</code> attribute.
303 * @see MethodMapper
304 */
305 @SuppressWarnings("unused")
306 private static String getOriginalDB(ReferenceBase<?> reference) {
307 String result = "";
308
309 if (reference != null) {
310 Set<IdentifiableSource> sources = reference.getSources();
311 if (sources.size() == 1) {
312 ReferenceBase citation = sources.iterator().next().getCitation();
313 if (citation != null) {
314 result = PesiTransformer.databaseString2Abbreviation(citation.getTitleCache()); //or just title
315 } else {
316 logger.warn("OriginalDB can not be determined because the citation of this source is NULL: " + sources.iterator().next().getUuid());
317 }
318 } else if (sources.size() > 1) {
319 logger.warn("Taxon has multiple IdentifiableSources: " + reference.getUuid() + " (" + reference.getTitleCache() + ")");
320 int count = 1;
321 for (IdentifiableSource source : sources) {
322 ReferenceBase citation = source.getCitation();
323 if (citation != null) {
324 result += PesiTransformer.databaseString2Abbreviation(citation.getTitleCache());
325 if (count < sources.size()) {
326 result += "; ";
327 }
328 count++;
329 }
330 }
331 } else {
332 result = null;
333 }
334 }
335
336 return result;
337 }
338
339 /* (non-Javadoc)
340 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
341 */
342 @Override
343 protected boolean isIgnore(PesiExportState state) {
344 return ! (((PesiExportConfigurator) state.getConfig()).getDoReferences().equals(DO_REFERENCES.ALL));
345 }
346
347 /**
348 * Returns the CDM to PESI specific export mappings.
349 * @return The {@link PesiExportMapping PesiExportMapping}.
350 */
351 private PesiExportMapping getMapping() {
352 PesiExportMapping mapping = new PesiExportMapping(dbTableName);
353
354 mapping.addMapper(IdMapper.NewInstance("SourceId"));
355 mapping.addMapper(MethodMapper.NewInstance("IMIS_Id", this));
356 mapping.addMapper(MethodMapper.NewInstance("SourceCategoryFK", this));
357 mapping.addMapper(MethodMapper.NewInstance("SourceCategoryCache", this));
358 mapping.addMapper(MethodMapper.NewInstance("Name", this));
359 mapping.addMapper(DbStringMapper.NewInstance("referenceAbstract", "Abstract"));
360 mapping.addMapper(DbStringMapper.NewInstance("title", "Title"));
361 mapping.addMapper(MethodMapper.NewInstance("AuthorString", this));
362 mapping.addMapper(DbTimePeriodMapper.NewInstance("datePublished", "RefYear"));
363 mapping.addMapper(MethodMapper.NewInstance("NomRefCache", this));
364 mapping.addMapper(DbStringMapper.NewInstance("uri", "Link"));
365 mapping.addMapper(MethodMapper.NewInstance("Notes", this));
366 mapping.addMapper(MethodMapper.NewInstance("RefIdInSource", this));
367 mapping.addMapper(MethodMapper.NewInstance("OriginalDB", this));
368
369 return mapping;
370 }
371
372 }