PageRenderTime 47ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/app/vHMS/Facade/UserScheduleService.java

https://gitlab.com/MaheshDe/VHMS
Java | 567 lines | 358 code | 88 blank | 121 comment | 76 complexity | bf453b89afd78bbac596ccb3d402ad6b MD5 | raw file
  1. package vHMS.Facade;
  2. import java.io.IOException;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.apache.commons.beanutils.BeanUtilsBean;
  7. import org.modelmapper.ModelMapper;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Service;
  10. import play.db.ebean.Transactional;
  11. import vHMS.Core.Models.User;
  12. import vHMS.Core.Models.UserSchedule;
  13. import vHMS.Core.ViewModels.Response;
  14. import vHMS.Core.ViewModels.UserScheduleViewModel;
  15. import vHMS.Core.ViewModels.UserViewModel;
  16. import Base.Data.IGenericMongoDao;
  17. import Base.Utilities.CommonUtils;
  18. import Base.Utilities.HelperUtil;
  19. import Base.Utilities.NullAwareBeanUtilsBean;
  20. import Base.Utilities.Email.SmtpSender;
  21. import com.fasterxml.jackson.core.JsonProcessingException;
  22. import com.google.common.reflect.TypeToken;
  23. import com.google.gson.Gson;
  24. import com.mongodb.BasicDBList;
  25. import com.mongodb.BasicDBObject;
  26. import com.mongodb.BasicDBObjectBuilder;
  27. /**
  28. * The UserScheduleService is the implementation for the interface
  29. * IUserScheduleService.
  30. *
  31. * @author MaheshDe
  32. * @since 2015-08-21
  33. */
  34. @Service
  35. @Transactional
  36. public class UserScheduleService implements IUserScheduleService {
  37. @Autowired
  38. private IGenericMongoDao genericDAO;
  39. @Autowired
  40. private SmtpSender defaultEmailSender;
  41. // Created HelperUtil instance.
  42. private HelperUtil helperUtil = new HelperUtil();
  43. // Created ModelMapper instance
  44. private ModelMapper modelmapper = new ModelMapper();
  45. // Created NullAwareBeanUtilsBean instance.
  46. private BeanUtilsBean notNull = new NullAwareBeanUtilsBean();
  47. /**
  48. * Implementation of interface IUserScheduleService all method which returns
  49. * a list of User schedule data.
  50. *
  51. * @see vHMS.Facade.IUserScheduleService#all(vHMS.Core.Models.
  52. * UserScheduleViewModel
  53. */
  54. @Override
  55. public Response all(UserScheduleViewModel viewModel)
  56. throws JsonProcessingException, IOException {
  57. // TODO Auto-generated method stub
  58. Response response = new Response();
  59. // Fetch all the records from the table.
  60. List<UserSchedule> allUserSchedule = null;
  61. if (viewModel.where != null) {
  62. // for appending the where and the like for search criteria
  63. viewModel.where = helperUtil
  64. .ConstructWhereClauseFromJson(viewModel.where);
  65. }
  66. allUserSchedule = genericDAO.all(
  67. CommonUtils.getMessage("Userschedule"), UserSchedule.class,
  68. viewModel.where, viewModel.limit, viewModel.skip);
  69. try {
  70. // to check whether there is a list of entity returns true response
  71. if (null != allUserSchedule && allUserSchedule.size() > 0) {
  72. response.success = true;
  73. response.message = CommonUtils
  74. .getMessage("success.userSchedule.Criteria.search");
  75. response.ViewModels = allUserSchedule;
  76. } else {// Returns false response
  77. response.success = false;
  78. response.message = CommonUtils
  79. .getMessage("error.userSchedule.Criteria.search");
  80. }
  81. } catch (Exception e) {
  82. response.success = false;
  83. response.message = CommonUtils.getMessage("error.processfailure");
  84. response.ExceptionMessage = e.getMessage();
  85. }
  86. return response;
  87. }
  88. /**
  89. * Implementation of interface IUserScheduleService create method which
  90. * saves/create a user schedule data.
  91. *
  92. * @see vHMS.Facade.IUserScheduleService#create(vHMS.Core.Models.
  93. * UserScheduleViewModel
  94. */
  95. @Override
  96. public Response create(UserScheduleViewModel viewModel) throws Exception {
  97. // TODO Auto-generated method stub
  98. Response response = new Response();
  99. UserSchedule userScheduleExist = null;
  100. String id = null;
  101. try {
  102. if (viewModel != null) {
  103. Map<String, Object> map = new HashMap<String, Object>();
  104. map.put("code", viewModel.userCode);
  105. User userExist = genericDAO.findOneByAttribute(
  106. CommonUtils.getMessage("User"), map, User.class);
  107. if (null != userExist) {
  108. map.clear();
  109. map.put("userCode", viewModel.userCode);
  110. /*
  111. * if (null != viewModel.startTime && viewModel.startTime !=
  112. * "" && viewModel.startTime.trim().length() > 0) {
  113. * map.put("startTime", BasicDBObjectBuilder .start("$gte",
  114. * viewModel.startTime) .get());
  115. *
  116. * }
  117. *
  118. * if (null != viewModel.endTime && viewModel.endTime != ""
  119. * && viewModel.endTime.trim().length() > 0) {
  120. * map.put("endTime", BasicDBObjectBuilder .start("$lte",
  121. * viewModel.endTime) .get());
  122. *
  123. * }
  124. */
  125. BasicDBList ORList = new BasicDBList();
  126. if (null != viewModel.startTime
  127. && viewModel.startTime != ""
  128. && viewModel.startTime.trim().length() > 0) {
  129. BasicDBList list = new BasicDBList();
  130. BasicDBObject startobj = new BasicDBObject("startTime",
  131. BasicDBObjectBuilder.start("$lte",
  132. viewModel.startTime).get());
  133. list.add(startobj);
  134. BasicDBObject endobj = new BasicDBObject("endTime",
  135. BasicDBObjectBuilder.start("$gte",
  136. viewModel.startTime).get());
  137. list.add(endobj);
  138. ORList.add(new BasicDBObject("$and", list));
  139. }
  140. if (null != viewModel.endTime && viewModel.endTime != ""
  141. && viewModel.endTime.trim().length() > 0) {
  142. BasicDBList list = new BasicDBList();
  143. BasicDBObject startobj = new BasicDBObject("startTime",
  144. BasicDBObjectBuilder.start("$lte",
  145. viewModel.endTime).get());
  146. list.add(startobj);
  147. BasicDBObject endobj = new BasicDBObject("endTime",
  148. BasicDBObjectBuilder.start("$gte",
  149. viewModel.endTime).get());
  150. list.add(endobj);
  151. ORList.add(new BasicDBObject("$and", list));
  152. }
  153. if (null != viewModel.endTime && viewModel.endTime != ""
  154. && viewModel.endTime.trim().length() > 0
  155. && null != viewModel.startTime
  156. && viewModel.startTime != ""
  157. && viewModel.startTime.trim().length() > 0) {
  158. BasicDBList list = new BasicDBList();
  159. BasicDBObject startobj = new BasicDBObject("startTime",
  160. BasicDBObjectBuilder.start("$gte",
  161. viewModel.startTime).get());
  162. list.add(startobj);
  163. BasicDBObject endobj = new BasicDBObject("endTime",
  164. BasicDBObjectBuilder.start("$lte",
  165. viewModel.endTime).get());
  166. list.add(endobj);
  167. ORList.add(new BasicDBObject("$and", list));
  168. }
  169. if (ORList.size() > 0) {
  170. map.put("$or", ORList);
  171. }
  172. map.put("day", viewModel.day);
  173. userScheduleExist = genericDAO.findOneByAttribute(
  174. CommonUtils.getMessage("Userschedule"), map,
  175. UserSchedule.class);
  176. if (null != userScheduleExist) {
  177. response.success = false;
  178. response.message = CommonUtils
  179. .getMessage("error.createUserSchedule.exists");
  180. } else {
  181. // Calling the helper class to set the mandatory
  182. // properties of
  183. // the view model
  184. viewModel = helperUtil.AttachCommonFields(viewModel,
  185. UserScheduleViewModel.class);
  186. // convert viewmodel object to model object before
  187. // saving it
  188. // to DB
  189. UserSchedule userSchedule = modelmapper.map(viewModel,
  190. UserSchedule.class);
  191. // Create the new record in table.
  192. Object[] obj = genericDAO.save(
  193. CommonUtils.getMessage("Userschedule"),
  194. userSchedule);
  195. for (Object objId : obj) {
  196. id = String.valueOf(objId);
  197. userSchedule.setId(id);
  198. }
  199. response.success = true;
  200. response.message = CommonUtils
  201. .getMessage("success.userSchedule.creation");
  202. response.ViewModel = modelmapper.map(userSchedule,
  203. UserScheduleViewModel.class);
  204. }
  205. } else {
  206. response.success = false;
  207. response.message = CommonUtils
  208. .getMessage("error.noUserData.exists");
  209. }
  210. }
  211. } catch (Exception e) {
  212. response.success = false;
  213. response.message = CommonUtils.getMessage("error.processfailure");
  214. response.ExceptionMessage = e.getMessage();
  215. }
  216. return response;
  217. }
  218. /**
  219. * Implementation of interface IUserScheduleService findById method which
  220. * search a User schedule data with the given code.
  221. *
  222. * @see vHMS.Facade.IUserScheduleService#findById(vHMS.Core.Models.
  223. * UserScheduleViewModel
  224. */
  225. @Override
  226. public Response findById(UserScheduleViewModel viewModel) throws Exception {
  227. // TODO Auto-generated method stub
  228. Response response = new Response();
  229. try {
  230. Map<String, Object> map = new HashMap<String, Object>();
  231. UserSchedule userSchedule = null;
  232. if (viewModel != null) {
  233. map.put("code", viewModel.code);
  234. // Fetch the record by code.
  235. userSchedule = genericDAO.findOneByAttribute(
  236. CommonUtils.getMessage("Userschedule"), map,
  237. UserSchedule.class);
  238. if (userSchedule != null) {
  239. UserScheduleViewModel userScheduleViewModel = modelmapper
  240. .map(userSchedule, UserScheduleViewModel.class);
  241. response.success = true;
  242. response.message = CommonUtils
  243. .getMessage("success.userSchedule.Criteria.search");
  244. response.ViewModel = userScheduleViewModel;
  245. } else {
  246. response.success = false;
  247. response.message = CommonUtils
  248. .getMessage("error.userSchedule.Criteria.search");
  249. }
  250. }
  251. } catch (Exception e) {
  252. response.success = false;
  253. response.message = CommonUtils.getMessage("error.processfailure");
  254. response.ExceptionMessage = e.getMessage();
  255. }
  256. return response;
  257. }
  258. /**
  259. * Implementation of interface IUserScheduleService count method which
  260. * returns number of user schedule data present in a particular table.
  261. *
  262. * @see vHMS.Facade.IUserScheduleService#count(vHMS.Core.Models.
  263. * UserScheduleViewModel
  264. */
  265. @Override
  266. public Response count(UserScheduleViewModel viewModel) {
  267. // TODO Auto-generated method stub
  268. Response response = new Response();
  269. try {
  270. // Condition to check whether where attribute is empty to check for
  271. // search criteria
  272. if (viewModel.where != null) {
  273. // for appending the where and the like for search criteria
  274. viewModel.where = helperUtil
  275. .ConstructWhereClauseFromJson(viewModel.where);
  276. }
  277. // Count of the record in the list or serach by criteria
  278. int count = genericDAO.count(viewModel.entityObject, User.class);
  279. // adding count to the json object
  280. response.data.put("count", count);
  281. response.success = true;
  282. response.message = CommonUtils
  283. .getMessage("success.countMasterdata");
  284. } catch (Exception e) {
  285. // Return false if it ia an error
  286. response.success = false;
  287. response.message = CommonUtils.getMessage("error.processfailure");
  288. if (null != e.getMessage()) {
  289. response.ExceptionMessage = e.getMessage();
  290. } else {
  291. response.ExceptionMessage = e.toString();
  292. }
  293. }
  294. return response;
  295. }
  296. /**
  297. * Implementation of interface IUserScheduleService delete method which
  298. * deletes a user schedule data for the given code.
  299. *
  300. * @see vHMS.Facade.IUserScheduleService#delete(vHMS.Core.Models.
  301. * UserScheduleViewModel
  302. */
  303. @Override
  304. public Response delete(UserScheduleViewModel viewModel) {
  305. // TODO Auto-generated method stub
  306. Response response = new Response();
  307. try {
  308. Map<String, Object> map = new HashMap<String, Object>();
  309. if (viewModel != null) {
  310. map.put("code", viewModel.code);
  311. // Fetch the record by code.
  312. UserSchedule user = genericDAO.findOneByAttribute(
  313. viewModel.entityObject, map, UserSchedule.class);
  314. // if record found then delete record and returns true
  315. if (user != null) {
  316. // Deleting the user with the code
  317. genericDAO.delete(viewModel.entityObject, user.getId(),
  318. User.class);
  319. UserScheduleViewModel userScheduleData = modelmapper.map(
  320. user, UserScheduleViewModel.class);
  321. response.success = true;
  322. response.message = CommonUtils
  323. .getMessage("success.user.data.deletion");
  324. response.ViewModel = userScheduleData;
  325. // response.status = true;
  326. } else {
  327. response.success = false;
  328. response.message = CommonUtils
  329. .getMessage("error.usercode.not.exists");
  330. }
  331. }
  332. } catch (Exception e) {
  333. response.success = false;
  334. response.message = CommonUtils.getMessage("error.processfailure");
  335. response.ExceptionMessage = e.getMessage();
  336. }
  337. return response;
  338. }
  339. /**
  340. * Implementation of interface IUserScheduleService update method which
  341. * updates a user schedule data for the given code.
  342. *
  343. * @see vHMS.Facade.IUserScheduleService#update(vHMS.Core.Models.
  344. * UserScheduleViewModel
  345. */
  346. @Override
  347. public Response update(UserScheduleViewModel viewModel) {
  348. // TODO Auto-generated method stub
  349. Response response = new Response();
  350. UserSchedule userScheduleData = null;
  351. try {
  352. if (viewModel != null) {
  353. Map<String, Object> map = new HashMap<String, Object>();
  354. map.put("code", viewModel.code);
  355. UserSchedule userScheduleExist = genericDAO.findOneByAttribute(
  356. CommonUtils.getMessage("Userschedule"), map,
  357. UserSchedule.class);
  358. map.clear();
  359. map.put("userCode", viewModel.userCode);
  360. if (null != viewModel.startTime && viewModel.startTime != ""
  361. && viewModel.startTime.trim().length() > 0) {
  362. map.put("startTime",
  363. BasicDBObjectBuilder.start("$gte",
  364. viewModel.startTime).get());
  365. }
  366. if (null != viewModel.endTime && viewModel.endTime != ""
  367. && viewModel.endTime.trim().length() > 0) {
  368. map.put("endTime",
  369. BasicDBObjectBuilder.start("$lte",
  370. viewModel.endTime).get());
  371. }
  372. map.put("day", viewModel.day);
  373. userScheduleData = genericDAO.findOneByAttribute(
  374. viewModel.entityObject, map, UserSchedule.class);
  375. if (null != userScheduleExist) {
  376. if (null != userScheduleData
  377. && !(userScheduleExist.getCode()
  378. .equals(viewModel.code))) {
  379. response.success = false;
  380. response.message = CommonUtils
  381. .getMessage("error.updateUserSchedule.exists");
  382. } else {
  383. // Calling the helper class to set the mandatory
  384. // properties of
  385. // the view model
  386. viewModel = helperUtil.AttachCommonFields(viewModel,
  387. UserScheduleViewModel.class);
  388. // Mapping the viewModel to the Model(User).
  389. UserSchedule userSchedule = modelmapper.map(viewModel,
  390. UserSchedule.class);
  391. // override the old properties with new properties
  392. // exists in the
  393. // right side object
  394. notNull.copyProperties(userScheduleExist, userSchedule);
  395. // Update the existing record with new values.
  396. genericDAO.updateById(viewModel.entityObject,
  397. userScheduleExist, userScheduleExist.getId());
  398. UserSchedule updatedUserSchedule = genericDAO
  399. .findOneById(
  400. CommonUtils.getMessage("Userschedule"),
  401. userScheduleExist.getId(),
  402. UserSchedule.class);
  403. response.success = true;
  404. response.ViewModel = modelmapper.map(
  405. updatedUserSchedule, UserViewModel.class);
  406. response.message = CommonUtils
  407. .getMessage("success.update.UserSchedule");
  408. }
  409. } else {
  410. response.success = false;
  411. response.message = CommonUtils
  412. .getMessage("error.nodata.exists");
  413. }
  414. }
  415. } catch (Exception e) {
  416. // Exception Handling
  417. response.success = false;
  418. response.message = CommonUtils.getMessage("error.processfailure");
  419. response.ExceptionMessage = e.getMessage();
  420. }
  421. return response;
  422. }
  423. /**
  424. * Implementation of interface IUserScheduleService findAllByAttributes
  425. * method which returs a list of User Schedule data based on search criteria
  426. *
  427. * @see
  428. * vHMS.Facade.IUserScheduleService#findAllByAttributes(vHMS.Core.Models
  429. * . UserScheduleViewModel
  430. */
  431. @SuppressWarnings("deprecation")
  432. @Override
  433. public Response findAllByAttributes(UserScheduleViewModel viewModel)
  434. throws Exception {
  435. // TODO Auto-generated method stub
  436. Response response = new Response();
  437. try {
  438. // Converting UserScheduleViewModel object to UserScheduleEntity
  439. // Pojo
  440. UserSchedule userScheduleData = modelmapper.map(viewModel,
  441. UserSchedule.class);
  442. Gson gson = new Gson();
  443. // Converting UserScheduleEntity to a JSON string
  444. String master = gson.toJson(userScheduleData);
  445. // Converting JSON string to a HashMap
  446. Map<String, Object> map = new Gson().fromJson(master,
  447. new TypeToken<HashMap<String, Object>>() {
  448. }.getType());
  449. if (viewModel.where != null) {
  450. // for appending the where and the like for search criteria
  451. viewModel.where = helperUtil
  452. .ConstructWhereClauseFromJson(viewModel.where);
  453. }
  454. // Fetch the Entity based on the search criteria
  455. List<UserSchedule> allUserScheduleEntity = genericDAO
  456. .findAllByAttributes(viewModel.entityObject, map,
  457. UserSchedule.class, viewModel.limit, viewModel.skip);
  458. // Returns true if there is a record matching the criteria
  459. if (null != allUserScheduleEntity
  460. && allUserScheduleEntity.size() > 0) {
  461. response.success = true;
  462. response.message = CommonUtils
  463. .getMessage("success.userSchedule.Criteria.search");
  464. response.ViewModels = allUserScheduleEntity;
  465. } else {
  466. response.success = false;
  467. response.message = CommonUtils
  468. .getMessage("error.userSchedule.Criteria.search");
  469. }
  470. } catch (Exception e) {
  471. // Handling Exception
  472. response.success = false;
  473. response.message = CommonUtils.getMessage("error.processfailure");
  474. // response.status = false;
  475. response.ExceptionMessage = e.getMessage();
  476. }
  477. return response;
  478. }
  479. }