Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public class DataTablesInput {
private List<ColumnParameter> columns;

public DataTablesInput() {
this.draw = 1;
this.start = 0;
this.length = 10;
this.search = new SearchParameter();
this.order = new ArrayList<OrderParameter>();
this.columns = new ArrayList<ColumnParameter>();
Expand Down Expand Up @@ -127,6 +130,56 @@ public Map<String, ColumnParameter> getColumnsAsMap() {
return map;
}

/**
* Find a column by its name
*
* @param columnName the name of the column
* @return the given Column, or <code>null</code> if not found
*/
public ColumnParameter getColumn(String columnName) {
if (columnName == null) {
return null;
}
for (ColumnParameter column : columns) {
if (columnName.equals(column.getData())) {
return column;
}
}
return null;
}

/**
* Add a new column
*
* @param columnName the name of the column
* @param searchable whether the column is searchable or not
* @param orderable whether the column is orderable or not
* @param searchValue if any, the search value to apply
*/
public void addColumn(String columnName, boolean searchable, boolean orderable,
String searchValue) {
this.columns.add(new ColumnParameter(columnName, "", searchable, orderable,
new SearchParameter(searchValue, false)));
}

/**
* Add an order on the given column
*
* @param columnName the name of the column
* @param ascending whether the sorting is ascending or descending
*/
public void addOrder(String columnName, boolean ascending) {
if (columnName == null) {
return;
}
for (int i = 0; i < columns.size(); i++) {
if (!columnName.equals(columns.get(i).getData())) {
continue;
}
order.add(new OrderParameter(i, ascending ? "asc" : "desc"));
}
}

@Override
public String toString() {
return "DataTablesInput [draw=" + draw + ", start=" + start + ", length=" + length + ", search="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ public class DataTablesOutput<T> {
* parameter, in order to prevent Cross Site Scripting (XSS) attacks.
*/
@JsonView(View.class)
private Integer draw;
private int draw;

/**
* Total records, before filtering (i.e. the total number of records in the database)
*/
@JsonView(View.class)
private Long recordsTotal = 0L;
private long recordsTotal = 0L;

/**
* Total records, after filtering (i.e. the total number of records after filtering has been
* applied - not just the number of records being returned for this page of data).
*/
@JsonView(View.class)
private Long recordsFiltered = 0L;
private long recordsFiltered = 0L;

/**
* The data to be displayed in the table. This is an array of data source objects, one for each
Expand All @@ -48,27 +48,27 @@ public class DataTablesOutput<T> {
public interface View {
}

public Integer getDraw() {
public int getDraw() {
return draw;
}

public void setDraw(Integer draw) {
public void setDraw(int draw) {
this.draw = draw;
}

public Long getRecordsTotal() {
public long getRecordsTotal() {
return recordsTotal;
}

public void setRecordsTotal(Long recordsTotal) {
public void setRecordsTotal(long recordsTotal) {
this.recordsTotal = recordsTotal;
}

public Long getRecordsFiltered() {
public long getRecordsFiltered() {
return recordsFiltered;
}

public void setRecordsFiltered(Long recordsFiltered) {
public void setRecordsFiltered(long recordsFiltered) {
this.recordsFiltered = recordsFiltered;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ public void setSearch(SearchParameter search) {
this.search = search;
}

/**
* Set the search value to apply to this column
*
* @param searchValue if any, the search value to apply
*/
public void setSearchValue(String searchValue) {
this.search.setValue(searchValue);
}

@Override
public String toString() {
return "ColumnParameter [data=" + data + ", name=" + name + ", searchable=" + searchable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.springframework.data.jpa.datatables.model;

import org.springframework.data.jpa.datatables.repository.DataTablesRepository;

public interface BillRepository extends DataTablesRepository<Bill, Integer> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.springframework.data.jpa.datatables.model;

import org.springframework.data.jpa.datatables.repository.DataTablesRepository;

public interface GameRepository extends DataTablesRepository<Game, Integer> {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.springframework.data.jpa.datatables.repository;
package org.springframework.data.jpa.datatables.model;

import org.springframework.data.jpa.datatables.model.Home;
import org.springframework.data.repository.CrudRepository;

public interface HomeRepository extends CrudRepository<Home, Integer> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.springframework.data.jpa.datatables.model;

import org.springframework.data.jpa.datatables.repository.DataTablesRepository;

public interface UserRepository extends DataTablesRepository<User, Integer> {

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.util.ArrayList;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.datatables.Config;
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;
import org.springframework.data.jpa.datatables.model.Bill;
import org.springframework.data.jpa.datatables.parameter.ColumnParameter;
import org.springframework.data.jpa.datatables.parameter.OrderParameter;
import org.springframework.data.jpa.datatables.parameter.SearchParameter;
import org.springframework.data.jpa.datatables.model.BillRepository;
import org.springframework.data.jpa.datatables.specification.PreFilteringSpecification;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Expand All @@ -28,74 +23,52 @@ public class BillRepositoryTest {
@Autowired
private BillRepository billRepository;

/**
* Insert sample data at the beginning of all tests
*/
@Before
public void setUp() {
if (billRepository.count() > 0)
return;
for (int i = 0; i < 12; i++) {
Bill bill = new Bill();
bill.setHasBeenPayed(i % 2 == 0);
bill.setAmount((i + 1) * 100);
if (i == 0) {
bill.setDescription("foo%");
} else if (i == 1) {
bill.setDescription("foo_");
} else {
bill.setDescription("foo" + i);
}
billRepository.save(bill);
}
}

@Test
public void testWithoutFilter() {
DataTablesInput input = getBasicInput();

DataTablesOutput<Bill> output = billRepository.findAll(input);
assertNotNull(output);
assertNull(output.getError());
assertEquals(12, (long) output.getRecordsFiltered());
assertEquals(12, (long) output.getRecordsTotal());
assertEquals(12, output.getRecordsFiltered());
assertEquals(12, output.getRecordsTotal());
}

@Test
public void testBooleanFilter() {
DataTablesInput input = getBasicInput();

input.getColumns().get(2).getSearch().setValue("TRUE");
input.getColumn("hasBeenPayed").setSearchValue("TRUE");
DataTablesOutput<Bill> output = billRepository.findAll(input);
assertNotNull(output);
assertNull(output.getError());
assertEquals(6, (long) output.getRecordsFiltered());
assertEquals(6, output.getRecordsFiltered());
}

@Test
public void testBooleanFilter2() {
DataTablesInput input = getBasicInput();

input.getColumns().get(2).getSearch().setValue("TRUE+FALSE");
input.getColumn("hasBeenPayed").setSearchValue("TRUE+FALSE");
DataTablesOutput<Bill> output = billRepository.findAll(input);
assertNotNull(output);
assertNull(output.getError());
assertEquals(12, (long) output.getRecordsFiltered());
assertEquals(12, output.getRecordsFiltered());
}

@Test
public void testEscapeCharacter() {
DataTablesInput input = getBasicInput();

input.getColumns().get(3).getSearch().setValue("foo%");
input.getColumn("description").setSearchValue("foo%");
DataTablesOutput<Bill> output = billRepository.findAll(input);
assertNotNull(output);
assertEquals(1, (long) output.getRecordsFiltered());
assertEquals(1, output.getRecordsFiltered());

input.getColumns().get(3).getSearch().setValue("foo_");
input.getColumn("description").setSearchValue("foo_");
output = billRepository.findAll(input);
assertNotNull(output);
assertEquals(1, (long) output.getRecordsFiltered());
assertEquals(1, output.getRecordsFiltered());
}

@Test
Expand All @@ -105,8 +78,8 @@ public void testWithPreFiltering() {
DataTablesOutput<Bill> output =
billRepository.findAll(input, null, new PreFilteringSpecification<Bill>());
assertNotNull(output);
assertEquals(6, (long) output.getRecordsFiltered());
assertEquals(6, (long) output.getRecordsTotal());
assertEquals(6, output.getRecordsFiltered());
assertEquals(6, output.getRecordsTotal());
}

/**
Expand All @@ -115,23 +88,11 @@ public void testWithPreFiltering() {
*/
private static DataTablesInput getBasicInput() {
DataTablesInput input = new DataTablesInput();
input.setDraw(1);
input.setStart(0);
input.setLength(10);
input.setSearch(new SearchParameter("", false));
input.setOrder(new ArrayList<OrderParameter>());
input.getOrder().add(new OrderParameter(0, "asc"));

input.setColumns(new ArrayList<ColumnParameter>());
input.getColumns()
.add(new ColumnParameter("id", "", true, true, new SearchParameter("", false)));
input.getColumns()
.add(new ColumnParameter("amount", "", true, true, new SearchParameter("", false)));
input.getColumns()
.add(new ColumnParameter("hasBeenPayed", "", true, true, new SearchParameter("", false)));
input.getColumns()
.add(new ColumnParameter("description", "", true, true, new SearchParameter("", false)));

input.addColumn("id", true, true, "");
input.addColumn("amount", true, true, "");
input.addColumn("hasBeenPayed", true, true, "");
input.addColumn("description", true, true, "");
input.addOrder("id", true);
return input;
}
}

This file was deleted.

Loading