Project

General

Profile

Download (4.17 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2021 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.persistence.dto;
10

    
11
import java.util.UUID;
12

    
13
import eu.etaxonomy.cdm.common.CdmUtils;
14
import eu.etaxonomy.cdm.model.common.CdmBase;
15

    
16
/**
17
 * @author a.mueller
18
 * @since 31.03.2021
19
 */
20
public class ReferencingObjectDto extends UuidAndTitleCache<CdmBase> {
21

    
22
    private static final long serialVersionUID = -6990153096653819574L;
23

    
24
    private CdmBase referencedEntity;
25

    
26
    private UuidAndTitleCache<CdmBase> openInTarget;
27

    
28
//*************************** CONSTRUCTOR **************************/
29

    
30
    public ReferencingObjectDto(){
31
        super(null, null);
32
    }
33

    
34
    public ReferencingObjectDto(UUID uuid, Integer id) {
35
        super(uuid, id, null);
36
    }
37

    
38
    @SuppressWarnings({ "unchecked", "rawtypes" })
39
    public ReferencingObjectDto(Class type, UUID uuid, Integer id) {
40
        super(type, uuid, id, null);
41
    }
42

    
43
    @SuppressWarnings({ "rawtypes", "unchecked" })
44
    public ReferencingObjectDto(String typeStr, UUID uuid, Integer id) throws ClassNotFoundException {
45
        super((Class)Class.forName(typeStr), uuid, id, null);
46
    }
47

    
48
    public ReferencingObjectDto(CdmBase referencedEntity) {
49
        super(referencedEntity.getClass(), referencedEntity.getUuid(), referencedEntity.getId(), null);
50
        this.referencedEntity = referencedEntity;
51
    }
52

    
53
//************************ GETTER / SETTER *****************************/
54

    
55
    public CdmBase getReferencedEntity() {
56
        return referencedEntity;
57
    }
58
    public void setReferencedEntity(CdmBase referencedEntity) {
59
        this.referencedEntity = referencedEntity;
60
    }
61

    
62
    public UuidAndTitleCache<CdmBase> getOpenInTarget() {
63
        return openInTarget;
64
    }
65
    public void setOpenInTarget(UuidAndTitleCache<CdmBase> openInTarget) {
66
        this.openInTarget = openInTarget;
67
    }
68

    
69
//**************** METHODS ***********************************/
70

    
71
    /**
72
     * Returns the best matching "open in" type.
73
     */
74
    public Class<? extends CdmBase> getBestOpenInTargetType(){
75
        Class<? extends CdmBase> result = null;
76
        if (this.openInTarget != null){
77
            result = this.openInTarget.getType();
78
        }
79
        if (result == null){
80
            result = this.getType();
81
        }
82
        if (result == null && this.getReferencedEntity() != null){
83
            result = this.getReferencedEntity().getClass();
84
        }
85
        return result;
86
    }
87

    
88
// *********************** EQUALS *************************************/
89

    
90
    //TODO move partly up to UuidAndTitleCache
91

    
92
    @Override
93
    public int hashCode() {
94
        final int prime = 31;
95
        int result = 1;
96
        result = prime * result + ((getUuid() == null) ? 0 : getUuid().hashCode());
97
        result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
98
        result = prime * result + ((getType() == null) ? 0 : getType().hashCode());
99
        result = prime * result + ((getReferencedEntity() == null) ? 0 : getReferencedEntity().hashCode());
100
        return result;
101
    }
102

    
103
    @Override
104
    public boolean equals(Object obj) {
105
        if (this == obj) {
106
            return true;
107
        }else if (obj == null || ! (obj instanceof ReferencingObjectDto)) {
108
            return false;
109
        }
110
        ReferencingObjectDto other = (ReferencingObjectDto) obj;
111

    
112
        if (!CdmUtils.nullSafeEqual(this.getType(), other.getType())){
113
            return false;
114
        }
115
        if (!CdmUtils.nullSafeEqual(this.getId(), other.getId())){
116
            return false;
117
        }
118
        if (!CdmUtils.nullSafeEqual(this.getUuid(), other.getUuid())){
119
            return false;
120
        }
121
        //TODO allow only 1 side has entity
122
        if (!CdmUtils.nullSafeEqual(this.getReferencedEntity(), other.getReferencedEntity())){
123
            return false;
124
        }
125

    
126
        return true;
127
    }
128
// ********************** STRING ****************************************/
129

    
130
    @Override
131
    public String toString() {
132
        return "RefObjDto[type=" + (getType()!=null? getType().getSimpleName():"-") + ":" + getId() + "]";
133
    }
134
}
(11-11/29)