| 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 | import java.util.UUID; |
|---|
| 15 | |
|---|
| 16 | import javax.persistence.FetchType; |
|---|
| 17 | import javax.persistence.MappedSuperclass; |
|---|
| 18 | import javax.persistence.OneToMany; |
|---|
| 19 | import javax.xml.bind.annotation.XmlAccessType; |
|---|
| 20 | import javax.xml.bind.annotation.XmlAccessorType; |
|---|
| 21 | import javax.xml.bind.annotation.XmlElement; |
|---|
| 22 | import javax.xml.bind.annotation.XmlElementWrapper; |
|---|
| 23 | import javax.xml.bind.annotation.XmlType; |
|---|
| 24 | |
|---|
| 25 | import org.apache.log4j.Logger; |
|---|
| 26 | import org.hibernate.annotations.Cascade; |
|---|
| 27 | import org.hibernate.annotations.CascadeType; |
|---|
| 28 | |
|---|
| 29 | import eu.etaxonomy.cdm.strategy.merge.Merge; |
|---|
| 30 | import eu.etaxonomy.cdm.strategy.merge.MergeMode; |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | * Abstract superclass implementing human annotations and machine markers to be assigned to CDM objects. |
|---|
| 34 | * @author m.doering |
|---|
| 35 | * @version 1.0 |
|---|
| 36 | * @created 08-Nov-2007 13:06:10 |
|---|
| 37 | */ |
|---|
| 38 | @XmlAccessorType(XmlAccessType.FIELD) |
|---|
| 39 | @XmlType(name = "AnnotatableEntity", propOrder = { |
|---|
| 40 | "markers", |
|---|
| 41 | "annotations" |
|---|
| 42 | }) |
|---|
| 43 | @MappedSuperclass |
|---|
| 44 | public abstract class AnnotatableEntity extends VersionableEntity implements IAnnotatableEntity { |
|---|
| 45 | private static final long serialVersionUID = 9151211842542443102L; |
|---|
| 46 | @SuppressWarnings("unused") |
|---|
| 47 | private static final Logger logger = Logger.getLogger(AnnotatableEntity.class); |
|---|
| 48 | |
|---|
| 49 | @XmlElementWrapper(name = "Markers", nillable = true) |
|---|
| 50 | @XmlElement(name = "Marker") |
|---|
| 51 | @OneToMany(fetch=FetchType.LAZY) |
|---|
| 52 | @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE, CascadeType.DELETE_ORPHAN}) |
|---|
| 53 | @Merge(MergeMode.ADD_CLONE) |
|---|
| 54 | protected Set<Marker> markers; |
|---|
| 55 | |
|---|
| 56 | @XmlElementWrapper(name = "Annotations", nillable = true) |
|---|
| 57 | @XmlElement(name = "Annotation") |
|---|
| 58 | @OneToMany(fetch=FetchType.LAZY) |
|---|
| 59 | @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE, CascadeType.DELETE_ORPHAN}) |
|---|
| 60 | @Merge(MergeMode.ADD_CLONE) |
|---|
| 61 | protected Set<Annotation> annotations; |
|---|
| 62 | |
|---|
| 63 | protected AnnotatableEntity() { |
|---|
| 64 | super(); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | //*************** MARKER ********************************************** |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | public Set<Marker> getMarkers(){ |
|---|
| 71 | if(markers == null) { |
|---|
| 72 | this.markers = new HashSet<Marker>(); |
|---|
| 73 | } |
|---|
| 74 | return this.markers; |
|---|
| 75 | } |
|---|
| 76 | public void addMarker(Marker marker){ |
|---|
| 77 | if (marker != null){ |
|---|
| 78 | marker.setMarkedObj(this); |
|---|
| 79 | getMarkers().add(marker); |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | public void removeMarker(Marker marker){ |
|---|
| 83 | if(getMarkers().contains(marker)) { |
|---|
| 84 | getMarkers().remove(marker); |
|---|
| 85 | marker.setMarkedObj(null); |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | public boolean hasMarker(MarkerType type, boolean value){ |
|---|
| 90 | return hasMarker(type.getUuid(), value); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | public boolean hasMarker(UUID uuidMarkerType, boolean value){ |
|---|
| 94 | for (Marker marker: getMarkers()){ |
|---|
| 95 | if (marker.getMarkerType().getUuid().equals(uuidMarkerType)){ |
|---|
| 96 | if (marker.getFlag() == value){ |
|---|
| 97 | return true; |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | return false; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | //*************** ANNOTATIONS ********************************************** |
|---|
| 105 | |
|---|
| 106 | public Set<Annotation> getAnnotations(){ |
|---|
| 107 | if(annotations == null) { |
|---|
| 108 | this.annotations = new HashSet<Annotation>(); |
|---|
| 109 | } |
|---|
| 110 | return this.annotations; |
|---|
| 111 | } |
|---|
| 112 | public void addAnnotation(Annotation annotation){ |
|---|
| 113 | if (annotation != null){ |
|---|
| 114 | annotation.setAnnotatedObj(this); |
|---|
| 115 | getAnnotations().add(annotation); |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | public void removeAnnotation(Annotation annotation){ |
|---|
| 120 | if(getAnnotations().contains(annotation)) { |
|---|
| 121 | getAnnotations().remove(annotation); |
|---|
| 122 | annotation.setAnnotatedObj(null); |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | //********************** CLONE *****************************************/ |
|---|
| 127 | |
|---|
| 128 | /* (non-Javadoc) |
|---|
| 129 | * @see eu.etaxonomy.cdm.model.common.VersionableEntity#clone() |
|---|
| 130 | */ |
|---|
| 131 | @Override |
|---|
| 132 | public Object clone() throws CloneNotSupportedException{ |
|---|
| 133 | AnnotatableEntity result = (AnnotatableEntity)super.clone(); |
|---|
| 134 | |
|---|
| 135 | //Annotations |
|---|
| 136 | result.annotations = new HashSet<Annotation>(); |
|---|
| 137 | for (Annotation annotation : getAnnotations()){ |
|---|
| 138 | Annotation newAnnotation = (Annotation)annotation.clone(); |
|---|
| 139 | result.addAnnotation(newAnnotation); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | //Markers |
|---|
| 143 | result.markers = new HashSet<Marker>(); |
|---|
| 144 | for (Marker marker : getMarkers()){ |
|---|
| 145 | Marker newMarker = (Marker)marker.clone(); |
|---|
| 146 | result.addMarker(newMarker); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | //no changes to: - |
|---|
| 150 | return result; |
|---|
| 151 | } |
|---|
| 152 | } |
|---|