'Display value of selected element of p:selectOneMenu and edit it

i am trying to select an object from a select many menu, which displays the available objects just fine. Then, when i select the one i am interested in, i wish to pass it to my backing bean to display its values in knobs and edit those knobs to change values. Neither works. Here is my xhtml code:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3c.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:ng="http://xmlns.jcp.org/jsf/passthrough"
    xmlns:p="http://primefaces.org/ui" xmlns:o="http://omnifaces.org/ui"
    template="/WEB-INF/templates/main.xhtml">
    <ui:define name="content">
        <h:head>
        </h:head>
        <h:form id="limitForm">
            <style type="text/css">
                .ui-selectonemenu{width: 120% !important;}
                .ui-selectonemenu-label{width: 120% !important;}
            </style>
            <p:panel header="Threshold Configuration">
               <h:panelGrid columns="2" cellpadding="5">
                   <p:outputLabel for="room" value="Room:" />
                   <p:selectOneMenu id="room" value="#{selectOneRoomBean.room}" style="width:150px">
                       <f:selectItem itemLabel="Select Room" itemValue=""></f:selectItem>
                       <f:selectItems value="#{selectOneRoomBean.rooms}" var="room" itemLabel="#{room}" itemValue="#{room}"></f:selectItems>
                       <p:ajax event="change" update=":limitForm" listener="#{selectOneRoomBean.room}"></p:ajax>
                   </p:selectOneMenu>
                   <h:outputText value="Test:"/>
                   <h:outputText value="#{selectOneRoomBean.room}"/>
               </h:panelGrid>
            </p:panel>

            <p:panel id="limitPanel" widgetVar="limitPanel">
                <h1 style="font-weight:normal">Limits
                </h1>

                <div class="p-field p-col-12 p-md-4">
                    <h5 style="margin-top:0">Temperature</h5>
                    <p:knob value="#{selectOneRoomBean.room.temperature}" labelTemplate="{value}°C" foregroundColor="red"> </p:knob>
                </div>

                <div class="p-field p-col-12 p-md-4">
                    <h5 style="margin-top:0">Humidity</h5>
                    <p:knob value="#{selectOneRoomBean.room.humidity}" labelTemplate="{value}%" foregroundColor="blue"> </p:knob>
                </div>

                <div class="p-field p-col-12 p-md-4">
                    <h5 style="margin-top:0">Airquality</h5>
                    <p:knob value="#{selectOneRoomBean.room.airquality}" max="5" foregroundColor="purple"> </p:knob>
                </div>

                <div class="p-field p-col-12 p-md-4">
                    <h5 style="margin-top:0">Volume</h5>
                    <p:knob value="#{selectOneRoomBean.room.volume}" foregroundColor="green"> </p:knob>
                </div>

                <div class="p-field p-col-12 p-md-4">
                    <h5 style="margin-top:0">Brightness</h5>
                    <p:knob value="#{selectOneRoomBean.room.brightness}" foregroundColor="orange"> </p:knob>
                </div>
            </p:panel>
        </h:form>
    </ui:define>
</ui:composition>

and here is my backing bean

package at.qe.skeleton.ui.beans;

import at.qe.skeleton.model.Room;
import at.qe.skeleton.services.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.inject.Inject;
import java.util.Collection;

@Component
@Scope("view")
public class SelectOneRoomBean {

    @Autowired
    private RoomService roomService;


    private Room room;

    public Collection<Room> getRooms() {
        return roomService.getAllRooms();}

    public void setRoom(Room room) {
        this.room = room;
        doReloadRoom();
    }

    public void doReloadRoom() {
        room = roomService.loadRoom(room.getRoomID());
    }

    public Room getRoom() {
        return room;
    }

}

Not sure if it matters, but this is my Room object:

package at.qe.skeleton.model;

import at.qe.skeleton.model.sensor.SensorDataType;
import org.springframework.data.domain.Persistable;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**Room is an Entity modeling an existing Room in an office, storing its associated sensors, the department it
 * belongs to, the type of room as well as a fixedOccupancy boolean
 */

@Entity
@Table(name = "room")
public class Room implements Serializable, Persistable<Long> {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private long roomID = 0;

    private Boolean fixedOccupancy;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "department_id")
    private Department department;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "room")
    private List<Employee> employees = new ArrayList<>();

    @Enumerated(EnumType.STRING)
    private RoomType roomType;

    @ElementCollection
    @MapKeyEnumerated(EnumType.STRING)
    private Map<SensorDataType, Long> limits = new HashMap<SensorDataType, Long>();

    public void setRoomID(long roomID) {
        this.roomID = roomID;
    }

    public Boolean getFixedOccupancy() {
        return fixedOccupancy;
    }

    public void setFixedOccupancy(Boolean fixedOccupancy) {
        this.fixedOccupancy = fixedOccupancy;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }

    public RoomType getRoomType() {
        return roomType;
    }

    public void setRoomType(RoomType roomType) {
        this.roomType = roomType;
    }

    public Map<SensorDataType, Long> getLimits() {
        return limits;
    }

    public void setLimits(Map<SensorDataType, Long> limits) {
        this.limits = limits;
    }

    public Long getTemperature() {
        return limits.get(SensorDataType.TEMPERATURE);
    }

    public void setTemperature(Long value) {this.limits.put(SensorDataType.TEMPERATURE, value);}


    public Long getHumidity() {
        return limits.get(SensorDataType.HUMIDITY);
    }

    public void setHumidity(Long value) {this.limits.put(SensorDataType.HUMIDITY, value);}

    public Long getAirquality() {
        return limits.get(SensorDataType.AIRQUALITY);
    }

    public void setAirquality(Long value) {this.limits.put(SensorDataType.AIRQUALITY, value);}


    public Long getVolume() {
        return limits.get(SensorDataType.VOLUME);
    }

    public void setVolume(Long value) {this.limits.put(SensorDataType.VOLUME, value);}


    public Long getBrightness() {
        return limits.get(SensorDataType.BRIGHTNESS);
    }

    public void setBrightness(Long value) {this.limits.put(SensorDataType.BRIGHTNESS, value);}



    public long getRoomID() {
        return roomID;
    }


    @Override
    public Long getId() {
        return this.roomID;
    }

    @Override
    public boolean isNew() {
        return (this.roomID == 0);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Room room = (Room) o;
        return getId().equals(room.getId());
    }

    @Override
    public String toString() {
        return roomType + " ID: " + roomID;
    }
}

Any help is greatly appreciated. I am somewhat of a noobie when it comes to java and this is my first ever posting here, so apologies for any mistakes.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source