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.Map;
|
15
|
import java.util.UUID;
|
16
|
|
17
|
import org.apache.commons.lang.StringUtils;
|
18
|
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
|
19
|
import org.springframework.transaction.TransactionStatus;
|
20
|
|
21
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
22
|
import eu.etaxonomy.cdm.api.service.IVocabularyService;
|
23
|
import eu.etaxonomy.cdm.database.update.DatabaseTypeNotSupportedException;
|
24
|
import eu.etaxonomy.cdm.io.common.CdmImportBase;
|
25
|
import eu.etaxonomy.cdm.io.common.DbImportStateBase;
|
26
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
27
|
import eu.etaxonomy.cdm.model.common.Extension;
|
28
|
import eu.etaxonomy.cdm.model.common.ExtensionType;
|
29
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
30
|
import eu.etaxonomy.cdm.model.term.TermVocabulary;
|
31
|
|
32
|
/**
|
33
|
* This class maps a database attribute to CDM extension added to the target class
|
34
|
* TODO maybe this class should not inherit from DbSingleAttributeImportMapperBase
|
35
|
* as it does not map to a single attribute
|
36
|
* @author a.mueller
|
37
|
* @since 12.05.2009
|
38
|
*/
|
39
|
public class DbImportExtensionMapper extends DbSingleAttributeImportMapperBase<DbImportStateBase<?,?>, IdentifiableEntity>
|
40
|
implements IDbImportMapper<DbImportStateBase<?,?>,IdentifiableEntity>{
|
41
|
|
42
|
private static final Logger logger = LogManager.getLogger(DbImportExtensionMapper.class);
|
43
|
|
44
|
//************************** FACTORY METHODS ***************************************************************/
|
45
|
|
46
|
/**
|
47
|
* @param dbAttributeString
|
48
|
* @param uuid
|
49
|
* @param label
|
50
|
* @param text
|
51
|
* @param labelAbbrev
|
52
|
* @return
|
53
|
*/
|
54
|
@Deprecated
|
55
|
public static DbImportExtensionMapper NewInstance(String dbAttributeString, UUID uuid, String label, String text, String labelAbbrev){
|
56
|
return new DbImportExtensionMapper(dbAttributeString, uuid, label, text, labelAbbrev);
|
57
|
}
|
58
|
|
59
|
public static DbImportExtensionMapper NewInstance(String dbAttributeString, ExtensionType extensionType){
|
60
|
return new DbImportExtensionMapper(dbAttributeString, extensionType);
|
61
|
}
|
62
|
|
63
|
//***************** VARIABLES **********************************************************/
|
64
|
|
65
|
private ExtensionType extensionType;
|
66
|
private String label;
|
67
|
private String text;
|
68
|
private String labelAbbrev;
|
69
|
private UUID uuid;
|
70
|
|
71
|
//******************************** CONSTRUCTOR *****************************************************************/
|
72
|
/**
|
73
|
* @param dbAttributeString
|
74
|
* @param uuid
|
75
|
* @param label
|
76
|
* @param text
|
77
|
* @param labelAbbrev
|
78
|
*/
|
79
|
private DbImportExtensionMapper(String dbAttributeString, UUID uuid, String label, String text, String labelAbbrev) {
|
80
|
super(dbAttributeString, dbAttributeString);
|
81
|
this.uuid = uuid;
|
82
|
this.label = label;
|
83
|
this.text = text;
|
84
|
this.labelAbbrev = labelAbbrev;
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* @param dbAttributeString
|
89
|
* @param extensionType
|
90
|
*/
|
91
|
private DbImportExtensionMapper(String dbAttributeString, ExtensionType extensionType) {
|
92
|
super(dbAttributeString, dbAttributeString);
|
93
|
this.extensionType = extensionType;
|
94
|
}
|
95
|
|
96
|
//****************************** METHODS ***************************************************/
|
97
|
|
98
|
@Override
|
99
|
public void initialize(DbImportStateBase<?,?> state, Class<? extends CdmBase> destinationClass) {
|
100
|
importMapperHelper.initialize(state, destinationClass);
|
101
|
CdmImportBase<?, ?> currentImport = state.getCurrentIO();
|
102
|
if (currentImport == null){
|
103
|
throw new IllegalStateException("Current import is not available. Please make sure the the state knows about the current import (state.setCurrentImport())) !");
|
104
|
}
|
105
|
|
106
|
try {
|
107
|
if ( checkDbColumnExists()){
|
108
|
if (this.extensionType == null){
|
109
|
this.extensionType = getExtensionType(currentImport, uuid, label, text, labelAbbrev);
|
110
|
}
|
111
|
}else{
|
112
|
ignore = true;
|
113
|
}
|
114
|
} catch (DatabaseTypeNotSupportedException e) {
|
115
|
//do nothing - checkDbColumnExists is not possible
|
116
|
}
|
117
|
}
|
118
|
|
119
|
public boolean invoke(Map<String, Object> valueMap, CdmBase cdmBase){
|
120
|
Object dbValueObject = valueMap.get(this.getSourceAttribute().toLowerCase());
|
121
|
String dbValue = dbValueObject == null? null: dbValueObject.toString();
|
122
|
return invoke(dbValue, cdmBase);
|
123
|
}
|
124
|
|
125
|
private boolean invoke(String dbValue, CdmBase cdmBase){
|
126
|
if (ignore){
|
127
|
return true;
|
128
|
}
|
129
|
if (cdmBase instanceof IdentifiableEntity){
|
130
|
IdentifiableEntity identifiableEntity = (IdentifiableEntity) cdmBase;
|
131
|
invoke(dbValue, identifiableEntity);
|
132
|
return true;
|
133
|
}else{
|
134
|
throw new IllegalArgumentException("extended object must be of type identifiable entity.");
|
135
|
}
|
136
|
|
137
|
}
|
138
|
|
139
|
@Override
|
140
|
public IdentifiableEntity invoke(ResultSet rs, IdentifiableEntity identifiableEntity) throws SQLException {
|
141
|
String dbValue = rs.getString(getSourceAttribute());
|
142
|
return invoke(dbValue, identifiableEntity);
|
143
|
}
|
144
|
|
145
|
private IdentifiableEntity invoke(String dbValue, IdentifiableEntity identifiableEntity){
|
146
|
if (ignore){
|
147
|
return identifiableEntity;
|
148
|
}
|
149
|
if (StringUtils.isNotBlank(dbValue)){
|
150
|
Extension.NewInstance(identifiableEntity, dbValue.trim(), extensionType);
|
151
|
if (extensionType == null){
|
152
|
logger.warn("No extension type available for extension");
|
153
|
}
|
154
|
}
|
155
|
return identifiableEntity;
|
156
|
}
|
157
|
|
158
|
protected ExtensionType getExtensionType(CdmImportBase<?, ?> currentImport, UUID uuid, String label,
|
159
|
String text, String labelAbbrev){
|
160
|
|
161
|
ITermService termService = currentImport.getTermService();
|
162
|
ExtensionType extensionType = (ExtensionType)termService.find(uuid);
|
163
|
if (extensionType == null){
|
164
|
extensionType = ExtensionType.NewInstance(text, label, labelAbbrev);
|
165
|
extensionType.setUuid(uuid);
|
166
|
//set vocabulary //TODO allow user defined vocabularies
|
167
|
UUID uuidExtensionTypeVocabulary = UUID.fromString("117cc307-5bd4-4b10-9b2f-2e14051b3b20");
|
168
|
IVocabularyService vocService = currentImport.getVocabularyService();
|
169
|
TransactionStatus tx = currentImport.startTransaction();
|
170
|
TermVocabulary<ExtensionType> voc = vocService.find(uuidExtensionTypeVocabulary);
|
171
|
currentImport.getVocabularyService().saveOrUpdate(voc);
|
172
|
if (voc != null){
|
173
|
voc.addTerm(extensionType);
|
174
|
}else{
|
175
|
logger.warn("Could not find default extensionType vocabulary. Vocabulary not set for new extension type.");
|
176
|
}
|
177
|
//save
|
178
|
termService.save(extensionType);
|
179
|
currentImport.commitTransaction(tx);
|
180
|
}
|
181
|
return extensionType;
|
182
|
}
|
183
|
|
184
|
|
185
|
//not used
|
186
|
@Override
|
187
|
public Class<String> getTypeClass(){
|
188
|
return String.class;
|
189
|
}
|
190
|
|
191
|
}
|