refactoring
This commit is contained in:
1 parent
abbac0b416
commit
2df87040ed
11 files changed
+129
-256
No files matched your search
@@ -1,7 +1,5 @@
|
||||
package application;
|
||||
|
||||
import application.model.Role;
|
||||
import application.model.User;
|
||||
import application.repositories.RoleRepository;
|
||||
import application.repositories.UserRepository;
|
||||
import application.utils.TokensGenerator;
|
||||
@@ -18,8 +16,6 @@ import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.JstlView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
/**
|
||||
* Created by lukasgol on 01.04.14.
|
||||
@@ -42,8 +38,6 @@ public class Config extends SpringBootServletInitializer {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(Config.class, args);
|
||||
UserRepository userRepository=context.getBean(UserRepository.class);
|
||||
RoleRepository roleRepository=context.getBean(RoleRepository.class);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -23,22 +23,26 @@ public class RoleController {
|
||||
private RoleRepository roleRepository;
|
||||
|
||||
@RequestMapping(value = "/add")
|
||||
@ResponseBody
|
||||
public String addRole(@RequestParam String rolename,@RequestParam String password) {
|
||||
public void addRole(@RequestParam String rolename,@RequestParam String password) {
|
||||
Role role = new Role(rolename,password);
|
||||
roleRepository.save(role);
|
||||
return "saved role: "+role.getRoleId();
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("/{roleId}")
|
||||
@ResponseBody
|
||||
public String getRole(@PathVariable("roleId") Long roleId) {
|
||||
public String getRole(@PathVariable("roleId") Long roleId) throws JSONException{
|
||||
Role role = roleRepository.findOne(roleId);
|
||||
return "role: "+role.toString();
|
||||
JSONArray roleArray = new JSONArray();
|
||||
JSONObject roleJSON = new JSONObject();
|
||||
roleJSON.put("id", role.getRoleId());
|
||||
roleJSON.put("name", role.getRoleName());
|
||||
roleJSON.put("password", role.getPassword());
|
||||
roleArray.put(roleJSON);
|
||||
|
||||
return roleArray.toString();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/all")
|
||||
@RequestMapping
|
||||
public
|
||||
@ResponseBody
|
||||
String listRolesJson(ModelMap model) throws JSONException {
|
||||
|
||||
@@ -11,15 +11,12 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Created by lukasgol on 21.04.14.
|
||||
@@ -38,81 +35,32 @@ public class UserController {
|
||||
|
||||
@RequestMapping(value = "/add")
|
||||
@ResponseBody
|
||||
public String addUser(@RequestParam String username) {
|
||||
User user = new User();
|
||||
//powinno mapowac obiekt json na obiekt user
|
||||
public String addUser(@RequestBody User user) {
|
||||
/*User user = new User();
|
||||
user.setFirstName(username);
|
||||
Set<Role> roles = new HashSet<Role>();
|
||||
roles.add(new Role("lala","lala"));
|
||||
roles.add(new Role("lala1","lala1"));
|
||||
user.setRoles(roles);
|
||||
user.setRoles(roles);*/
|
||||
userRepository.save(user);
|
||||
return "saved user: "+user.getUserId();
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("/{userId}")
|
||||
public String getUser(@PathVariable("userId") Long userId) {
|
||||
public String getUser(@PathVariable("userId") Long userId) throws JSONException{
|
||||
User user = userRepository.findOne(userId);
|
||||
return "user: "+user.toString();
|
||||
JSONObject userJSON = new JSONObject();
|
||||
userJSON.put("id", user.getUserId());
|
||||
userJSON.put("firstname", user.getFirstName());
|
||||
userJSON.put("lastname", user.getLastName());
|
||||
userJSON.put("email", user.getEmail());
|
||||
JSONArray userArray = new JSONArray();
|
||||
userArray.put(userJSON);
|
||||
return userArray.toString();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/init")
|
||||
public
|
||||
@ResponseBody
|
||||
String initialise() {
|
||||
|
||||
Role ceo = new Role("CEO","101010");
|
||||
Role worker = new Role("Worker","12345");
|
||||
roleRepository.save(new Role("Cleaner","54321"));
|
||||
ceo = roleRepository.save(ceo);
|
||||
|
||||
|
||||
User user1 = new User("Łukasz", "Goliński", "boss@nfc.pl");
|
||||
user1.addRole(worker);
|
||||
user1.addRole(ceo);
|
||||
userRepository.save(user1);
|
||||
|
||||
|
||||
ArrayList<Role> roles = (ArrayList<Role>) roleRepository.findAll();
|
||||
|
||||
for(Role r: roles){
|
||||
System.out.println(r.getRoleName());
|
||||
}
|
||||
|
||||
Role r = roles.get(0);
|
||||
|
||||
User user3 = new User("Monika", "Bigas", "boss_monika@nfc.pl");
|
||||
user3.addRole(worker);
|
||||
user3.addRole(ceo);
|
||||
userRepository.save(user3);
|
||||
//entityManager.merge(user3);
|
||||
|
||||
User user2 = new User("Marcin", "Michałek", "boss_marcin@nfc.pl");
|
||||
user2.addRole(ceo);
|
||||
userRepository.save(user2);
|
||||
|
||||
|
||||
|
||||
|
||||
ArrayList<User> users = (ArrayList<User>) userRepository.findAll();
|
||||
for(User u: users){
|
||||
System.out.println(u.getFirstName()+" "+u.getLastName());
|
||||
}
|
||||
|
||||
for (User user : userRepository.findAll()) {
|
||||
System.out.println("USER: " + user.getFirstName() + " " + user.getLastName() + " " + user.getEmail());
|
||||
System.out.println("roles: ");
|
||||
for (Role role : user.getRoles()) {
|
||||
System.out.println(" -" + role.getRoleName());
|
||||
}
|
||||
System.out.println("enduser");
|
||||
System.out.println("");
|
||||
}
|
||||
return "initialised";
|
||||
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/all")
|
||||
@RequestMapping
|
||||
public
|
||||
@ResponseBody
|
||||
String listUsersJson(ModelMap model) throws JSONException {
|
||||
@@ -122,6 +70,17 @@ public class UserController {
|
||||
JSONObject userJSON = new JSONObject();
|
||||
userJSON.put("id", user.getUserId());
|
||||
userJSON.put("firstname", user.getFirstName());
|
||||
userJSON.put("lastname", user.getLastName());
|
||||
userJSON.put("email", user.getEmail());
|
||||
JSONArray roleArray = new JSONArray();
|
||||
for(Role r:user.getRoles()){
|
||||
JSONObject roleJSON = new JSONObject();
|
||||
roleJSON.put("id", r.getRoleId());
|
||||
roleJSON.put("name", r.getRoleName());
|
||||
roleJSON.put("password", r.getPassword());
|
||||
roleArray.put(roleJSON);
|
||||
}
|
||||
userJSON.put("roles", roleArray);
|
||||
userArray.put(userJSON);
|
||||
}
|
||||
return userArray.toString();
|
||||
|
||||
@@ -29,19 +29,14 @@ public class Role {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
|
||||
return password;
|
||||
}
|
||||
//getters setters
|
||||
public String getPassword() { return password; }
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getRoleName() {
|
||||
|
||||
return roleName;
|
||||
}
|
||||
public String getRoleName() {return roleName;}
|
||||
|
||||
public void setRoleName(String roleName) {
|
||||
this.roleName = roleName;
|
||||
@@ -50,6 +45,8 @@ public class Role {
|
||||
public Long getRoleId() {
|
||||
return roleId;
|
||||
}
|
||||
//end
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.util.Set;
|
||||
|
||||
@Entity(name = "account")
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@Column(name = "userId")
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@@ -20,6 +21,13 @@ public class User {
|
||||
@Basic
|
||||
private String email;
|
||||
|
||||
@ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
|
||||
@JoinTable(
|
||||
name="User_Role",
|
||||
joinColumns = @JoinColumn( name="User_ID"),
|
||||
inverseJoinColumns = @JoinColumn( name="Role_ID"))
|
||||
private Set<Role> roles = new HashSet<Role>(0);
|
||||
|
||||
public User(String firstName, String lastName, String email) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
@@ -29,12 +37,11 @@ public class User {
|
||||
public User() {
|
||||
}
|
||||
|
||||
@ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
|
||||
@JoinTable(
|
||||
name="User_Role",
|
||||
joinColumns = @JoinColumn( name="User_ID"),
|
||||
inverseJoinColumns = @JoinColumn( name="Role_ID"))
|
||||
private Set<Role> roles = new HashSet<Role>(0);
|
||||
public void addRole(Role role){
|
||||
roles.add(role);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//getter and setter
|
||||
private Set<Role> getRole(){
|
||||
@@ -81,8 +88,4 @@ public class User {
|
||||
this.roles = roles;
|
||||
}
|
||||
//end
|
||||
|
||||
public void addRole(Role role){
|
||||
roles.add(role);
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package application.model;
|
||||
|
||||
/**
|
||||
* Created by Marcin on 2014-04-19.
|
||||
*/
|
||||
public class UserLevel {
|
||||
public java.util.List<String> getItems() {
|
||||
java.util.List<String> list = new java.util.ArrayList<String>();
|
||||
list.add("Head of security");
|
||||
list.add("CEO");
|
||||
list.add("Cleaner");
|
||||
return list;
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package application.utils;
|
||||
|
||||
/**
|
||||
* Created by lukasgol on 02.04.14.
|
||||
*/
|
||||
|
||||
public class Action {
|
||||
String name;
|
||||
String param;
|
||||
|
||||
public Action(String name, String param) {
|
||||
this.name = name;
|
||||
this.param = param;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setParam(String param) {
|
||||
this.param = param;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getParam() {
|
||||
return param;
|
||||
}
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
package application.utils; /**
|
||||
* Created by lukasgol on 01.04.14.
|
||||
*/
|
||||
|
||||
import application.model.User;
|
||||
import application.repositories.RoleRepository;
|
||||
import application.repositories.UserRepository;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@Controller
|
||||
@PropertySource("app.properties")
|
||||
public class Controller_test {
|
||||
|
||||
//application.repositories
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
@Autowired
|
||||
private RoleRepository roleRepository;
|
||||
//end
|
||||
@Autowired
|
||||
ConfigurableApplicationContext context;
|
||||
|
||||
@SuppressWarnings("SpringJavaAutowiringInspection")
|
||||
@Autowired
|
||||
PropertiesSolver ps;
|
||||
@Value("${my.prop}")
|
||||
String val;
|
||||
|
||||
//get log4j handler
|
||||
private static final Logger logger = Logger.getLogger(Controller_test.class);
|
||||
|
||||
@RequestMapping("/action")
|
||||
public
|
||||
@ResponseBody
|
||||
String hello(@RequestParam(value = "name", required = false, defaultValue = "empty") String name,
|
||||
@RequestParam(value = "param", required = false, defaultValue = "empty") String param) {
|
||||
Action action=new Action(name,param);
|
||||
return "application.utils.Action: " + action.getName() + " with parameter: "+action.getParam()+". :-) ";
|
||||
}
|
||||
|
||||
@RequestMapping("/str")
|
||||
@ResponseBody
|
||||
String hello(@RequestParam(value = "action", required = false, defaultValue = "empty") String action) {
|
||||
return "Just String: "+action;
|
||||
}
|
||||
|
||||
@RequestMapping("/val")
|
||||
@ResponseBody
|
||||
String getVal(@RequestParam(value = "param", required = false, defaultValue = "my.prop") String param){
|
||||
System.out.println("lala");
|
||||
if(logger.isDebugEnabled()){
|
||||
logger.debug("Property:"+param);
|
||||
}
|
||||
String recived_val;
|
||||
if (param.equals("my.prop"))return "property "+param+" is: "+ val ;
|
||||
else if((recived_val=ps.getProperty(param))!=null){
|
||||
return "property "+param+" is: "+ recived_val;
|
||||
}
|
||||
else {
|
||||
logger.debug("Value:"+recived_val);
|
||||
return "not found";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping(value = "/users", method = RequestMethod.GET)
|
||||
public
|
||||
@ResponseBody
|
||||
String listUsersJson(ModelMap model) throws JSONException {
|
||||
JSONArray userArray = new JSONArray();
|
||||
for (User user : userRepository.findAll()) {
|
||||
JSONObject userJSON = new JSONObject();
|
||||
userJSON.put("id", user.getUserId());
|
||||
userJSON.put("firstName", user.getFirstName());
|
||||
userJSON.put("lastName", user.getLastName());
|
||||
userJSON.put("email", user.getEmail());
|
||||
userArray.put(userJSON);
|
||||
}
|
||||
return userArray.toString();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/adduser", method = RequestMethod.GET)
|
||||
@ResponseBody public String addUser(@RequestParam String username) {
|
||||
User user = new User();
|
||||
user.setFirstName(username);
|
||||
user.setEmail("lala@gmail.com");
|
||||
userRepository.save(user);
|
||||
return "saved user: "+user.toString();
|
||||
}
|
||||
|
||||
@RequestMapping("/delete/{userId}")
|
||||
@ResponseBody public String deleteUser(@PathVariable("userId") Long userId) {
|
||||
userRepository.delete(userRepository.findOne(userId));
|
||||
return "deleted user";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package application.utils;
|
||||
|
||||
import application.model.Role;
|
||||
import application.model.User;
|
||||
import application.repositories.RoleRepository;
|
||||
import application.repositories.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by lukasgol on 30.04.14.
|
||||
*/
|
||||
public class Initializer {
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
@Autowired
|
||||
private RoleRepository roleRepository;
|
||||
|
||||
|
||||
public void init(){
|
||||
Role ceo = new Role("CEO","101010");
|
||||
Role worker = new Role("Worker","12345");
|
||||
roleRepository.save(new Role("Cleaner","54321"));
|
||||
ceo = roleRepository.save(ceo);
|
||||
|
||||
|
||||
User user1 = new User("Łukasz", "Goliński", "boss@nfc.pl");
|
||||
user1.addRole(worker);
|
||||
user1.addRole(ceo);
|
||||
userRepository.save(user1);
|
||||
|
||||
|
||||
ArrayList<Role> roles = (ArrayList<Role>) roleRepository.findAll();
|
||||
|
||||
for(Role r: roles){
|
||||
System.out.println(r.getRoleName());
|
||||
}
|
||||
|
||||
Role r = roles.get(0);
|
||||
|
||||
User user3 = new User("Monika", "Bigas", "boss_monika@nfc.pl");
|
||||
user3.addRole(worker);
|
||||
user3.addRole(ceo);
|
||||
userRepository.save(user3);
|
||||
//entityManager.merge(user3);
|
||||
|
||||
User user2 = new User("Marcin", "Michałek", "boss_marcin@nfc.pl");
|
||||
user2.addRole(ceo);
|
||||
userRepository.save(user2);
|
||||
|
||||
|
||||
|
||||
|
||||
ArrayList<User> users = (ArrayList<User>) userRepository.findAll();
|
||||
for(User u: users){
|
||||
System.out.println(u.getFirstName()+" "+u.getLastName());
|
||||
}
|
||||
|
||||
for (User user : userRepository.findAll()) {
|
||||
System.out.println("USER: " + user.getFirstName() + " " + user.getLastName() + " " + user.getEmail());
|
||||
System.out.println("roles: ");
|
||||
for (Role role : user.getRoles()) {
|
||||
System.out.println(" -" + role.getRoleName());
|
||||
}
|
||||
System.out.println("enduser");
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user