Project

General

Profile

Download (1.99 KB) Statistics
| Branch: | Tag: | Revision:
1 5cf8568b Andreas Kohlbecker
/**
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 a84bf85f Andreas Kohlbecker
 * 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 5cf8568b Andreas Kohlbecker
 * @author a.kohlbecker
23 7fa325bc Andreas Müller
 * @since Jun 25, 2013
24 5cf8568b Andreas Kohlbecker
 *
25 601a2066 Andreas Kohlbecker
 * @deprecated better avoid using this PropertyEditor since it will cause Hibernate to load the
26 dc3fd428 Andreas Kohlbecker
 *             term the data base. Use plain uuids instead for better performance where possible!
27 601a2066 Andreas Kohlbecker
 *
28 5cf8568b Andreas Kohlbecker
 */
29 601a2066 Andreas Kohlbecker
@Deprecated
30 5cf8568b Andreas Kohlbecker
public class TermBasePropertyEditor<T extends DefinedTermBase<?>> extends PropertyEditorSupport {
31
32 a84bf85f Andreas Kohlbecker
    protected final ITermService termService;
33 5cf8568b Andreas Kohlbecker
34
    public TermBasePropertyEditor(ITermService termService){
35
        super();
36
        this.termService = termService;
37
    }
38
39
    @Override
40
    public void setAsText(String text) {
41 a84bf85f Andreas Kohlbecker
        setValue(textToTerm(text));
42
    }
43 5cf8568b Andreas Kohlbecker
44 a84bf85f Andreas Kohlbecker
    /**
45
     * @param text
46
     * @return
47
     */
48
    @SuppressWarnings("unchecked")
49
    protected T textToTerm(String text) {
50 5cf8568b Andreas Kohlbecker
        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 a84bf85f Andreas Kohlbecker
        return term;
69 5cf8568b Andreas Kohlbecker
    }
70
}