Project

General

Profile

Download (3.8 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.out;
11

    
12
import java.sql.Types;
13

    
14
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
15

    
16
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
17
import eu.etaxonomy.cdm.model.common.CdmBase;
18
import eu.etaxonomy.cdm.model.description.DescriptionBase;
19
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
20
import eu.etaxonomy.cdm.model.description.TaxonDescription;
21
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
22
import eu.etaxonomy.cdm.model.name.TaxonName;
23
import eu.etaxonomy.cdm.model.taxon.Taxon;
24

    
25
/**
26
 * @author a.mueller
27
 * @since 12.05.2009
28
 */
29
public class DbDescriptionElementTaxonMapper extends DbSingleAttributeExportMapperBase<DbExportStateBase<?, IExportTransformer>> implements IDbExportMapper<DbExportStateBase<?, IExportTransformer>, IExportTransformer>{
30
	@SuppressWarnings("unused")
31
	private static final Logger logger = LogManager.getLogger(DbDescriptionElementTaxonMapper.class);
32

    
33
	private boolean isCache = false;
34
	private boolean cacheIsNameTitleCache = false;
35

    
36
	public static DbDescriptionElementTaxonMapper NewInstance(String dbAttributeString){
37
		return new DbDescriptionElementTaxonMapper(dbAttributeString, false, false, null);
38
	}
39

    
40
	public static DbDescriptionElementTaxonMapper NewInstance(String dbAttributeString, boolean isCache, boolean cacheIsNameTitleCache, Object defaultValue){
41
		return new DbDescriptionElementTaxonMapper(dbAttributeString, isCache, cacheIsNameTitleCache, defaultValue);
42
	}
43

    
44
	protected DbDescriptionElementTaxonMapper(String dbAttributeString, boolean isCache, boolean cacheIsNameTitleCache, Object defaultValue) {
45
		super("inDescription.taxon", dbAttributeString, defaultValue);
46
		this.isCache = isCache;
47
		this.cacheIsNameTitleCache = cacheIsNameTitleCache;
48
	}
49

    
50
	@Override
51
	protected Object getValue(CdmBase cdmBase) {
52
		Object result = null;
53
		if (cdmBase.isInstanceOf(DescriptionElementBase.class)){
54
			DescriptionElementBase element = CdmBase.deproxy(cdmBase, DescriptionElementBase.class);
55
			DescriptionBase<?> inDescription = element.getInDescription();
56
			if (inDescription != null ){
57
				if (inDescription.isInstanceOf(TaxonDescription.class)) {
58
					TaxonDescription taxonDescription = CdmBase.deproxy(inDescription, TaxonDescription.class);
59
					Taxon taxon = taxonDescription.getTaxon();
60
					if (isCache){
61
						if (cacheIsNameTitleCache && taxon.getName() != null){
62
							return taxon.getName().getTitleCache();
63
						}else{
64
							return taxon.getTitleCache();
65
						}
66
					}else{
67
						result = getState().getDbId(taxon);
68
					}
69
				}else if (inDescription.isInstanceOf(TaxonNameDescription.class)){
70
					TaxonNameDescription nameDescription = CdmBase.deproxy(inDescription, TaxonNameDescription.class);
71
					TaxonName taxonName = nameDescription.getTaxonName();
72
					if (isCache){
73
						return taxonName.getTitleCache();
74
					}else{
75
						result = getState().getDbId(taxonName);
76
					}
77
				}else{
78
					throw new ClassCastException("Description of type "+ inDescription.getClass().getName() + " not handled yet");
79
				}
80
			}else{
81
				throw new ClassCastException("DescriptionElement has no description "+ element.getUuid());
82
			}
83
			return result;
84
		}else{
85
			throw new ClassCastException("CdmBase for "+this.getClass().getName() +" must be of type DescriptionElementBase, but was " + cdmBase.getClass());
86
		}
87
	}
88

    
89
	@Override
90
	protected int getSqlType() {
91
		if (isCache){
92
			return Types.VARCHAR;
93
		}else{
94
			return Types.INTEGER;
95
		}
96
	}
97

    
98
	@Override
99
	public Class<?> getTypeClass() {
100
		if (isCache){
101
			return String.class;
102
		}else{
103
			return  Integer.class;
104
		}
105
	}
106
}
(10-10/44)