Skip to content

Commit f6fdf54

Browse files
[refactor] update mappings (and reduce boilerplate with lombok) (darrachequesne#35)
1 parent fa7afd7 commit f6fdf54

File tree

12 files changed

+162
-357
lines changed

12 files changed

+162
-357
lines changed

pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.github.darrachequesne</groupId>
77
<artifactId>spring-data-jpa-datatables</artifactId>
8-
<version>2.6</version>
8+
<version>3.0.0-SNAPSHOT</version>
99

1010
<name>Spring Data JPA for DataTables</name>
1111
<description>Spring Data JPA extension to work with the great jQuery plug-in DataTables (http://datatables.net/)</description>
@@ -62,6 +62,7 @@
6262
<maven.compiler.target>${java.version}</maven.compiler.target>
6363
<source.encoding>UTF-8</source.encoding>
6464

65+
<lombok.version>1.14.8</lombok.version>
6566
<spring.version>4.2.0.RELEASE</spring.version>
6667
<spring.data.jpa.version>1.8.2.RELEASE</spring.data.jpa.version>
6768
<hibernate.version>4.3.11.Final</hibernate.version>
@@ -76,6 +77,12 @@
7677
</properties>
7778

7879
<dependencies>
80+
<dependency>
81+
<groupId>org.projectlombok</groupId>
82+
<artifactId>lombok</artifactId>
83+
<version>${lombok.version}</version>
84+
<scope>provided</scope>
85+
</dependency>
7986

8087
<dependency>
8188
<groupId>org.springframework.data</groupId>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.springframework.data.jpa.datatables.mapping;
2+
3+
import javax.validation.constraints.NotNull;
4+
5+
import org.hibernate.validator.constraints.NotBlank;
6+
7+
import lombok.AllArgsConstructor;
8+
import lombok.Data;
9+
import lombok.NoArgsConstructor;
10+
11+
@Data
12+
@NoArgsConstructor
13+
@AllArgsConstructor
14+
public class Column {
15+
16+
/**
17+
* Column's data source
18+
*
19+
* @see http://datatables.net/reference/option/columns.data
20+
*/
21+
@NotBlank
22+
private String data;
23+
24+
/**
25+
* Column's name
26+
*
27+
* @see http://datatables.net/reference/option/columns.name
28+
*/
29+
private String name;
30+
31+
/**
32+
* Flag to indicate if this column is searchable (true) or not (false).
33+
*
34+
* @see http://datatables.net/reference/option/columns.searchable
35+
*/
36+
@NotNull
37+
private Boolean searchable;
38+
39+
/**
40+
* Flag to indicate if this column is orderable (true) or not (false).
41+
*
42+
* @see http://datatables.net/reference/option/columns.orderable
43+
*/
44+
@NotNull
45+
private Boolean orderable;
46+
47+
/**
48+
* Search value to apply to this specific column.
49+
*/
50+
@NotNull
51+
private Search search;
52+
53+
/**
54+
* Set the search value to apply to this column
55+
*
56+
* @param searchValue if any, the search value to apply
57+
*/
58+
public void setSearchValue(String searchValue) {
59+
this.search.setValue(searchValue);
60+
}
61+
62+
}

src/main/java/org/springframework/data/jpa/datatables/mapping/DataTablesInput.java

Lines changed: 18 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import javax.validation.constraints.NotNull;
1010

1111
import org.hibernate.validator.constraints.NotEmpty;
12-
import org.springframework.data.jpa.datatables.parameter.ColumnParameter;
13-
import org.springframework.data.jpa.datatables.parameter.OrderParameter;
14-
import org.springframework.data.jpa.datatables.parameter.SearchParameter;
1512

13+
import lombok.Data;
14+
15+
@Data
1616
public class DataTablesInput {
1717

1818
/**
@@ -23,15 +23,15 @@ public class DataTablesInput {
2323
*/
2424
@NotNull
2525
@Min(0)
26-
private Integer draw;
26+
private Integer draw = 1;
2727

2828
/**
2929
* Paging first record indicator. This is the start point in the current data set (0 index based -
3030
* i.e. 0 is the first record).
3131
*/
3232
@NotNull
3333
@Min(0)
34-
private Integer start;
34+
private Integer start = 0;
3535

3636
/**
3737
* Number of records that the table can display in the current draw. It is expected that the
@@ -41,90 +41,33 @@ public class DataTablesInput {
4141
*/
4242
@NotNull
4343
@Min(-1)
44-
private Integer length;
44+
private Integer length = 10;
4545

4646
/**
4747
* Global search parameter.
4848
*/
4949
@NotNull
50-
private SearchParameter search;
50+
private Search search = new Search();
5151

5252
/**
5353
* Order parameter
5454
*/
5555
@NotEmpty
56-
private List<OrderParameter> order;
56+
private List<Order> order = new ArrayList<Order>();
5757

5858
/**
5959
* Per-column search parameter
6060
*/
6161
@NotEmpty
62-
private List<ColumnParameter> columns;
63-
64-
public DataTablesInput() {
65-
this.draw = 1;
66-
this.start = 0;
67-
this.length = 10;
68-
this.search = new SearchParameter();
69-
this.order = new ArrayList<OrderParameter>();
70-
this.columns = new ArrayList<ColumnParameter>();
71-
}
72-
73-
public Integer getDraw() {
74-
return draw;
75-
}
76-
77-
public void setDraw(Integer draw) {
78-
this.draw = draw;
79-
}
80-
81-
public Integer getStart() {
82-
return start;
83-
}
84-
85-
public void setStart(Integer start) {
86-
this.start = start;
87-
}
88-
89-
public Integer getLength() {
90-
return length;
91-
}
92-
93-
public void setLength(Integer length) {
94-
this.length = length;
95-
}
96-
97-
public SearchParameter getSearch() {
98-
return search;
99-
}
100-
101-
public void setSearch(SearchParameter search) {
102-
this.search = search;
103-
}
104-
105-
public List<OrderParameter> getOrder() {
106-
return order;
107-
}
108-
109-
public void setOrder(List<OrderParameter> order) {
110-
this.order = order;
111-
}
112-
113-
public List<ColumnParameter> getColumns() {
114-
return columns;
115-
}
116-
117-
public void setColumns(List<ColumnParameter> columns) {
118-
this.columns = columns;
119-
}
62+
private List<Column> columns = new ArrayList<Column>();
12063

12164
/**
12265
*
123-
* @return a {@link Map} of {@link ColumnParameter} indexed by name
66+
* @return a {@link Map} of {@link Column} indexed by name
12467
*/
125-
public Map<String, ColumnParameter> getColumnsAsMap() {
126-
Map<String, ColumnParameter> map = new HashMap<String, ColumnParameter>();
127-
for (ColumnParameter column : columns) {
68+
public Map<String, Column> getColumnsAsMap() {
69+
Map<String, Column> map = new HashMap<String, Column>();
70+
for (Column column : columns) {
12871
map.put(column.getData(), column);
12972
}
13073
return map;
@@ -136,11 +79,11 @@ public Map<String, ColumnParameter> getColumnsAsMap() {
13679
* @param columnName the name of the column
13780
* @return the given Column, or <code>null</code> if not found
13881
*/
139-
public ColumnParameter getColumn(String columnName) {
82+
public Column getColumn(String columnName) {
14083
if (columnName == null) {
14184
return null;
14285
}
143-
for (ColumnParameter column : columns) {
86+
for (Column column : columns) {
14487
if (columnName.equals(column.getData())) {
14588
return column;
14689
}
@@ -158,8 +101,8 @@ public ColumnParameter getColumn(String columnName) {
158101
*/
159102
public void addColumn(String columnName, boolean searchable, boolean orderable,
160103
String searchValue) {
161-
this.columns.add(new ColumnParameter(columnName, "", searchable, orderable,
162-
new SearchParameter(searchValue, false)));
104+
this.columns.add(new Column(columnName, "", searchable, orderable,
105+
new Search(searchValue, false)));
163106
}
164107

165108
/**
@@ -176,14 +119,8 @@ public void addOrder(String columnName, boolean ascending) {
176119
if (!columnName.equals(columns.get(i).getData())) {
177120
continue;
178121
}
179-
order.add(new OrderParameter(i, ascending ? "asc" : "desc"));
122+
order.add(new Order(i, ascending ? "asc" : "desc"));
180123
}
181124
}
182125

183-
@Override
184-
public String toString() {
185-
return "DataTablesInput [draw=" + draw + ", start=" + start + ", length=" + length + ", search="
186-
+ search + ", order=" + order + ", columns=" + columns + "]";
187-
}
188-
189126
}

src/main/java/org/springframework/data/jpa/datatables/mapping/DataTablesOutput.java

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
import com.fasterxml.jackson.annotation.JsonView;
77

8+
import lombok.Data;
9+
10+
@Data
811
public class DataTablesOutput<T> {
912

1013
/**
@@ -48,50 +51,4 @@ public class DataTablesOutput<T> {
4851
public interface View {
4952
}
5053

51-
public int getDraw() {
52-
return draw;
53-
}
54-
55-
public void setDraw(int draw) {
56-
this.draw = draw;
57-
}
58-
59-
public long getRecordsTotal() {
60-
return recordsTotal;
61-
}
62-
63-
public void setRecordsTotal(long recordsTotal) {
64-
this.recordsTotal = recordsTotal;
65-
}
66-
67-
public long getRecordsFiltered() {
68-
return recordsFiltered;
69-
}
70-
71-
public void setRecordsFiltered(long recordsFiltered) {
72-
this.recordsFiltered = recordsFiltered;
73-
}
74-
75-
public List<T> getData() {
76-
return data;
77-
}
78-
79-
public void setData(List<T> data) {
80-
this.data = data;
81-
}
82-
83-
public String getError() {
84-
return error;
85-
}
86-
87-
public void setError(String error) {
88-
this.error = error;
89-
}
90-
91-
@Override
92-
public String toString() {
93-
return "DataTablesOutput [draw=" + draw + ", recordsTotal=" + recordsTotal
94-
+ ", recordsFiltered=" + recordsFiltered + ", error=" + error + "]";
95-
}
96-
9754
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.springframework.data.jpa.datatables.mapping;
2+
3+
import javax.validation.constraints.Min;
4+
import javax.validation.constraints.NotNull;
5+
import javax.validation.constraints.Pattern;
6+
7+
import lombok.AllArgsConstructor;
8+
import lombok.Data;
9+
import lombok.NoArgsConstructor;
10+
11+
@Data
12+
@NoArgsConstructor
13+
@AllArgsConstructor
14+
public class Order {
15+
16+
/**
17+
* Column to which ordering should be applied. This is an index reference to the columns array of
18+
* information that is also submitted to the server.
19+
*/
20+
@NotNull
21+
@Min(0)
22+
private Integer column;
23+
24+
/**
25+
* Ordering direction for this column. It will be asc or desc to indicate ascending ordering or
26+
* descending ordering, respectively.
27+
*/
28+
@NotNull
29+
@Pattern(regexp = "(desc|asc)")
30+
private String dir;
31+
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.springframework.data.jpa.datatables.mapping;
2+
3+
import javax.validation.constraints.NotNull;
4+
5+
import lombok.AllArgsConstructor;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
@Data
10+
@NoArgsConstructor
11+
@AllArgsConstructor
12+
public class Search {
13+
14+
/**
15+
* Global search value. To be applied to all columns which have searchable as true.
16+
*/
17+
@NotNull
18+
private String value;
19+
20+
/**
21+
* true if the global filter should be treated as a regular expression for advanced searching,
22+
* false otherwise. Note that normally server-side processing scripts will not perform regular
23+
* expression searching for performance reasons on large data sets, but it is technically possible
24+
* and at the discretion of your script.
25+
*/
26+
@NotNull
27+
private Boolean regex;
28+
29+
}

0 commit comments

Comments
 (0)