Project

General

Profile

Download (10.1 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2019 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.model.name;
10

    
11
import java.util.HashMap;
12
import java.util.Map;
13

    
14
import javax.persistence.Entity;
15
import javax.persistence.FetchType;
16
import javax.persistence.MapKeyJoinColumn;
17
import javax.persistence.OneToMany;
18
import javax.persistence.Transient;
19
import javax.validation.constraints.NotNull;
20
import javax.xml.bind.annotation.XmlAccessType;
21
import javax.xml.bind.annotation.XmlAccessorType;
22
import javax.xml.bind.annotation.XmlElement;
23
import javax.xml.bind.annotation.XmlRootElement;
24
import javax.xml.bind.annotation.XmlTransient;
25
import javax.xml.bind.annotation.XmlType;
26
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
27

    
28
import org.hibernate.annotations.Cascade;
29
import org.hibernate.annotations.CascadeType;
30
import org.hibernate.envers.Audited;
31
import org.hibernate.search.annotations.Field;
32
import org.hibernate.search.annotations.FieldBridge;
33
import org.hibernate.search.annotations.Store;
34

    
35
import eu.etaxonomy.cdm.hibernate.search.MultilanguageTextFieldBridge;
36
import eu.etaxonomy.cdm.jaxb.MultilanguageTextAdapter;
37
import eu.etaxonomy.cdm.model.common.Language;
38
import eu.etaxonomy.cdm.model.common.LanguageString;
39
import eu.etaxonomy.cdm.model.reference.Reference;
40

    
41
/**
42
 * The class representing a typification of one or several {@link TaxonName taxon names} by text only.<BR>
43
 * This is for<BR>
44
 *   1. verbatim type citations<BR>
45
 *   2. rapid data entry<BR>
46
 *   3. type information that can not be atomized with current data model<BR>
47
 *
48
 * @author a.mueller
49
 * @since 23.01.2019
50
 */
51
@XmlRootElement(name = "TextualTypeDesignation")
52
@XmlAccessorType(XmlAccessType.FIELD)
53
@XmlType(name = "TextualTypeDesignation", propOrder = {
54
      "text",
55
      "isVerbatim"
56
})
57
@Entity
58
@Audited
59
public class TextualTypeDesignation extends TypeDesignationBase<SpecimenTypeDesignationStatus> {
60

    
61
    private static final long serialVersionUID = 7610574857727305296L;
62

    
63
    @XmlElement(name = "Text")
64
    @XmlJavaTypeAdapter(MultilanguageTextAdapter.class)
65
    @OneToMany (fetch= FetchType.LAZY, orphanRemoval=true)
66
    @MapKeyJoinColumn(name="text_mapkey_id")
67
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE, CascadeType.DELETE})
68
    @Field(name="text", store=Store.YES)
69
    @FieldBridge(impl=MultilanguageTextFieldBridge.class)
70
    @NotNull
71
    private Map<Language, LanguageString> text = new HashMap<>();
72

    
73
    private boolean isVerbatim;
74

    
75
    @XmlTransient
76
    @Transient
77
    private boolean isHashMapHibernateBugFixed = false;
78

    
79
//********************** FACTORY *********************************/
80

    
81
    public static TextualTypeDesignation NewInstance() {
82
        TextualTypeDesignation result = new TextualTypeDesignation();
83
        return result;
84
    }
85

    
86
    public static TextualTypeDesignation NewInstance(String text, Language language, boolean isVerbatim,
87
            Reference citation, String citationMicroReference, String originalNameString) {
88
        TextualTypeDesignation result = new TextualTypeDesignation(text, language, isVerbatim,
89
                citation, citationMicroReference, originalNameString);
90
        return result;
91
    }
92

    
93
//********************** CONSTRUCTOR *********************************/
94

    
95
    protected TextualTypeDesignation() {
96
        super();
97
    }
98

    
99
    protected TextualTypeDesignation(String text, Language language, boolean isVerbatim, Reference citation, String citationMicroReference, String originalNameString) {
100
        super(citation, citationMicroReference, originalNameString);
101
        language = Language.UNDETERMINED();
102
        LanguageString ls = LanguageString.NewInstance(text, language);
103
        this.putText(ls);
104
        this.setVerbatim(isVerbatim);
105
    }
106

    
107
//********************** GETTER /SETTER *********************************/
108

    
109
    public Map<Language, LanguageString> getText() {
110
        return text;
111
    }
112

    
113
    /**
114
     * Creates a {@link LanguageString language string} based on the given text string
115
     * and the given {@link Language language}, returns it and adds it to the multilanguage
116
     * text representing the content of <i>this</i> text data.
117
     *
118
     * @param language  the language in which the text string is formulated
119
     * @param text      the string representing the content of the text data
120
     *                  in a particular language
121
     *
122
     * @see             #getMultilanguageText()
123
     * @see             #putText(LanguageString)
124
     * @return          the previous language string associated with the given Language, or null if there was no mapping for the given Language
125
     */
