Project

General

Profile

Download (3.46 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
import java.util.Set;
14

    
15
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
16
import org.hibernate.Hibernate;
17

    
18
import eu.etaxonomy.cdm.io.common.DbExportConfiguratorBase;
19
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
20
import eu.etaxonomy.cdm.model.common.CdmBase;
21
import eu.etaxonomy.cdm.model.common.ExtensionType;
22
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
23

    
24
/**
25
 * @author a.mueller
26
 * @since 12.05.2009
27
 */
28
public class DbObjectMapper
29
        extends DbSingleAttributeExportMapperBase<DbExportStateBase<?, IExportTransformer>>{
30

    
31
	private static final Logger logger = LogManager.getLogger(DbObjectMapper.class);
32

    
33
	boolean isCache;
34

    
35
	public static DbObjectMapper NewInstance(String cdmAttributeString, String dbAttributeString){
36
		return new DbObjectMapper(cdmAttributeString, dbAttributeString, null, false, false);
37
	}
38

    
39
    public static DbObjectMapper NewNotNullInstance(String cdmAttributeString, String dbAttributeString){
40
        return new DbObjectMapper(cdmAttributeString, dbAttributeString, null, false, true);
41
    }
42

    
43
	public static DbObjectMapper NewInstance(String cdmAttributeString, String dbAttributeString, boolean isCache){
44
		return new DbObjectMapper(cdmAttributeString, dbAttributeString, null, isCache, false);
45
	}
46

    
47
	protected DbObjectMapper(String cdmAttributeString, String dbAttributeString, Object defaultValue, boolean isCache, boolean notNull) {
48
		super(cdmAttributeString, dbAttributeString, defaultValue);
49
		this.isCache = isCache;
50
		this.notNull = notNull;
51
	}
52

    
53
	@Override
54
	protected Object getValue(CdmBase cdmBase) {
55
		CdmBase value = (CdmBase)super.getValue(cdmBase);
56
		Object result;
57
		if (value == null){
58
			return null;
59
		}
60
		if (! Hibernate.isInitialized(value)){
61
			Hibernate.initialize(value);
62
		}
63
		if (isCache){
64
			if (value.isInstanceOf(IdentifiableEntity.class)){
65
				IdentifiableEntity<?> identEntity = CdmBase.deproxy(value, IdentifiableEntity.class);
66
				String titleCache = identEntity.getTitleCache();
67
				if (titleCache == null || titleCache.length()>250 || titleCache.endsWith("...")){
68
				    Set<String> fullCache = identEntity.getExtensions(ExtensionType.uuidExtNonTruncatedCache);
69
				    if (!fullCache.isEmpty()){
70
				        titleCache = fullCache.iterator().next();
71
				        if (fullCache.size()>1){
72
				            logger.warn("Entity has more than 1 'Non truncated cache' extensions. This should not happen. Arbitrary one taken.");
73
				        }
74
				    }
75
				}
76
				result = titleCache;
77
			}else{
78
				result = value.toString();
79
			}
80
		}else{
81
			result = getId(value);
82
		}
83

    
84
		return result;
85
	}
86

    
87
	protected Integer getId(CdmBase cdmBase){
88
		DbExportStateBase<?, IExportTransformer> state = getState();
89
		DbExportConfiguratorBase<?,?,?> config = state.getConfig();
90
		if (false && config.getIdType() == DbExportConfiguratorBase.IdType.CDM_ID){
91
			return cdmBase.getId();
92
		}else{
93
			Integer id = getState().getDbId(cdmBase);
94
			return id;
95
		}
96
	}
97

    
98
	@Override
99
	protected int getSqlType() {
100
		if (isCache){
101
			return Types.VARCHAR;
102
		}else{
103
			return Types.INTEGER;
104
		}
105
	}
106

    
107
	@Override
108
	public Class<?> getTypeClass() {
109
		if (isCache){
110
			return String.class;
111
		}else{
112
			return CdmBase.class;
113
		}
114
	}
115
}
(26-26/44)