View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.tasksrs.model;
18  
19  // JSON: uncomment to include json support (note json is not part of the JAX-RS standard)
20  //import org.codehaus.jackson.annotate.JsonIgnore;
21  
22  import static javax.persistence.GenerationType.IDENTITY;
23  
24  import java.io.Serializable;
25  import java.io.StringReader;
26  
27  import javax.persistence.Entity;
28  import javax.persistence.GeneratedValue;
29  import javax.persistence.Id;
30  import javax.persistence.ManyToOne;
31  import javax.xml.bind.JAXB;
32  import javax.xml.bind.annotation.XmlAttribute;
33  import javax.xml.bind.annotation.XmlElement;
34  import javax.xml.bind.annotation.XmlRootElement;
35  import javax.xml.bind.annotation.XmlTransient;
36  
37  /**
38   * User's task entity which is marked up with JPA annotations and JAXB for serializing XML
39   * (and JSON if required)
40   *
41   * @author Oliver Kiss and others
42   */
43  @SuppressWarnings("serial")
44  @Entity
45  @XmlRootElement(name = "task")
46  public class Task implements Serializable {
47  
48      @Id
49      @GeneratedValue(strategy = IDENTITY)
50      private Long id;
51  
52      @ManyToOne
53      private User owner;
54  
55      private String title;
56  
57      public Task() {
58      }
59  
60      public Task(String title) {
61          super();
62          this.title = title;
63      }
64  
65      @XmlAttribute
66      public Long getId() {
67          return id;
68      }
69  
70      public void setId(Long id) {
71          this.id = id;
72      }
73  
74      @XmlTransient
75      // JSON: uncomment to include json support (note json is not part of the JAX-RS standard)
76  //    @JsonIgnore
77      public User getOwner() {
78          return owner;
79      }
80  
81      @XmlAttribute
82      public String getOwnerName() {
83          return owner.getUsername();
84      }
85  
86      public void setOwner(User owner) {
87          this.owner = owner;
88      }
89  
90      @XmlElement
91      public String getTitle() {
92          return title;
93      }
94  
95      public void setTitle(String title) {
96          this.title = title;
97      }
98  
99      @Override
100     public int hashCode() {
101         final int prime = 31;
102         int result = 1;
103         result = prime * result + ((owner == null) ? 0 : owner.hashCode());
104         result = prime * result + ((title == null) ? 0 : title.hashCode());
105         return result;
106     }
107 
108     @Override
109     public boolean equals(Object obj) {
110         if (this == obj) {
111             return true;
112         }
113         if (obj == null) {
114             return false;
115         }
116         if (getClass() != obj.getClass()) {
117             return false;
118         }
119         Task other = (Task) obj;
120         if (owner == null) {
121             if (other.owner != null) {
122                 return false;
123             }
124         } else if (!owner.equals(other.owner)) {
125             return false;
126         }
127         if (title == null) {
128             if (other.title != null) {
129                 return false;
130             }
131         } else if (!title.equals(other.title)) {
132             return false;
133         }
134         return true;
135     }
136 
137     public static Task stringToTask(String content) {
138         return JAXB.unmarshal(new StringReader(content), Task.class);
139     }
140 }