| 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 java.util.HashSet; |
|---|
| 13 | import java.util.Set; |
|---|
| 14 | |
|---|
| 15 | import javax.persistence.MappedSuperclass; |
|---|
| 16 | import javax.persistence.Transient; |
|---|
| 17 | import javax.xml.bind.annotation.XmlAccessType; |
|---|
| 18 | import javax.xml.bind.annotation.XmlAccessorType; |
|---|
| 19 | import javax.xml.bind.annotation.XmlAttribute; |
|---|
| 20 | import javax.xml.bind.annotation.XmlEnum; |
|---|
| 21 | import javax.xml.bind.annotation.XmlEnumValue; |
|---|
| 22 | import javax.xml.bind.annotation.XmlRootElement; |
|---|
| 23 | import javax.xml.bind.annotation.XmlTransient; |
|---|
| 24 | import javax.xml.bind.annotation.XmlType; |
|---|
| 25 | |
|---|
| 26 | import org.apache.log4j.Logger; |
|---|
| 27 | |
|---|
| 28 | import eu.etaxonomy.cdm.model.reference.Reference; |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * @author m.doering |
|---|
| 32 | */ |
|---|
| 33 | @XmlAccessorType(XmlAccessType.FIELD) |
|---|
| 34 | @XmlType(name = "RelationshipBase") |
|---|
| 35 | @XmlRootElement(name = "RelationshipBase") |
|---|
| 36 | @MappedSuperclass |
|---|
| 37 | public abstract class RelationshipBase<FROM extends IRelated, TO extends IRelated, TYPE extends RelationshipTermBase> extends ReferencedEntityBase implements Cloneable { |
|---|
| 38 | private static final long serialVersionUID = -5030154633820061997L; |
|---|
| 39 | @SuppressWarnings("unused") |
|---|
| 40 | private static final Logger logger = Logger.getLogger(RelationshipBase.class); |
|---|
| 41 | |
|---|
| 42 | @XmlAttribute(name = "isDoubtful") |
|---|
| 43 | private boolean doubtful; |
|---|
| 44 | |
|---|
| 45 | //this set is used only for persistence (CdmDeleteListener) to delete savely relationships and update their former related objects |
|---|
| 46 | @XmlTransient |
|---|
| 47 | @Transient |
|---|
| 48 | protected Set<IRelated> deletedObjects = new HashSet<IRelated>(); |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | /** |
|---|
| 52 | * enumeration and String representation of the <code>relatedFrom</code> and |
|---|
| 53 | * <code>relatedTo</code> bean propertys. Intended to be used in the |
|---|
| 54 | * persistence layer only. |
|---|
| 55 | */ |
|---|
| 56 | @XmlEnum |
|---|
| 57 | public enum Direction { |
|---|
| 58 | @XmlEnumValue("relatedFrom") |
|---|
| 59 | relatedFrom, |
|---|
| 60 | @XmlEnumValue("relatedTo") |
|---|
| 61 | relatedTo |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | protected RelationshipBase(){ |
|---|
| 65 | super(); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | * Creates a relationship between 2 objects and adds it to the respective |
|---|
| 70 | * relation sets of both objects. |
|---|
| 71 | * |
|---|
| 72 | * @param from |
|---|
| 73 | * @param to |
|---|
| 74 | * @param type |
|---|
| 75 | * @param citation |
|---|
| 76 | * @param citationMicroReference |
|---|
| 77 | */ |
|---|
| 78 | protected RelationshipBase(FROM from, TO to, TYPE type, Reference citation, String citationMicroReference) { |
|---|
| 79 | super(citation, citationMicroReference, null); |
|---|
| 80 | setRelatedFrom(from); |
|---|
| 81 | setRelatedTo(to); |
|---|
| 82 | setType(type); |
|---|
| 83 | from.addRelationship(this); |
|---|
| 84 | to.addRelationship(this); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | public abstract TYPE getType(); |
|---|
| 88 | |
|---|
| 89 | public abstract void setType(TYPE type); |
|---|
| 90 | |
|---|
| 91 | protected abstract FROM getRelatedFrom(); |
|---|
| 92 | |
|---|
| 93 | protected abstract void setRelatedFrom(FROM relatedFrom); |
|---|
| 94 | |
|---|
| 95 | protected abstract TO getRelatedTo(); |
|---|
| 96 | |
|---|
| 97 | protected abstract void setRelatedTo(TO relatedTo); |
|---|
| 98 | |
|---|
| 99 | /** |
|---|
| 100 | * A boolean flag that marks the relationship between two objects as doubtful |
|---|
| 101 | * Please be aware that this flag should not be used to mark any status of the |
|---|
| 102 | * objects themselfs. E.g. when marking a synonym relationship as doubtful |
|---|
| 103 | * this means that it is doubtful that the synonym is really a synonym to the |
|---|
| 104 | * taxon. It does not mean that the synonym is doubtfully a synonym. |
|---|
| 105 | * @return true, if the relationship is doubtful, false otherwise |
|---|
| 106 | */ |
|---|
| 107 | public boolean isDoubtful(){ |
|---|
| 108 | return this.doubtful; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | public void setDoubtful(boolean doubtful){ |
|---|
| 112 | this.doubtful = doubtful; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | public boolean isRemoved(){ |
|---|
| 116 | if ( this.getRelatedFrom() == null ^ this.getRelatedTo() == null){ |
|---|
| 117 | throw new IllegalStateException("A relationship may have only both related object as null or none. But just one is null!"); |
|---|
| 118 | } |
|---|
| 119 | return this.getRelatedFrom() == null || this.getRelatedTo() == null; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | public Set getDeletedObjects(){ |
|---|
| 123 | return this.deletedObjects; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | // TODO |
|---|
| 127 | // UUID toUuid; |
|---|
| 128 | // UUID fromUuid; |
|---|
| 129 | // |
|---|
| 130 | // @Transient |
|---|
| 131 | // public UUID getToUuidCache(){ |
|---|
| 132 | // return relationTo.getUuid(); |
|---|
| 133 | // } |
|---|
| 134 | // protected void setToUuid(UUID uuid){ |
|---|
| 135 | // toUuid = uuid; |
|---|
| 136 | // } |
|---|
| 137 | // |
|---|
| 138 | // public UUID getFromUuid(){ |
|---|
| 139 | // return relationTo.getUuid(); |
|---|
| 140 | // } |
|---|
| 141 | // protected void setFromUuid(UUID uuid){ |
|---|
| 142 | // fromUuid = uuid; |
|---|
| 143 | // } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | //*********************** CLONE ********************************************************/ |
|---|
| 147 | |
|---|
| 148 | /** |
|---|
| 149 | * Clones <i>this</i> relationship. This is a shortcut that enables to create |
|---|
| 150 | * a new instance that differs only slightly from <i>this</i> relationship by |
|---|
| 151 | * modifying only some of the attributes. |
|---|
| 152 | * @throws CloneNotSupportedException |
|---|
| 153 | * |
|---|
| 154 | * @see eu.etaxonomy.cdm.model.common.RelationshipBase#clone() |
|---|
| 155 | * @see java.lang.Object#clone() |
|---|
| 156 | */ |
|---|
| 157 | @Override |
|---|
| 158 | public Object clone() throws CloneNotSupportedException { |
|---|
| 159 | RelationshipBase<FROM,TO,TYPE> result = (RelationshipBase<FROM,TO,TYPE>)super.clone(); |
|---|
| 160 | //no changes to: doubtful, deletedObjects |
|---|
| 161 | return result; |
|---|
| 162 | } |
|---|
| 163 | } |
|---|