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
|
import org.hibernate.Hibernate;
|
16
|
|
17
|
import eu.etaxonomy.cdm.io.common.DbExportConfiguratorBase;
|
18
|
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
|
19
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
20
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
21
|
|
22
|
/**
|
23
|
* @author a.mueller
|
24
|
* @created 12.05.2009
|
25
|
*/
|
26
|
public class DbObjectMapper extends DbSingleAttributeExportMapperBase<DbExportStateBase<?, IExportTransformer>> implements IDbExportMapper<DbExportStateBase<?, IExportTransformer>, IExportTransformer> {
|
27
|
@SuppressWarnings("unused")
|
28
|
private static final Logger logger = Logger.getLogger(DbObjectMapper.class);
|
29
|
|
30
|
boolean isCache;
|
31
|
|
32
|
public static DbObjectMapper NewInstance(String cdmAttributeString, String dbAttributeString){
|
33
|
return new DbObjectMapper(cdmAttributeString, dbAttributeString, null, false);
|
34
|
}
|
35
|
|
36
|
public static DbObjectMapper NewInstance(String cdmAttributeString, String dbAttributeString, boolean isCache){
|
37
|
return new DbObjectMapper(cdmAttributeString, dbAttributeString, null, isCache);
|
38
|
}
|
39
|
|
40
|
/**
|
41
|
* @param dbAttributeString
|
42
|
* @param cdmAttributeString
|
43
|
*/
|
44
|
protected DbObjectMapper(String cdmAttributeString, String dbAttributeString, Object defaultValue, boolean isCache) {
|
45
|
super(cdmAttributeString, dbAttributeString, defaultValue);
|
46
|
this.isCache = isCache;
|
47
|
}
|
48
|
|
49
|
@Override
|
50
|
protected Object getValue(CdmBase cdmBase) {
|
51
|
CdmBase value = (CdmBase)super.getValue(cdmBase);
|
52
|
Object result;
|
53
|
if (value == null){
|
54
|
return null;
|
55
|
}
|
56
|
if (! Hibernate.isInitialized(value)){
|
57
|
Hibernate.initialize(value);
|
58
|
}
|
59
|
if (isCache){
|
60
|
if (value.isInstanceOf(IdentifiableEntity.class)){
|
61
|
IdentifiableEntity<?> identEntity = CdmBase.deproxy(value, IdentifiableEntity.class);
|
62
|
result = identEntity.getTitleCache();
|
63
|
}else{
|
64
|
result = value.toString();
|
65
|
}
|
66
|
}else{
|
67
|
result = getId(value);
|
68
|
}
|
69
|
|
70
|
// getState().getConfig().getCdmAppController().commitTransaction(tx);
|
71
|
return result;
|
72
|
}
|
73
|
|
74
|
protected Integer getId(CdmBase cdmBase){
|
75
|
DbExportStateBase<?, IExportTransformer> state = getState();
|
76
|
DbExportConfiguratorBase config = state.getConfig();
|
77
|
if (false && config.getIdType() == DbExportConfiguratorBase.IdType.CDM_ID){
|
78
|
return cdmBase.getId();
|
79
|
}else{
|
80
|
Integer id = getState().getDbId(cdmBase);
|
81
|
return id;
|
82
|
}
|
83
|
}
|
84
|
|
85
|
@Override
|
86
|
protected int getSqlType() {
|
87
|
if (isCache){
|
88
|
return Types.VARCHAR;
|
89
|
}else{
|
90
|
return Types.INTEGER;
|
91
|
}
|
92
|
}
|
93
|
|
94
|
@Override
|
95
|
public Class<?> getTypeClass() {
|
96
|
if (isCache){
|
97
|
return String.class;
|
98
|
}else{
|
99
|
return CdmBase.class;
|
100
|
}
|
101
|
}
|
102
|
}
|