Skip to content

Commit 35a1032

Browse files
committed
implementacao da camada de repository da entidade Book & tests unitarios do repository;
1 parent dc0f457 commit 35a1032

File tree

6 files changed

+520
-0
lines changed

6 files changed

+520
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package com.library.app.book.model;
2+
3+
import com.library.app.author.model.Author;
4+
import com.library.app.category.model.Category;
5+
6+
import javax.persistence.*;
7+
import javax.validation.constraints.NotNull;
8+
import javax.validation.constraints.Size;
9+
import java.io.Serializable;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
/**
14+
* @author gabriel.freitas
15+
*/
16+
@Entity
17+
@Table(name = "lib_book")
18+
public class Book implements Serializable {
19+
20+
@Id
21+
@GeneratedValue(strategy = GenerationType.IDENTITY)
22+
private Long id;
23+
24+
@NotNull
25+
@Size(min = 10, max = 150)
26+
private String title;
27+
28+
@NotNull
29+
@ManyToOne
30+
@JoinColumn(name = "category_id")
31+
private Category category;
32+
33+
@ManyToMany(fetch = FetchType.EAGER)
34+
@JoinTable(name = "lib_book_author",
35+
joinColumns = @JoinColumn(name = "book_id"), inverseJoinColumns = @JoinColumn(name = "author_id"),
36+
uniqueConstraints = @UniqueConstraint(columnNames = {"book_id", "author_id"}))
37+
@JoinColumn(name = "author_id")
38+
@OrderBy(value = "name")
39+
@NotNull
40+
@Size(min = 1)
41+
private List<Author> authors;
42+
43+
@Lob
44+
@NotNull
45+
@Size(min = 10)
46+
private String description;
47+
48+
@NotNull
49+
private Double price;
50+
51+
public Long getId() {
52+
return id;
53+
}
54+
55+
public void setId(final Long id) {
56+
this.id = id;
57+
}
58+
59+
public String getTitle() {
60+
return title;
61+
}
62+
63+
public void setTitle(final String title) {
64+
this.title = title;
65+
}
66+
67+
public Category getCategory() {
68+
return category;
69+
}
70+
71+
public void setCategory(final Category category) {
72+
this.category = category;
73+
}
74+
75+
public List<Author> getAuthors() {
76+
if (authors == null) {
77+
authors = new ArrayList<>();
78+
}
79+
return authors;
80+
}
81+
82+
public void setAuthors(final List<Author> authors) {
83+
this.authors = authors;
84+
}
85+
86+
public boolean addAuthor(final Author author) {
87+
if (!getAuthors().contains(author)) {
88+
getAuthors().add(author);
89+
return true;
90+
}
91+
return false;
92+
}
93+
94+
public String getDescription() {
95+
return description;
96+
}
97+
98+
public void setDescription(final String description) {
99+
this.description = description;
100+
}
101+
102+
public Double getPrice() {
103+
return price;
104+
}
105+
106+
public void setPrice(final Double price) {
107+
this.price = price;
108+
}
109+
110+
@Override
111+
public int hashCode() {
112+
final int prime = 31;
113+
int result = 1;
114+
result = prime * result + ((id == null) ? 0 : id.hashCode());
115+
return result;
116+
}
117+
118+
@Override
119+
public boolean equals(final Object obj) {
120+
if (this == obj)
121+
return true;
122+
if (obj == null)
123+
return false;
124+
if (getClass() != obj.getClass())
125+
return false;
126+
final Book other = (Book) obj;
127+
if (id == null) {
128+
if (other.id != null)
129+
return false;
130+
} else if (!id.equals(other.id))
131+
return false;
132+
return true;
133+
}
134+
135+
@Override
136+
public String toString() {
137+
return "Book [id=" + id + ", title=" + title + ", price=" + price + "]";
138+
}
139+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.library.app.book.model.filter;
2+
3+
import com.library.app.common.model.filter.GenericFilter;
4+
5+
/**
6+
* @author gabriel.freitas
7+
*/
8+
public class BookFilter extends GenericFilter {
9+
10+
private String title;
11+
private Long categoryId;
12+
13+
public String getTitle() {
14+
return title;
15+
}
16+
17+
public void setTitle(final String title) {
18+
this.title = title;
19+
}
20+
21+
public Long getCategoryId() {
22+
return categoryId;
23+
}
24+
25+
public void setCategoryId(final Long categoryId) {
26+
this.categoryId = categoryId;
27+
}
28+
29+
@Override
30+
public String toString() {
31+
return "BookFilter [title=" + title + ", categoryId=" + categoryId + ", toString()=" + super.toString() + "]";
32+
}
33+
34+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.library.app.book.repository;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import javax.ejb.Stateless;
7+
import javax.persistence.EntityManager;
8+
import javax.persistence.PersistenceContext;
9+
10+
import com.library.app.book.model.Book;
11+
import com.library.app.book.model.filter.BookFilter;
12+
import com.library.app.common.model.PaginatedData;
13+
import com.library.app.common.repository.GenericRepository;
14+
/**
15+
* @author gabriel.freitas
16+
*/
17+
@Stateless
18+
public class BookRepository extends GenericRepository<Book> {
19+
20+
@PersistenceContext
21+
EntityManager em;
22+
23+
@Override
24+
protected Class<Book> getPersistentClass() {
25+
return Book.class;
26+
}
27+
28+
@Override
29+
protected EntityManager getEntityManager() {
30+
return em;
31+
}
32+
33+
public PaginatedData<Book> findByFilter(final BookFilter bookFilter) {
34+
final StringBuilder clause = new StringBuilder("Where e.id is not null");
35+
final Map<String, Object> queryParameters = new HashMap<>();
36+
if (bookFilter.getTitle() != null) {
37+
clause.append(" AND UPPER(e.title) Like UPPER(:title)");
38+
queryParameters.put("title", "%" + bookFilter.getTitle() + "%");
39+
}
40+
if (bookFilter.getCategoryId() != null) {
41+
clause.append(" AND e.category.id = :categoryId");
42+
queryParameters.put("categoryId", bookFilter.getCategoryId());
43+
}
44+
45+
return findByParameters(clause.toString(), bookFilter.getPaginationData(), queryParameters, "title ASC");
46+
}
47+
48+
}

0 commit comments

Comments
 (0)