So I have the following code:
<h:dataTable value="#{findRepairsBean.repairs}" var="r"
styleClass="repair-table"
headerClass="repair-table-header"
rowClasses="repair-table-odd-row,repair-table-even-row">
<h:column>
<f:facet name="header">Product</f:facet>
#{r.item.productName}
</h:column>
<h:column>
<f:facet name="header">Brand</f:facet>
#{r.item.brand}
</h:column>
<h:column>
<f:facet name="header">Category</f:facet>
#{r.item.category}
</h:column>
<h:column>
<!-- column header -->
<f:facet name="header">Defect</f:facet>
<!-- row record -->
#{r.details.defect}
</h:column>
<h:column>
<f:facet name="header">Description</f:facet>
#{r.details.description}
</h:column>
<h:column>
<f:facet name="header">Status</f:facet>
#{r.details.status}
</h:column>
<h:column>
<h:outputLabel for="price" value="Price: "/>
<h:inputText id="price" value="#{findRepairsBean.price}"/>
<h:message for="price" styleClass="error" />
</h:column>
<h:column>
<h:commandButton styleClass="mybutton"
value="Place Bid" action="#{findRepairsBean.placeBid(???)}"/>
</h:column>
</h:dataTable>
This is my bean:
@Component
@Scope("session")
public class FindRepairsBean implements Serializable{
@Autowired
private RepairService repairService;
@Autowired
private UserService userService;
private String keyWord;
private int price;
private List<Repair> repairs;
//private Repair repair;
@Inject
private UserBean userBean;
public void findRepairs(){
repairs = repairService.findRepairsByKeyword(keyWord);
}
public void placeBid(Repair repair){
try {
Repairer repairer = (Repairer) userService.getUser(userBean.getUsername());
repairService.placeBid(repairer, repair, price);
} catch(UserServiceException ex){
Logger.getLogger(TestRepairCafe.class.getName()).log(Level.SEVERE, null, ex);
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(ex.getLocalizedMessage())); // Add global message
}
}
public String getKeyWord() {
return keyWord;
}
public void setKeyWord(String keyWord) {
this.keyWord = keyWord;
}
public List<Repair> getRepairs() {
return repairs;
}
public void setRepairs(List<Repair> repairs) {
this.repairs = repairs;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
And this is the model of Repair:
public class Repair implements Comparable<Repair>, Serializable
{
private static int repairCounter = 0;
private final int repairId;
private Client client;
private Repairer repairer;
private RepairDetails details;
private Item item;
private final List<Bid> bids;
private Repair()
{
this.repairId = repairCounter++;
this.bids = new ArrayList<>();
}
public Repair(Item item, RepairDetails repDetails)
{
this();
this.item = item;
this.details = repDetails;
}
public void addBid(Bid bid)
{
this.bids.add(bid);
}
public void addClient(Client client)
{
this.client = client;
}
public void setRepairer(Repairer repairer)
{
this.repairer = repairer;
}
public int getRepairId()
{
return repairId;
}
public List<Bid> getBids()
{
return bids;
}
public RepairDetails getDetails()
{
return details;
}
public Client getClient()
{
return client;
}
public Repairer getRepairer()
{
return repairer;
}
public Item getItem()
{
return item;
}
public void setDetails(RepairDetails details)
{
this.details = details;
}
public void setItem(Item item)
{
this.item = item;
}
@Override
public int compareTo(Repair o)
{
return this.details.getSubmitDate().compareTo(o.getDetails().getSubmitDate());
}
@Override
public String toString()
{
return String.format("%s -%s", this.getDetails().getDescription(), item.getProductName());
}
}
Now, in this xhtml page I am making a datatable of all repairs which contain a keyword, which is working perfectly. However the placeBid() method in my bean is supposed to place a bid on a Repair, only I do not know how to give the current Repair in the table to the placeBid() method. I would expect I could just do it with #{r}, however this is not compiling. I want to store each repair in the list somehow so I can acces it in my method for each row in the table.