Revision f905a388
Added by Andreas Müller almost 2 years ago
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/common/mapping/DbImportDescriptionElementCreationMapperBase.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2007 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 |
|
|
10 |
package eu.etaxonomy.cdm.io.common.mapping; |
|
11 |
|
|
12 |
import java.sql.ResultSet; |
|
13 |
import java.sql.SQLException; |
|
14 |
import java.util.Set; |
|
15 |
|
|
16 |
import org.apache.commons.lang.StringUtils; |
|
17 |
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger; |
|
18 |
|
|
19 |
import eu.etaxonomy.cdm.io.common.DbImportStateBase; |
|
20 |
import eu.etaxonomy.cdm.model.common.CdmBase; |
|
21 |
import eu.etaxonomy.cdm.model.description.DescriptionElementBase; |
|
22 |
import eu.etaxonomy.cdm.model.description.TaxonDescription; |
|
23 |
import eu.etaxonomy.cdm.model.reference.OriginalSourceType; |
|
24 |
import eu.etaxonomy.cdm.model.reference.Reference; |
|
25 |
import eu.etaxonomy.cdm.model.taxon.Synonym; |
|
26 |
import eu.etaxonomy.cdm.model.taxon.Taxon; |
|
27 |
import eu.etaxonomy.cdm.model.taxon.TaxonBase; |
|
28 |
|
|
29 |
/** |
|
30 |
* @author a.mueller |
|
31 |
* @since 11.03.2010 |
|
32 |
*/ |
|
33 |
public abstract class DbImportDescriptionElementCreationMapperBase<ELEMENT extends DescriptionElementBase, STATE extends DbImportStateBase<?,?>> extends DbImportObjectCreationMapperBase<ELEMENT, STATE> { |
|
34 |
|
|
35 |
private static final Logger logger = LogManager.getLogger(DbImportDescriptionElementCreationMapperBase.class); |
|
36 |
|
|
37 |
//******************************* ATTRIBUTES ***************************************/ |
|
38 |
|
|
39 |
protected String taxonNamespace; |
|
40 |
protected String dbTaxonFkAttribute; |
|
41 |
protected boolean isImageGallery = false; |
|
42 |
protected String dbCitationAttribute; //if there is a single source available |
|
43 |
protected String sourceNamespace; |
|
44 |
protected String dbMicroCitationAttribute; |
|
45 |
|
|
46 |
//********************************* CONSTRUCTOR ****************************************/ |
|
47 |
|
|
48 |
protected DbImportDescriptionElementCreationMapperBase(String dbIdAttribute, String objectToCreateNamespace, String dbTaxonFkAttribute, String taxonNamespace) { |
|
49 |
super(dbIdAttribute, objectToCreateNamespace); |
|
50 |
this.taxonNamespace = taxonNamespace; |
|
51 |
this.dbTaxonFkAttribute = dbTaxonFkAttribute; |
|
52 |
} |
|
53 |
|
|
54 |
//************************************ METHODS *******************************************/ |
|
55 |
|
|
56 |
public DbImportDescriptionElementCreationMapperBase<ELEMENT,STATE> setSource(String dbCitationAttribute, String sourceNamespace, String dbMicroCitationAttribute){ |
|
57 |
this.dbCitationAttribute = dbCitationAttribute; |
|
58 |
this.sourceNamespace = sourceNamespace; |
|
59 |
this.dbMicroCitationAttribute = dbMicroCitationAttribute; |
|
60 |
return this; |
|
61 |
} |
|
62 |
|
|
63 |
@Override |
|
64 |
protected ELEMENT doInvoke(ResultSet rs, ELEMENT element) throws SQLException { |
|
65 |
Taxon taxon = getAcceptedTaxon(rs); |
|
66 |
if (taxon != null){ |
|
67 |
element = addDescriptionElement(taxon, element); |
|
68 |
}else{ |
|
69 |
logger.info("Taxon could not be determined. Description element was not add to any description or taxon"); |
|
70 |
} |
|
71 |
//Source |
|
72 |
if (StringUtils.isNotBlank(dbCitationAttribute)){ |
|
73 |
addSource(rs, element); |
|
74 |
} |
|
75 |
return element; |
|
76 |
} |
|
77 |
|
|
78 |
private void addSource(ResultSet rs, ELEMENT element) throws SQLException { |
|
79 |
String microCitation = getStringDbValue(rs, dbMicroCitationAttribute); |
|
80 |
Reference citation = (Reference) getState().getRelatedObject(sourceNamespace, String.valueOf(rs.getObject(dbCitationAttribute))); |
|
81 |
element.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, citation, microCitation); |
|
82 |
} |
|
83 |
|
|
84 |
protected Taxon getAcceptedTaxon(ResultSet rs) throws SQLException { |
|
85 |
String taxonFk = getForeignKey(rs, dbTaxonFkAttribute); |
|
86 |
TaxonBase<?> taxonBase = (TaxonBase<?>)getRelatedObject(taxonNamespace, taxonFk); |
|
87 |
Taxon taxon = null; |
|
88 |
if (taxonBase == null){ |
|
89 |
logger.warn("TaxonBase not found: " + taxonFk); |
|
90 |
}else if (taxonBase instanceof Taxon){ |
|
91 |
taxon = (Taxon)taxonBase; |
|
92 |
|
|
93 |
}else if (taxonBase instanceof Synonym){ |
|
94 |
Synonym synonym = CdmBase.deproxy(taxonBase, Synonym.class); |
|
95 |
Taxon accTaxon = synonym.getAcceptedTaxon(); |
|
96 |
if (accTaxon == null){ |
|
97 |
logger.warn("Synonym '" + synonym.getTitleCache() + "' ("+ taxonFk + ") has no accepted taxon. Can't define a taxon to add the description element to"); |
|
98 |
}else { |
|
99 |
taxon = accTaxon; |
|
100 |
} |
|
101 |
}else{ //null |
|
102 |
throw new IllegalStateException("TaxonBase must either be null, Taxon or Synonym but was something else"); |
|
103 |
} |
|
104 |
return taxon; |
|
105 |
} |
|
106 |
|
|
107 |
/** |
|
108 |
* Adds a description element to the taxon's first description which is not an image gallery. |
|
109 |
* If no such description exists a new one is generated. Returns the element or, if null if taxon is null. |
|
110 |
*/ |
|
111 |
protected ELEMENT addDescriptionElement(Taxon taxon, ELEMENT element) { |
|
112 |
if (taxon == null){ |
|
113 |
return null; |
|
114 |
}else{ |
|
115 |
TaxonDescription description = getTaxonDescription(taxon, isImageGallery); |
|
116 |
description.addElement(element); |
|
117 |
return element; |
|
118 |
} |
|
119 |
} |
|
120 |
|
|
121 |
protected TaxonDescription getTaxonDescription(Taxon taxon, boolean isImageGallery) { |
|
122 |
Set<TaxonDescription> descriptions = taxon.getDescriptions(); |
|
123 |
TaxonDescription description = null; |
|
124 |
if (descriptions.size() > 0){ |
|
125 |
for (TaxonDescription desc : descriptions){ |
|
126 |
if ( desc.isImageGallery() == isImageGallery ){ |
|
127 |
description = desc; |
|
128 |
break; |
|
129 |
} |
|
130 |
} |
|
131 |
} |
|
132 |
if (description == null){ |
|
133 |
description = TaxonDescription.NewInstance(taxon, isImageGallery); |
|
134 |
} |
|
135 |
return description; |
|
136 |
} |
|
137 |
} |
|
1 |
/** |
|
2 |
* Copyright (C) 2007 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 |
|
|
10 |
package eu.etaxonomy.cdm.io.common.mapping; |
|
11 |
|
|
12 |
import java.sql.ResultSet; |
|
13 |
import java.sql.SQLException; |
|
14 |
import java.util.Set; |
|
15 |
|
|
16 |
import org.apache.commons.lang.StringUtils; |
|
17 |
import org.apache.logging.log4j.LogManager; |
|
18 |
import org.apache.logging.log4j.Logger; |
|
19 |
|
|
20 |
import eu.etaxonomy.cdm.io.common.DbImportStateBase; |
|
21 |
import eu.etaxonomy.cdm.model.common.CdmBase; |
|
22 |
import eu.etaxonomy.cdm.model.description.DescriptionElementBase; |
|
23 |
import eu.etaxonomy.cdm.model.description.TaxonDescription; |
|
24 |
import eu.etaxonomy.cdm.model.reference.OriginalSourceType; |
|
25 |
import eu.etaxonomy.cdm.model.reference.Reference; |
|
26 |
import eu.etaxonomy.cdm.model.taxon.Synonym; |
|
27 |
import eu.etaxonomy.cdm.model.taxon.Taxon; |
|
28 |
import eu.etaxonomy.cdm.model.taxon.TaxonBase; |
|
29 |
|
|
30 |
/** |
|
31 |
* @author a.mueller |
|
32 |
* @since 11.03.2010 |
|
33 |
*/ |
|
34 |
public abstract class DbImportDescriptionElementCreationMapperBase<ELEMENT extends DescriptionElementBase, STATE extends DbImportStateBase<?,?>> extends DbImportObjectCreationMapperBase<ELEMENT, STATE> { |
|
35 |
|
|
36 |
private static final Logger logger = LogManager.getLogger(); |
|
37 |
|
|
38 |
//******************************* ATTRIBUTES ***************************************/ |
|
39 |
|
|
40 |
protected String taxonNamespace; |
|
41 |
protected String dbTaxonFkAttribute; |
|
42 |
protected boolean isImageGallery = false; |
|
43 |
protected String dbCitationAttribute; //if there is a single source available |
|
44 |
protected String sourceNamespace; |
|
45 |
protected String dbMicroCitationAttribute; |
|
46 |
|
|
47 |
//********************************* CONSTRUCTOR ****************************************/ |
|
48 |
|
|
49 |
protected DbImportDescriptionElementCreationMapperBase(String dbIdAttribute, String objectToCreateNamespace, String dbTaxonFkAttribute, String taxonNamespace) { |
|
50 |
super(dbIdAttribute, objectToCreateNamespace); |
|
51 |
this.taxonNamespace = taxonNamespace; |
|
52 |
this.dbTaxonFkAttribute = dbTaxonFkAttribute; |
|
53 |
} |
|
54 |
|
|
55 |
//************************************ METHODS *******************************************/ |
|
56 |
|
|
57 |
public DbImportDescriptionElementCreationMapperBase<ELEMENT,STATE> setSource(String dbCitationAttribute, String sourceNamespace, String dbMicroCitationAttribute){ |
|
58 |
this.dbCitationAttribute = dbCitationAttribute; |
|
59 |
this.sourceNamespace = sourceNamespace; |
|
60 |
this.dbMicroCitationAttribute = dbMicroCitationAttribute; |
|
61 |
return this; |
|
62 |
} |
|
63 |
|
|
64 |
@Override |
|
65 |
protected ELEMENT doInvoke(ResultSet rs, ELEMENT element) throws SQLException { |
|
66 |
Taxon taxon = getAcceptedTaxon(rs); |
|
67 |
if (taxon != null){ |
|
68 |
element = addDescriptionElement(taxon, element); |
|
69 |
}else{ |
|
70 |
logger.info("Taxon could not be determined. Description element was not add to any description or taxon"); |
|
71 |
} |
|
72 |
//Source |
|
73 |
if (StringUtils.isNotBlank(dbCitationAttribute)){ |
|
74 |
addSource(rs, element); |
|
75 |
} |
|
76 |
return element; |
|
77 |
} |
|
78 |
|
|
79 |
private void addSource(ResultSet rs, ELEMENT element) throws SQLException { |
|
80 |
String microCitation = getStringDbValue(rs, dbMicroCitationAttribute); |
|
81 |
Reference citation = (Reference) getState().getRelatedObject(sourceNamespace, String.valueOf(rs.getObject(dbCitationAttribute))); |
|
82 |
element.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, citation, microCitation); |
|
83 |
} |
|
84 |
|
|
85 |
protected Taxon getAcceptedTaxon(ResultSet rs) throws SQLException { |
|
86 |
String taxonFk = getForeignKey(rs, dbTaxonFkAttribute); |
|
87 |
TaxonBase<?> taxonBase = (TaxonBase<?>)getRelatedObject(taxonNamespace, taxonFk); |
|
88 |
Taxon taxon = null; |
|
89 |
if (taxonBase == null){ |
|
90 |
logger.warn("TaxonBase not found: " + taxonFk); |
|
91 |
}else if (taxonBase instanceof Taxon){ |
|
92 |
taxon = (Taxon)taxonBase; |
|
93 |
|
|
94 |
}else if (taxonBase instanceof Synonym){ |
|
95 |
Synonym synonym = CdmBase.deproxy(taxonBase, Synonym.class); |
|
96 |
Taxon accTaxon = synonym.getAcceptedTaxon(); |
|
97 |
if (accTaxon == null){ |
|
98 |
logger.warn("Synonym '" + synonym.getTitleCache() + "' ("+ taxonFk + ") has no accepted taxon. Can't define a taxon to add the description element to"); |
|
99 |
}else { |
|
100 |
taxon = accTaxon; |
|
101 |
} |
|
102 |
}else{ //null |
|
103 |
throw new IllegalStateException("TaxonBase must either be null, Taxon or Synonym but was something else"); |
|
104 |
} |
|
105 |
return taxon; |
|
106 |
} |
|
107 |
|
|
108 |
/** |
|
109 |
* Adds a description element to the taxon's first description which is not an image gallery. |
|
110 |
* If no such description exists a new one is generated. Returns the element or, if null if taxon is null. |
|
111 |
*/ |
|
112 |
protected ELEMENT addDescriptionElement(Taxon taxon, ELEMENT element) { |
|
113 |
if (taxon == null){ |
|
114 |
return null; |
|
115 |
}else{ |
|
116 |
TaxonDescription description = getTaxonDescription(taxon, isImageGallery); |
|
117 |
description.addElement(element); |
|
118 |
return element; |
|
119 |
} |
|
120 |
} |
|
121 |
|
|
122 |
protected TaxonDescription getTaxonDescription(Taxon taxon, boolean isImageGallery) { |
|
123 |
Set<TaxonDescription> descriptions = taxon.getDescriptions(); |
|
124 |
TaxonDescription description = null; |
|
125 |
if (descriptions.size() > 0){ |
|
126 |
for (TaxonDescription desc : descriptions){ |
|
127 |
if ( desc.isImageGallery() == isImageGallery ){ |
|
128 |
description = desc; |
|
129 |
break; |
|
130 |
} |
|
131 |
} |
|
132 |
} |
|
133 |
if (description == null){ |
|
134 |
description = TaxonDescription.NewInstance(taxon, isImageGallery); |
|
135 |
} |
|
136 |
return description; |
|
137 |
} |
|
138 |
} |
Also available in: Unified diff
cleanup