Change of id in form

7

I have a form where I fill in a series of fields of a JPA entity, the case is that between the fields there is a list which I am filling in with a modal, until then everything is correct, in fact I have this method done for several controllers and It goes perfectly.

The problem is when I go to edit the file, everything loads correctly but when I hit the Delete button in the list, when I get to the controller's method, the id of the form is different from what it should be, or is - 1 or is the next number (which does not make much sense).

I leave the part of the code that may be influencing to see if someone sees something that I could not see.

List

<table class="table table-striped table-condensed flip-content dataTable" th:if="${concert.choirs.size()>0}">
    <thead class="flip-content">
        <tr>
            <th width="90%">
                Nombre
            </th>
            <th width="10%"></th>
        </tr>
    </thead>
    <tbody id="cuerpoTablaCorista">
        <tr th:each="choir,status : ${concert.choirs}">
            <td th:text="${choir.choir.name}"></td>
            <td class="operations"> 
                <a class="delete"  href="/concert/delete_choir/__${status.index}__}" th:href="@{/concert/delete_choir/__${status.index}__}">
                    Borrar
                </a>
            </td>       
        </tr>
    </tbody>
</table>

Controller methods

@RequestMapping(value = "concert/delete_choir/{index}", method = RequestMethod.GET)
    public String deleteChoir(@PathVariable String index, @ModelAttribute("concert") ConcertForm concertForm,
            RedirectAttributes ra, Model model) {

        ConcertChoir concertChoir = concertForm.getChoirs().get(Integer.parseInt(index));

    concertForm.getChoirs().remove(Integer.parseInt(index));

    if (null != concertChoir.getId()) {
        basicServ.delete(concertChoir);
    }
    model.addAttribute("concert", concertForm);

    return "redirect:/concert";
}

@RequestMapping(value = "/concert", method = RequestMethod.GET)
    public String concert(@ModelAttribute("concert") ConcertForm concertForm, Model model) {

    model.addAllAttributes(Utils.rellenaCombosConcierto(basicServ));

    model.addAttribute("concert", concertForm);

    return "concert/new";
}

@RequestMapping(value = "concert/edit/{idConcert}", method = RequestMethod.GET)
    public String edit(@PathVariable String idConcert, Model model) {

        Concert concert = (Concert) basicServ.findById(Long.valueOf(idConcert), Concert.class);
        ConcertForm concertForm = new ConcertForm(concert);

        model.addAttribute("concert", concertForm);

        return "redirect:/concert";
    }

Form

public class ConcertForm extends BaseForm {

    private String name;

    private String date;

    private String act;

    private String notes;

    private Long country;

    private Long province;

    private String provinceName;

    private Long city;

    private String cityName;

    private Long schedule;

    private Long orchestra;

    private Long director;

    private Long composer;

    private Long choir;

    private String choirName;

    private Long soloist;

    private String soloistName;

    private ConcertModalForm concertModal;

    private List<ConcertSoloist> soloists = new ArrayList<ConcertSoloist>();

    private List<ConcertChoir> choirs = new ArrayList<ConcertChoir>();

