source: VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/main/java/eu/clarin/cmdi/virtualcollectionregistry/model/VirtualCollection.java @ 191

Last change on this file since 191 was 191, checked in by oschonef, 14 years ago
  • fix link to JBoss maven repository
  • update to JPA 2.0 (Hibernate 3.5.0-CR-1 and Hibernate JPA API 1.0.0-CR-1)
  • Property svn:eol-style set to native
File size: 7.9 KB
Line 
1package eu.clarin.cmdi.virtualcollectionregistry.model;
2
3import java.util.Date;
4import java.util.HashMap;
5import java.util.LinkedHashSet;
6import java.util.Set;
7
8import javax.persistence.CascadeType;
9import javax.persistence.Column;
10import javax.persistence.Embedded;
11import javax.persistence.Entity;
12import javax.persistence.EnumType;
13import javax.persistence.Enumerated;
14import javax.persistence.FetchType;
15import javax.persistence.GeneratedValue;
16import javax.persistence.GenerationType;
17import javax.persistence.Id;
18import javax.persistence.JoinColumn;
19import javax.persistence.ManyToOne;
20import javax.persistence.NamedQueries;
21import javax.persistence.NamedQuery;
22import javax.persistence.OneToMany;
23import javax.persistence.OrderBy;
24import javax.persistence.Table;
25import javax.persistence.Temporal;
26import javax.persistence.TemporalType;
27import javax.persistence.Version;
28import javax.xml.bind.annotation.XmlAccessType;
29import javax.xml.bind.annotation.XmlAccessorType;
30import javax.xml.bind.annotation.XmlAttribute;
31import javax.xml.bind.annotation.XmlElement;
32import javax.xml.bind.annotation.XmlElementWrapper;
33import javax.xml.bind.annotation.XmlElements;
34import javax.xml.bind.annotation.XmlEnum;
35import javax.xml.bind.annotation.XmlEnumValue;
36import javax.xml.bind.annotation.XmlRootElement;
37import javax.xml.bind.annotation.XmlSeeAlso;
38import javax.xml.bind.annotation.XmlTransient;
39import javax.xml.bind.annotation.XmlType;
40import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
41
42import eu.clarin.cmdi.virtualcollectionregistry.model.mapper.DateAdapter;
43
44@Entity
45@Table(name = "virtual_collection")
46@NamedQueries({
47        @NamedQuery(name = "VirtualCollection.findAll",
48                                query = "SELECT c FROM VirtualCollection c"),
49    @NamedQuery(name = "VirtualCollection.countAll",
50                        query = "SELECT COUNT(c) FROM VirtualCollection c"),
51    @NamedQuery(name = "VirtualCollection.findByOwner",
52                        query = "SELECT c FROM VirtualCollection c " +
53                                "WHERE c.owner = :owner"),
54        @NamedQuery(name = "VirtualCollection.countByOwner",
55                        query = "SELECT COUNT(c) FROM VirtualCollection c " +
56                                "WHERE c.owner = :owner")
57})
58@XmlRootElement(name = "VirtualCollection")
59@XmlAccessorType(XmlAccessType.FIELD)
60@XmlType(propOrder = { "name", "description", "creationDate", "visibility",
61                "type", "origin", "creator", "resources" })
62@XmlSeeAlso({ Creator.class, Resource.class })
63public class VirtualCollection {
64        @XmlType(namespace = "urn:x-vcr:virtualcollection:visibility")
65        @XmlEnum(String.class)
66        public static enum Visibility {
67                @XmlEnumValue("advertised")
68                ADVERTISED,
69                @XmlEnumValue("non-advertised")
70                NON_ADVERTISED;
71        } // enum Visibility
72
73        @XmlType(namespace = "urn:x-vcr:virtualcollection:type")
74        @XmlEnum(String.class)
75        public static enum Type {
76                @XmlEnumValue("extensional")
77                EXTENSIONAL,
78                @XmlEnumValue("intensional")
79                INTENSIONAL
80        }
81
82        @Id
83        @GeneratedValue(strategy = GenerationType.AUTO)
84        @Column(name = "id")
85        @XmlAttribute(name = "id")
86        private long id;
87        @ManyToOne(cascade = { CascadeType.PERSIST,
88                                                   CascadeType.REFRESH,
89                           CascadeType.MERGE },
90               fetch = FetchType.EAGER)
91        @JoinColumn(name = "owner_id", nullable = false)
92        @XmlTransient
93        private User owner;
94        @Column(name = "pid", nullable = false)
95        @XmlAttribute(name = "persistentId")
96        private String pid;
97        @Column(name = "name", nullable = false)
98        @XmlElement(name = "Name")
99        private String name;
100        @Column(name = "description")
101        @XmlElement(name = "Description")
102        private String description;
103        @Column(name = "creation_date")
104        @Temporal(TemporalType.DATE)
105        @XmlElement(name = "CreationDate")
106        @XmlJavaTypeAdapter(DateAdapter.class)
107        private Date creationDate;
108        @Column(name = "visibility")
109        @Enumerated(EnumType.ORDINAL)
110        @XmlElement(name = "Visibility")
111        private Visibility visibility = Visibility.ADVERTISED;
112        @Column(name = "type")
113        @Enumerated(EnumType.ORDINAL)
114        @XmlElement(name = "Type")
115        private Type type = Type.EXTENSIONAL;
116        @Column(name = "origin")
117        @XmlElement(name = "Origin")
118        private String origin;
119        @Embedded
120        @XmlElement(name = "Creator")
121        private Creator creator;
122        @OneToMany(cascade = CascadeType.ALL,
123                           fetch = FetchType.LAZY,
124                           orphanRemoval = true)
125        @JoinColumn(name = "vc_id", nullable = false)
126        @OrderBy("id")
127        @XmlElementWrapper(name = "Resources")
128        @XmlElements({ @XmlElement(name = "ResourceProxy",
129                               type = ResourceProxy.class),
130                   @XmlElement(name = "ResourceMetadata",
131                                       type = ResourceMetadata.class) })
132        private Set<Resource> resources = new LinkedHashSet<Resource>();
133        @Column(name = "created", nullable = false, updatable = false)
134        @Temporal(TemporalType.TIMESTAMP)
135        @XmlTransient
136        private Date createdDate = new Date();
137        @Column(name = "modified", nullable = false)
138        @Temporal(TemporalType.TIMESTAMP)
139        @Version
140        @XmlTransient
141        private Date modifedDate;
142
143        public long getId() {
144                return id;
145        }
146
147        public void setOwner(User owner) {
148                if (owner == null) {
149                        throw new IllegalArgumentException("owner == null");
150                }
151                this.owner = owner;
152        }
153
154        public User getOwner() {
155                return owner;
156        }
157
158        public void setPid(String pid) {
159                if (name == null) {
160                        throw new IllegalArgumentException("name == null");
161                }
162                this.pid = pid;
163        }
164       
165        public String getPid() {
166                return pid;
167        }
168
169        public void setName(String name) {
170                if (name == null) {
171                        throw new IllegalArgumentException("name == null");
172                }
173                this.name = name;
174        }
175
176        public String getName() {
177                return name;
178        }
179
180        public void setDescription(String description) {
181                this.description = description;
182        }
183
184        public String getDescription() {
185                return description;
186        }
187
188        public void setCreationDate(Date creationDate) {
189                if (creationDate == null) {
190                        throw new IllegalArgumentException("creationDate == null");
191                }
192                this.creationDate = creationDate;
193        }
194
195        public Date getCreationDate() {
196                return creationDate;
197        }
198
199        public void setVisibility(Visibility visibility) {
200                if (visibility == null) {
201                        throw new IllegalArgumentException("visibility == null");
202                }
203                this.visibility = visibility;
204        }
205
206        public Visibility getVisibility() {
207                return visibility;
208        }
209
210        public void setType(Type style) {
211                if (style == null) {
212                        throw new IllegalArgumentException("style == null");
213                }
214                this.type = style;
215        }
216
217        public Type getType() {
218                return type;
219        }
220
221        public void setOrigin(String origin) {
222                this.origin = origin;
223        }
224
225        public String getOrigin() {
226                return origin;
227        }
228
229        public void setCreator(Creator creator) {
230                this.creator = creator;
231        }
232
233        public Creator getCreator() {
234                return creator;
235        }
236
237        public Set<Resource> getResources() {
238                return resources;
239        }
240
241        public Date getCreatedDate() {
242                return createdDate;
243        }
244
245        public void setModifiedDate(Date modifiedDate) {
246                if (modifiedDate == null) {
247                        throw new IllegalArgumentException("modifiedDate == null");
248                }
249                this.modifedDate = modifiedDate;
250        }
251       
252        public Date getModifiedDate() {
253                return modifedDate;
254        }
255
256        public void updateFrom(VirtualCollection vc) {
257                if (this == vc) {
258                        return;
259                }
260                this.setName(vc.getName());
261                this.setDescription(vc.getDescription());
262                this.setCreationDate(vc.getCreationDate());
263                this.setVisibility(vc.getVisibility());
264                this.setType(vc.getType());
265                this.setOrigin(vc.getOrigin());
266                this.creator.setName(vc.getCreator().getName());
267                this.creator.setEMail(vc.getCreator().getEMail());
268                this.creator.setOrganisation(vc.getCreator().getOrganisation());
269
270                HashMap<Integer, Resource> old_res =
271                        new HashMap<Integer, Resource>(this.resources.size());
272                for(Resource r : this.resources) {
273                        old_res.put(r.getSignature(), r);
274                       
275                }
276                HashMap<Integer, Resource> new_res =
277                        new HashMap<Integer, Resource>(vc.getResources().size());
278                for(Resource r : vc.getResources()) {
279                        new_res.put(r.getSignature(), r);
280                }
281                for (Resource r : new_res.values()) {
282                        if (!old_res.containsKey(r.getSignature())) {
283                                resources.add(r);
284                        }
285                }
286                for (Resource r : old_res.values()) {
287                        if (!new_res.containsKey(r.getSignature())) {
288                                resources.remove(r);
289                        }
290                }
291        }
292
293} // class VirtualCollection
Note: See TracBrowser for help on using the repository browser.