Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vendor neutral implementation for pagination #6

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions spring-boot-datatable/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,26 @@
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
</dependency>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>


<!-- https://mvnrepository.com/artifact/oracle/ojdbc6 -->
<!--
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,154 +1,116 @@
/**
*
*/
package com.opencodez.controllers;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.opencodez.domain.User;
import com.opencodez.domain.UserModel;
import com.opencodez.domain.pagination.DataTableRequest;
import com.opencodez.domain.pagination.DataTableResults;
import com.opencodez.domain.pagination.PaginationCriteria;
import com.opencodez.repo.GenericRepo;
import com.opencodez.repo.UserRepository;
import com.opencodez.util.AppUtil;

/**
* @author pavan.solapure
*
*/
@Controller
public class BaseController {

@Autowired
private UserRepository userRepo;

@Autowired
private GenericRepo genericRepo;

/** The entity manager. */
@PersistenceContext
private EntityManager entityManager;


@RequestMapping(value="/", method = RequestMethod.GET)
public ModelAndView home(@RequestParam(value = "name", defaultValue = "World") String name) {
ModelAndView mv = new ModelAndView("index");
mv.addObject("userModel", new UserModel());
List<UserModel> userList = genericRepo.getUserModel();
mv.addObject("userlist", userList);
return mv;
}

@RequestMapping(value="/users/mysql", method=RequestMethod.GET)
public String listUsers(Model model) {
return "users_mysql";
}

@RequestMapping(value="/users/paginated/mysql", method=RequestMethod.GET)
@ResponseBody
public String listUsersPaginated(HttpServletRequest request, HttpServletResponse response, Model model) {

DataTableRequest<User> dataTableInRQ = new DataTableRequest<User>(request);
PaginationCriteria pagination = dataTableInRQ.getPaginationRequest();

String baseQuery = "SELECT USER_ID as id, USER_NAME as name, SALARY as salary, (SELECT COUNT(1) FROM MYUSERS) AS total_records FROM MYUSERS";
String paginatedQuery = AppUtil.buildPaginatedQuery(baseQuery, pagination);

System.out.println(paginatedQuery);

Query query = entityManager.createNativeQuery(paginatedQuery, UserModel.class);

@SuppressWarnings("unchecked")
List<UserModel> userList = query.getResultList();

DataTableResults<UserModel> dataTableResult = new DataTableResults<UserModel>();
dataTableResult.setDraw(dataTableInRQ.getDraw());
dataTableResult.setListOfDataObjects(userList);
if (!AppUtil.isObjectEmpty(userList)) {
dataTableResult.setRecordsTotal(userList.get(0).getTotalRecords()
.toString());
if (dataTableInRQ.getPaginationRequest().isFilterByEmpty()) {
dataTableResult.setRecordsFiltered(userList.get(0).getTotalRecords()
.toString());
} else {
dataTableResult.setRecordsFiltered(Integer.toString(userList.size()));
}
}
return new Gson().toJson(dataTableResult);
}

@RequestMapping(value="/users/oracle", method=RequestMethod.GET)
public String listUsersOracle(Model model) {
return "users";
}

@RequestMapping(value="/users/paginated/orcl", method=RequestMethod.GET)
@ResponseBody
public String listUsersPaginatedForOracle(HttpServletRequest request, HttpServletResponse response, Model model) {

DataTableRequest<User> dataTableInRQ = new DataTableRequest<User>(request);
PaginationCriteria pagination = dataTableInRQ.getPaginationRequest();
String baseQuery = "SELECT USER_ID as id, USER_NAME as name, SALARY as salary FROM MYUSERS";
String paginatedQuery = AppUtil.buildPaginatedQueryForOracle(baseQuery, pagination);

System.out.println(paginatedQuery);

Query query = entityManager.createNativeQuery(paginatedQuery, UserModel.class);

@SuppressWarnings("unchecked")
List<UserModel> userList = query.getResultList();

DataTableResults<UserModel> dataTableResult = new DataTableResults<UserModel>();
dataTableResult.setDraw(dataTableInRQ.getDraw());
dataTableResult.setListOfDataObjects(userList);
if (!AppUtil.isObjectEmpty(userList)) {
dataTableResult.setRecordsTotal(userList.get(0).getTotalRecords()
.toString());
if (dataTableInRQ.getPaginationRequest().isFilterByEmpty()) {
dataTableResult.setRecordsFiltered(userList.get(0).getTotalRecords()
.toString());
} else {
dataTableResult.setRecordsFiltered(Integer.toString(userList.size()));
}
}
return new Gson().toJson(dataTableResult);
}

@RequestMapping(value="/adduser", method=RequestMethod.POST)
public String addUser(@ModelAttribute UserModel userModel, Model model) {
if(null != userModel) {

if(!AppUtil.isObjectEmpty(userModel.getId()) &&
!AppUtil.isObjectEmpty(userModel.getName()) &&
!AppUtil.isObjectEmpty(userModel.getSalary())) {

User u = new User();
u.setId(userModel.getId());
u.setName(userModel.getName());
u.setSalary(userModel.getSalary());
userRepo.save(u);
}
}
return "redirect:/";
}

}
/**
*
*/
package com.opencodez.controllers;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.google.gson.Gson;
import com.opencodez.domain.User;
import com.opencodez.domain.pagination.DataTableRequest;
import com.opencodez.domain.pagination.DataTableResults;
import com.opencodez.domain.pagination.PaginationCriteria;
import com.opencodez.repo.UserRepository;
import com.opencodez.util.AppUtil;

