Advertisement
Guest User

Untitled

a guest
Sep 10th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.54 KB | None | 0 0
  1. package com.infy.controller;
  2.  
  3. import java.sql.SQLException;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.context.annotation.Scope;
  11. import org.springframework.http.HttpStatus;
  12. import org.springframework.http.ResponseEntity;
  13. import org.springframework.stereotype.Controller;
  14. import org.springframework.transaction.annotation.Propagation;
  15. import org.springframework.transaction.annotation.Transactional;
  16. import org.springframework.web.bind.annotation.PathVariable;
  17. import org.springframework.web.bind.annotation.RequestBody;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import org.springframework.web.bind.annotation.RequestMethod;
  20. import org.springframework.web.bind.annotation.ResponseBody;
  21. import org.springframework.web.context.WebApplicationContext;
  22.  
  23. import com.infy.dto.UserDTO;
  24. import com.infy.service.IUserService;
  25. import com.infy.service.*;
  26.  
  27. //users/user
  28. @Controller
  29.  
  30.  
  31. public class UserController {
  32.  
  33.  
  34. @Autowired
  35. WebApplicationContext context;
  36. @Autowired
  37. IUserService userService;
  38.  
  39.  
  40. @RequestMapping(value="/signUp",method = RequestMethod.POST,produces="application/json",consumes="application/json")
  41. public ResponseEntity <Map<String,String>> signUp(@RequestBody UserDTO userDto) {
  42. //System.out.println(context.isPrototype("userController"));
  43. System.out.println(context.getBean("dataSource"));
  44. Map<String,String> message=new HashMap<String,String>();
  45. try
  46. {
  47.  
  48.  
  49. message.put("message",userService.addUser(userDto));
  50.  
  51. }
  52. catch(Exception e)
  53. {
  54. message.put("message","Unexpected error accured.");
  55. System.out.println(e.getStackTrace());
  56. return new ResponseEntity<Map<String,String>>(message, HttpStatus.NOT_ACCEPTABLE);
  57.  
  58. }
  59.  
  60. return new ResponseEntity<Map<String,String>>(message, HttpStatus.OK);
  61.  
  62.  
  63.  
  64.  
  65. }
  66.  
  67. }
  68.  
  69. package com.infy.service;
  70.  
  71.  
  72. import java.sql.SQLException;
  73. import java.sql.Timestamp;
  74. import java.util.Calendar;
  75. import java.util.Date;
  76. import java.util.List;
  77.  
  78. import javax.management.RuntimeErrorException;
  79.  
  80. import org.springframework.beans.factory.annotation.Autowired;
  81. import org.springframework.context.annotation.Scope;
  82. import org.springframework.stereotype.Service;
  83. import org.springframework.transaction.annotation.Isolation;
  84. import org.springframework.transaction.annotation.Propagation;
  85. import org.springframework.transaction.annotation.Transactional;
  86.  
  87. import com.infy.constants.SignUpConstants;
  88. import com.infy.dto.UserDTO;
  89. import com.infy.mapper.UserMapper;
  90.  
  91. @Service
  92.  
  93. public class UserServiceImpl implements IUserService {
  94.  
  95. @Autowired
  96. UserMapper userMapper;
  97.  
  98. @Override
  99. @Transactional(propagation = Propagation.REQUIRED, timeout = 1, isolation = Isolation.READ_COMMITTED,rollbackFor=Exception.class)
  100. public String addUser(UserDTO userDto) throws Exception {
  101.  
  102. // step 1: validate it
  103. // Step 2: check if the user id is not already exist;
  104. // Step 3: If user Id does not exist then insert it into the db
  105. // step 4: If exist return false;
  106. // step 5: something wrong then roll back
  107.  
  108. if (validateUser(userDto)) {
  109.  
  110.  
  111. if (!isUserExist(userDto)) {
  112.  
  113. //Date date=new Date();
  114. Date date=new Date();
  115.  
  116. userDto.setInsertDate(new Timestamp(date.getTime()));
  117. userDto.setLastUpdate(new Timestamp(date.getTime()));
  118. return Integer.toString(userMapper.addUser(userDto));
  119.  
  120. } else {
  121.  
  122. return SignUpConstants.USER_ALREADY_EXIST;
  123.  
  124. }
  125.  
  126. } else {
  127.  
  128. return SignUpConstants.VALIDATION_FALIURE;
  129. }
  130.  
  131. }
  132.  
  133. public boolean validateUser(UserDTO userDto) {
  134.  
  135. if (userDto.getBirthDate() != null && userDto.getCity() != null && userDto.getEmail() != null
  136. && userDto.getFullName() != null && userDto.getMobileNo() != null && userDto.getGender() != null
  137. && userDto.getPassword() != null && userDto.getState() != null) {
  138. return true;
  139.  
  140. } else {
  141. return false;
  142.  
  143. }
  144.  
  145. }
  146.  
  147. @Transactional(propagation = Propagation.REQUIRED)
  148. public boolean isUserExist(UserDTO userDto)throws Exception {
  149.  
  150. if (userDto != null && userMapper.getExistingUser(userDto) > 0) {
  151.  
  152. return true;
  153.  
  154. // TODO Auto-generated catch block
  155.  
  156.  
  157.  
  158. }
  159.  
  160. return false;
  161. }
  162.  
  163. }
  164.  
  165. package com.infy.mapper;
  166.  
  167. import java.util.List;
  168.  
  169. import org.apache.ibatis.annotations.Insert;
  170. import org.apache.ibatis.annotations.Options;
  171. import org.apache.ibatis.annotations.Select;
  172.  
  173. import com.infy.dto.UserDTO;
  174.  
  175. public interface UserMapper {
  176.  
  177. @Insert("INSERT INTO n_users (full_name,gender,birth_date,mobile_no,email,password,city,state,insert_date,last_update) VALUES (#{fullName},#{gender},#{birthDate},#{mobileNo},#{email},#{password},#{city},#{state},#{insertDate},#{lastUpdate})")
  178. public int addUser(UserDTO userDto) throws Exception;
  179.  
  180. @Select("Select count(*) from n_users where email=#{email}")
  181. public int getExistingUser(UserDTO userDto);
  182.  
  183.  
  184.  
  185.  
  186. }
  187.  
  188. <beans xmlns="http://www.springframework.org/schema/beans"
  189. xmlns:context="http://www.springframework.org/schema/context"
  190. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  191. xsi:schemaLocation="
  192. http://www.springframework.org/schema/beans
  193. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  194. http://www.springframework.org/schema/context
  195. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  196. http://www.springframework.org/schema/mvc
  197. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  198. http://www.springframework.org/schema/tx
  199. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  200. http://www.springframework.org/schema/aop
  201. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
  202. xmlns:aop="http://www.springframework.org/schema/aop"
  203. xmlns:tx="http://www.springframework.org/schema/tx">
  204.  
  205. <!-- <context:annotation-config /> -->
  206. <!-- Enable Annotation based Declarative Transaction Management -->
  207.  
  208.  
  209. <context:component-scan base-package="com.infy" />
  210.  
  211. <mvc:annotation-driven />
  212. <tx:annotation-driven transaction-manager="transactionManager" />
  213. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  214. <property name="dataSource" ref="dataSource" />
  215. </bean>
  216. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  217. <property name="dataSource" ref="dataSource" />
  218. <property name="typeAliasesPackage" value="com.infy.dto"/>
  219. <property name="mapperLocations" value="classpath*:com/infy/mapper/*.xml" />
  220. </bean>
  221. <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  222. <constructor-arg index="0" ref="sqlSessionFactory" />
  223. </bean>
  224. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  225. <property name="basePackage" value="com.infy.mapper" />
  226. </bean>
  227.  
  228. <mvc:interceptors>
  229. <bean class="com.infy.interceptors.RequestHandlerInterceptor" />
  230. </mvc:interceptors>
  231.  
  232. <bean id="dataSource"
  233. class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
  234.  
  235. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  236. <property name="url" value="jdbc:mysql://localhost:3306/sample" />
  237. <property name="username" value="**" />
  238. <property name="password" value="**" />
  239. </bean>
  240.  
  241. </beans>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement