Project

General

Profile

Download (5.9 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 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

    
10
package eu.etaxonomy.cdm.model.common;
11

    
12
import javax.persistence.MappedSuperclass;
13
import javax.xml.bind.annotation.XmlAccessType;
14
import javax.xml.bind.annotation.XmlAccessorType;
15
import javax.xml.bind.annotation.XmlAttribute;
16
import javax.xml.bind.annotation.XmlEnum;
17
import javax.xml.bind.annotation.XmlEnumValue;
18
import javax.xml.bind.annotation.XmlRootElement;
19
import javax.xml.bind.annotation.XmlType;
20

    
21
import org.apache.log4j.Logger;
22
import org.hibernate.envers.Audited;
23

    
24
import eu.etaxonomy.cdm.model.reference.Reference;
25

    
26
/**
27
 * Concrete implementations of this abstract base class express a directed relationship between two
28
 * cdm entities ( {@link IRelated} ). The most important properties of the relationship are:
29
 * <ul>
30
 * <li>{@link #getRelatedFrom(IRelated)}</li>
31
 * <li>{@link #getRelatedTo(IRelated)}</li>
32
 * <li>The {@code <TYPE>}, a RelationshipTermBase which specifies the kind of the relationship</li>
33
 * </ul>
34
 * A relationship thus is forming a directed graph consisting of two nodes and an edge:
35
 * <pre>
36
     relatedFrom -----[TYPE]----> relatedTo
37
   </pre>
38
 * Whereas the direction of the relation can be valid for the direct (everted) and also for the inverted {@link Direction} direction.
39
 * This directional validity is defined by {@link RelationshipTermBase#isSymmetric()}
40
 *
41
 *
42
 *
43
 * @author m.doering
44
 * @author a.kohlbecker (Documentation)
45
 */
46
@XmlAccessorType(XmlAccessType.FIELD)
47
@XmlType(name = "RelationshipBase")
48
@XmlRootElement(name = "RelationshipBase")
49
@MappedSuperclass
50
@Audited
51
public abstract class RelationshipBase<FROM extends IRelated, TO extends IRelated, TYPE extends RelationshipTermBase> extends ReferencedEntityBase implements Cloneable {
52
    private static final long serialVersionUID = -5030154633820061997L;
53
    @SuppressWarnings("unused")
54
    private static final Logger logger = Logger.getLogger(RelationshipBase.class);
55

    
56
    @XmlAttribute(name = "isDoubtful")
57
    private boolean doubtful;
58

    
59
    /*this set is used only for persistence (CdmDeleteListener) to delete safely relationships and update their former related objects
60
    @XmlTransient
61
    @Transient
62
   protected Set<IRelated> deletedObjects = new HashSet<IRelated>();
63
*/
64

    
65
    /**
66
     * Enumeration and String representation of the <code>relatedFrom</code> (invers) and
67
     * <code>relatedTo</code> (direct, everted) bean properties. Intended to be used in the
68
     * persistence layer only.( But also used in the service layer now - a.kohlbecker )
69
     *
70
     * See also {@link RelationshipBase} for an explanation on relationships in general.
71
     */
72
    @XmlEnum
73
    public enum Direction {
74
        @XmlEnumValue("relatedFrom")
75
        relatedFrom,
76
        @XmlEnumValue("relatedTo")
77
        relatedTo
78
    }
79

    
80
    protected RelationshipBase(){
81
        super();
82
    }
83

    
84
    /**
85
     * Creates a relationship between 2 objects and adds it to the respective
86
     * relation sets of both objects.
87
     *
88
     * @param from
89
     * @param to
90
     * @param type
91
     * @param citation
92
     * @param citationMicroReference
93
     */
94
    protected RelationshipBase(FROM from, TO to, TYPE type, Reference citation, String citationMicroReference) {
95
        super(citation, citationMicroReference, null);
96
        setRelatedFrom(from);
97
        setRelatedTo(to);
98
        setType(type);
99
        from.addRelationship(this);
100
        to.addRelationship(this);
101
    }
102

    
103
    public abstract TYPE getType();
104

    
105
    public abstract void setType(TYPE type);
106

    
107
    protected abstract FROM getRelatedFrom();
108

    
109
    protected abstract void setRelatedFrom(FROM relatedFrom);
110

    
111
    protected abstract TO getRelatedTo();
112

    
113
    protected abstract void setRelatedTo(TO relatedTo);
114

    
115
    /**
116
     * A boolean flag that marks the relationship between two objects as doubtful
117
     * Please be aware that this flag should not be used to mark any status of the
118
     * objects themselves. E.g. when marking a synonym relationship as doubtful
119
     * this means that it is doubtful that the synonym is really a synonym to the
120
     * taxon. It does not mean that the synonym is doubtfully a synonym.
121
     * @return true, if the relationship is doubtful, false otherwise
122
     */
123
    public boolean isDoubtful(){
124
        return this.doubtful;
125
    }
126

    
127
    public void setDoubtful(boolean doubtful){
128
        this.doubtful = doubtful;
129
    }
130

    
131
    public boolean isRemoved(){
132
        if ( this.getRelatedFrom() == null ^ this.getRelatedTo() == null){
133
            throw new IllegalStateException("A relationship may have only both related object as null or none. But just one is null!");
134
        }
135
        return this.getRelatedFrom() == null || this.getRelatedTo() == null;
136
    }
137

    
138
 /*   public Set getDeletedObjects(){
139
        return this.deletedObjects;
140
    }
141
*/
142
// TODO
143
//	UUID toUuid;
144
//	UUID fromUuid;
145
//
146
//	@Transient
147
//	public UUID getToUuidCache(){
148
//		return relationTo.getUuid();
149
//	}
150
//	protected void setToUuid(UUID uuid){
151
//		toUuid = uuid;
152
//	}
153
//
154
//	public UUID getFromUuid(){
155
//		return relationTo.getUuid();
156
//	}
157
//	protected void setFromUuid(UUID uuid){
158
//		fromUuid = uuid;
159
//	}
160

    
161

    
162
//*********************** CLONE ********************************************************/
163

    
164
    /**
165
     * Clones <i>this</i> relationship. This is a shortcut that enables to create
166
     * a new instance that differs only slightly from <i>this</i> relationship by
167
     * modifying only some of the attributes.
168
     * @throws CloneNotSupportedException
169
     *
170
     * @see eu.etaxonomy.cdm.model.common.RelationshipBase#clone()
171
     * @see java.lang.Object#clone()
172
     */
173
    @Override
174
    public Object clone() throws CloneNotSupportedException {
175
        RelationshipBase<FROM,TO,TYPE> result = (RelationshipBase<FROM,TO,TYPE>)super.clone();
176
        //no changes to: doubtful, deletedObjects
177
        return result;
178
    }
179
}
(59-59/72)