/**
* @author pavan.solapure
*
*/
@Controller
public class BaseController {
@Autowired
private UserRepository userRepo;

/** The entity manager. */
@PersistenceContext
private EntityManager entityManager;

@RequestMapping(value="/", method = RequestMethod.GET)
public ModelAndView home(@RequestParam(value = "name", defaultValue = "World") String name) {
ModelAndView mv = new ModelAndView("index");
mv.addObject("userModel", new User());
List<User> userList = userRepo.findAll();
mv.addObject("userlist", userList);
return mv;
}

@RequestMapping(value="/users", method=RequestMethod.GET)
public String listUsersOracle(Model model) {
return "users";
}

@RequestMapping(value="/users/paginated", method=RequestMethod.GET)
@ResponseBody
public String listUsersPaginatedForOracle(HttpServletRequest request, HttpServletResponse response, Model model) {
DataTableRequest<User> dataTableInRQ = new DataTableRequest<User>(request);
String sortBy = dataTableInRQ.getOrder().getData();
Direction dir = Direction.fromString(dataTableInRQ.getOrder().getSortDir());
Sort sort = new Sort(new Sort.Order(dir, sortBy));

Integer pageNo =(Integer.parseInt(request.getParameter(PaginationCriteria.PAGE_NO)));
Integer pageSize = (Integer.parseInt(request.getParameter(PaginationCriteria.PAGE_SIZE)));
Pageable pageable = new PageRequest(pageNo / pageSize, pageSize, sort);

Page<User> page = userRepo.findAll(pageable);

Integer totalRecords = (int)page.getTotalElements();

List<User> userList = page.getContent();
for(User user : userList) {
user.setTotalRecords(totalRecords);
}

DataTableResults<User> dataTableResult = new DataTableResults<User>();

dataTableResult.setDraw(dataTableInRQ.getDraw());
dataTableResult.setListOfDataObjects(userList);
if (!AppUtil.isObjectEmpty(userList)) {
dataTableResult.setRecordsTotal(userList.get(0).getTotalRecords()
.toString());
if (dataTableInRQ.getPaginationRequest().isFilterByEmpty()) {
dataTableResult.setRecordsFiltered(userList.get(0).getTotalRecords()
.toString());
} else {
dataTableResult.setRecordsFiltered(Integer.toString(userList.size()));
}
}
return new Gson().toJson(dataTableResult);
}

@RequestMapping(value="/adduser", method=RequestMethod.POST)
public String addUser(@ModelAttribute User userModel, Model model) {
if(null != userModel) {

if(!AppUtil.isObjectEmpty(userModel.getId()) &&
!AppUtil.isObjectEmpty(userModel.getName()) &&
!AppUtil.isObjectEmpty(userModel.getSalary())) {

User u = new User();
u.setId(userModel.getId());
u.setName(userModel.getName());
u.setSalary(userModel.getSalary());
userRepo.save(u);
}
}
return "redirect:/";
}

}
30 changes: 14 additions & 16 deletions spring-boot-datatable/src/main/java/com/opencodez/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
* @author pavan.solapure
Expand All @@ -24,8 +25,10 @@ public class User {
private String name;

@Column(name = "SALARY")
private String salary;

private Integer salary;

@Transient
private Integer totalRecords;

/**
* @return the id
Expand Down Expand Up @@ -54,25 +57,20 @@ public void setName(String name) {
/**
* @return the salary
*/
public String getSalary() {
public Integer getSalary() {
return salary;
}
/**
* @param salary the salary to set
*/
public void setSalary(String salary) {
public void setSalary(Integer salary) {
this.salary = salary;
}
// /**
// * @return the totalRecords
// */
// public Integer getTotalRecords() {
// return totalRecords;
// }
// /**
// * @param totalRecords the totalRecords to set
// */
// public void setTotalRecords(Integer totalRecords) {
// this.totalRecords = totalRecords;
// }

public Integer getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(Integer totalRecords) {
this.totalRecords = totalRecords;
}
}
Loading