Skip to content

Commit

Permalink
INITIAL DB MODEL
Browse files Browse the repository at this point in the history
  • Loading branch information
FairWindCo committed Oct 12, 2015
2 parents 6f57f22 + ea17747 commit 3a86095
Show file tree
Hide file tree
Showing 69 changed files with 9,861 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
Expand All @@ -36,33 +37,43 @@ public String list(Model model) {
@RequestMapping(value = "/listing", method = RequestMethod.POST)
@ResponseBody
public JGridRowsResponse<ContactType> getTable(HttpServletRequest request){
//Pegeable
/*
nd:1444413790954
rows:10
page:1
sidx:id
sord:asc
*/
PageRequest paget=null;
if(request.getParameter("page")!=null) {
int page=0;
int rowcount=10;
filters:{"groupOp":"AND","rules":[{"field":"name","op":"bw","data":"dfadsfdasfd"}]}
*/
PageRequest pageRequest=null;
if(request.getParameter("page")!=null){
int rows=10;
int page;
try {
page = Integer.parseInt(request.getParameter("page"))-1;
rowcount=request.getParameter("rows")!=null?Integer.parseInt(request.getParameter("rows")):10;
if(request.getParameter("sidx")!=null && request.getParameter("sord")!=null) {
String sortField=request.getParameter("sidx");
String sort=request.getParameter("sord");
paget = new PageRequest(page, rowcount,"asc".equals(sort)? Sort.Direction.ASC: Sort.Direction.DESC,sortField);
page = Integer.parseInt(request.getParameter("page")) - 1;
rows = request.getParameter("rows") == null ? 10 : Integer.parseInt(request.getParameter("rows"));
if(request.getParameter("sidx")!=null){
String direction=request.getParameter("sord");
pageRequest=new PageRequest(page,rows,"asc".equals(direction)? Sort.Direction.ASC: Sort.Direction.DESC,request.getParameter("sidx"));
} else {
paget = new PageRequest(page, rowcount);
pageRequest=new PageRequest(page,rows);
}
}catch (Exception e){

}catch (NumberFormatException ex){
//do nothing
}
}
if(paget!=null){
return new JGridRowsResponse<ContactType>(repositoryContactType.findAll(paget));
}else{

}/**/
String filterName=request.getParameter("name");
if(pageRequest!=null){
if(filterName!=null && !filterName.isEmpty()){
return new JGridRowsResponse<ContactType>(repositoryContactType.findByNameContains(filterName, pageRequest));
} else
return new JGridRowsResponse<ContactType>(repositoryContactType.findAll(pageRequest));
} else {
if(filterName!=null && !filterName.isEmpty()){
return new JGridRowsResponse<ContactType>(repositoryContactType.findByNameContains(filterName));
} else
return new JGridRowsResponse<ContactType>(repositoryContactType.findAll());
}
}
Expand All @@ -80,24 +91,38 @@ public String add(@ModelAttribute ContactType contactType, BindingResult result)

@Transactional(readOnly = false)
@RequestMapping(value = "/edit", method = {RequestMethod.POST,RequestMethod.GET})
public void modify(@RequestParam String oper,@ModelAttribute ContactType contactType, BindingResult result,HttpServletResponse response) throws Exception{
if (result.hasErrors()) {
response.sendError(400, result.toString());
public void editor(@RequestParam String oper,ContactType contactType,BindingResult result,HttpServletResponse response)throws IOException{
if(result.hasErrors()){
response.sendError(400,result.toString());
return;
}
System.out.println("TEST METHOD EXEC!");
switch (oper) {
case "edit":
case "add": {
switch (oper){
case "add":
repositoryContactType.save(contactType);
response.setStatus(200);
}break;
case "del": {
break;
case "edit":
ContactType fromDB=repositoryContactType.getOne(contactType.getId());
if(fromDB!=null) {
repositoryContactType.save(contactType);
if(contactType.getVersion()>=fromDB.getVersion()) {
repositoryContactType.save(contactType);
response.setStatus(200);
} else {
response.sendError(406,"ANOTHER TRANSACTION MODIFICATION");
}
} else {
response.sendError(406,"NO CONTACT TYPE FOUND");
}

break;
case "del":
repositoryContactType.delete(contactType.getId());
response.setStatus(200);
}break;
break;
default:
System.out.println("NO METHOD FOUND!");
response.sendError(406, "Unknown command!");
response.sendError(406,"UNKNOWN OPERATION");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package ua.pp.fairwind.favorid.internalDB.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
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 ua.pp.fairwind.favorid.internalDB.jgrid.JGridRowsResponse;
import ua.pp.fairwind.favorid.internalDB.model.administrative.Role;
import ua.pp.fairwind.favorid.internalDB.model.administrative.User;
import ua.pp.fairwind.favorid.internalDB.repository.RoleRepository;
import ua.pp.fairwind.favorid.internalDB.repository.UserRepository;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
* Created by Ñåðãåé on 07.10.2015.
*/
@Controller
@RequestMapping("/users")
public class ConterpartyController {
@Autowired
UserRepository userRepository;
@Autowired
RoleRepository roleRepository;

@Secured("ROLE_USER")
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String list(Model model) {
return "user_list";
}

@Transactional(readOnly = true)
@RequestMapping(value = "/listing", method = RequestMethod.POST)
@ResponseBody
public JGridRowsResponse<User> getTable(HttpServletRequest request){
PageRequest pageRequest=null;
if(request.getParameter("page")!=null){
int rows=10;
int page;
try {
page = Integer.parseInt(request.getParameter("page")) - 1;
rows = request.getParameter("rows") == null ? 10 : Integer.parseInt(request.getParameter("rows"));
if(request.getParameter("sidx")!=null){
String direction=request.getParameter("sord");
pageRequest=new PageRequest(page,rows,"asc".equals(direction)? Sort.Direction.ASC: Sort.Direction.DESC,request.getParameter("sidx"));
} else {
pageRequest=new PageRequest(page,rows);
}
}catch (NumberFormatException ex){
//do nothing
}

}/**/
String filterName=request.getParameter("userName");
if(pageRequest!=null){
if(filterName!=null && !filterName.isEmpty()){
return new JGridRowsResponse<>(userRepository.findByUserNameContains(filterName, pageRequest));
} else
return new JGridRowsResponse<>(userRepository.findAll(pageRequest));
} else {
if(filterName!=null && !filterName.isEmpty()){
return new JGridRowsResponse<>(userRepository.findByUserNameContains(filterName));
} else
return new JGridRowsResponse<>(userRepository.findAll());
}
}

@Transactional(readOnly = true)
@RequestMapping(value = "/roles", method = RequestMethod.POST)
@ResponseBody
public JGridRowsResponse<Role> getRoleTable(HttpServletRequest request,@RequestParam Long userID){
PageRequest pageRequest=null;
if(request.getParameter("page")!=null){
int rows=10;
int page;
try {
page = Integer.parseInt(request.getParameter("page")) - 1;
if(page<0)page=0;
rows = request.getParameter("rows") == null ? 10 : Integer.parseInt(request.getParameter("rows"));
if(request.getParameter("sidx")!=null){
String direction=request.getParameter("sord");
pageRequest=new PageRequest(page,rows,"asc".equals(direction)? Sort.Direction.ASC: Sort.Direction.DESC,request.getParameter("sidx"));
} else {
pageRequest=new PageRequest(page,rows);
}
}catch (NumberFormatException ex){
//do nothing
}
}/**/
if(pageRequest!=null){
return new JGridRowsResponse<>(userRepository.getUserRoles(userID, pageRequest));
} else {
return new JGridRowsResponse<>(userRepository.getUserRoles(userID));
}
}

@Transactional(readOnly = true)
@RequestMapping(value = "/avaibleroles", method = {RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public List<Role> getAvaibleRoles(@RequestParam long userID){
return userRepository.getAvaibleUserRoles(userID);
}

@Transactional(readOnly = false)
@RequestMapping(value = "/edit", method = {RequestMethod.POST,RequestMethod.GET})
public void editor(@RequestParam String oper,User user,BindingResult result,HttpServletResponse response)throws IOException{
if(result.hasErrors()){
response.sendError(400,result.toString());
return;
}
switch (oper){
case "add":
case "edit":
userRepository.save(user);
response.setStatus(200);
break;
case "del":
userRepository.delete(user.getUserID());
response.setStatus(200);
break;
default:
response.sendError(406,"UNKNOWN OPERATION");
}
}



@Transactional(readOnly = false)
@RequestMapping(value = "/editroles", method = {RequestMethod.POST,RequestMethod.GET})
public void rulesEditor(@RequestParam String oper,@RequestParam long userID,@RequestParam long role_id,HttpServletResponse response)throws IOException{
switch (oper){
case "add":{
Role role=roleRepository.getOne(role_id);
User user=userRepository.findOne(userID);
if(user!=null && role!=null) {
user.getUserRoles().add(role);
userRepository.save(user);
}
response.setStatus(200);}
break;
case "del":{
Role role=roleRepository.getOne(role_id);
User user=userRepository.findOne(userID);
if(user!=null && role!=null) {
user.getUserRoles().remove(role);
userRepository.save(user);
}
response.setStatus(200);}
break;
default:
response.sendError(406,"UNKNOWN OPERATION");
}
}

}
Loading

0 comments on commit 3a86095

Please sign in to comment.