Project

General

Profile

Download (4.22 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.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.TaxonNameBase;
23
import eu.etaxonomy.cdm.model.taxon.Taxon;
24

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

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

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

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

    
45
	/**
46
	 * @param dbAttributeString
47
	 * @param cdmAttributeString
48
	 */
49
	protected DbDescriptionElementTaxonMapper(String dbAttributeString, boolean isCache, boolean cacheIsNameTitleCache, Object defaultValue) {
50
		super("inDescription.taxon", dbAttributeString, defaultValue);
51
		this.isCache = isCache;
52
		this.cacheIsNameTitleCache = cacheIsNameTitleCache;
53
	}
54

    
55
	/* (non-Javadoc)
56
	 * @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbSingleAttributeExportMapperBase#getValue(eu.etaxonomy.cdm.model.common.CdmBase)
57
	 */
58
	@Override
59
	protected Object getValue(CdmBase cdmBase) {
60
		Object result = null;
61
		if (cdmBase.isInstanceOf(DescriptionElementBase.class)){
62
			DescriptionElementBase element = CdmBase.deproxy(cdmBase, DescriptionElementBase.class);
63
			DescriptionBase<?> inDescription = element.getInDescription();
64
			if (inDescription != null ){
65
				if (inDescription.isInstanceOf(TaxonDescription.class)) {
66
					TaxonDescription taxonDescription = CdmBase.deproxy(inDescription, TaxonDescription.class);
67
					Taxon taxon = taxonDescription.getTaxon();
68
					if (isCache){
69
						if (cacheIsNameTitleCache && taxon.getName() != null){
70
							return taxon.getName().getTitleCache();
71
						}else{
72
							return taxon.getTitleCache();
73
						}
74
					}else{
75
						result = getState().getDbId(taxon);
76
					}
77
				}else if (inDescription.isInstanceOf(TaxonNameDescription.class)){
78
					TaxonNameDescription nameDescription = CdmBase.deproxy(inDescription, TaxonNameDescription.class);
79
					TaxonNameBase<?,?> taxonName = nameDescription.getTaxonName();
80
					if (isCache){
81
						return taxonName.getTitleCache();
82
					}else{
83
						result = getState().getDbId(taxonName);
84
					}
85
				}else{
86
					throw new ClassCastException("Description of type "+ inDescription.getClass().getName() + " not handled yet");
87
				}
88
			}else{
89
				throw new ClassCastException("DescriptionElement has no description "+ element.getUuid());
90
			}
91
			return result;
92
		}else{
93
			throw new ClassCastException("CdmBase for "+this.getClass().getName() +" must be of type DescriptionElementBase, but was " + cdmBase.getClass());
94
		}
95
	}
96

    
97
	/* (non-Javadoc)
98
	 * @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbSingleAttributeExportMapperBase#getValueType()
99
	 */
100
	@Override
101
	protected int getSqlType() {
102
		if (isCache){
103
			return Types.VARCHAR;
104
		}else{
105
			return Types.INTEGER;
106
		}
107
	}
108

    
109

    
110
	/* (non-Javadoc)
111
	 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
112
	 */
113
	@Override
114
	public Class<?> getTypeClass() {
115
		if (isCache){
116
			return String.class;
117
		}else{
118
			return  Integer.class;
119
		}
120
	}
121
}
(10-10/40)