3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.pesi
.out
;
12 import java
.sql
.SQLException
;
13 import java
.util
.List
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.springframework
.stereotype
.Component
;
18 import org
.springframework
.transaction
.TransactionStatus
;
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
;
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;
43 private static int modCount
= 1000;
44 private static final String dbTableName
= "Source";
45 private static final String pluralString
= "Sources";
47 public PesiSourceExport() {
52 * @see eu.etaxonomy.cdm.io.pesi.out.PesiExportBase#getStandardMethodParameter()
55 public Class
<?
extends CdmBase
> getStandardMethodParameter() {
56 return standardMethodParameter
;
60 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
63 protected boolean doCheck(PesiExportState state
) {
64 boolean result
= true;
69 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
72 protected boolean doInvoke(PesiExportState state
) {
74 logger
.error("*** Started Making " + pluralString
+ " ...");
76 // Get the limit for objects to save within a single transaction.
77 int limit
= state
.getConfig().getLimitSave();
79 // Stores whether this invoke was successful or not.
80 boolean success
= true ;
82 // PESI: Clear the database table Source.
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.");
89 // Get specific mappings: (CDM) Reference -> (PESI) Source
90 PesiExportMapping mapping
= getMapping();
92 // Initialize the db mapper
93 mapping
.initialize(state
);
95 // PESI: Create the Sources
96 // TODO: Store CDM2PESI identifier pairs for later use in other export classes - PesiExportState
99 TransactionStatus txStatus
= null;
100 List
<ReferenceBase
> list
= null;
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) {
107 logger
.error("Fetched " + list
.size() + " " + pluralString
+ ". Exporting...");
108 for (ReferenceBase
<?
> reference
: list
) {
109 doCount(count
++, modCount
, pluralString
);
110 success
&= mapping
.invoke(reference
);
113 // Commit transaction
114 commitTransaction(txStatus
);
115 logger
.error("Committed transaction.");
116 logger
.error("Exported " + (count
- pastCount
) + " " + pluralString
+ ". Total: " + count
);
120 txStatus
= startTransaction(true);
121 logger
.error("Started new transaction. Fetching some " + pluralString
+ " (max: " + limit
+ ") ...");
123 if (list
.size() == 0) {
124 logger
.error("No " + pluralString
+ " left to fetch.");
126 // Commit transaction
127 commitTransaction(txStatus
);
128 logger
.error("Committed transaction.");
130 logger
.error("*** Finished Making " + pluralString
+ " ..." + getSuccessString(success
));
133 } catch (SQLException e
) {
135 logger
.error(e
.getMessage());
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.
145 protected boolean doDelete(PesiExportState state
) {
146 PesiExportConfigurator pesiConfig
= (PesiExportConfigurator
) state
.getConfig();
149 Source destination
= pesiConfig
.getDestination();
152 sql
= "DELETE FROM Occurrence";
153 destination
.setQuery(sql
);
154 destination
.update(sql
);
157 sql
= "DELETE FROM Taxon";
158 destination
.setQuery(sql
);
159 destination
.update(sql
);
162 sql
= "DELETE FROM " + dbTableName
;
163 destination
.setQuery(sql
);
164 destination
.update(sql
);
170 * Returns the <code>IMIS_Id</code> attribute.
171 * @param reference The {@link ReferenceBase Reference}.
172 * @return The <code>IMIS_Id</code> attribute.
175 @SuppressWarnings("unused")
176 private static Integer
getIMIS_Id(ReferenceBase
<?
> reference
) {
178 // Where is the IMIS_Id from an ERMS import stored in CDM?
183 * Returns the <code>SourceCategoryFK</code> attribute.
184 * @param reference The {@link ReferenceBase Reference}.
185 * @return The <code>SourceCategoryFK</code> attribute.
188 @SuppressWarnings("unused")
189 private static Integer
getSourceCategoryFK(ReferenceBase
<?
> reference
) {
190 return PesiTransformer
.reference2SourceCategoryFK(reference
);
194 * Returns the <code>SourceCategoryCache</code> attribute.
195 * @param reference The {@link ReferenceBase Reference}.
196 * @return The <code>SourceCategoryCache</code> attribute.
199 @SuppressWarnings("unused")
200 private static String
getSourceCategoryCache(ReferenceBase
<?
> reference
) {
201 return PesiTransformer
.getSourceCategoryCache(reference
);
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.
210 @SuppressWarnings("unused")
211 private static String
getName(ReferenceBase
<?
> reference
) {
212 if (reference
!= null) {
213 return reference
.getTitleCache(); // was getTitle()
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.
225 @SuppressWarnings("unused")
226 private static String
getAuthorString(ReferenceBase
<?
> reference
) {
227 String result
= null;
229 if (reference
!= null) {
230 TeamOrPersonBase team
= reference
.getAuthorTeam();
232 // result = team.getTitleCache();
233 result
= team
.getNomenclaturalTitle();
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.
248 @SuppressWarnings("unused")
249 private static String
getNomRefCache(ReferenceBase
<?
> reference
) {
250 if (reference
!= null) {
251 return reference
.getTitleCache();
258 * Returns the <code>Notes</code> attribute.
259 * @param reference The {@link ReferenceBase Reference}.
260 * @return The <code>Notes</code> attribute.
263 @SuppressWarnings("unused")
264 private static String
getNotes(ReferenceBase
<?
> reference
) {
270 * Returns the <code>RefIdInSource</code> attribute.
271 * @param reference The {@link ReferenceBase Reference}.
272 * @return The <code>RefIdInSource</code> attribute.
275 @SuppressWarnings("unused")
276 private static String
getRefIdInSource(ReferenceBase
<?
> reference
) {
277 String result
= null;
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() + ")");
286 for (IdentifiableSource source
: sources
) {
287 result
+= source
.getIdInSource();
288 if (count
< sources
.size()) {
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.
305 @SuppressWarnings("unused")
306 private static String
getOriginalDB(ReferenceBase
<?
> reference
) {
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
316 logger
.warn("OriginalDB can not be determined because the citation of this source is NULL: " + sources
.iterator().next().getUuid());
318 } else if (sources
.size() > 1) {
319 logger
.warn("Taxon has multiple IdentifiableSources: " + reference
.getUuid() + " (" + reference
.getTitleCache() + ")");
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()) {
340 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
343 protected boolean isIgnore(PesiExportState state
) {
344 return ! (((PesiExportConfigurator
) state
.getConfig()).getDoReferences().equals(DO_REFERENCES
.ALL
));
348 * Returns the CDM to PESI specific export mappings.
349 * @return The {@link PesiExportMapping PesiExportMapping}.
351 private PesiExportMapping
getMapping() {
352 PesiExportMapping mapping
= new PesiExportMapping(dbTableName
);
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));