public void validateID(AjaxBehaviorEvent event) {

    String idValidate = IclubWebHelper.validateId(bean.getPIdNum(), bean.getPGender());
    if (idValidate != null && !idValidate.trim().equalsIgnoreCase("")) {

      IclubWebHelper.addMessage(idValidate, FacesMessage.SEVERITY_ERROR);
    } else {
      String dateOfBirth = bean.getPIdNum().toString().substring(0, 6);
      SimpleDateFormat formatter = new SimpleDateFormat("yyMMdd");
      try {
        Date dateOfBirthD = formatter.parse(dateOfBirth);
        bean.setPDob(dateOfBirthD);
      } catch (ParseException e) {
        e.printStackTrace();
      }
    }
  }
  public List<IclubDocumentBean> getDocs() {
    if (bean != null && bean.getPId() != null) {
      WebClient client =
          IclubWebHelper.createCustomClient(D_BASE_URL + "get/entity/" + bean.getPId() + "" + "/1");
      Collection<? extends IclubDocumentModel> models =
          new ArrayList<IclubDocumentModel>(
              client.accept(MediaType.APPLICATION_JSON).getCollection(IclubDocumentModel.class));
      client.close();
      docs = new ArrayList<IclubDocumentBean>();
      if (models != null && models.size() > 0) {
        for (IclubDocumentModel model : models) {
          IclubDocumentBean bean = IclubDocumentTrans.fromWStoUI(model);

          docs.add(bean);
        }
      }
    } else {
      docs = new ArrayList<IclubDocumentBean>();
    }
    return docs;
  }
  public String updateLogin() {

    try {
      if (validateLoginForm(!updateLogin)) {
        IclubLoginModel model = new IclubLoginModel();
        WebClient client = null;

        if (loginBean.getLId() != null) {
          client = IclubWebHelper.createCustomClient(LOG_BASE_URL + "mod");
          model.setLId(loginBean.getLId());

        } else {
          client = IclubWebHelper.createCustomClient(LOG_BASE_URL + "add");
          model.setLId(UUID.randomUUID().toString());
        }
        model.setLCrtdDt(new Date(System.currentTimeMillis()));
        model = IclubLoginTrans.fromUItoWS(loginBean);

        model.setLPasswd(Base64.encodeBase64URLSafeString(DigestUtils.md5(loginBean.getLPasswd())));
        model.setIclubPersonAByLCrtdBy(bean.getPId());
        model.setIclubPersonBByLPersonId(getSessionUserId());
        model.setIclubRoleType(2l);

        ResponseModel response = null;
        if (updateLogin) {
          response = client.accept(MediaType.APPLICATION_JSON).put(model, ResponseModel.class);
        } else {
          response = client.accept(MediaType.APPLICATION_JSON).post(model, ResponseModel.class);
        }

        if (response.getStatusCode() == 0) {
          IclubWebHelper.addObjectIntoSession("social_update_profile", null);
          IclubWebHelper.addMessage(
              "Personal Details Updated Successfully", FacesMessage.SEVERITY_INFO);
          loadBean = false;
          return "userDashboard";
        } else {
          IclubWebHelper.addMessage(
              "Fail :: " + response.getStatusDesc(), FacesMessage.SEVERITY_ERROR);
        }
      }

    } catch (Exception e) {
      IclubWebHelper.addMessage("Fail :: " + e.getMessage(), FacesMessage.SEVERITY_ERROR);
    }
    return null;
  }
  public void updatePassword() {

    try {
      if (validateLoginForm(!updateLogin)) {
        IclubLoginModel model = new IclubLoginModel();
        WebClient client = null;

        if (loginBean.getLId() != null) {
          client = IclubWebHelper.createCustomClient(BASE_URL + "mod");
          model.setLId(loginBean.getLId());

        } else {
          client = IclubWebHelper.createCustomClient(LOG_BASE_URL + "add");
          model.setLId(UUID.randomUUID().toString());
        }
        model.setLCrtdDt(new Date(System.currentTimeMillis()));
        model.setLLastDate(loginBean.getLLastDate());
        model.setLName(loginBean.getLName());
        model.setLPasswd(Base64.encodeBase64URLSafeString(DigestUtils.md5(loginBean.getLPasswd())));
        model.setLSecAns(loginBean.getLSecAns());
        model.setIclubPersonAByLCrtdBy(bean.getPId());
        model.setIclubPersonBByLPersonId(getSessionUserId());
        model.setIclubRoleType(2l);
        model.setIclubSecurityQuestion(loginBean.getIclubSecurityQuestion());

        ResponseModel response = null;
        if (updateLogin) {
          response = client.accept(MediaType.APPLICATION_JSON).put(model, ResponseModel.class);
        } else {
          response = client.accept(MediaType.APPLICATION_JSON).post(model, ResponseModel.class);
        }

        if (response.getStatusCode() == 0) {
          IclubWebHelper.addMessage(
              "Personal Details Updated Successfully", FacesMessage.SEVERITY_INFO);

        } else {
          IclubWebHelper.addMessage(
              "Fail :: " + response.getStatusDesc(), FacesMessage.SEVERITY_ERROR);
        }
      }

    } catch (Exception e) {
      IclubWebHelper.addMessage("Fail :: " + e.getMessage(), FacesMessage.SEVERITY_ERROR);
    }
  }
  public IclubPersonBean getBean() {
    if (bean == null) {
      bean = new IclubPersonBean();
    }
    if (!loadBean) {
      if (IclubWebHelper.getObjectIntoSession(BUNDLE.getString("logged.in.user.id")) != null) {
        WebClient client =
            IclubWebHelper.createCustomClient(BASE_URL + "get/" + getSessionUserId());

        IclubPersonModel model =
            (IclubPersonModel)
                (client.accept(MediaType.APPLICATION_JSON).get(IclubPersonModel.class));
        client.close();
        bean = IclubPersonTrans.fromWStoUI(model);

        if (IclubWebHelper.getObjectIntoSession(BUNDLE.getString("logged.in.login.id")) != null) {
          client =
              IclubWebHelper.createCustomClient(
                  LOG_BASE_URL
                      + "get/"
                      + IclubWebHelper.getObjectIntoSession(BUNDLE.getString("logged.in.login.id"))
                          .toString());
        } else {
          client = IclubWebHelper.createCustomClient(LOG_BASE_URL + "person/" + bean.getPFName());
        }

        IclubLoginModel loginModel =
            (IclubLoginModel)
                (client.accept(MediaType.APPLICATION_JSON).get(IclubLoginModel.class));
        if (loginModel != null && loginModel.getLId() != null) {
          updateLogin = true;
          loginBean = IclubLoginTrans.fromWStoUI(loginModel);
        } else {
          updateLogin = false;
          loginBean = new IclubLoginBean();
        }
      }
    }

    return bean;
  }
  public boolean validateForm(boolean flag) {
    boolean ret = true;
    if (bean.getPFName() == null || bean.getPFName().trim().equalsIgnoreCase("")) {
      IclubWebHelper.addMessage(("First Name Cannot be empty"), FacesMessage.SEVERITY_ERROR);
      ret = ret && false;
    }
    if (bean.getPLName() == null || bean.getPLName().trim().equalsIgnoreCase("")) {
      IclubWebHelper.addMessage(("Last Name Cannot be empty"), FacesMessage.SEVERITY_ERROR);
      ret = ret && false;
    }
    if (bean.getPMobile() == null || bean.getPMobile().trim().equalsIgnoreCase("")) {
      IclubWebHelper.addMessage(("Mobile Number Cannot be empty"), FacesMessage.SEVERITY_ERROR);
      ret = ret && false;
    }

    if (bean.getPGender() == null || bean.getPGender().trim().equalsIgnoreCase("")) {
      IclubWebHelper.addMessage(("Gender Cannot be empty"), FacesMessage.SEVERITY_ERROR);
      ret = ret && false;
    }

    if (bean.getPIdNum() == null || bean.getPIdNum().trim().equalsIgnoreCase("")) {
      IclubWebHelper.addMessage(("Id Number Cannot be empty"), FacesMessage.SEVERITY_ERROR);
      ret = ret && false;
    }
    if (bean.getIclubIdType() == null) {
      IclubWebHelper.addMessage(("Please Select ID Type"), FacesMessage.SEVERITY_ERROR);
      ret = ret && false;
    }
    if (bean.getPIsPensioner() == null || bean.getPIsPensioner().trim().equalsIgnoreCase("")) {
      IclubWebHelper.addMessage(("Please Select Pensioner"), FacesMessage.SEVERITY_ERROR);
      ret = ret && false;
    }
    if (bean.getPDob() == null) {
      IclubWebHelper.addMessage(("Please Select DOB"), FacesMessage.SEVERITY_ERROR);
      ret = ret && false;
    } else if (IclubWebHelper.calculateYearDiff(bean.getPDob().getTime()) <= 18) {
      IclubWebHelper.addMessage(("You must be over 18 years"), FacesMessage.SEVERITY_ERROR);
      ret = ret && false;
    }
    if (bean.getPIdIssueDt() == null) {
      IclubWebHelper.addMessage(("Please Select IssueDate"), FacesMessage.SEVERITY_ERROR);
      ret = ret && false;
    } else if (IclubWebHelper.isCurrentDate(bean.getPIdIssueDt().getTime())) {
      IclubWebHelper.addMessage(
          ("Issue Date Should be less than Current Date"), FacesMessage.SEVERITY_ERROR);
      ret = ret && false;
    } else if (bean.getPIdExpiryDt() != null
        && !IclubWebHelper.isCurrentDate(bean.getPIdExpiryDt().getTime())) {
      IclubWebHelper.addMessage(
          ("Expiry Date Should be greater than Current Date"), FacesMessage.SEVERITY_ERROR);
      ret = ret && false;
    } else if (bean.getPIdExpiryDt() != null
        && !IclubWebHelper.isCurrentDate(bean.getPIdExpiryDt().getTime())) {
      IclubWebHelper.addMessage(
          ("Expiry Date Should be greater than Current Date"), FacesMessage.SEVERITY_ERROR);
      ret = ret && false;
    } else if (((Long) bean.getPIdIssueDt().getTime())
            .compareTo(((Long) bean.getPIdExpiryDt().getTime()))
        >= 0) {
      IclubWebHelper.addMessage(
          (" Id Issue Date Should be less than Expiry Date"), FacesMessage.SEVERITY_ERROR);
      ret = ret && false;
    }
    String idValidate = IclubWebHelper.validateId(bean.getPIdNum(), bean.getPGender());
    if (idValidate != null && !idValidate.trim().equalsIgnoreCase("")) {

      IclubWebHelper.addMessage(idValidate, FacesMessage.SEVERITY_ERROR);
      ret = ret && false;
    }
    return ret;
  }
  public static IclubPersonModel fromUItoWS(IclubPersonBean bean) {

    IclubPersonModel model = new IclubPersonModel();

    model.setPId(bean.getPId());
    model.setPCrtdDt(bean.getPCrtdDt());
    model.setPDob(bean.getPDob());
    model.setPEmail(bean.getPEmail());
    model.setPFName(bean.getPFName());
    model.setPIdNum(bean.getPIdNum());
    model.setPLName(bean.getPLName());
    model.setPAge(bean.getPAge());
    model.setPMobile(bean.getPMobile());
    model.setPAddress(bean.getPAddress());
    model.setPContactPref(bean.getPContactPref());
    model.setPGender(bean.getPGender());
    model.setPContactPref(bean.getPContactPref());
    model.setPIdExpiryDt(bean.getPIdExpiryDt());
    model.setPInitials(bean.getPInitials());
    model.setPIsPensioner(bean.getPIsPensioner());
    model.setPIdIssueCntry(
        bean.getPIdIssueCntry() != null ? bean.getPIdIssueCntry().longValue() : null);
    model.setPIdIssueDt(bean.getPIdIssueDt());
    model.setPLat(bean.getPLat());
    model.setPLong(bean.getPLong());
    model.setPOccupation(bean.getPOccupation());
    model.setPTitle(bean.getPTitle());
    model.setPZipCd(bean.getPZipCd());
    model.setIclubIdType(bean.getIclubIdType());
    model.setItLongDesc(bean.getItLongDesc());
    model.setIclubMaritalStatus(bean.getIclubMaritalStatus());
    model.setMsLongDesc(bean.getMsLongDesc());
    model.setIclubPerson(bean.getIclubPerson());
    model.setPFNameAndLName(bean.getPFNameAndLName());
    model.setIclubCohort(bean.getIclubCohort());
    model.setCEmail(bean.getCEmail());
    model.setIclubCohortInvite(bean.getIclubCohortInvite());

    return model;
  }
  public static IclubPersonBean fromWStoUI(IclubPersonModel model) {

    IclubPersonBean bean = new IclubPersonBean();

    bean.setPId(model.getPId());
    bean.setPCrtdDt(model.getPCrtdDt());
    bean.setPDob(model.getPDob());
    bean.setPEmail(model.getPEmail());
    bean.setPFName(model.getPFName());
    bean.setPIdNum(model.getPIdNum());
    bean.setPLName(model.getPLName());
    bean.setPAge(model.getPAge());
    bean.setPMobile(model.getPMobile());
    bean.setPAddress(model.getPAddress());
    bean.setPContactPref(model.getPContactPref());
    bean.setPGender(model.getPGender());
    bean.setPContactPref(model.getPContactPref());
    bean.setPIdExpiryDt(model.getPIdExpiryDt());
    bean.setPInitials(model.getPInitials());
    bean.setPIsPensioner(model.getPIsPensioner());
    bean.setPIdIssueCntry(
        model.getPIdIssueCntry() != null ? model.getPIdIssueCntry().longValue() : null);
    bean.setPIdIssueDt(model.getPIdIssueDt());
    bean.setPLat(model.getPLat());
    bean.setPLong(model.getPLong());
    bean.setPOccupation(model.getPOccupation());
    bean.setPTitle(model.getPTitle());
    bean.setPZipCd(model.getPZipCd());
    bean.setIclubIdType(model.getIclubIdType());
    bean.setItLongDesc(model.getItLongDesc());
    bean.setIclubMaritalStatus(model.getIclubMaritalStatus());
    bean.setMsLongDesc(model.getMsLongDesc());
    bean.setIclubPerson(model.getIclubPerson());
    bean.setPFNameAndLName(model.getPFNameAndLName());
    bean.setIclubCohort(model.getIclubCohort());
    bean.setCEmail(model.getCEmail());
    bean.setIclubCohortInvite(model.getIclubCohortInvite());

    return bean;
  }