Project

General

Profile

Download (8.73 KB) Statistics
| Branch: | Tag: | Revision:
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.UUID;
15

    
16
import org.apache.log4j.Logger;
17

    
18
import eu.etaxonomy.cdm.api.service.IClassificationService;
19
import eu.etaxonomy.cdm.io.common.CdmImportBase;
20
import eu.etaxonomy.cdm.io.common.DbImportConfiguratorBase;
21
import eu.etaxonomy.cdm.io.common.DbImportStateBase;
22
import eu.etaxonomy.cdm.model.common.CdmBase;
23
import eu.etaxonomy.cdm.model.reference.Reference;
24
import eu.etaxonomy.cdm.model.taxon.Classification;
25
import eu.etaxonomy.cdm.model.taxon.Taxon;
26
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
27
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
28

    
29
/**
30
 * @author a.mueller
31
 * @since 02.03.2010
32
 * @param <CDM_BASE>
33
 * @param <STATE>
34
 */
35
public class DbImportTaxIncludedInMapper<STATE extends DbImportStateBase<DbImportConfiguratorBase<STATE>,?>> extends DbImportMultiAttributeMapperBase<CdmBase, STATE> {
36
	private static final Logger logger = Logger.getLogger(DbImportTaxIncludedInMapper.class);
37

    
38
//******************************** FACTORY METHOD ***************************************************/
39

    
40
	public static DbImportTaxIncludedInMapper<?> NewInstance(String dbChildAttribute, String dbChildNamespace, String dbParentAttribute, String parentNamespace, String dbAlternativeParentAttribute, String alternativeParentNamespace, String dbTreeAttribute){
41
		String citationNamespace = null;
42
		String citationAttribute = null;
43
		return new DbImportTaxIncludedInMapper<>(dbChildAttribute, dbChildNamespace, dbParentAttribute, parentNamespace, dbAlternativeParentAttribute, alternativeParentNamespace, dbTreeAttribute, citationAttribute, citationNamespace);
44
	}
45

    
46
//******************************* ATTRIBUTES ***************************************/
47
	private String fromAttribute;
48
	private String toAttribute;
49

    
50
	private String fromNamespace;
51
	private String toNamespace;
52

    
53
	private String citationAttribute;
54
	private String citationNamespace;
55

    
56
	private String microCitationAttribute;
57
	private String treeAttribute;
58
	private String alternativeAttribute;
59
	private String alternativeNamespace;
60

    
61

    
62
//********************************* CONSTRUCTOR ****************************************/
63
	/**
64
	 * @param relatedObjectNamespace
65
	 * @param mappingImport
66
	 */
67
	protected DbImportTaxIncludedInMapper(String fromAttribute, String fromNamespace, String toAttribute, String toNamespace, String alternativeAttribute, String alternativeNamespace, String treeAttribute, String citationAttribute, String citationNamespace) {
68
		super();
69
		//TODO make it a single attribute mapper
70
		this.fromAttribute = fromAttribute;
71
		this.fromNamespace = fromNamespace;
72
		this.toAttribute = toAttribute;
73
		this.toNamespace = toNamespace;
74
		this.treeAttribute = treeAttribute;
75
		this.alternativeAttribute = alternativeAttribute;
76
		this.alternativeNamespace = alternativeNamespace;
77
		this.citationAttribute = citationAttribute;
78
		this.citationNamespace = citationNamespace;
79
	}
80

    
81
//************************************ METHODS *******************************************/
82

    
83
	@Override
84
    public CdmBase invoke(ResultSet rs, CdmBase cdmBase) throws SQLException {
85
		STATE state = getState();
86
		CdmImportBase<?,?> currentImport = state.getCurrentIO();
87
		if (currentImport instanceof ICheckIgnoreMapper){
88
			boolean ignoreRecord = ((ICheckIgnoreMapper)currentImport).checkIgnoreMapper(this, rs);
89
			if (ignoreRecord){
90
				return cdmBase;
91
			}
92
		}
93

    
94
		TaxonBase<?> fromObject = (TaxonBase<?>)getRelatedObject(rs, fromAttribute, fromNamespace);
95
		TaxonBase<?> toObject = (TaxonBase<?>)getRelatedObject(rs, toAttribute, toNamespace);
96
		TaxonBase<?> alternativeToObject = (TaxonBase<?>)getRelatedObject(rs, alternativeAttribute, alternativeNamespace);
97

    
98
		String fromId = String.valueOf(rs.getObject(fromAttribute));
99
		String toId = rs.getObject(toAttribute) == null ? null : String.valueOf(rs.getObject(toAttribute));
100
		String alternativeToId = rs.getObject(alternativeAttribute) == null ? null : String.valueOf(rs.getObject(alternativeAttribute));
101

    
102
		if (toId == null){
103
			return fromObject;
104
		}
105

    
106

    
107
		Reference citation = (Reference)getRelatedObject(rs, citationAttribute, citationNamespace);
108
		String microCitation = null;
109
		if (citationAttribute != null){
110
			microCitation = rs.getString(microCitationAttribute);
111
		}
112
		//TODO check int
113
		Integer treeFk = null;
114
		if (treeAttribute != null){
115
			treeFk = rs.getInt(treeAttribute);
116
		}
117

    
118
		if (fromObject == null){
119
			String warning  = "The child taxon could not be found. Child taxon not added to the tree";
120
			logger.warn(warning);
121
			return cdmBase;
122
		}
123
		Taxon fromTaxon;
124
		try {
125
			fromTaxon = checkTaxonType(fromObject, "Child", fromId);
126
		} catch (IllegalArgumentException e2) {
127
			//fromTaxon is null
128
			return cdmBase;
129
		}
130

    
131
		if (toObject == null){
132
			String warning  = "The parent taxon could not be found. Child taxon (" + fromTaxon.getTitleCache() + "; " + fromTaxon.getUuid() + ") not added to the tree";
133
			logger.warn(warning);
134
			return cdmBase;
135
		}
136

    
137
		Taxon toTaxon;
138
		try {
139
			toTaxon = checkTaxonType(toObject, "Parent", toId);
140
		} catch (IllegalArgumentException e) {
141
			if (alternativeToObject != null){
142
				try {
143
					toTaxon = checkTaxonType(alternativeToObject, "Alternative parent", alternativeToId);
144
				} catch (IllegalArgumentException e1) {
145
					logger.warn("Alternative taxon is of wrong type: " +  alternativeToObject.getTitleCache() + "; " + alternativeToObject.getUuid());
146
					return cdmBase;
147
				}
148
			}else{
149

    
150
				return cdmBase;
151
			}
152
		}
153

    
154
		if (fromTaxon.equals(toTaxon)){
155
			String warning  = "A taxon may not be a child of itself. Taxon not added to the tree: " + toTaxon.getTitleCache() + ", " + toTaxon.getLsid().toString();
156
			logger.warn(warning);
157
			return cdmBase;
158
		}
159
		//maps the reference
160
		makeTaxonomicallyIncluded(state, treeFk, fromTaxon, toTaxon, citation, microCitation);
161
		return fromTaxon;
162
	}
163

    
164

    
165

    
166

    
167

    
168
	/**
169
	 * TODO copied from BM import. May be more generic
170
	 * @param state
171
	 * @param classificationMap
172
	 * @param treeRefFk
173
	 * @param child
174
	 * @param parent
175
	 * @param citation
176
	 * @param microCitation
177
	 * @return
178
	 */
179

    
180
	public static final String TAXONOMIC_TREE_NAMESPACE = "Classification";
181

    
182
	private boolean makeTaxonomicallyIncluded(STATE state, Integer classificationRefFk, Taxon child, Taxon parent, Reference citation, String microCitation){
183
		String treeKey;
184
		UUID treeUuid;
185
		if (classificationRefFk == null){
186
			treeKey = "1";  // there is only one tree and it gets the key '1'
187
			treeUuid = state.getConfig().getClassificationUuid();
188
		}else{
189
			treeKey =String.valueOf(classificationRefFk);
190
			treeUuid = state.getTreeUuidByTreeKey(treeKey);
191
		}
192
		Classification tree = (Classification)state.getRelatedObject(TAXONOMIC_TREE_NAMESPACE, treeKey);
193
		if (tree == null){
194
			IClassificationService service = state.getCurrentIO().getClassificationService();
195
			tree = service.find(treeUuid);
196
			if (tree == null){
197
				String treeName = state.getConfig().getClassificationName();
198
				tree = Classification.NewInstance(treeName);
199
				tree.setUuid(treeUuid);
200
				//FIXME tree reference
201
				//tree.setReference(ref);
202
				service.save(tree);
203
			}
204
			state.addRelatedObject(TAXONOMIC_TREE_NAMESPACE, treeKey, tree);
205
		}
206

    
207
		TaxonNode childNode = tree.addParentChild(parent, child, citation, microCitation);
208
		return (childNode != null);
209
	}
210

    
211

    
212
	/**
213
	 *	//TODO copied from DbImportObjectMapper. Maybe these can be merged again in future
214
	 * @param rs
215
	 * @param dbAttribute
216
	 * @return
217
	 * @throws SQLException
218
	 */
219
	@Override
220
    protected CdmBase getRelatedObject(ResultSet rs, String dbAttribute, String namespace) throws SQLException {
221
		CdmBase result = null;
222
		if (dbAttribute != null){
223
			Object dbValue = rs.getObject(dbAttribute);
224
			String id = String.valueOf(dbValue);
225
			DbImportStateBase<?,?> state = importMapperHelper.getState();
226
			result = state.getRelatedObject(namespace, id);
227
		}
228
		return result;
229
	}
230

    
231

    
232
	/**
233
	 * Checks if cdmBase is of type Taxon
234
	 * @param taxonBase
235
	 * @param typeString
236
	 * @param id
237
	 * @return
238
	 */
239
	private Taxon checkTaxonType(TaxonBase<?> taxonBase, String typeString, String id) throws IllegalArgumentException{
240
		if (! taxonBase.isInstanceOf(Taxon.class)){
241
			String warning = typeString + " (" + id + ") is not of type Taxon but of type " + taxonBase.getClass().getSimpleName();
242
			logger.warn(warning);
243
			throw new IllegalArgumentException(warning);
244
		}
245
		return (CdmBase.deproxy(taxonBase, Taxon.class));
246
	}
247

    
248

    
249
}
(35-35/51)