Project

General

Profile

Download (1.99 KB) Statistics
| Branch: | Tag: | Revision:
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.common.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
 * @date Jun 25, 2013
24
 *
25
 * @deprecated better avoid using this PropertyEditor since it will cause Hibernate to load the
26
 *             term the data base. Use plain uuids instead for better performance where possible!
27
 *
28
 */
29
@Deprecated
30
public class TermBasePropertyEditor<T extends DefinedTermBase<?>> extends PropertyEditorSupport {
31

    
32
    protected final ITermService termService;
33

    
34
    public TermBasePropertyEditor(ITermService termService){
35
        super();
36
        this.termService = termService;
37
    }
38

    
39
    @Override
40
    public void setAsText(String text) {
41
        setValue(textToTerm(text));
42
    }
43

    
44
    /**
45
     * @param text
46
     * @return
47
     */
48
    @SuppressWarnings("unchecked")
49
    protected T textToTerm(String text) {
50
        T term = null;
51
        // 1. try treating as UUID
52
        try {
53
            UUID uuid = UUID.fromString(text);
54
            term = (T) termService.load(uuid);
55
        } catch (Exception e1) {
56
         // 1. try treating as ID
57
            try {
58
                int id = Integer.parseInt(text);
59
                term = (T) termService.find(id);
60
            } catch (Exception e2) {
61
                /* IGNORE */
62
            }
63
        }
64

    
65
        if(term == null){
66
            throw new java.lang.IllegalArgumentException("No TermBase instance found for the supplied identifier " + text);
67
        }
68
        return term;
69
    }
70
}
(15-15/18)