/TutorTrackServices/src/main/java/com/tutortrack/api/service/impl/TutorTrackServiceImpl.java

https://gitlab.com/kundan987/TutorTrack · Java · 247 lines · 212 code · 30 blank · 5 comment · 16 complexity · fbadcae4d5c664fa907d67dafec6addf MD5 · raw file

  1. package com.tutortrack.api.service.impl;
  2. import java.security.NoSuchAlgorithmException;
  3. import java.security.SecureRandom;
  4. import java.util.Random;
  5. import org.apache.commons.lang3.math.NumberUtils;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.dao.DuplicateKeyException;
  10. import org.springframework.stereotype.Service;
  11. import com.tutortrack.api.dao.ITutorTrackServiceDao;
  12. import com.tutortrack.api.datastructure.TutorAggregateRating;
  13. import com.tutortrack.api.datastructure.TutorClassSubject;
  14. import com.tutortrack.api.datastructure.TutorReviewRating;
  15. import com.tutortrack.api.dto.StudentInfo;
  16. import com.tutortrack.api.dto.TutorInfo;
  17. import com.tutortrack.api.exception.ErrorCode;
  18. import com.tutortrack.api.exception.TutorTrackException;
  19. import com.tutortrack.api.request.UserInfoRequest;
  20. import com.tutortrack.api.request.ValidateNumberRequest;
  21. import com.tutortrack.api.response.StudentBasicResponse;
  22. import com.tutortrack.api.response.TutorBasicResponse;
  23. import com.tutortrack.api.response.TutorTrackResponse;
  24. import com.tutortrack.api.response.UserInfoResponse;
  25. import com.tutortrack.api.response.ValidateNumberResponse;
  26. import com.tutortrack.api.service.ITutorTrackService;
  27. /*
  28. * Author : Kundan Mishra
  29. * Date : 08-June-2016
  30. */
  31. @Service("tutorTrackService")
  32. public class TutorTrackServiceImpl implements ITutorTrackService {
  33. @Autowired
  34. ITutorTrackServiceDao tutorTrackServiceDao;
  35. private static final Logger LOG = LoggerFactory.getLogger(TutorTrackServiceImpl.class);
  36. @Override
  37. public void sendOtp(String mobileNumber) {
  38. // TODO Auto-generated method stub
  39. }
  40. @Override
  41. public ValidateNumberResponse validateNumber(ValidateNumberRequest request) {
  42. LOG.info(request.toString());
  43. ValidateNumberResponse response = new ValidateNumberResponse();
  44. response.setValidated(true);
  45. return response;
  46. }
  47. @Override
  48. public TutorBasicResponse createTutorInfo(TutorInfo tutorInfo) {
  49. TutorBasicResponse response = new TutorBasicResponse();
  50. TutorAggregateRating tutorAggregateRating = new TutorAggregateRating();
  51. TutorTrackResponse ttresponse = new TutorTrackResponse();
  52. try{
  53. String tutorId = generateTutorId();
  54. tutorInfo.setTutorId(tutorId);
  55. tutorTrackServiceDao.insertTutorInfo(tutorInfo);
  56. tutorAggregateRating.setAvgRating(0);
  57. tutorAggregateRating.setNumReviews(0);
  58. tutorAggregateRating.setTutorId(tutorInfo.getTutorId());
  59. ttresponse = createTutorAggregateRating(tutorAggregateRating);
  60. response.setEmail(tutorInfo.getEmail());
  61. response.setMobile(tutorInfo.getMobile());
  62. response.setName(tutorInfo.getName());
  63. response.setTutorId(tutorId);
  64. response.setOtpCode(generateOtp());
  65. if(!ttresponse.getStatus().equalsIgnoreCase("SUCCESS")){
  66. response.setStatus("FAIL");
  67. }
  68. }catch (DuplicateKeyException ex) {
  69. throw new TutorTrackException(ErrorCode.DUPLICATE_KEY_EXCEPTION,"Mobile/Email is already registered.");
  70. }catch (Exception ex) {
  71. throw new TutorTrackException(ErrorCode.ERROR_INSERTING_TUTOR_INFO);
  72. }
  73. return response;
  74. }
  75. private String generateTutorId() {
  76. String tutorId = null;
  77. try {
  78. SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");
  79. Integer randNumber = new Integer(prng.nextInt());
  80. if (randNumber < 0) {
  81. randNumber = randNumber * (-1);
  82. }
  83. tutorId = "T"+randNumber.toString();
  84. } catch (NoSuchAlgorithmException ex) {
  85. LOG.error(ex.toString());
  86. }
  87. return tutorId;
  88. }
  89. private Integer generateOtp() {
  90. Integer otpCode = null;
  91. Random random = new Random();
  92. otpCode = random.nextInt(9999 - 1000) + 999;
  93. return otpCode;
  94. }
  95. @Override
  96. public StudentBasicResponse createStudentInfo(StudentInfo studentInfo) {
  97. StudentBasicResponse response = new StudentBasicResponse();
  98. try{
  99. String studentId = generateStudentId();
  100. studentInfo.setStudentId(studentId);
  101. tutorTrackServiceDao.insertStudentInfo(studentInfo);
  102. response.setEmail(studentInfo.getEmail());
  103. response.setMobile(studentInfo.getMobile());
  104. response.setName(studentInfo.getName());
  105. response.setStudentId(studentId);
  106. response.setOtpCode(generateOtp());
  107. response.setStatus("SUCCESS");
  108. }catch (DuplicateKeyException ex) {
  109. throw new TutorTrackException(ErrorCode.DUPLICATE_KEY_EXCEPTION,"Mobile/Email is already registered.");
  110. }catch (Exception ex) {
  111. throw new TutorTrackException(ErrorCode.ERROR_INSERTING_STUDENT_INFO);
  112. }
  113. return response;
  114. }
  115. private String generateStudentId() {
  116. String studentId = null;
  117. try {
  118. SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");
  119. Integer randNumber = new Integer(prng.nextInt());
  120. if (randNumber < 0) {
  121. randNumber = randNumber * (-1);
  122. }
  123. studentId = "S"+randNumber.toString();
  124. } catch (NoSuchAlgorithmException ex) {
  125. LOG.error(ex.toString());
  126. }
  127. return studentId;
  128. }
  129. @Override
  130. public TutorTrackResponse createTutorClassSubjectMapping(TutorClassSubject tutorClassSubject) {
  131. TutorTrackResponse response = new TutorTrackResponse();
  132. try{
  133. tutorTrackServiceDao.insertTutorClassSubjectMapping(tutorClassSubject);
  134. response.setStatus("SUCCESS");
  135. }catch (DuplicateKeyException ex) {
  136. throw new TutorTrackException(ErrorCode.DUPLICATE_KEY_EXCEPTION,"Class Subject mapping already exists");
  137. }catch (Exception ex) {
  138. throw new TutorTrackException(ErrorCode.ERROR_INSERTING_TUTOR_CLASS_SUBJECT_MAPPING);
  139. }
  140. return response;
  141. }
  142. @Override
  143. public TutorTrackResponse createTutorReviewRatingMapping(TutorReviewRating tutorReviewRating) {
  144. TutorTrackResponse response = new TutorTrackResponse();
  145. Integer reviewPresent = 0;
  146. if(tutorReviewRating.getReview() != null){
  147. reviewPresent = 1;
  148. }
  149. try{
  150. Integer count = tutorTrackServiceDao.getTutorRatingCount(tutorReviewRating.getTutorId());
  151. tutorTrackServiceDao.insertTutorReviewRatingMapping(tutorReviewRating);
  152. tutorTrackServiceDao.updateTutorAggregateRating(reviewPresent, count, tutorReviewRating.getRating(), tutorReviewRating.getTutorId());
  153. response.setStatus("SUCCESS");
  154. }catch (DuplicateKeyException ex) {
  155. throw new TutorTrackException(ErrorCode.DUPLICATE_KEY_EXCEPTION,"Student Id review rating mapping already exists");
  156. }catch (Exception ex) {
  157. throw new TutorTrackException(ErrorCode.ERROR_INSERTING_TUTOR_REVIEW_RATING_MAPPING);
  158. }
  159. return response;
  160. }
  161. @Override
  162. public TutorTrackResponse createTutorAggregateRating(TutorAggregateRating tutorAggregateRating) {
  163. TutorTrackResponse response = new TutorTrackResponse();
  164. try{
  165. tutorTrackServiceDao.insertTutorAggregateRating(tutorAggregateRating);
  166. response.setStatus("SUCCESS");
  167. }catch (DuplicateKeyException ex) {
  168. throw new TutorTrackException(ErrorCode.DUPLICATE_KEY_EXCEPTION,"Tutor Aggregate Rating already exists");
  169. }catch (Exception ex) {
  170. throw new TutorTrackException(ErrorCode.ERROR_INSERTING_TUTOR_AGGREGATE_RATING);
  171. }
  172. return response;
  173. }
  174. @Override
  175. public UserInfoResponse getUserInfo(UserInfoRequest request) {
  176. UserInfoResponse response = new UserInfoResponse();
  177. String studentId = null;
  178. String tutorId = null;
  179. try {
  180. LOG.info(request.getMobileOrEmail());
  181. if (NumberUtils.isNumber(request.getMobileOrEmail())) {
  182. studentId = tutorTrackServiceDao.getStudentIdFromMobile(request.getMobileOrEmail());
  183. tutorId = tutorTrackServiceDao.getTutorIdFromMobile(request.getMobileOrEmail());
  184. } else {
  185. studentId = tutorTrackServiceDao.getStudentIdFromEmail(request.getMobileOrEmail());
  186. tutorId = tutorTrackServiceDao.getTutorIdFromEmail(request.getMobileOrEmail());
  187. }
  188. if (studentId != null) {
  189. StudentInfo studentInfo = tutorTrackServiceDao.getStudentInfo(studentId);
  190. if (studentInfo.getPassword().equalsIgnoreCase(request.getPassword())) {
  191. response.setStatus("SUCCESS");
  192. response.setMessage("Successfully Signed In");
  193. response.setStudentInfo(studentInfo);
  194. } else {
  195. response.setStatus("FAIL");
  196. response.setMessage("User/Password didn't match");
  197. }
  198. } else if (tutorId != null) {
  199. TutorInfo tutorInfo = tutorTrackServiceDao.getTutorInfo(tutorId);
  200. if (tutorInfo.getPassword().equalsIgnoreCase(request.getPassword())) {
  201. response.setStatus("SUCCESS");
  202. response.setMessage("Successfully Signed In");
  203. response.setTutorInfo(tutorInfo);
  204. } else {
  205. response.setStatus("FAIL");
  206. response.setMessage("User/Password didn't match");
  207. }
  208. }else {
  209. response.setStatus("FAIL");
  210. response.setMessage("You are not registered yet!.");
  211. }
  212. } catch (Exception e) {
  213. throw new TutorTrackException(ErrorCode.ERROR_SELECTING_USER_INFO);
  214. }
  215. return response;
  216. }
  217. }