Project

General

Profile

Revision ed77d49a

IDed77d49ad31afcc8178c768238be487e4acdb9d1
Parent a1525050
Child c6a0721d

Added by Andreas Müller over 3 years ago

Add SourceableEntity #5534

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/SourceableEntity.java
1
// $Id$
2
/**
3
* Copyright (C) 2016 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.model.common;
11

  
12
import java.util.HashSet;
13
import java.util.Set;
14

  
15
import javax.persistence.FetchType;
16
import javax.persistence.MappedSuperclass;
17
import javax.persistence.OneToMany;
18
import javax.xml.bind.annotation.XmlAccessType;
19
import javax.xml.bind.annotation.XmlAccessorType;
20
import javax.xml.bind.annotation.XmlElement;
21
import javax.xml.bind.annotation.XmlElementWrapper;
22
import javax.xml.bind.annotation.XmlType;
23

  
24
import org.hibernate.annotations.Cascade;
25
import org.hibernate.annotations.CascadeType;
26
import org.hibernate.envers.Audited;
27

  
28
import eu.etaxonomy.cdm.model.reference.Reference;
29
import eu.etaxonomy.cdm.strategy.merge.Merge;
30
import eu.etaxonomy.cdm.strategy.merge.MergeMode;
31

  
32
/**
33
 * @author a.mueller
34
 * @date 26.04.2016
35
 *
36
 */
37
@XmlAccessorType(XmlAccessType.FIELD)
38
@XmlType(name = "SourceableEntity", propOrder = {
39
    "sources"
40
})
41
@Audited
42
@MappedSuperclass
43
public abstract class SourceableEntity<TYPE extends OriginalSourceBase>
44
        extends AnnotatableEntity
45
        implements ISourceable<TYPE>{
46

  
47
    private static final long serialVersionUID = 5690608760116052681L;
48

  
49
    @XmlElementWrapper(name = "Sources")
50
    @XmlElement(name = "DescriptionElementSource")
51
    @OneToMany(fetch = FetchType.LAZY, orphanRemoval=true)
52
    @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
53
    @Merge(MergeMode.ADD_CLONE)
54
    protected Set<TYPE> sources = new HashSet<TYPE>();
55

  
56
    // *********************** METHODS ****************************************/
57

  
58
    @Override
59
    public Set<TYPE> getSources() {
60
        return this.sources;
61
    }
62

  
63
    @Override
64
    public void addSource(TYPE source) {
65
        if (source != null){
66
            ISourceable<TYPE> oldSourcedObj = source.getSourcedObj();
67
            if (oldSourcedObj != null && oldSourcedObj != this){
68
                oldSourcedObj.getSources().remove(source);
69
            }
70
            this.sources.add(source);
71
            source.setSourcedObj(this);
72
        }
73
    }
74

  
75
    @Override
76
    public TYPE addSource(OriginalSourceType type, String id, String idNamespace, Reference citation, String microCitation) {
77
        if (id == null && idNamespace == null && citation == null && microCitation == null){
78
            return null;
79
        }
80
        TYPE source = sourceInstance(type, id, idNamespace, citation, microCitation);
81
//        TYPE source = TYPE.NewInstance(type, id, idNamespace, citation, microCitation);
82
        addSource(source);
83
        return source;
84
    }
85

  
86
    @Override
87
    public void addSources(Set<TYPE> sources){
88
        for (TYPE source:sources){
89
            addSource(source);
90
        }
91
    }
92

  
93

  
94
    @Override
95
    public TYPE addImportSource(String id, String idNamespace, Reference<?> citation, String microCitation) {
96
        if (id == null && idNamespace == null && citation == null && microCitation == null){
97
            return null;
98
        }
99
        TYPE source = sourceInstance(OriginalSourceType.Import, id, idNamespace, citation, microCitation);
100
//        TYPE source = DescriptionElementSource.NewInstance(OriginalSourceType.Import, id, idNamespace, citation, microCitation);
101
        addSource(source);
102
        return source;
103
    }
104

  
105

  
106

  
107
    @Override
108
    public void removeSource(TYPE source) {
109
        this.sources.remove(source);
110
    }
111

  
112
    protected abstract TYPE sourceInstance(OriginalSourceType type, String id, String idNamespace, Reference<?> citation, String microCitation);
113

  
114
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)