Condition IF, ELSE

Asked

Viewed 200 times

-1

I have a Java Spring MVC application, with Hibernate and JPA and HTML interface.

I have Two forms that depend on the Notebooks class registered and its attributes.

The forms have the following names, registration, change.

In the first registration form I enter the data of a new Notebook, detail only some fields such as id, numberCaderno, date, whoDigited, whoRecebeu and n°Notebooks, only this and save in the database and a new ID is created for this record.

When I do a search, in the id=1 case that was generated by the system, a form is displayed with the data of the previous form saved, these data are filled in what I do is just add new data in this second form, filling in the new data, has a checkbox in this form with the attributes statusPendentes and statusFinalized where I choose Pending or Finished, so I have it saved in the database. let’s say I opted for the checkbox statusFinalized statusFinalized in the database.

When I do the search again, in case id=1 it will fall on the same page to change, detail if I typed in the search id=1 it has to return a message on the screen, "This notebook is finished can not be changed"because I registered in the previous register as status.

Method ID of Class Controllercaderno that is called when I do a search by ID and shows the results of the form, both for statusPendentes or statusFinalized, if it is finalized, have to return the message "Este caderno bla bla", at the moment is returning the message for both as many statusPendent as statusFinalized.?

@GetMapping("id")
public String buscarNumeroID(@Valid CadernosCadastrados objeto, Model model, boolean statusPendentes, boolean statusFinalizados, Long id) {
    List<CadernosCadastrados> cadernos = daoCadernosCadastrados.buscarNumeroID(id); 

        if(cadernos.equals(cadernos) != statusFinalizados) {    

            model.addAttribute("mensagem", "Este caderno está finalizado não 
             pode ser alterado!");

        }

        if(cadernos.equals(cadernos) != statusPendentes) {
         model.addAttribute("cadernos", cadernos); 
         return "public/alterar";

    }


    return "public/sucessos";

}

Dao’s ID method, where I search the ID data saved in the database.

public List<CadernosCadastrados> buscarID(Long  id) {
    TypedQuery<CadernosCadastrados> query = entityManager.createQuery(
            "SELECT d FROM CadernosCadastrados d WHERE d.id = :id order by id desc", CadernosCadastrados.class);
    query.setParameter("id", id); 
    return query.getResultList();


} 

Method to change the Controllercaderno class, where I bring the list and have the new records saved from the previous form.

@RequestMapping("alterar")
public String alterar(CadernosCadastrados  objeto, Long id, BindingResult result, Model model, boolean statusPendentes, boolean statusFinalizados) {
    List<CadernosCadastrados> cadernos = daoCadernosCadastrados.buscarNumeroID(id);

      daoCadernosCadastrados.alterar(objeto);

            return "public/sucessos";  

}

Method dao Change, from the Controllercaderno class where I save the data from the previous form and save the data in the database.

public void alter(Notebooks registered object) {

    entityManager.merge(objeto);

}

  • Here you search the notebook by the right number? List<Notessested> notebooks = from theCadernosCasted.SearchNumerCadern(numberCadern); Does the Notessested object then have two attributes statusPendent and statusFinalized? So what if one of the two attributes is null return different pages? Sorry, I can’t understand very well.

  • So friend I put these attributes because when calling in the bank, and checks if it is Pending or Finished. i created this normal attribute in the Notebook class with String, the best solution is to make an Enum is not.

1 answer

0

Sorry, buddy, I don’t know if I can really understand what you need and what your difficulty is, but here we go...

I noticed some strange things in your search method.

  1. If the intention is only to search by notebook number, why is passing as parameter Notebooks, statusPendente, statusFinalized and id? Also none of these parameters are being used in the body of the method.

  2. From what I understand, when performing a survey, the system should return a form with the data of the notebook and the information if it is new, pending, finished or non-existent. If the return of this method is a page form by which daoCadernosCadastrados.buscarNumeroCaderno(numeroCaderno) returns a list of notebooks? You can have more than one notebook in the database with the same number? If this is possible, where is the treatment of this possible return? Why is it not usual to display more than one notebook in one form. If this is not possible, it is good to review the implementation of the method ofCadernosCartists.searchNumbersCadern(numberCadern) and make the return consistent with the situation.

After you observe these issues and make the code clearer you will be able to assist you more accurately, but to do what you need I believe that the code below solves, is not the best way but is already a step further from the original version:

@GetMapping("numeroCaderno")
public String buscarPorNumeroCaderno(Model model, String numeroCaderno) {
    Caderno caderno = daoCadernosCadastrados.buscarNumeroCaderno(numeroCaderno);

    if (caderno != null) {

        model.addAttribute("mensagem", "");
        model.addAttribute("caderno", caderno);

        if (enumStatus.PENDENTE.equals(caderno.status)) {
             return "public/caderno_pendente";
        }

        if (enumStatus.FINALIZADO.equals(caderno.status)) {
             return "public/caderno_finalizado";
        }

        return "public/caderno_novo";  

    }

    return "public/sem_resultados";

}
  • Hello friend, so come on, I have a form, I register and saved up to ae normal, no problems when I do the search in the case "numeroCaderno" it returns the same page with the data filled and a checkbox is in this checkbox that I mark if it is "Pending" and when you do the search it returns this page "Pending" if I choose "Finished" it returns this same page but with the name of "Finished" and if I do a search and do not find in the bank any "Notebook" neither Pending nor Finished have to return the page "sem_resultados".

  • So friend, I followed your logic and with that the return is giving "sem_results" and I registered a notebook as "pending", the right would be the pending page and not sem_results.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.