コード例 #1
0
ファイル: TP5_Update.java プロジェクト: NicolasKriegk/tp_java
  /** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // recupération de la personne en base
    Integer profileId = Integer.valueOf(request.getParameter("profileId"));
    // recherche de la personne à modifier
    Personne updatedPerson = new Personne();
    updatedPerson.setId(profileId);
    updatedPerson = gestionEcoleService.rechercherPersonne(updatedPerson).get(0);
    // affectation des nouvelles valeurs
    String inputNom = request.getParameter("inputNom");
    updatedPerson.setNom(inputNom);
    String inputPrenom = request.getParameter("inputPrenom");
    updatedPerson.setPrenom(inputPrenom);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
    String inputDateNaissString = request.getParameter("inputDateNaiss");
    try {
      Date inputDateNaiss = simpleDateFormat.parse(inputDateNaissString);
      updatedPerson.setDateNaiss(inputDateNaiss);
    } catch (ParseException e) {
      throw new RuntimeException(e);
    }

    String inputPassword = request.getParameter("inputPassword");
    if (inputPassword != null && !inputPassword.isEmpty()) {
      updatedPerson.setPassw(inputPassword);
    }

    String inputPromotionString = request.getParameter("inputPromotion");
    if (!inputPromotionString.isEmpty()) {
      Integer inputPromotion = Integer.valueOf(inputPromotionString);
      Promotion searchPromotion = new Promotion();
      searchPromotion.setId(inputPromotion);
      searchPromotion = gestionEcoleService.rechercherPromotion(searchPromotion).get(0);
      updatedPerson.setPromotion(searchPromotion);
    } else {
      updatedPerson.setPromotion(null);
    }

    // mise à jour
    updatedPerson = gestionEcoleService.updatePersonne(updatedPerson);

    request.setAttribute("personneSelected", updatedPerson);
    RequestDispatcher dispatcher = request.getRequestDispatcher("/TP5_Controller");
    dispatcher.forward(request, response);
  }