Advertisement
Guest User

Untitled

a guest
Feb 18th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.87 KB | None | 0 0
  1. import hu.pte.clms.model.domain.User;
  2. import org.springframework.data.domain.Page;
  3. import org.springframework.data.domain.Pageable;
  4. import org.springframework.data.jpa.repository.JpaRepository;
  5. import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
  6. import org.springframework.stereotype.Repository;
  7.  
  8. @Repository
  9. public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor{
  10. }
  11.  
  12. import hu.pte.clms.model.domain.User;
  13. import hu.pte.clms.repository.UserRepository;
  14. import hu.pte.clms.service.UserService;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.transaction.annotation.Transactional;
  18.  
  19. import java.util.List;
  20.  
  21. @Service
  22. @Transactional
  23. public class UserServiceImpl implements UserService{
  24.  
  25. @Autowired
  26. private UserRepository userRepository;
  27.  
  28. @Override
  29. public List<User> listAll(){
  30. return userRepository.findAll();
  31. }
  32.  
  33. /* And another methods with this scheme */
  34.  
  35. import hu.pte.clms.model.domain.User;
  36. import hu.pte.clms.model.dto.UserDTO;
  37. import hu.pte.clms.service.UserService;
  38. import org.springframework.beans.factory.annotation.Autowired;
  39. import org.springframework.http.HttpStatus;
  40. import org.springframework.http.ResponseEntity;
  41. import org.springframework.security.core.Authentication;
  42. import org.springframework.security.core.context.SecurityContextHolder;
  43. import org.springframework.web.bind.annotation.*;
  44.  
  45. import java.util.List;
  46. import java.util.stream.Collectors;
  47.  
  48. @RestController
  49. @RequestMapping("/api")
  50. public class UserController{
  51.  
  52. @Autowired
  53. private UserService userService;
  54.  
  55. @RequestMapping(value = "/user/all", method = RequestMethod.GET)
  56. public ResponseEntity<List<UserDTO>> listAll(){
  57. return new ResponseEntity<>(userService.listAll().stream().map(user ->
  58. new UserDTO(user.getId(), user.getFirstName(), user.getLastName(), user.getCity(), user.getCountry(), user.getBio(), user.getPictureUrl())).collect(Collectors.toList()), HttpStatus.OK);
  59. }
  60.  
  61. @RequestMapping(value = "/auth/user")
  62. public LoginResult get(){
  63. Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  64. if(!auth.getName().equals("anonymousUser")){
  65. User user = userService.findByUsername(auth.getName());
  66. return new LoginResult(auth.getName(), auth.getAuthorities(), user);
  67. }
  68. return null;
  69. }
  70. }
  71.  
  72. import hu.pte.clms.model.domain.User;
  73. import org.springframework.security.core.GrantedAuthority;
  74. import org.springframework.security.core.userdetails.UserDetails;
  75.  
  76. import java.util.Collection;
  77. import java.util.List;
  78.  
  79. public class LoginResult implements UserDetails{
  80. private String password;
  81. private String name;
  82. private User user;
  83. private Collection<? extends GrantedAuthority> authorities;
  84.  
  85. public LoginResult(String name, Collection<? extends GrantedAuthority> authorities, User user){
  86. this.name = name;
  87. this.authorities = authorities;
  88. this.user = user;
  89. }
  90.  
  91. public LoginResult(String username, String s, boolean b, boolean userNonExpired, boolean credentialsNonExpired, boolean userNonLocked, Collection<? extends GrantedAuthority> authorities){}
  92.  
  93. public LoginResult(String username, String password, List<GrantedAuthority> grantedAuthorities){
  94. this.name = username;
  95. this.password = password;
  96. this.authorities = grantedAuthorities;
  97. }
  98.  
  99. import com.fasterxml.jackson.annotation.JsonIgnore;
  100. import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  101. import com.fasterxml.jackson.annotation.JsonInclude;
  102. import hu.pte.clms.model.domain.relationship.UserSkill;
  103.  
  104. import javax.persistence.*;
  105. import java.io.Serializable;
  106. import java.util.ArrayList;
  107. import java.util.List;
  108.  
  109. @Entity
  110. @Table(name = "USER")
  111. @JsonIgnoreProperties(ignoreUnknown = true)
  112. @JsonInclude(value = JsonInclude.Include.NON_NULL)
  113. public class User implements Serializable{
  114.  
  115. private static final long serialVersionUID = 1L;
  116.  
  117. @Id
  118. @GeneratedValue
  119. private Long id;
  120.  
  121. @Column(name = "USERNAME")
  122. private String username;
  123.  
  124. @Column(name = "PASSWORD")
  125. private String password;
  126.  
  127. @Column(name = "FIRST_NAME")
  128. private String firstName;
  129.  
  130. @Column(name = "LAST_NAME")
  131. private String lastName;
  132.  
  133. @Column(name = "AGE")
  134. private Short age;
  135.  
  136. @Column(name = "SEX")
  137. private String sex;
  138.  
  139. @Column(name = "PHONE")
  140. private String phone;
  141.  
  142. @Column(name = "SKYPE")
  143. private String skype;
  144.  
  145. @Column(name = "PRIMARY_EMAIL")
  146. private String primaryEmail;
  147.  
  148. @Column(name = "SECONDARY_EMAIL")
  149. private String secondaryEmail;
  150.  
  151. @Column(name = "CITY")
  152. private String city;
  153.  
  154. @Column(name = "COUNTRY")
  155. private String country;
  156.  
  157. @Column(name = "BIO")
  158. private String bio;
  159.  
  160. @Column(name = "PICTURE_URL")
  161. private String pictureUrl;
  162.  
  163. @OneToOne(cascade = CascadeType.ALL)
  164. @JoinColumn(name = "CONFIG_ID")
  165. private Config config;
  166.  
  167. @JsonIgnore
  168. @ManyToMany
  169. @JoinTable(name = "REL_USER_ROLE", joinColumns = {@JoinColumn(name = "USER_ID")}, inverseJoinColumns = {@JoinColumn(name = "ROLE_ID")})
  170. private List<Role> roles = new ArrayList<>();
  171.  
  172. @ManyToMany(fetch = FetchType.EAGER)
  173. @JoinTable(name = "REL_USER_SECURITY_ROLE", joinColumns = {@JoinColumn(name = "USER_ID")}, inverseJoinColumns = {@JoinColumn(name = "SECURITY_ROLE_ID")})
  174. private List<SecurityRole> securityRoles = new ArrayList<>();
  175.  
  176. @ManyToMany(mappedBy = "user")
  177. private List<UserSkill> skills = new ArrayList<>();
  178.  
  179. @JsonIgnore
  180. @ManyToMany
  181. @JoinTable(name = "REL_USER_PROJECT", joinColumns = {@JoinColumn(name = "USER_ID")}, inverseJoinColumns = {@JoinColumn(name = "PROJECT_ID")})
  182. private List<Project> projects = new ArrayList<>();
  183.  
  184. @JsonIgnore
  185. @OneToMany(mappedBy = "reviewed", cascade = CascadeType.ALL)
  186. private List<Review> reviews = new ArrayList<>();
  187.  
  188. /*Getters & setters*/
  189.  
  190. import com.fasterxml.jackson.annotation.JsonInclude;
  191. import hu.pte.clms.model.domain.*;
  192. import hu.pte.clms.model.domain.relationship.UserSkill;
  193. import java.util.ArrayList;
  194. import java.util.List;
  195.  
  196. @JsonInclude(JsonInclude.Include.NON_EMPTY)
  197. public class UserDTO{
  198.  
  199. private Long id;
  200. private String username;
  201. private String password;
  202. private String firstName;
  203. private String lastName;
  204. private Short age;
  205. private String sex;
  206. private String phone;
  207. private String skype;
  208. private String primaryEmail;
  209. private String secondaryEmail;
  210. private String city;
  211. private String country;
  212. private String bio;
  213. private String pictureUrl;
  214. private Config config;
  215. private List<Role> roles = new ArrayList<>();
  216. private List<SecurityRole> securityRoles = new ArrayList<>();
  217. private List<UserSkill> skills = new ArrayList<>();
  218. private List<Project> projects = new ArrayList<>();
  219. private List<Review> reviews = new ArrayList<>();
  220.  
  221. public UserDTO(){
  222. }
  223.  
  224. public UserDTO(User user){
  225. this.id = user.getId();
  226. this.username = user.getUsername();
  227. this.password = user.getPassword();
  228. this.firstName = user.getFirstName();
  229. this.lastName = user.getLastName();
  230. this.age = user.getAge();
  231. this.sex = user.getSex();
  232. this.phone = user.getPhone();
  233. this.skype = user.getSkype();
  234. this.primaryEmail = user.getPrimaryEmail();
  235. this.secondaryEmail = user.getSecondaryEmail();
  236. this.city = user.getCity();
  237. this.country = user.getCountry();
  238. this.bio = user.getBio();
  239. this.pictureUrl = user.getPictureUrl();
  240. this.config = user.getConfig();
  241. this.roles = user.getRoles();
  242. this.securityRoles = user.getSecurityRoles();
  243. this.skills = user.getSkills();
  244. this.projects = user.getProjects();
  245. this.reviews = user.getReviews();
  246. }
  247.  
  248. public UserDTO(Long id, String firstName, String lastName, String city, String country, String bio, String pictureUrl){
  249. this.id = id;
  250. this.firstName = firstName;
  251. this.lastName = lastName;
  252. this.city = city;
  253. this.country = country;
  254. this.bio = bio;
  255. this.pictureUrl = pictureUrl;
  256. }
  257. /*Getters & setters*/
  258. }
  259.  
  260. import hu.pte.clms.service.UserService;
  261. import org.springframework.beans.factory.annotation.Autowired;
  262. import org.springframework.boot.CommandLineRunner;
  263. import org.springframework.boot.SpringApplication;
  264. import org.springframework.boot.autoconfigure.SpringBootApplication;
  265.  
  266. @SpringBootApplication
  267. public class Application implements CommandLineRunner{
  268.  
  269. @Autowired
  270. private UserService userService;
  271.  
  272. public static void main(String[] args) {
  273. SpringApplication app = new SpringApplication(Application.class);
  274. app.setShowBanner(false);
  275. app.setRegisterShutdownHook(true);
  276. }
  277.  
  278. @Override
  279. public void run(String... strings) throws Exception{
  280. userService.listAll();
  281. }
  282. }
  283.  
  284. ...
  285.  
  286. <parent>
  287. <groupId>org.springframework.boot</groupId>
  288. <artifactId>spring-boot-starter-parent</artifactId>
  289. <version>1.2.2.RELEASE</version>
  290. </parent>
  291.  
  292. <dependencies>
  293. <dependency>
  294. <groupId>org.apache.httpcomponents</groupId>
  295. <artifactId>httpclient</artifactId>
  296. <version>4.4.1</version>
  297. </dependency>
  298.  
  299. <dependency>
  300. <groupId>org.springframework.boot</groupId>
  301. <artifactId>spring-boot-starter-web</artifactId>
  302. <version>${spring.boot.version}</version>
  303. </dependency>
  304. <dependency>
  305. <groupId>org.springframework.boot</groupId>
  306. <artifactId>spring-boot-starter-actuator</artifactId>
  307. <version>${spring.boot.version}</version>
  308. </dependency>
  309.  
  310. <dependency>
  311. <groupId>org.springframework.boot</groupId>
  312. <artifactId>spring-boot-starter-security</artifactId>
  313. <version>${spring.boot.version}</version>
  314. </dependency>
  315.  
  316. <dependency>
  317. <groupId>org.springframework.boot</groupId>
  318. <artifactId>spring-boot-starter-data-jpa</artifactId>
  319. <version>${spring.boot.version}</version>
  320. </dependency>
  321.  
  322. <dependency>
  323. <groupId>org.springframework</groupId>
  324. <artifactId>spring-context</artifactId>
  325. <version>4.1.4.RELEASE</version>
  326. </dependency>
  327.  
  328. <dependency>
  329. <groupId>org.springframework</groupId>
  330. <artifactId>spring-context-support</artifactId>
  331. <version>4.1.4.RELEASE</version>
  332. </dependency>
  333.  
  334. <dependency>
  335. <groupId>mysql</groupId>
  336. <artifactId>mysql-connector-java</artifactId>
  337. <version>5.1.34</version>
  338. </dependency>
  339. </dependencies>
  340.  
  341. spring.datasource:
  342. url: jdbc:mysql://localhost:3306/clms?autoReconnect=true
  343. username: clms
  344. password: clms
  345. testOnBorrow: true
  346. validationQuery: SELECT 1
  347. driverClassName: com.mysql.jdbc.Driver
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement