1
2
3
4
5
6
7
8
9
10
11 package eu.etaxonomy.cdm.io.common.mapping;
12
13 import java.sql.ResultSet;
14 import java.sql.SQLException;
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.UUID;
18
19 import org.apache.log4j.Logger;
20
21 import eu.etaxonomy.cdm.common.CdmUtils;
22 import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState;
23 import eu.etaxonomy.cdm.io.common.CdmImportBase;
24 import eu.etaxonomy.cdm.io.common.DbImportStateBase;
25 import eu.etaxonomy.cdm.io.common.ICdmIO;
26 import eu.etaxonomy.cdm.io.erms.ICheckIgnoreMapper;
27 import eu.etaxonomy.cdm.model.common.CdmBase;
28 import eu.etaxonomy.cdm.model.common.DescriptionElementSource;
29 import eu.etaxonomy.cdm.model.common.IOriginalSource;
30 import eu.etaxonomy.cdm.model.common.ISourceable;
31 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
32 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
33 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
34 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
35 import eu.etaxonomy.cdm.model.taxon.Synonym;
36 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
37 import eu.etaxonomy.cdm.model.taxon.Taxon;
38 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
39 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
40 import eu.etaxonomy.cdm.model.taxon.TaxonomicTree;
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class DbImportSynonymMapper<STATE extends DbImportStateBase> extends DbImportMultiAttributeMapperBase<CdmBase, STATE> {
55 private static final Logger logger = Logger.getLogger(DbImportSynonymMapper.class);
56
57 /
58
59 public static DbImportSynonymMapper<?> NewInstance(String dbFromAttribute, String dbToAttribute, String relatedObjectNamespace, String relTypeAttribute){
60 return new DbImportSynonymMapper(dbFromAttribute, dbToAttribute, null, relatedObjectNamespace, relTypeAttribute);
61 }
62
63 /
64 private String fromAttribute;
65 private String toAttribute;
66
67 private String relatedObjectNamespace;
68 private String citationAttribute;
69 private String microCitationAttribute;
70 private String relationshipTypeAttribute;
71
72
73 /
74
75
76
77
78 protected DbImportSynonymMapper(String fromAttribute, String toAttribute, TaxonRelationshipType relType, String relatedObjectNamespace, String relTypeAttribute) {
79 super();
80
81 this.fromAttribute = fromAttribute;
82 this.toAttribute = toAttribute;
83
84 this.relatedObjectNamespace = relatedObjectNamespace;
85 this.relationshipTypeAttribute = relTypeAttribute;
86 if (relTypeAttribute != null){
87 logger.warn("Synonymrelationship type not yet implemented");
88 }
89 }
90
91 /
92
93
94
95
96 public CdmBase invoke(ResultSet rs, CdmBase cdmBase) throws SQLException {
97 STATE state = getState();
98 ICdmIO currentImport = state.getCurrentIO();
99 if (currentImport instanceof ICheckIgnoreMapper){
100 boolean ignoreRecord = ((ICheckIgnoreMapper)currentImport).checkIgnoreMapper(this, rs);
101 if (ignoreRecord){
102 return cdmBase;
103 }
104 }
105
106 TaxonBase fromObject = (TaxonBase)getRelatedObject(rs, fromAttribute);
107 TaxonBase toObject = (TaxonBase)getRelatedObject(rs, toAttribute);
108 String fromId = String.valueOf(rs.getObject(fromAttribute));
109 String toId = String.valueOf(rs.getObject(toAttribute));
110
111
112 ReferenceBase citation = (ReferenceBase)getRelatedObject(rs, citationAttribute);
113 String microCitation = null;
114 if (citationAttribute != null){
115 microCitation = rs.getString(microCitationAttribute);
116 }
117
118
119 if (fromObject == null){
120 String warning = "The synonym (" + fromId + ") could not be found. Synonym not added to accepted taxon";
121 logger.warn(warning);
122 return cdmBase;
123 }
124 Synonym synonym = checkSynonymType(fromObject, fromId);
125
126 if (toObject == null){
127 String warning = "The accepted taxon (" + toId + ") could not be found. Synonym not added to accepted taxon";
128 logger.warn(warning);
129 return cdmBase;
130 }
131 Taxon taxon = checkTaxonType(toObject, "Accepted taxon", toId);
132
133 SynonymRelationshipType relType = SynonymRelationshipType.SYNONYM_OF();
134
135 taxon.addSynonym(synonym, relType, citation, microCitation);
136 return synonym;
137 }
138
139
140
141
142
143
144
145
146 protected CdmBase getRelatedObject(ResultSet rs, String dbAttribute) throws SQLException {
147 CdmBase result = null;
148 if (dbAttribute != null){
149 Object dbValue = rs.getObject(dbAttribute);
150 String id = String.valueOf(dbValue);
151 DbImportStateBase state = importMapperHelper.getState();
152 result = state.getRelatedObject(relatedObjectNamespace, id);
153 }
154 return result;
155 }
156
157
158
159
160
161
162 private Taxon checkTaxonType(TaxonBase taxonBase, String typeString, String id) {
163 if (! taxonBase.isInstanceOf(Taxon.class)){
164 String warning = typeString + " (" + id + ") is not of type Taxon but of type " + taxonBase.getClass().getSimpleName();
165 logger.warn(warning);
166 throw new IllegalArgumentException(warning);
167 }
168 return (taxonBase.deproxy(taxonBase, Taxon.class));
169 }
170
171
172
173
174
175 private Synonym checkSynonymType(CdmBase cdmBase, String id) {
176 if (! cdmBase.isInstanceOf(Synonym.class)){
177 String warning = "Synonym (" + id + ") is not of type Synonym but of type " + cdmBase.getClass().getSimpleName();
178 logger.warn(warning);
179 throw new IllegalArgumentException(warning);
180 }
181 return (cdmBase.deproxy(cdmBase, Synonym.class));
182 }
183
184
185 }