0

I'm facing some problems using the beans and pages with JSF, when I need to pass an object between two different beans/pages. The idea is to have a page, where I've the list of all the orders, with the possibility to click on the edit command button to edit it, or to create a new one. So this is the orders.xhtml part, where there is a dataTable, within a form:

<p:column style="width:32px;text-align: center">
                            <p:commandButton icon="ui-icon-edit" title="View"
                                action="#{createOrderSelectionView.onEditProductionOrder(productionOrder)}">
                                <f:param name="productionOrder" value="#{productionOrder.pk}"></f:param>
                            </p:commandButton>

And this is the part to create a new one:

f:facet name="left">
                            <p:button value="Nuova bolla" icon="ui-icon-plus"
                                outcome="#{createOrderSelectionView.onNewProductionOrder()}" />
                        </f:facet>

This is the destination beans:

    @Named
@RequestScoped
public class CreateOrderSelectionView implements Serializable {

    @Inject
    CompanyController companyController;
    @Inject
    MachineController machineController;
    @Inject
    ProductionOrderController productionOrderController;

    private ProductionOrder productionOrder;
    private DualListModel<Machine> machines;

    @PostConstruct
    public void init() {
        System.out.println(productionOrder == null);
        productionOrder = new ProductionOrder();
        List<Machine> machinesSource = machineController.getAllMachines();
        List<Machine> machinesTarget = new ArrayList<Machine>();
        machines = new DualListModel<Machine>(machinesSource, machinesTarget);
    }

    public String onCompletedSave() {
        productionOrder.setMachines(machines.getTarget());
        productionOrderController.store(productionOrder);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                "La bolla n° " + productionOrder.getOrderNumber() + " è stata correttamente salvata.", null));
        FacesContext.getCurrentInstance().getExternalContext().getFlash().setKeepMessages(true);
        productionOrder = null;
        return "orders?faces-redirect=true";
    }

    public ProductionOrder getProductionOrder() {
        return productionOrder;
    }

    public void setProductionOrder(ProductionOrder productionOrder) {
        this.productionOrder = productionOrder;
    }

    public String onNewProductionOrder() {
        this.productionOrder = new ProductionOrder();
        productionOrder.setCompany(new Company());
        List<Machine> machinesSource = machineController.getAllMachines();
        List<Machine> machinesTarget = new ArrayList<Machine>();
        machines = new DualListModel<Machine>(machinesSource, machinesTarget);
        return "createProductionOrder?faces-redirect=true";
    }

    public String onEditProductionOrder(ProductionOrder productionOrder) {
        this.productionOrder = productionOrderController.findProductionOrderByPk(productionOrder);
        List<Machine> machinesSource = new ArrayList<>();
        List<Machine> machinesTarget = productionOrder.getMachines();
        System.out.println(machinesTarget.size());
        for (Machine machine : machineController.getAllMachines()) {
            boolean found = false;
            for (Machine machine2 : machinesTarget) {
                if (machine.getPk().equals(machine2.getPk())) {
                    found = true;
                }
            }
            if (!found) {
                machinesSource.add(machine);
            }
        }

        machines = new DualListModel<Machine>(machinesSource, machinesTarget);
        return "createProductionOrder";
    }

    public DualListModel<Machine> getMachines() {
        return machines;
    }

    public void setMachines(DualListModel<Machine> machines) {
        this.machines = machines;
    }

    public List<ShoeSizeAndQuantity> getShoeSizeAndQuantities() {
        /*
         * if (productionOrder.getShoeSizeAndQuantities() == null) {
         * List<ShoeSizeAndQuantity> shoeSizeAndQuantities = new ArrayList<>(); for (int
         * i = 38; i <= 47; i++) { shoeSizeAndQuantities.add(new
         * ShoeSizeAndQuantity(String.valueOf(i), productionOrder));
         * shoeSizeAndQuantities.add(new ShoeSizeAndQuantity(String.valueOf(i + 0.5),
         * productionOrder)); }
         * productionOrder.setShoeSizeAndQuantities(shoeSizeAndQuantities); } return
         * productionOrder.getShoeSizeAndQuantities();
         */
        return new ArrayList<>();
    }

    public void setShoeSizeAndQuantities(List<ShoeSizeAndQuantity> shoeSizeAndQuantities) {
        productionOrder.setShoeSizeAndQuantities(shoeSizeAndQuantities);
    }

    public List<Company> completeTextCompany(String query) {
        String lower = query.toLowerCase();
        List<Company> companies = companyController.getAllClients();
        List<Company> result = new ArrayList<>();
        for (Company company : companies) {
            if (company.getName().toLowerCase().startsWith(lower) || company.getVatNumber().startsWith(lower)) {
                result.add(company);
            }
        }
        return result;
    }

    public boolean canDelete() {
        return productionOrder != null && productionOrder.getPk() != null;
    }

    public String remove() {
        System.out.println(productionOrder.getPk());
        productionOrderController.remove(productionOrder);
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_INFO,
                        "La bolla n° " + productionOrder.getOrderNumber() + " è stata eliminata correttamente.",
                        "Bolla cancellata!"));
        productionOrder = null;
        return "orders?faces-redirect=true";
    }

}

The problem is that with no redirect, I can't pass the data to the new beans. Is something wrong with my codes?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
I love coding
  • 1,183
  • 3
  • 18
  • 47

0 Answers0