Project

General

Profile

Download (8.88 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
 * @created 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

    
84
	/* (non-Javadoc)
85
	 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
86
	 */
87
	@Override
88
    public CdmBase invoke(ResultSet rs, CdmBase cdmBase) throws SQLException {
89
		STATE state = getState();
90
		CdmImportBase<?,?> currentImport = state.getCurrentIO();
91
		if (currentImport instanceof ICheckIgnoreMapper){
92
			boolean ignoreRecord = ((ICheckIgnoreMapper)currentImport).checkIgnoreMapper(this, rs);
93
			if (ignoreRecord){
94
				return cdmBase;
95
			}
96
		}
97

    
98
		TaxonBase<?> fromObject = (TaxonBase<?>)getRelatedObject(rs, fromAttribute, fromNamespace);
99
		TaxonBase<?> toObject = (TaxonBase<?>)getRelatedObject(rs, toAttribute, toNamespace);
100
		TaxonBase<?> alternativeToObject = (TaxonBase<?>)getRelatedObject(rs, alternativeAttribute, alternativeNamespace);
101

    
102
		String fromId = String.valueOf(rs.getObject(fromAttribute));
103
		String toId = rs.getObject(toAttribute) == null ? null : String.valueOf(rs.getObject(toAttribute));
104
		String alternativeToId = rs.getObject(alternativeAttribute) == null ? null : String.valueOf(rs.getObject(alternativeAttribute));
105

    
106
		if (toId == null){
107
			return fromObject;
108
		}
109

    
110

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

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

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

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

    
154
				return cdmBase;
155
			}
156
		}
157

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

    
168

    
169

    
170

    
171

    
172
	/**
173
	 * TODO copied from BM import. May be more generic
174
	 * @param state
175
	 * @param classificationMap
176
	 * @param treeRefFk
177
	 * @param child
178
	 * @param parent
179
	 * @param citation
180
	 * @param microCitation
181
	 * @return
182
	 */
183

    
184
	public static final String TAXONOMIC_TREE_NAMESPACE = "Classification";
185

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

    
211
		TaxonNode childNode = tree.addParentChild(parent, child, citation, microCitation);
212
		return (childNode != null);
213
	}
214

    
215

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

    
235

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

    
252

    
253
}
(35-35/51)