126
    public LanguageString putText(Language language, String text) {
127
        fixHashMapHibernateBug();
128
        //** end workaround
129
        LanguageString languageString = this.text.get(language);
130
        if (languageString != null){
131
            languageString.setText(text);
132
        }else{
133
            languageString = LanguageString.NewInstance(text, language);
134
        }
135
        this.text.put(language , languageString);
136
        return languageString;
137
    }
138

    
139

    
140
    /**
141
     * Adds a translated {@link LanguageString text in a particular language}
142
     * to the multi-language text representing the content of <i>this</i> text data.
143
     * The given language string will be returned.
144
     *
145
     * @param languageString    the language string representing the content of
146
     *                          the text data in a particular language
147
     * @see                     #getText()
148
     * @see                     #putText(String, Language)
149
     * @see                     HashMap#put(Object, Object)
150
     * @return                  the previous language string associated with key, or null if there was no mapping for key
151
     */
152
    public LanguageString putText(LanguageString languageString) {
153
        if (languageString == null){
154
            return null;
155
        }else{
156
            Language language = languageString.getLanguage();
157
            return this.text.put(language, languageString);
158
        }
159
    }
160
    /**
161
     * Removes from the multilanguage representing the content of
162
     * <i>this</i> text data the one {@link LanguageString language string}
163
     * with the given {@link Language language}. Returns the removed
164
     * language string.
165
     *
166
     * @param  language the language in which the language string to be removed
167
     *                  has been formulated
168
     * @return          the language string associated with the given language or null if there was no mapping for the given Language
169
     * @see             #getMultilanguageText()
170
     */
171
    public LanguageString removeText(Language language) {
172
        fixHashMapHibernateBug();
173
        return this.text.remove(language);
174
    }
175
    /**
176
     * Returns the multi-language text with the content of <i>this</i> text data for
177
     * a specific language.
178
     *
179
     * @param language the language in which the text string looked for is formulated
180
     * @return
181
     */
182
    public LanguageString getLanguageText(Language language){
183
        return getText().get(language);
184
    }
185
    public String getText(Language language){
186
        if (getText().get(language) != null){
187
            return getText().get(language).getText();
188
        }else {
189
            return null;
190
        }
191
    }
192

    
193
    /**
194
     * Flag indicating if this textual type designation is a citation (e.g. original citation).
195
     * This may have influence on the correct formatting of type designations.
196
     */
197
    public boolean isVerbatim() {
198
        return isVerbatim;
199
    }
200

    
201
    public void setVerbatim(boolean isVerbatim) {
202
        this.isVerbatim = isVerbatim;
203
    }
204

    
205
    //copy from TextData
206
    private void fixHashMapHibernateBug() {
207
        //workaround for key problem
208
        if(! isHashMapHibernateBugFixed){
209
            HashMap<Language, LanguageString> tmp = new HashMap<>();
210
            tmp.putAll(text);
211
            text.clear();
212
            text.putAll(tmp);
213

    
214
            isHashMapHibernateBugFixed = true;
215
        }
216
    }
217

    
218
    //copied and adapted from TermBase
219
    public String getPreferredText(Language language) {
220
        String repr = getText(language);
221
        if(repr == null){
222
            repr = getText(Language.DEFAULT());
223
        }
224
        if(repr == null){
225
            repr = getText().isEmpty() ? null : getText().values().iterator().next().getText();
226
        }
227
        return repr;
228
    }
229

    
230
    /**
231
     * {@inheritDoc}
232
     *
233
     * @deprecated usually a {@link TextualTypeDesignation} may have multiple types,
234
     * therefore the type is not defined and can not be classified to have a designation
235
     * source or not
236
     */
237
    @Deprecated
238
    @Override
239
    public boolean hasDesignationSource() {
240
        return false;
241
    }
242

    
243
    /**
244
     * @deprecated not relevant for {@link TextualTypeDesignation} throws Exception
245
     */
246
    @Deprecated
247
    @Override
248
    public void setTypeStatus(SpecimenTypeDesignationStatus typeStatus) {
249
        throw new RuntimeException("Method should not be called in textbased type designaiton");
250
    }
251

    
252
    /**
253
     * @deprecated not relevant for {@link TextualTypeDesignation} throws Exception
254
     */
255
    @Deprecated
256
    @Override
257
    public void setNotDesignated(boolean notDesignated) {
258
        throw new RuntimeException("Method should not be called in textbased type designaiton");
259
    }
260

    
261
    /**
262
     * @deprecated not relevant for {@link TextualTypeDesignation} throws Exception
263
     */
264
    @Deprecated
265
    @Override
266
    public void setCitationMicroReference(String citationMicroReference) {
267
        throw new RuntimeException("Method should not be called in textbased type designaiton");
268
    }
269

    
270
    /**
271
     * @deprecated not relevant for {@link TextualTypeDesignation} throws Exception
272
     */
273
    @Deprecated
274
    @Override
275
    public void setCitation(Reference citation) {
276
        throw new RuntimeException("Method should not be called in textbased type designaiton");
277
    }
278

    
279
    /**
280
     * {@inheritDoc}
281
     *
282
     * @deprecated a textual type designation has no specific type
283
     * therefore the type can not be removed.
284
     */
285
    @Override
286
    @Deprecated
287
    public void removeType() {
288
        //nothing to do
289
    }
290
}
(35-35/39)