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
16 import org.apache.log4j.Logger;
17
18 import eu.etaxonomy.cdm.io.common.CdmImportBase;
19 import eu.etaxonomy.cdm.io.common.DbImportStateBase;
20 import eu.etaxonomy.cdm.io.erms.ICheckIgnoreMapper;
21 import eu.etaxonomy.cdm.model.common.CdmBase;
22 import eu.etaxonomy.cdm.model.name.NameTypeDesignationStatus;
23 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
24 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class DbImportNameTypeDesignationMapper<STATE extends DbImportStateBase<?,?>, T extends IDbImportTransformed> extends DbImportMultiAttributeMapperBase<CdmBase, STATE> {
39 private static final Logger logger = Logger.getLogger(DbImportNameTypeDesignationMapper.class);
40
41 /
42
43 public static DbImportNameTypeDesignationMapper<?,?> NewInstance(String dbFromAttribute, String dbToAttribute, String relatedObjectNamespace, String desigStatusAttribute){
44 return new DbImportNameTypeDesignationMapper(dbFromAttribute, dbToAttribute, null, relatedObjectNamespace, desigStatusAttribute);
45 }
46
47 /
48 private String fromAttribute;
49 private String toAttribute;
50 private NameTypeDesignationStatus designationStatus;
51 private String relatedObjectNamespace;
52 private String citationAttribute;
53 private String microCitationAttribute;
54 private String designationStatusAttribute;
55
56
57 /
58
59
60
61
62 protected DbImportNameTypeDesignationMapper(String fromAttribute, String toAttribute, NameTypeDesignationStatus designationStatus, String relatedObjectNamespace, String desigStatusAttribute) {
63 super();
64
65 this.fromAttribute = fromAttribute;
66 this.toAttribute = toAttribute;
67 this.relatedObjectNamespace = relatedObjectNamespace;
68 this.designationStatusAttribute = desigStatusAttribute;
69 this.designationStatus = designationStatus;
70 }
71
72 /
73
74
75
76
77 public CdmBase invoke(ResultSet rs, CdmBase cdmBase) throws SQLException {
78 STATE state = importMapperHelper.getState();
79 CdmImportBase currentImport = state.getCurrentIO();
80 if (currentImport instanceof ICheckIgnoreMapper){
81 boolean ignoreRecord = ((ICheckIgnoreMapper)currentImport).checkIgnoreMapper(this, rs);
82 if (ignoreRecord){
83 return cdmBase;
84 }
85 }
86
87 CdmBase fromObject = getRelatedObject(rs, fromAttribute);
88 CdmBase toObject = getRelatedObject(rs, toAttribute);
89
90 ReferenceBase citation = (ReferenceBase)getRelatedObject(rs, citationAttribute);
91 String microCitation = null;
92 if (citationAttribute != null){
93 microCitation = rs.getString(microCitationAttribute);
94 }
95
96 Object designationStatusValue = null;
97 if (citationAttribute != null){
98 designationStatusValue = rs.getObject(designationStatusAttribute);
99 }
100
101
102 if (fromObject == null){
103 String warning = "Higher rank name could not be found. Name type not added to higher rank name";
104 logger.warn(warning);
105 return cdmBase;
106 }
107 TaxonNameBase typifiedName = checkTaxonNameBaseType(fromObject);
108
109 if (toObject == null){
110 String warning = "Species name could not be found. Name type not added to higher rank name";
111 logger.warn(warning);
112 return cdmBase;
113 }
114 TaxonNameBase typeName = checkTaxonNameBaseType(toObject);
115
116 boolean addToAllHomotypicNames = false;
117 String originalNameString = null;
118
119 NameTypeDesignationStatus status = this.designationStatus;
120 if (designationStatusValue != null){
121
122 if (currentImport instanceof IDbImportTransformed){
123 IDbImportTransformer transformer = ((IDbImportTransformed)currentImport).getTransformer();
124 status = transformer.transformNameTypeDesignationStatus(designationStatusValue);
125 }
126 }
127 typifiedName.addNameTypeDesignation(typeName, citation, microCitation, originalNameString, status, addToAllHomotypicNames);
128
129 return typifiedName;
130 }
131
132
133
134
135
136
137
138
139 protected CdmBase getRelatedObject(ResultSet rs, String dbAttribute) throws SQLException {
140 CdmBase result = null;
141 if (dbAttribute != null){
142 Object dbValue = rs.getObject(dbAttribute);
143 String id = String.valueOf(dbValue);
144 DbImportStateBase state = importMapperHelper.getState();
145 result = state.getRelatedObject(relatedObjectNamespace, id);
146 }
147 return result;
148 }
149
150
151
152
153
154 private TaxonNameBase checkTaxonNameBaseType(CdmBase cdmBase) {
155 if (! cdmBase.isInstanceOf(TaxonNameBase.class)){
156 String warning = "Type name or typifier name is not of type TaxonNameBase but " + cdmBase.getClass().getName();
157 logger.warn(warning);
158 throw new IllegalArgumentException(warning);
159 }
160 return (cdmBase.deproxy(cdmBase, TaxonNameBase.class));
161 }
162
163
164 }