    public ConcertForm(Concert c) {
        this.id = c.getId();
        this.createDate = c.getCreateDate();
        this.createdBy = c.getCreatedBy();

        this.name = c.getName();
        this.date = Utils.simpleDateToString(c.getDate());
        this.act = c.getAct();
        this.notes = c.getNotes();
        this.country = c.getCountry().getId();
        this.province = c.getProvince().getId();
        this.provinceName = c.getProvince().getName();
        this.city = c.getCity().getId();
        this.cityName = c.getCity().getName();
        this.schedule = c.getSchedule().getId();
        this.director = c.getDirector().getId();
        this.composer = c.getComposer().getId();
        this.orchestra = c.getOrchestra().getId();

        this.choirs.addAll(c.getChoirs());
        this.soloists.addAll(c.getSoloists());
    }
    public Concert getConcert(BasicService bs) {
        Concert c = new Concert();

        c.setId(this.id);
        c.setCreateDate(this.createDate);
        c.setCreatedBy(this.createdBy);

        c.setName(this.name);
        c.setDate(Utils.validaFecha(this.date));
        c.setAct(this.act);
        c.setNotes(this.notes);
        c.setCountry((Country) (null != this.country && this.country != -1 ? bs.findById(this.country, Country.class)
            : null));
        if (null == this.id) {
            c.setProvince((Province) (null != this.province && this.province != -1
                ? bs.findById(this.province, Province.class) : null));
            c.setCity((City) (null != this.city && this.city != -1 ? bs.findById(this.city, City.class) : null));
        } else {
            Concert concertAux = (Concert) bs.findById(this.id, Concert.class);
            if (null == this.province || this.province == -1) {
                c.setProvince(concertAux.getProvince());
            } else {
                c.setProvince((Province) (null != this.province && this.province != -1
                    ? bs.findById(this.province, Province.class) : null));
            }
            if (null == this.city || this.city == -1) {
                c.setCity(concertAux.getCity());
            } else {
                c.setCity((City) (null != this.city && this.city != -1 ? bs.findById(this.city, City.class) : null));

            }
        }
        c.setSchedule((Schedule) (null != this.schedule && this.schedule != -1
            ? bs.findById(this.schedule, Schedule.class) : null));
        c.setDirector((ConcertDirector) (null != this.director && this.director != -1
            ? bs.findById(this.director, ConcertDirector.class) : null));
        c.setComposer((ConcertComposer) (null != this.composer && this.composer != -1
            ? bs.findById(this.composer, ConcertComposer.class) : null));
        c.setOrchestra((Orchestra) (null != this.orchestra && this.orchestra != -1
            ? bs.findById(this.orchestra, Orchestra.class) : null));

        for (ConcertChoir cc : this.choirs) {
             cc.setConcert(c);
        }
        for (ConcertSoloist cs : this.soloists) {
            cs.setConcert(c);
        }
        c.getSoloists().addAll(this.soloists);
        c.getChoirs().addAll(this.choirs);

        return c;
    }


    //getters y setters
}
    
asked by Raider 02.12.2015 в 13:03
source

1 answer

3

Ok, I found the problem.

In the main html the following function was embedded, which was executed each time the page was loaded

function cargaCombosLocale(lookupUrl, parentSelectElementId,
        childSelectElementId, isNew) {
    var idSeleccionado = $('#' + parentSelectElementId).val();
    if (idSeleccionado == '-1') {
        if(childSelectElementId === 'provinceField'){
            $("#provinceField").select2('data', {
                id : '-1',
                text : 'Provincia'
            });
            $("#cityField").select2('data', {
                id : '-1',
                text : 'Ciudad'
            });
            $('#' + childSelectElementId).prop("readonly", true);
            $('#cityField').prop("readonly", true);
        } if(childSelectElementId === 'cityField'){
            $("#cityField").select2('data', {
                id : '-1',
                text : 'Ciudad'
            });
            $('#cityField').prop("readonly", true);
        }


    } else {
        $('#' + childSelectElementId).prop("readonly", false);
        if(childSelectElementId === 'provinceField'){
            $("#provinceField").select2('data', {
                id : '-1',
                text : 'Provincia'
            });
            $("#cityField").select2('data', {
                id : '-1',
                text : 'Ciudad'
            });
            $('#cityField').prop("readonly", true);
        } 
        if(childSelectElementId === 'cityField'){
            $("#cityField").select2('data', {
                id : '-1',
                text : 'Ciudad'
            });
        }
    }
    $.ajax({
        type : 'GET',
        url : lookupUrl,
        data : {
            id : idSeleccionado,
            isNew : isNew
        },
        success : function(data) {
            var html = '';
            var len = data.length;
            for (var i = 0; i < len; i++) {
                html += '<option value="' + data[i].id + '">' + data[i].name
                        + '</option>';
            }

            $('#' + childSelectElementId).html(html);
        },
        error : function(request, status, error) {
            alert(error);
        },
    });
}

Basically it is a function that is responsible for loading the values of the city and province combos. The parameters that were passed to him were correct, what I do not understand is because he changed the id. Anyway it was solved by replacing them with another function that was using in another controller.

    
answered by 02.12.2015 / 16:21
source