Project

General

Profile

Download (4.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.reference;
11

    
12
import java.util.ArrayList;
13
import java.util.HashSet;
14
import java.util.List;
15

    
16
import javax.persistence.Entity;
17
import javax.persistence.FetchType;
18
import javax.persistence.JoinColumn;
19
import javax.persistence.OneToMany;
20
import javax.xml.bind.annotation.XmlAccessType;
21
import javax.xml.bind.annotation.XmlAccessorType;
22
import javax.xml.bind.annotation.XmlElement;
23
import javax.xml.bind.annotation.XmlElementWrapper;
24
import javax.xml.bind.annotation.XmlRootElement;
25
import javax.xml.bind.annotation.XmlType;
26

    
27
import org.apache.log4j.Logger;
28
import org.hibernate.annotations.Cascade;
29
import org.hibernate.annotations.CascadeType;
30
import org.hibernate.annotations.IndexColumn;
31
import org.hibernate.collection.PersistentSet;
32
import org.hibernate.envers.Audited;
33
import org.hibernate.search.annotations.Indexed;
34

    
35
import eu.etaxonomy.cdm.model.common.Annotation;
36
import eu.etaxonomy.cdm.strategy.cache.reference.IReferenceBaseCacheStrategy;
37

    
38
/**
39
 * This (abstract) class represents all different kind of published {@link StrictReferenceBase references}
40
 * which constitute a physical or virtual unit. A reference is a published
41
 * reference if it can be consulted by the general public.
42
 * 
43
 * @author m.doering
44
 * @version 1.0
45
 * @created 08-Nov-2007 13:06:46
46
 */
47
@XmlAccessorType(XmlAccessType.FIELD)
48
@XmlType(name = "PublicationBase", propOrder = {
49
    "publishers"
50
})
51
@XmlRootElement(name = "PublicationBase")
52
@Entity
53
@Indexed(index = "eu.etaxonomy.cdm.model.reference.ReferenceBase")
54
@Audited
55
public abstract class PublicationBase<S extends IReferenceBaseCacheStrategy> extends StrictReferenceBase<S> {
56
	private static final Logger logger = Logger.getLogger(PublicationBase.class);
57

    
58
	@XmlElementWrapper(name = "Publishers")
59
	@XmlElement(name = "Publisher")
60
    @OneToMany (cascade = {javax.persistence.CascadeType.ALL}, fetch= FetchType.LAZY)
61
	@IndexColumn(name="sortIndex", base = 0)
62
	@JoinColumn (name = "referenceBase_id")
63
	@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE_ORPHAN})
64
	private List<Publisher> publishers = new ArrayList<Publisher>();
65
	
66
	
67
	public PublicationBase(){
68
		super();
69
	}
70

    
71
	/**
72
	 * Returns the list of publishers representing the name of the publisher and the place. 
73
	 * A publisher is mostly an institution or a private company which assumed the 
74
	 * global responsibility for the publication process whereas the place is 
75
	 * mostly a city .<BR>
76
	 * 
77
	 * @return  the list of publishers of <i>this</i>
78
	 * 			publication
79
	 * @see 	#getEditor()
80
	 */
81
	public List<Publisher> getPublishers(){
82
		return publishers;
83
	}
84
	
85

    
86
	public Publisher getPublisher(int index) throws IndexOutOfBoundsException{
87
		try{	
88
//			return this.publishers.iterator().next();
89
			return this.publishers.get(index);
90
		} catch (IndexOutOfBoundsException e) {
91
			logger.warn("IndexOutOfBoundsException. Index is " + index + " but must be 0 <= index < " + publishers.size());
92
			throw e;
93
		}
94
	}
95
	
96
	public void addPublisher(String publisher, String place){
97
		Publisher newPublisher = new Publisher(publisher, place);
98
		publishers.add(newPublisher);
99
	}
100
	public void addPublisher(String publisher, String place, int index) throws IndexOutOfBoundsException{
101
		try {
102
			Publisher newPublisher = new Publisher(publisher, place);
103
//			publishers.add(newPublisher);
104
			publishers.add(index, newPublisher);
105
		} catch (IndexOutOfBoundsException e) {
106
			logger.warn("IndexOutOfBoundsException. Index is " + index + " but must be 0 <= index < " + publishers.size());
107
			throw e;
108
		}
109
	}
110
	
111
	public void removePublisher(Publisher publisher){
112
		publishers.remove(publisher);
113
	}
114
	
115
//*********** CLONE **********************************/	
116

    
117

    
118
	/** 
119
	 * Clones <i>this</i> publication. This is a shortcut that enables to
120
	 * create a new instance that differs only slightly from <i>this</i>
121
	 * publication by modifying only some of the attributes.<BR>
122
	 * This method overrides the clone method from {@link StrictReferenceBase StrictReferenceBase}.
123
	 * 
124
	 * @see StrictReferenceBase#clone()
125
	 * @see eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity#clone()
126
	 * @see java.lang.Object#clone()
127
	 */
128
	@Override
129
	public Object clone() {
130
		PublicationBase result = (PublicationBase)super.clone();
131
		//Publisher
132
		result.publishers = new ArrayList<Publisher>();
133
		for (Publisher publisher : this.publishers ){
134
			Publisher newPublisher;
135
			try {
136
				newPublisher = (Publisher)publisher.clone();
137
			} catch (CloneNotSupportedException e) {
138
				//Publisher implements Cloneable therefore this should not be reached
139
				throw new RuntimeException("Publisher does not implement Cloneable");
140
			}
141
			result.addPublisher(newPublisher.getPublisherName(), newPublisher.getPlace());
142
		}
143
		//No changes: -
144
		return result;
145
	}
146

    
147
}
(19-19/28)