1
|
/**
|
2
|
* Copyright (C) 2013 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
|
package eu.etaxonomy.cdm.remote.editor;
|
10
|
|
11
|
import java.beans.PropertyEditorSupport;
|
12
|
import java.util.UUID;
|
13
|
|
14
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
15
|
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
|
16
|
|
17
|
/**
|
18
|
* This PropertyEditor translates a single string identifiers into a
|
19
|
* {@link DefinedTermBase} instance.
|
20
|
* The instance can be identified either by UUID or by ID.
|
21
|
*
|
22
|
* @author a.kohlbecker
|
23
|
* @since Jun 25, 2013
|
24
|
*
|
25
|
* @deprecated better avoid using this PropertyEditor since it will cause Hibernate
|
26
|
* to load the term from the data base. Use plain uuids instead for better
|
27
|
* performance where possible!
|
28
|
*
|
29
|
*/
|
30
|
@Deprecated
|
31
|
public class TermBasePropertyEditor<T extends DefinedTermBase<?>> extends PropertyEditorSupport {
|
32
|
|
33
|
protected final ITermService termService;
|
34
|
|
35
|
public TermBasePropertyEditor(ITermService termService){
|
36
|
super();
|
37
|
this.termService = termService;
|
38
|
}
|
39
|
|
40
|
@Override
|
41
|
public void setAsText(String text) {
|
42
|
setValue(textToTerm(text));
|
43
|
}
|
44
|
|
45
|
/**
|
46
|
* @param text
|
47
|
* @return
|
48
|
*/
|
49
|
@SuppressWarnings("unchecked")
|
50
|
protected T textToTerm(String text) {
|
51
|
T term = null;
|
52
|
// 1. try treating as UUID
|
53
|
try {
|
54
|
UUID uuid = UUID.fromString(text);
|
55
|
term = (T) termService.load(uuid);
|
56
|
} catch (Exception e1) {
|
57
|
// 1. try treating as ID
|
58
|
try {
|
59
|
int id = Integer.parseInt(text);
|
60
|
term = (T) termService.find(id);
|
61
|
} catch (Exception e2) {
|
62
|
/* IGNORE */
|
63
|
}
|
64
|
}
|
65
|
|
66
|
if(term == null){
|
67
|
throw new java.lang.IllegalArgumentException("No TermBase instance found for the supplied identifier " + text);
|
68
|
}
|
69
|
return term;
|
70
|
}
|
71
|
}
|