PageRenderTime 73ms CodeModel.GetById 34ms RepoModel.GetById 2ms app.codeStats 0ms

/app/vHMS/Facade/AppointmentService.java

https://gitlab.com/MaheshDe/VHMS
Java | 637 lines | 416 code | 71 blank | 150 comment | 85 complexity | 2c2f76e5ab1a9187383d4ad8b92e4589 MD5 | raw file
  1. package vHMS.Facade;
  2. import java.lang.reflect.Type;
  3. import java.util.Date;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.apache.commons.beanutils.BeanUtilsBean;
  8. import org.modelmapper.ModelMapper;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Service;
  11. import org.springframework.transaction.annotation.Transactional;
  12. import vHMS.Core.Models.Appointment;
  13. import vHMS.Core.ViewModels.AppointmentDetailsViewModel;
  14. import vHMS.Core.ViewModels.AppointmentViewModel;
  15. import vHMS.Core.ViewModels.NotificationViewModel;
  16. import vHMS.Core.ViewModels.ReportViewModel;
  17. import vHMS.Core.ViewModels.Response;
  18. import vHMS.Core.ViewModels.ResponseViewModel;
  19. import Base.Data.IGenericMongoDao;
  20. import Base.Utilities.CommonUtils;
  21. import Base.Utilities.HelperUtil;
  22. import Base.Utilities.NullAwareBeanUtilsBean;
  23. import Base.Utilities.Report.ReportConstants;
  24. import Base.Utilities.Report.ReportProcessor;
  25. import Base.Utilities.Report.ReportUtils;
  26. import com.google.common.reflect.TypeToken;
  27. import com.google.gson.Gson;
  28. import com.mongodb.BasicDBList;
  29. import com.mongodb.BasicDBObject;
  30. import com.mongodb.BasicDBObjectBuilder;
  31. /**
  32. * The AppointmentService is a Class containing methods like create, update
  33. * delete etc.. related to Appointment , and implements IAppointmentService
  34. *
  35. *
  36. * @author ShunmugaRajaG
  37. * @version 1.0
  38. */
  39. @Service
  40. @Transactional
  41. public class AppointmentService implements IAppointmentService {
  42. // created instance using DI.
  43. @Autowired
  44. private IGenericMongoDao genericDAO;
  45. // Created NullAwareBeanUtilsBean instance.
  46. private BeanUtilsBean notNull = new NullAwareBeanUtilsBean();
  47. // Created HelperUtil instance.
  48. private HelperUtil helperUtil = new HelperUtil();
  49. private ModelMapper modelmapper = new ModelMapper();
  50. /*
  51. * Implementation of interface IAppointmentService all method which returns
  52. * a list of all appointment.
  53. */
  54. public Response all(AppointmentViewModel viewModel) throws Exception {
  55. Response response = new Response();
  56. try {
  57. // Condition to check whether where attribute is empty
  58. // To check for search criteria
  59. if (viewModel.where != null) {
  60. // for appending the where and the like for search criteria
  61. viewModel.where = helperUtil
  62. .ConstructWhereClauseFromJson(viewModel.where);
  63. }
  64. List<Appointment> list = null;
  65. if (viewModel != null) {
  66. list = genericDAO.all(viewModel.entityObject,
  67. Appointment.class, viewModel.where, viewModel.limit,
  68. viewModel.skip);
  69. }
  70. // Checking whether the list is empty or not
  71. if (null != list && list.size() > 0) {
  72. // Define the target type
  73. Type targetListType = new TypeToken<List<AppointmentViewModel>>() {
  74. }.getType();
  75. // Mapping the list with the target view model
  76. List<AppointmentViewModel> lists = modelmapper.map(list,
  77. targetListType);
  78. response.success = true;
  79. response.message = CommonUtils
  80. .getMessage("success.fetchAppointmentdata");
  81. // appending the list to the response viewModels list to didplay
  82. // in the UI
  83. response.ViewModels = lists;
  84. } else {
  85. response.success = false;
  86. response.message = CommonUtils
  87. .getMessage("error.fetchAppointmentdata");
  88. }
  89. } catch (Exception e) {
  90. // error in the database or connection
  91. response.success = false;
  92. response.message = CommonUtils.getMessage("error.processfailure");
  93. if (null != e.getMessage()) {
  94. response.ExceptionMessage = e.getMessage();
  95. } else {
  96. response.ExceptionMessage = e.toString();
  97. }
  98. }
  99. return response;
  100. }
  101. /*
  102. * Implementation of interface IAppointmentService create method which is
  103. * used to create/save an Appointment.
  104. */
  105. public Response create(AppointmentViewModel viewModel) throws Exception {
  106. Response response = new Response();
  107. String id = null;
  108. ResponseViewModel responseViewModel = new ResponseViewModel();
  109. try {
  110. if (viewModel != null && !checkAppointment(viewModel)) {
  111. // Calling the helper class to set the mandatory properties of
  112. // the
  113. // view model
  114. viewModel = helperUtil.AttachCommonFields(viewModel,
  115. AppointmentViewModel.class);
  116. // getting the source and target to set the properties
  117. Appointment appointment = modelmapper.map(viewModel,
  118. Appointment.class);
  119. // saving the data with the pojo and collection name
  120. // to get the ID for the saved item
  121. Object[] obj = genericDAO.save(viewModel.entityObject,
  122. appointment);
  123. // to sms to patient contact number
  124. NotificationViewModel notificationViewModel = new NotificationViewModel();
  125. notificationViewModel.setNotificationType("sms");
  126. notificationViewModel.setTo(viewModel.patientContactNumber
  127. + "," + viewModel.userContactNumber);
  128. notificationViewModel.setBody(CommonUtils
  129. .getMessage("sms.message.appointment.creation"));
  130. responseViewModel.smsNotificationViewModel = notificationViewModel;
  131. for (Object objId : obj) {
  132. id = String.valueOf(objId);
  133. appointment.setId(id);
  134. }
  135. responseViewModel.viewModel = modelmapper.map(appointment,
  136. AppointmentViewModel.class);
  137. response.success = true;
  138. response.message = CommonUtils
  139. .getMessage("success.createAppointment");
  140. // Binding the created data to the view model in the response
  141. response.ViewModel = responseViewModel;
  142. } else {
  143. response.success = false;
  144. response.message = CommonUtils
  145. .getMessage("error.createAppointment");
  146. }
  147. } catch (Exception e) {
  148. // return false if there is any database connection problem
  149. response.success = false;
  150. response.message = CommonUtils.getMessage("error.processfailure");
  151. if (null != e.getMessage()) {
  152. response.ExceptionMessage = e.getMessage();
  153. } else {
  154. response.ExceptionMessage = e.toString();
  155. }
  156. }
  157. return response;
  158. }
  159. /*
  160. * Implementation of interface IAppointmentService update method which is
  161. * used to update the Appointment details with the given id.
  162. */
  163. @SuppressWarnings("unused")
  164. public Response update(AppointmentViewModel viewModel) throws Exception {
  165. Response response = new Response();
  166. ResponseViewModel responseViewModel = new ResponseViewModel();
  167. try {
  168. // Calling the helper class to set the mandatory properties of the
  169. // view model
  170. viewModel = helperUtil.AttachCommonFields(viewModel,
  171. AppointmentViewModel.class);
  172. // Mapping the viewmodel to class of type
  173. Appointment appointment = modelmapper.map(viewModel,
  174. Appointment.class);
  175. // getting the id from the view model for updating for the specific
  176. // id
  177. Map<String, Object> map = new HashMap<String, Object>();
  178. map.put("code", appointment.getCode());
  179. Appointment appointmentdata = genericDAO.findOneByAttribute(
  180. viewModel.entityObject, map, Appointment.class);
  181. if (appointmentdata != null) {
  182. // override the old properties with new properties exists in the
  183. // right side object
  184. notNull.copyProperties(appointmentdata, appointment);
  185. // Updating the record by ID
  186. genericDAO.updateById(viewModel.entityObject, appointmentdata,
  187. appointmentdata.getId());
  188. AppointmentViewModel data = modelmapper.map(appointmentdata,
  189. AppointmentViewModel.class);
  190. response.success = true;
  191. response.message = CommonUtils
  192. .getMessage("success.updateAppointmentdata");
  193. // sending the response object as view model
  194. responseViewModel.viewModel = data;
  195. NotificationViewModel notificationViewModel = new NotificationViewModel();
  196. notificationViewModel.setNotificationType("sms");
  197. notificationViewModel.setTo(data.patientContactNumber + ","
  198. + viewModel.userContactNumber);
  199. notificationViewModel.setBody(CommonUtils
  200. .getMessage("sms.message.appointment.updation"));
  201. responseViewModel.smsNotificationViewModel = notificationViewModel;
  202. response.ViewModel = responseViewModel;
  203. } else {
  204. response.success = false;
  205. response.message = CommonUtils
  206. .getMessage("error.findAppointmentdata");
  207. }
  208. } catch (Exception e) {
  209. response.success = false;
  210. response.message = CommonUtils.getMessage("error.processfailure");
  211. if (null != e.getMessage()) {
  212. response.ExceptionMessage = e.getMessage();
  213. } else {
  214. response.ExceptionMessage = e.toString();
  215. }
  216. }
  217. return response;
  218. }
  219. /*
  220. * Implementation of interface IAppointmentService findAllByAttributes
  221. * method which fetches all the record with the given criteria.
  222. */
  223. public Response findAllByAttributes(AppointmentViewModel viewModel)
  224. throws Exception {
  225. Response response = new Response();
  226. int count = 0;
  227. try {
  228. if (viewModel != null) {
  229. // Converting MasterViewModel object to MasterEntity Pojo
  230. Appointment data = modelmapper
  231. .map(viewModel, Appointment.class);
  232. Gson gson = new Gson();
  233. // Converting MasterEntity to a JSON string
  234. String master = gson.toJson(data);
  235. Map<String, Object> map = gson.fromJson(master,
  236. new TypeToken<HashMap<String, Object>>() {
  237. }.getType());
  238. // changing query -- appointmentstartdatetime greater and equal
  239. // than input date
  240. if (null != viewModel.userCodes
  241. && viewModel.userCodes.length > 0) {
  242. map.put("userCode",
  243. BasicDBObjectBuilder.start("$in",
  244. viewModel.userCodes).get());
  245. }
  246. if (map.containsKey("appointmentStartDateTime")) {
  247. map.put("appointmentStartDateTime", BasicDBObjectBuilder
  248. .start("$gte", viewModel.appointmentStartDateTime)
  249. .get());
  250. }
  251. // changing query -- appointmentenddatetime less and equal than
  252. // input date
  253. if (map.containsKey("appointmentEndDateTime")) {
  254. map.put("appointmentEndDateTime", BasicDBObjectBuilder
  255. .start("$lte", viewModel.appointmentEndDateTime)
  256. .get());
  257. }
  258. if (null != viewModel.specialityCode
  259. && viewModel.specialityCode != ""
  260. && viewModel.specialityCode.trim().length() > 0) {
  261. /*
  262. * map.put("json.specialityCode",
  263. * BasicDBObjectBuilder.start("$match",
  264. * viewModel.specialityCode).get());
  265. */
  266. // elemMatchObj.put("$elemMatch", new
  267. // BasicDBObject("device", "10.10.20.2"));
  268. /*
  269. * map.put("json",
  270. * java.util.regex.Pattern.compile("specialityCode:" +
  271. * viewModel.specialityCode));
  272. */
  273. /*
  274. * BasicDBObject elemMatchObj = new BasicDBObject();
  275. * elemMatchObj.put("", new BasicDBObject("device",
  276. * "10.10.20.2"));
  277. */
  278. map.put("json.specialityCode", viewModel.specialityCode);
  279. }
  280. BasicDBObject orderBy= new BasicDBObject("appointmentStartDateTime",-1);
  281. List<Appointment> list = genericDAO.findAllByAttributes(
  282. viewModel.entityObject, map, Appointment.class,
  283. viewModel.limit, viewModel.skip,orderBy);
  284. // get count of records based on the filter
  285. if (viewModel.limit != null && null != list) {
  286. if (Integer.parseInt(viewModel.limit) >= list.size()) {
  287. count = genericDAO.count(viewModel.entityObject, map,
  288. Appointment.class);
  289. } else {
  290. count = list.size();
  291. }
  292. }
  293. // getting The list appointment by findbyQuery method and
  294. // passing query as param
  295. /*
  296. * List<Appointment> list = genericDAO.findByQuery(
  297. * viewModel.entityObject,contructQuery(map,viewModel),
  298. * Appointment.class, viewModel.limit, viewModel.skip);
  299. */
  300. // ModelMapper modelmapper = new ModelMapper();
  301. // Define the target type
  302. Type targetListType = new TypeToken<List<AppointmentViewModel>>() {
  303. }.getType();
  304. // Mapping the list with the target view model
  305. List<AppointmentViewModel> lists = modelmapper.map(list,
  306. targetListType);
  307. // Checking whether the list is empty or not
  308. if (null != lists && lists.size() > 0) {
  309. response.success = true;
  310. response.message = CommonUtils
  311. .getMessage("success.fetchAppointmentdata");
  312. response.ViewModel = count;
  313. // appending the list to the response viewModels list to
  314. // display
  315. // in the UI
  316. response.ViewModels = lists;
  317. } else {
  318. // List is empty response is false
  319. response.success = false;
  320. response.message = CommonUtils
  321. .getMessage("error.fetchAppointmentdata");
  322. }
  323. }
  324. } catch (Exception e) {
  325. // error in the database or connection
  326. response.success = false;
  327. response.message = CommonUtils.getMessage("error.processfailure");
  328. if (null != e.getMessage()) {
  329. response.ExceptionMessage = e.getMessage();
  330. } else {
  331. response.ExceptionMessage = e.toString();
  332. }
  333. }
  334. return response;
  335. }
  336. /*
  337. * Implementation of interface IAppointmentService findByCode method which
  338. * is used to find an appointment using code(primary key).
  339. *
  340. * @see vHMS.Facade.IAppointmentService#findByCode(vHMS.Core.Models.
  341. * AppointmentViewModel
  342. */
  343. public Response findByCode(AppointmentViewModel viewModel) throws Exception {
  344. Response response = new Response();
  345. try {
  346. // To find where the record exists
  347. Appointment appointmentexist = null;
  348. Map<String, Object> map = new HashMap<String, Object>();
  349. if (viewModel != null) {
  350. map.put("code", viewModel.code);
  351. // get the patient object by passing code
  352. appointmentexist = genericDAO.findOneByAttribute(
  353. CommonUtils.getMessage("Appointment"), map,
  354. Appointment.class);
  355. }
  356. if (null != appointmentexist) {
  357. AppointmentViewModel appData = modelmapper.map(
  358. appointmentexist, AppointmentViewModel.class);
  359. response.success = true;
  360. // if the record exists bind to the view model
  361. response.ViewModel = appData;
  362. response.message = CommonUtils
  363. .getMessage("success.findPatientdata");
  364. } else {
  365. response.success = false;
  366. response.message = CommonUtils
  367. .getMessage("error.findPatientdata");
  368. }
  369. } catch (Exception e) {
  370. // return false if there is any database connection problem
  371. response.success = false;
  372. response.message = CommonUtils.getMessage("error.processfailure");
  373. if (null != e.getMessage()) {
  374. response.ExceptionMessage = e.getMessage();
  375. } else {
  376. response.ExceptionMessage = e.toString();
  377. }
  378. }
  379. return response;
  380. }
  381. /*
  382. * Implementation of interface IUserService findByIdUser method which is
  383. * used to find an User using id(primary key).
  384. *
  385. * @see
  386. * HPFCR.Facade.IUserService#findById(HPFCR.Core.ViewModels.UserViewModel )
  387. */
  388. public Response appointmentReport(ReportViewModel reportViewModel)
  389. throws Exception {
  390. Response response = new Response();
  391. try {
  392. reportViewModel.templateFileName = CommonUtils
  393. .getMessage("report.appointment.templateName");
  394. reportViewModel.reportProcessor = new ReportProcessor();
  395. reportViewModel.ModelClass = AppointmentDetailsViewModel.class;
  396. reportViewModel.reportDatasource = ReportUtils.getDataSource(null,
  397. null);
  398. reportViewModel.imageFileLocation = CommonUtils
  399. .getApplicationAbsolutePath() + "/public/jasper/";
  400. reportViewModel.templateFileLocation = CommonUtils
  401. .getApplicationAbsolutePath() + "/public/jasper/";
  402. reportViewModel.reportOutputFileName = reportViewModel.templateFileName
  403. + new Date()
  404. + ReportConstants.DOT
  405. + reportViewModel.reportOutputFormat.toLowerCase();
  406. Map<String, Object> parameters = new HashMap<String, Object>();
  407. // parameters.put("userList", userList);
  408. // parameters.put("loggedonUser", "TEST USER");
  409. reportViewModel.reportParametersMap = parameters;
  410. response = reportViewModel.reportProcessor
  411. .executeReport(reportViewModel);
  412. // response.success = true;
  413. response.message = CommonUtils
  414. .getMessage("success.report.generated");
  415. } catch (Exception e) {
  416. // return false if there is any database connection problem
  417. response.success = false;
  418. response.message = CommonUtils.getMessage("error.processfailure");
  419. if (null != e.getMessage()) {
  420. response.ExceptionMessage = e.getMessage();
  421. } else {
  422. response.ExceptionMessage = e.toString();
  423. }
  424. }
  425. return response;
  426. }
  427. /*
  428. * Implementation of interface IAppointmentService findAllByAttributes
  429. * method which fetches all the record with the given criteria.
  430. */
  431. private boolean checkAppointment(AppointmentViewModel viewModel)
  432. throws Exception {
  433. boolean appointmentExist = false;
  434. try {
  435. if (viewModel != null) {
  436. // Converting MasterViewModel object to MasterEntity Pojo
  437. Appointment data = modelmapper
  438. .map(viewModel, Appointment.class);
  439. Gson gson = new Gson();
  440. // Converting MasterEntity to a JSON string
  441. String master = gson.toJson(data);
  442. // Converting JON string to a HashMap
  443. Map<String, Object> map = new Gson().fromJson(master,
  444. new TypeToken<HashMap<String, Object>>() {
  445. }.getType());
  446. Map<String, Object> newMap = new HashMap<String, Object>();
  447. // changing query -- appointmentstartdatetime greater and equal
  448. // than input date
  449. /* if (map.containsKey("appointmentStartDateTime")) {
  450. newMap.put("appointmentStartDateTime", BasicDBObjectBuilder
  451. .start("$gte", viewModel.appointmentStartDateTime)
  452. .get());
  453. }
  454. // changing query -- appointmentenddatetime less and equal than
  455. // input date
  456. if (map.containsKey("appointmentEndDateTime")) {
  457. newMap.put("appointmentEndDateTime", BasicDBObjectBuilder
  458. .start("$lte", viewModel.appointmentEndDateTime)
  459. .get());
  460. }*/
  461. BasicDBList ORList = new BasicDBList();
  462. //checking the less than and greater than appointmentStartDateTime
  463. if (null != viewModel.appointmentStartDateTime) {
  464. BasicDBList list = new BasicDBList();
  465. BasicDBObject startobj = new BasicDBObject("appointmentStartDateTime",
  466. BasicDBObjectBuilder.start("$lte",
  467. viewModel.appointmentStartDateTime).get());
  468. list.add(startobj);
  469. BasicDBObject endobj = new BasicDBObject("appointmentEndDateTime",
  470. BasicDBObjectBuilder.start("$gte",
  471. viewModel.appointmentStartDateTime).get());
  472. list.add(endobj);
  473. ORList.add(new BasicDBObject("$and", list));
  474. }
  475. //checking the less than and greater than appointmentEndDateTime
  476. if (null != viewModel.appointmentEndDateTime) {
  477. BasicDBList list = new BasicDBList();
  478. BasicDBObject startobj = new BasicDBObject("appointmentStartDateTime",
  479. BasicDBObjectBuilder.start("$lte",
  480. viewModel.appointmentEndDateTime).get());
  481. list.add(startobj);
  482. BasicDBObject endobj = new BasicDBObject("appointmentEndDateTime",
  483. BasicDBObjectBuilder.start("$gte",
  484. viewModel.appointmentEndDateTime).get());
  485. list.add(endobj);
  486. ORList.add(new BasicDBObject("$and", list));
  487. }
  488. //checking greater than appointmentStartDateTime and less than appointmentEndDateTime
  489. if (null != viewModel.appointmentEndDateTime && null != viewModel.appointmentStartDateTime)
  490. {
  491. BasicDBList list = new BasicDBList();
  492. BasicDBObject startobj = new BasicDBObject("appointmentStartDateTime",
  493. BasicDBObjectBuilder.start("$gte",
  494. viewModel.appointmentStartDateTime).get());
  495. list.add(startobj);
  496. BasicDBObject endobj = new BasicDBObject("appointmentEndDateTime",
  497. BasicDBObjectBuilder.start("$lte",
  498. viewModel.appointmentEndDateTime).get());
  499. list.add(endobj);
  500. ORList.add(new BasicDBObject("$and", list));
  501. }
  502. //putting into map.
  503. if (ORList.size() > 0) {
  504. newMap.put("$or", ORList);
  505. }
  506. if (map.containsKey("userCode")) {
  507. newMap.put("userCode", map.get("userCode"));
  508. }
  509. if (map.containsKey("status")) {
  510. newMap.put("status", map.get("status"));
  511. }
  512. if (map.containsKey("appointmentStatus")) {
  513. newMap.put("appointmentStatus",
  514. map.get("appointmentStatus"));
  515. }
  516. List<Appointment> list = genericDAO.findAllByAttributes(
  517. viewModel.entityObject, newMap, Appointment.class,
  518. viewModel.limit, viewModel.skip);
  519. if (list != null && !list.isEmpty()) {
  520. appointmentExist = true;
  521. }
  522. }
  523. } catch (Exception e) {
  524. appointmentExist = true;
  525. }
  526. return appointmentExist;
  527. }
  528. /*
  529. * Implementation of interface IAppointmentService findAllByAttributes
  530. * method which fetches all the record with the given criteria.
  531. */
  532. public Response checkAppointmentAvailablity(AppointmentViewModel viewModel)
  533. throws Exception {
  534. Response response = new Response();
  535. int count = 0;
  536. try {
  537. if (viewModel != null) {
  538. if (!checkAppointment(viewModel)) {
  539. response.success = true;
  540. response.message = CommonUtils
  541. .getMessage("success.availableBooking");
  542. response.ViewModel = count;
  543. // appending the list to the response viewModels list to
  544. // display
  545. // in the UI
  546. response.ViewModels = null;
  547. } else {
  548. // List is empty response is false
  549. response.success = false;
  550. response.message = CommonUtils
  551. .getMessage("error.AlreadyBooked");
  552. }
  553. }
  554. } catch (Exception e) {
  555. // error in the database or connection
  556. response.success = false;
  557. response.message = CommonUtils.getMessage("error.processfailure");
  558. if (null != e.getMessage()) {
  559. response.ExceptionMessage = e.getMessage();
  560. } else {
  561. response.ExceptionMessage = e.toString();
  562. }
  563. }
  564. return response;
  565. }
  566. }