Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Dauda Ainoo
Added: Nov 28, 2019 2:25 PM
Views: 5
Tags: no tags
  1. package com.ecobank.esc.operationsportal.controllers.users;
  2.  
  3. import com.ecobank.esc.operationsportal.commons.UserSession;
  4. import com.ecobank.esc.operationsportal.entities.UserAccount;
  5. import com.ecobank.esc.operationsportal.models.BranchInfo;
  6. import com.ecobank.esc.operationsportal.models.UserAccountInfo;
  7. import com.ecobank.esc.operationsportal.services.BaseDAO;
  8. import com.ecobank.esc.operationsportal.services.BasicServices;
  9. import com.ecobank.esc.operationsportal.services.UserAccountDAO;
  10. import com.ecobank.esc.operationsportal.utils.AppConstants;
  11. import com.ecobank.esc.operationsportal.utils.Msg;
  12. import com.ecobank.esc.operationsportal.utils.PageCommonInputs;
  13. import java.io.Serializable;
  14. import java.util.ArrayList;
  15. import java.util.Arrays;
  16. import java.util.Date;
  17. import java.util.List;
  18. import javax.annotation.PostConstruct;
  19. import javax.enterprise.context.SessionScoped;
  20. import javax.faces.model.SelectItem;
  21. import javax.inject.Inject;
  22. import javax.inject.Named;
  23. import org.apache.log4j.Logger;
  24.  
  25. /**
  26.  *
  27.  * @author dainoo
  28.  */
  29. @Named(value = "userAccountController")
  30. @SessionScoped
  31. public class UserAccountController implements Serializable {
  32.  
  33.     private static final Logger LOG = Logger.getLogger(UserAccountController.class.getName());
  34.  
  35.     @Inject
  36.     private UserSession userSession;
  37.     @Inject
  38.     private BasicServices baseServices;
  39.  
  40.     private PageCommonInputs pageCommonInputs = new PageCommonInputs();
  41.     private UserAccount userAccount = new UserAccount();
  42.     private List<UserAccountInfo> listOfUserAccounts = new ArrayList<>();
  43.     private List<SelectItem> listOfBranches;
  44.     private String affiliateCode, branchName;
  45.     private List<BranchInfo> branchList;
  46.  
  47.     public UserAccountController() {
  48.     }
  49.  
  50.     public void branchListener() {
  51.         try {
  52.             if (null == affiliateCode) {
  53.                 affiliateCode = userAccount.getAffiliateCode();
  54.             }
  55.             LOG.info("affiliateCode " + affiliateCode);
  56.             listOfBranches = new ArrayList<>();
  57.             branchList = BaseDAO.getBranchList(affiliateCode);
  58.             affiliateCode = null;
  59.             branchList.forEach((eachOne) -> {
  60.                 listOfBranches.add(new SelectItem(eachOne.getBranchCode(), eachOne.toString()));
  61.             });
  62.             LOG.info("listOfBranches size " + listOfBranches.size());
  63.         } catch (Exception e) {
  64.             LOG.info("branchListener exception " + Arrays.toString(e.getStackTrace()).replaceAll(", ", "\n"));
  65.         }
  66.     }
  67.  
  68.     @PostConstruct
  69.     public void init() {
  70.         try {
  71.             affiliateCode = null;
  72.             pageCommonInputs.setShowEditButtons(true);
  73.             cancelButton();
  74.         } catch (Exception e) {
  75.             LOG.info("create user account exception " + Arrays.toString(e.getStackTrace()).replaceAll(", ", "\n"));
  76.         }
  77.     }
  78.  
  79.     public void newButton() {
  80.         try {
  81.             affiliateCode = userSession.getAffiliate();
  82.             if (!userSession.isGroupUser()) {
  83.                 branchListener();
  84.             }
  85.             pageCommonInputs.setShowEditButtons(true);
  86.             pageCommonInputs.showDataInputsDisplay();
  87.             userAccount = new UserAccount();
  88.         } catch (Exception e) {
  89.             LOG.info("create user account exception " + Arrays.toString(e.getStackTrace()).replaceAll(", ", "\n"));
  90.         }
  91.     }
  92.  
  93.     public void cancelButton() {
  94.         try {
  95.             pageCommonInputs.showDataRecordsDisplay();
  96.             userAccount = new UserAccount();
  97.             viewAllButton();
  98.         } catch (Exception e) {
  99.             LOG.info("create user account exception " + Arrays.toString(e.getStackTrace()).replaceAll(", ", "\n"));
  100.         }
  101.     }
  102.  
  103.     public void saveButton() {
  104.         try {
  105.             userAccount.setCreatedDate(new Date());
  106.             userAccount.setModifiedDate(new Date());
  107.             userAccount.setVerified(AppConstants.NO);
  108.             userAccount.setCreatedBy(userSession.getUsername());
  109.             userAccount.setModifiedBy(userSession.getUsername());
  110.             UserAccount response = baseServices.saveEntity(userAccount);
  111.             String msg = null;
  112.             if (null == response) {
  113.                 msg = "User account creation failed;please try it again";
  114.                 LOG.info(msg);
  115.                 Msg.errorMsg(msg);
  116.             } else {
  117.                 msg = userAccount.getFullname() + "'s user account created successfully ;please notify authoriser to approve";
  118.                 Msg.infoMsg(msg);
  119.                 LOG.info(msg);
  120.                 pageCommonInputs.showDataInputsDisplay();
  121.                 userAccount = new UserAccount();
  122.             }
  123.         } catch (Exception e) {
  124.             LOG.info("create user account exception " + Arrays.toString(e.getStackTrace()).replaceAll(", ", "\n"));
  125.         }
  126.     }
  127.  
  128.     public void updateButton() {
  129.         try {
  130.             LOG.info("about to update username " + userAccount.getUsername());
  131.             userAccount.setModifiedBy(userSession.getUsername());
  132.             userAccount.setModifiedDate(new Date());
  133.             String msg = "";
  134.             if (baseServices.update(userAccount)) {
  135.                 msg = userAccount.getUsername() + " user account update successfully";
  136.                 LOG.info(msg);
  137.                 Msg.infoMsg(msg);
  138.                 cancelButton();
  139.             } else {
  140.                 msg = "Error updating user account " + userAccount.getUsername() + ":try it again or contact the support team";
  141.                 LOG.info(msg);
  142.                 Msg.errorMsg(msg);
  143.             }
  144.         } catch (Exception e) {
  145.             LOG.info("update user account exception " + Arrays.toString(e.getStackTrace()).replaceAll(", ", "\n"));
  146.         }
  147.     }
  148.  
  149.     public void selectButton(UserAccountInfo user) {
  150.         try {
  151.             LOG.info("user.getUserName() " + user.getUserName());
  152.             pageCommonInputs.showDataInputsDisplay();
  153.             pageCommonInputs.setShowEditButtons(false);
  154.  
  155.             userAccount.setAffiliateCode(user.getAffiliateCode());
  156.             userAccount.setUsername(user.getUserName());
  157.             userAccount.setBranchCode(user.getBranchCode());
  158.             userAccount.setEmailAddress(user.getEmail());
  159.             userAccount.setFullname(user.getFullname());
  160.             userAccount.setLang(user.getLanguage());
  161.             userAccount.setMobileNo(user.getMobileNo());
  162.             userAccount.setModifiedBy(user.getModifiedBy());
  163.             userAccount.setStatus(user.getStatus());
  164.             userAccount.setUserRole(user.getUserRole());
  165.             affiliateCode = user.getAffiliateCode();
  166.             branchListener();
  167.         } catch (Exception e) {
  168.             LOG.info("create user account exception " + Arrays.toString(e.getStackTrace()).replaceAll(", ", "\n"));
  169.         }
  170.     }
  171.  
  172.     public void searchButton() {
  173.         try {
  174.             listOfUserAccounts = new ArrayList<>();
  175.             listOfUserAccounts = UserAccountDAO.getUserAccountList(pageCommonInputs);
  176.             if (listOfUserAccounts.isEmpty()) {
  177.                 Msg.errorMsg("No record found");
  178.             }
  179.         } catch (Exception e) {
  180.             LOG.info("searchButton user account exception " + Arrays.toString(e.getStackTrace()).replaceAll(", ", "\n"));
  181.         }
  182.     }
  183.  
  184.     public void viewAllButton() {
  185.         try {
  186.             listOfUserAccounts = new ArrayList<>();
  187.             listOfUserAccounts = UserAccountDAO.getUserAccountList(pageCommonInputs);
  188.         } catch (Exception e) {
  189.             LOG.info("viewAllButton user account exception " + Arrays.toString(e.getStackTrace()).replaceAll(", ", "\n"));
  190.         }
  191.     }
  192.     //<editor-fold defaultstate="collapsed" desc="GETTERS AND SETTERS">
  193.  
  194.     public UserAccount getUserAccount() {
  195.         return userAccount;
  196.     }
  197.  
  198.     public void setUserAccount(UserAccount userAccount) {
  199.         this.userAccount = userAccount;
  200.     }
  201.  
  202.     public String getAffiliateCode() {
  203.         return affiliateCode;
  204.     }
  205.  
  206.     public void setAffiliateCode(String affiliateCode) {
  207.         this.affiliateCode = affiliateCode;
  208.     }
  209.  
  210.     public String getBranchName() {
  211.         return branchName;
  212.     }
  213.  
  214.     public void setBranchName(String branchName) {
  215.         this.branchName = branchName;
  216.     }
  217.  
  218.     public List<BranchInfo> getBranchList() {
  219.         return branchList;
  220.     }
  221.  
  222.     public void setBranchList(List<BranchInfo> branchList) {
  223.         this.branchList = branchList;
  224.     }
  225.  
  226.     public PageCommonInputs getPageCommonInputs() {
  227.         return pageCommonInputs;
  228.     }
  229.  
  230.     public void setPageCommonInputs(PageCommonInputs pageCommonInputs) {
  231.         this.pageCommonInputs = pageCommonInputs;
  232.     }
  233.  
  234.     public List<SelectItem> getListOfBranches() {
  235.         return listOfBranches;
  236.     }
  237.  
  238.     public void setListOfBranches(List<SelectItem> listOfBranches) {
  239.         this.listOfBranches = listOfBranches;
  240.     }
  241.  
  242.     public List<UserAccountInfo> getListOfUserAccounts() {
  243.         return listOfUserAccounts;
  244.     }
  245.  
  246.     public void setListOfUserAccounts(List<UserAccountInfo> listOfUserAccounts) {
  247.         this.listOfUserAccounts = listOfUserAccounts;
  248.     }
  249. //</editor-fold>
  250.  
  251. }
  252.