PageRenderTime 2551ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/app/vHMS/Facade/FaqService.java

https://gitlab.com/MaheshDe/VHMS
Java | 353 lines | 237 code | 66 blank | 50 comment | 27 complexity | 12abc13009276bd4e6989637fb73929f MD5 | raw file
  1. package vHMS.Facade;
  2. import java.io.IOException;
  3. import java.lang.reflect.Type;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.regex.Pattern;
  8. import org.apache.commons.beanutils.BeanUtilsBean;
  9. import org.apache.commons.lang3.RandomStringUtils;
  10. import org.modelmapper.ModelMapper;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import vHMS.Core.Models.Appointment;
  15. import vHMS.Core.Models.Faq;
  16. import vHMS.Core.Models.MasterEntity;
  17. import vHMS.Core.Models.PatientRegistration;
  18. import vHMS.Core.Models.User;
  19. import vHMS.Core.ViewModels.AppointmentViewModel;
  20. import vHMS.Core.ViewModels.FaqViewModel;
  21. import vHMS.Core.ViewModels.MasterViewModel;
  22. import vHMS.Core.ViewModels.NotificationViewModel;
  23. import vHMS.Core.ViewModels.PatientRegistrationViewModel;
  24. import vHMS.Core.ViewModels.Response;
  25. import vHMS.Core.ViewModels.ResponseViewModel;
  26. import vHMS.Core.ViewModels.UserViewModel;
  27. import Base.Data.IGenericMongoDao;
  28. import Base.Utilities.CommonUtils;
  29. import Base.Utilities.HelperUtil;
  30. import Base.Utilities.NullAwareBeanUtilsBean;
  31. import Base.Utilities.Email.SmtpSender;
  32. import com.fasterxml.jackson.core.JsonProcessingException;
  33. import com.google.common.reflect.TypeToken;
  34. import com.google.gson.Gson;
  35. import com.google.gson.GsonBuilder;
  36. import com.mongodb.BasicDBList;
  37. import com.mongodb.BasicDBObject;
  38. import com.mongodb.DBObject;
  39. /**
  40. * The FaqService is the implementation for the interface IFaqService.
  41. *
  42. * @author
  43. * @since
  44. */
  45. @Service
  46. @Transactional
  47. public class FaqService implements IFaqService {
  48. @Autowired
  49. private IGenericMongoDao genericDAO;
  50. private HelperUtil helperUtil = new HelperUtil();
  51. private ModelMapper modelmapper = new ModelMapper();
  52. private BeanUtilsBean notNull = new NullAwareBeanUtilsBean();
  53. @Autowired
  54. private SmtpSender defaultEmailSender;
  55. @Override
  56. public Response all(FaqViewModel viewModel) throws JsonProcessingException, IOException, Exception {
  57. // TODO Auto-generated method stub
  58. Response response = new Response();
  59. try {
  60. // Condition to check whether where attribute is empty
  61. // To check for search criteria
  62. if (viewModel.where != null) {
  63. // for appending the where and the like for search criteria
  64. viewModel.where = helperUtil
  65. .ConstructWhereClauseFromJson(viewModel.where);
  66. }
  67. List<Faq> list = genericDAO.all(viewModel.entityObject,
  68. Faq.class, viewModel.where, viewModel.limit,
  69. viewModel.skip);
  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<FaqViewModel>>() {
  74. }.getType();
  75. // Mapping the list with the target view model
  76. List<FaqViewModel> lists = modelmapper.map(list,
  77. targetListType);
  78. response.success = true;
  79. response.message = CommonUtils
  80. .getMessage("success.fetchFaqdata");
  81. // appending the list to the response viewModels list to display
  82. // in the UI
  83. response.ViewModels = lists;
  84. } else {
  85. response.success = false;
  86. response.message = CommonUtils
  87. .getMessage("error.fetchFaqdata");
  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. @Override
  102. public Response create(FaqViewModel viewModel) throws Exception {
  103. Response response = new Response();
  104. String id= null;
  105. Map<String, Object> map = new HashMap<String, Object>();
  106. try {
  107. /*viewModel = helperUtil.AttachCommonFields(viewModel,
  108. FaqViewModel.class);*/
  109. map.put("question", Pattern.compile(viewModel.question, Pattern.CASE_INSENSITIVE));
  110. Faq faqexist = genericDAO.findOneByAttribute(
  111. viewModel.entityObject, map, Faq.class);
  112. if(null != faqexist){
  113. response.success = false;
  114. response.message = CommonUtils
  115. .getMessage("error.createFaq.exists");
  116. }else{
  117. //calling helper class
  118. viewModel = helperUtil.AttachCommonFields(viewModel,
  119. FaqViewModel.class);
  120. // getting the source and target to set the properties
  121. Faq faq = modelmapper.map(viewModel,
  122. Faq.class);
  123. // Create the new record in table.
  124. Object[] obj = genericDAO.save(viewModel.entityObject, faq);
  125. /*map.clear();
  126. map.put("code", viewModel.code);
  127. */
  128. Faq faqSave = genericDAO.findOneByAttribute(
  129. viewModel.entityObject, map, Faq.class);
  130. for (Object objId : obj) {
  131. id = String.valueOf(objId);
  132. faq.setId(id);;
  133. }
  134. response.success = true;
  135. response.message = CommonUtils
  136. .getMessage("success.entity.creation");
  137. response.ViewModel = faqSave;
  138. }
  139. } catch (Exception e) {// Handles Exception
  140. response.success = false;
  141. response.message = CommonUtils.getMessage("error.processfailure");
  142. response.ExceptionMessage = e.getMessage();
  143. }
  144. return response;
  145. }
  146. @Override
  147. public Response update(FaqViewModel viewModel) throws Exception {
  148. // TODO Auto-generated method stub
  149. Response response = new Response();
  150. try {
  151. // Calling the helper class to set the mandatory properties of the
  152. // view model
  153. viewModel = helperUtil.AttachCommonFields(viewModel,
  154. FaqViewModel.class);
  155. Faq faq = modelmapper.map(viewModel,
  156. Faq.class);
  157. Map<String, Object> map = new HashMap<String, Object>();
  158. // Fetch the first record with given conditions satisfied.
  159. map.put("code", viewModel.code);
  160. // Check whether the record exists before update.
  161. Faq faqdata = genericDAO.findOneByAttribute(
  162. viewModel.entityObject, map, Faq.class);
  163. map.clear();
  164. map.put("question",
  165. Pattern.compile(viewModel.question, Pattern.CASE_INSENSITIVE));
  166. Faq faqduplication = genericDAO.findOneByAttribute(
  167. viewModel.entityObject, map, Faq.class);
  168. if (faqdata != null)
  169. {
  170. if(null != faqduplication
  171. && !(faqdata.getCode()
  172. .equalsIgnoreCase(faqduplication
  173. .getCode()))) {
  174. response.success = false;
  175. response.message = CommonUtils
  176. .getMessage("error.updateFaqdata.exists");
  177. }
  178. else{
  179. // override the old properties with new properties exists in the
  180. // right side object
  181. notNull.copyProperties(faqdata, faq);
  182. // Update the existing record wi8th new values.
  183. genericDAO.updateById(viewModel.entityObject, faqdata,
  184. faqdata.getId());
  185. Faq updatedFaq = genericDAO.findOneById(
  186. viewModel.entityObject, faqdata.getId(),
  187. Faq.class);
  188. response.success = true;
  189. response.ViewModel = modelmapper.map(updatedFaq,FaqViewModel.class);
  190. response.message = CommonUtils
  191. .getMessage("success.update.Faq");
  192. }
  193. } else {
  194. // To check whether the entity code exists if not returns the
  195. // response as false
  196. response.success = false;
  197. response.message = CommonUtils
  198. .getMessage("error.faqcode.not.exists");
  199. }
  200. } catch (Exception e) {
  201. // Exception Handling
  202. response.success = false;
  203. response.message = CommonUtils.getMessage("error.processfailure");
  204. response.ExceptionMessage = e.getMessage();
  205. }
  206. return response;
  207. }
  208. @Override
  209. public Response findAllByAttributes(FaqViewModel viewModel) throws Exception {
  210. // TODO Auto-generated method stub
  211. Response response = new Response();
  212. try {
  213. // Converting MasterViewModel object to MasterEntity Pojo
  214. Faq data = modelmapper
  215. .map(viewModel, Faq.class);
  216. Gson gson = new Gson();
  217. // Converting MasterEntity to a JSON string
  218. String faq = gson.toJson(data);
  219. // Converting JON string to a HashMap
  220. Map<String, Object> map = new Gson().fromJson(faq,
  221. new TypeToken<HashMap<String, Object>>() {
  222. }.getType());
  223. if (viewModel.where != null) {
  224. // for appending the where and the like for search criteria
  225. viewModel.where = helperUtil
  226. .ConstructWhereClauseFromJson(viewModel.where);
  227. }
  228. // Fetch the Entity based on the search criteria
  229. List<Faq> allFaq = genericDAO.findAllByAttributes(
  230. viewModel.entityObject, map, Faq.class,
  231. viewModel.limit, viewModel.skip);
  232. // Returns true if there is a record matching the criteria
  233. if (null != allFaq && allFaq.size() > 0) {
  234. response.success = true;
  235. response.message = CommonUtils
  236. .getMessage("success.faq.Criteria.search");
  237. response.ViewModels = allFaq;
  238. } else {
  239. response.success = false;
  240. response.message = CommonUtils
  241. .getMessage("error.faq.Criteria.search");
  242. }
  243. } catch (Exception e) {
  244. // Handling Exception
  245. response.success = false;
  246. response.message = CommonUtils.getMessage("error.processfailure");
  247. // response.status = false;
  248. response.ExceptionMessage = e.getMessage();
  249. }
  250. return response;
  251. }
  252. @Override
  253. public Response delete(FaqViewModel viewModel) throws Exception {
  254. // TODO Auto-generated method stub
  255. Response response = new Response();
  256. try {
  257. Map<String, Object> map = new HashMap<String, Object>();
  258. // Fetch the first record with given conditions satisfied.
  259. map.put("code", viewModel.code);
  260. Faq faqfind = genericDAO.findOneByAttribute(
  261. viewModel.entityObject, map, Faq.class);
  262. // if record found then delete record and returns true
  263. if (faqfind != null) {
  264. // Deleting the Entity with the ID
  265. genericDAO.delete(viewModel.entityObject, faqfind.getId(),
  266. Faq.class);
  267. FaqViewModel faqdata = modelmapper.map(faqfind,
  268. FaqViewModel.class);
  269. response.success = true;
  270. response.message = CommonUtils
  271. .getMessage("success.faq.deletion");
  272. response.ViewModel = faqdata;
  273. // response.status = true;
  274. } else {
  275. response.success = false;
  276. response.message = CommonUtils
  277. .getMessage("error.faqcode.not.exists");
  278. // response.status = false;
  279. }
  280. } catch (Exception e) {
  281. response.success = false;
  282. response.message = CommonUtils.getMessage("error.processfailure");
  283. response.ExceptionMessage = e.getMessage();
  284. }
  285. return response;
  286. }
  287. }