Condition IF, ELSE

-1

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

I have two forms that depend on the class CadernosCadastrados and their attributes.

Forms have the following names, cadastrodecadernos, change.

In the first form that is cadastrocaderno I enter the data of a new Caderno, just detail some fields like id, Number, date, whoSigned, whoReceived in ° Cadernos, just that and I have save in the bank 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 with the data of the first previous form is displayed, this data is previewed 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 Ended statuses where I choose Pending or Finished, so that the save command is saved in the database. let's say I opted for the End state checkbox is saved in the database as Ended status.

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 finalized can not be changed ", because I registered in the previous register as Status completed.

Method ID of the ControllerCode class that is called when I do a search by ID and shows the results of the form, both for StatusStatus or EndIndex status, if it is terminated, has to return the "This bla bla" message is currently returning the message for both statuses as Endpoints status.?

@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 method, of the ID, 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();


} 

ControllerClass Class change method, where I bring the list and have it save the new records 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";  

}

Change method of the ControllerCode class where I save the data from the previous form and thus save the data in the database.

public void change (CadernosCadastrados objeto) {

    entityManager.merge(objeto);

}

    
asked by anonymous 14.11.2018 / 15:57

1 answer

0

Sorry, my friend, I do not know if you can understand what you need and what your difficulty is, but come on ...

I noticed some strange things in your method to search for NotebookNumber and so I would like to leave some questions.

  • If the intent is just to search by notebook number, why is passing as parameter CadernosCadastrados, statusPendente, statusId and id? In addition, none of these parameters are being used in the body of the method.

  • As I understand it, when performing a search, the system must return a form with the data of the notebook and the information if it is new, pending, finalized or non-existent. If the return of this method is a page form why daoCadernosCadastrados.buscarNumeroCaderno(numeroCaderno) returns a list of notebooks? You can have in the database more than one with the same number? If this is possible, where is the treatment of this possible return? Because it is not usual to display more than one form. If this is not possible, it is good to review the method of the CadernosCadastrados.buscarNumeroCaderno (numberCaderno) and leave feedback consistent with the situation.

  • After you look at these questions and leave the code clearer, it will be possible to help you more accurately, but to do what you need to believe the code below resolves, it is not the best way, but you are already one step closer to 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";
    
    }
    
        
    16.11.2018 / 19:20