!!Sorgenti del corso
[{TableOfContents }]

%%tabbedSection 
%%tab-Parte1
!!Parte 1
!index.html (v1)
%%prettify 
{{{
<?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">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Pagina Iniziale</title>
    </h:head>
    <h:body>
        <h:form>
            <h:commandButton action="views/list" value="Entra"/>
        </h:form>
    </h:body>
</html>
}}}
/%

!list.xhtml (v1)
%%prettify 
{{{
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
   xmlns:ui="http://java.sun.com/jsf/facelets">

    <h:head>
        <title>Pagina Iniziale</title>
    </h:head>
    <h:body>
        <h1><h:outputText value="Hello World"/></h1>
    </h:body>

</html>
}}}
/%

/%
%%tab-Parte2

!!Parte 2
[{Image src='./cartella/sottocartella/header_logo.gif'}]

!masterLayout.xhtml (v1)
%%prettify 
{{{
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
   xmlns:ui="http://java.sun.com/jsf/facelets">

    <h:head>
        <title>Pagina Iniziale</title>
    </h:head>
    <h:body>
        <div id="header">
            <h:graphicImage library="images" name="header_logo.gif"/>
            JSF Sample Application
        </div>
        <h:form>
            <ui:insert name="content"/>
        </h:form>
    </h:body>

    <h:outputStylesheet library="css" name="style.css"/>
</html>

}}}
/%

!index.xhtml (v2)
%%prettify 
{{{
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                template="/template/masterLayout.xhtml">

    <ui:define name="content">
        <h:commandButton action="views/list" value="Entra"/>
    </ui:define>

</ui:composition>
}}}
/%



!list.xhtml (v2)
%%prettify 
{{{
<ui:composition
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                template="/template/masterLayout.xhtml">

    <ui:define name="content">
        <h1>
            <h:outputText value="Hello World!!!"/>
        </h1>
    </ui:define>

</ui:composition>
}}}
/%


!style.css (v1)
%%prettify 
{{{
body {
    font-family: Verdana,Arial,Helvetica,sans-serif;
    font-size: 11px;
    margin: 0px;
    padding: 0px;
}

#header {
    width: 100%;
    margin-bottom: 10px;
    border-bottom: 2px solid #999;
    color:#000066;
    font-size: 30px;
}
}}}
/%

/%
%%tab-Parte3

!!Parte 3
!ApplicationController.java
%%prettify 
{{{
package corsojsf.managedbeans;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;

@ManagedBean
@RequestScoped
public class ApplicationController {

    private int random=-1;

    public String paginaLista(){
        return "views/list";
    }

    public int getNumeroRandom(){
        if (random==-1)
            random= (int)(Math.random()*100);
        return random;
    }

}

}}}
/%

!marsterLayout.xhtml (porzione)
%%prettify 
{{{
[...]
JSF Sample Application #{applicationController.numeroRandom}
[...]
}}}
/%

!index.xhtml (porzione)
%%prettify 
{{{
[...]
 <ui:define name="content">
        <h:commandButton action="#{applicationController.paginaLista}" value="Entra"/>
 </ui:define>
[...]
}}}
/%

/%
%%tab-Parte4

!!Parte 4
!Camera.java
%%prettify 
{{{
package corsojsf.model;

import java.math.BigDecimal;

public class Camera {

     private String nome;
     private Integer numeroPosti;
     private BigDecimal prezzo;
 
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public Integer getNumeroPosti() {
        return numeroPosti;
    }
    public void setNumeroPosti(Integer numeroPosti) {
        this.numeroPosti = numeroPosti;
    }
    public BigDecimal getPrezzo() {
        return prezzo;
    }
    public void setPrezzo(BigDecimal prezzo) {
        this.prezzo = prezzo;
    }

}
}}}
/%

!Prenotazione.java
%%prettify 
{{{
package corsojsf.model;

import java.util.Date;

public class Prenotazione {

    private String cliente;
    private Date dataInizio;
    private Date dataFIne;
    private Camera camera;

    public Camera getCamera() {
        return camera;
    }
    public void setCamera(Camera camera) {
        this.camera = camera;
    }
    public String getCliente() {
        return cliente;
    }
    public void setCliente(String cliente) {
        this.cliente = cliente;
    }
    public Date getDataFIne() {
        return dataFIne;
    }
    public void setDataFIne(Date dataFIne) {
        this.dataFIne = dataFIne;
    }
    public Date getDataInizio() {
        return dataInizio;
    }
    public void setDataInizio(Date dataInizio) {
        this.dataInizio = dataInizio;
    }
}

}}}
/%

!list.xhtml
%%prettify 
{{{
<ui:composition
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                template="/template/masterLayout.xhtml"
                xmlns:f="http://java.sun.com/jsf/core">
                
    <ui:define name="content">
        <ui:include src="/sections/list/intestazioneCamere.xhtml">
            <ui:param name="titolo" value="Lista Camere"/>
        </ui:include>
        <ui:include src="/sections/list/tabellaCamere.xhtml"/>
    </ui:define>

</ui:composition>
}}}
/%

!tabellaCamere.xhtml
%%prettify 
{{{
<ui:composition
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core">

    <h:dataTable styleClass="lista" value="#{listController.camere}" var="camera">
            <h:column>
                <f:facet name="header">
                    <h:outputText value="nome"/>
                 </f:facet>
                <h:outputText value="#{camera.nome}"/>
            </h:column>
            <h:column>
                <f:facet name="header">
                    <h:outputText value="n.posti"/>
                 </f:facet>
                <h:outputText value="#{camera.numeroPosti}"/>
            </h:column>
            <h:column>
                <f:facet name="header">
                    <h:outputText value="prezzo"/>
                 </f:facet>
                <h:outputText value="#{camera.prezzo}"/>
            </h:column>
            <h:column>
                <h:outputLink value="Edit"/>
                #160;
                <h:outputLink value="Del"/>
            </h:column>
         </h:dataTable>

</ui:composition>
}}}
/%

!intestazioneCamere.xhtml
%%prettify 
{{{
<ui:composition
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core">
        <h3>
            <h:outputText value="Lista Camere"/>
        </h3>
        <h:commandLink action="#{listController.nuovaCamera}" value="Nuova"/>
</ui:composition>
}}}
/%


!intestazioneCamere.xhtml (porzione)
%%prettify 
{{{
[...]
    <h3>
        <h:outputText value="#{titolo}"/>
    </h3>
    <h3>
        <h:outputText style="display:block" value="Nessuna camera trovata"
                        rendered="#{empty listController.camere}"/>
    </h3>
[...]
}}}
/%


!tabellaCamere.xhtml (porzione)
%%prettify 
{{{
[...]
   <h:dataTable styleClass="lista" value="#{listController.camere}"
            var="camera" rendered="#{!empty listController.camere}">
[...]
}}}
/%
/%

%%tab-Parte5

!!Parte 5
!camera.xhtml
%%prettify 
{{{
<ui:composition
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                template="/template/masterLayout.xhtml"
                xmlns:f="http://java.sun.com/jsf/core">

    <ui:define name="content">
        <h:panelGrid columns="2">
            <h:outputLabel for="nome" value="nome camera"/>
            <h:inputText id="nome" value="#{listController.camera.nome}"/>
            <h:outputLabel for="posti" value="posti letto"/>
            <h:inputText id="posti" value="#{listController.camera.numeroPosti}"/>
            <h:outputLabel for="prezzo" value="prezzo"/>
            <h:inputText id="prezzo" value="#{listController.camera.prezzo}"/>
            <h:commandButton value="annulla" action="/views/list" immediate="true"/>
            <h:commandButton value="salva" action="#{listController.salvaCamera}"/>
        </h:panelGrid>
    </ui:define>

</ui:composition>
}}}
/%

!ListController.java (porzione)
%%prettify 
{{{
   /*Azioni*/
    public String nuovaCamera() {
        setCamera(new Camera());
        return "/views/camera";
    }

        public String salvaCamera() {
        Camera daSalvare = getCamera();
        if (getCamere() == null) {
            this.camere = new LinkedList<Camera>();
        }
        if (estraiCamera(daSalvare.getNome()) != null) {
            getCamere().remove(estraiCamera(daSalvare.getNome()));
        }
        getCamere().add(daSalvare);
        setCamera(null);
        return "/views/list";
    }

    private Camera estraiCamera(String nome) {
        for (Camera c : camere) {
            if (c.getNome().equalsIgnoreCase(nome)) {
                return c;
            }
        }
        return null;
    }
}}}
/%
/%

%%tab-Parte6
!!Parte 6

!list.xhtml (porzione)
%%prettify 
{{{
        <h:column>
           <h:commandLink value="Edit" action="#{listController.modificaCamera(camera)}"/>
             -
            <h:commandLink value="Del" action="#{listController.eliminaCamera(camera)}"/>
        </h:column>
}}}
/%


!ListController.java (porzione)
%%prettify 
{{{

   public String modificaCamera(Camera camera) {
        setCamera(camera);
        return "/views/camera";
    }
    
    
    public void eliminaCamera(Camera camera)
    {
        getCamere().remove(camera);
    }

}}}
/%

/%

%%tab-Parte7
!!Parte 7

!prenotazione.xhtml
%%prettify 
{{{
        <ui:composition
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    template="/template/masterLayout.xhtml"
    xmlns:f="http://java.sun.com/jsf/core">

    <ui:define name="content">
        <h:panelGrid columns="2">
            <h:outputLabel for="camera" value="camera"/>
            <h:selectOneMenu id="camera" value="#{listController.prenotazione.camera}">
                <f:selectItems value="#{listController.camere}" var="camera" itemValue="#{camera}" itemLabel="#{camera.nome}"/>
            </h:selectOneMenu>
            <h:outputLabel for="cliente" value="cliente"/>
            <h:inputText id="cliente" value="#{listController.prenotazione.cliente}" required="true">
            </h:inputText>
            <h:outputLabel for="dal" value="Dal"/>
            <h:inputText id="dal" value="#{listController.prenotazione.dataInizio}" required="true">
                <f:convertDateTime pattern="yyyymmdd" />
            </h:inputText>
            <h:outputLabel for="al" value="Al"/>
            <h:inputText id="al" value="#{listController.prenotazione.dataFine}" required="true">
                <f:convertDateTime pattern="yyyymmdd" />
            </h:inputText>
            <h:commandButton value="annulla" action="/views/list" immediate="true"/>
            <h:commandButton value="salva" action="#{listController.salvaPrenotazione}"/>
        </h:panelGrid>
    </ui:define>
</ui:composition>
}}}
/%


!CameraConverter.java
%%prettify 
{{{

   package corsojsf.managedbeans;

import corsojsf.model.Camera;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItems;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

@FacesConverter(forClass=Camera.class)
public class CameraConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        List<Camera> items = (List<Camera>) ((UISelectItems) component.getChildren().get(0)).getValue();
        for (Camera camera : items) {
            if (camera.getNome().equals(value)) {
                return camera;
            }
        }
        throw new ConverterException("Impossibile convertire " + value);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value instanceof Camera) {
            return ((Camera) value).getNome();
        } else {
            throw new IllegalArgumentException("Impossibile convertire oggetti " + value.getClass().getCanonicalName());
        }
    }
}


}}}
/%

!ListController.java (porzione)
%%prettify 
{{{

[...]
 public String nuovaPrenotazione() {
        setPrenotazione(new Prenotazione());
        return "/views/prenotazione";
    }

    public String modificaPrenotazione(Prenotazione prenotazione) {
        setPrenotazione(prenotazione);
        return "/views/prenotazione";
    }


     public void eliminaPrenotazione(Prenotazione prenotazione)
    {
        getPrenotazioni().remove(prenotazione);
    }

    public String salvaPrenotazione() {
        Prenotazione daSavlare = getPrenotazione();
        if (getPrenotazioni() == null) {
            this.prenotazioni = new LinkedList<Prenotazione>();
        }
        if (this.prenotazioni.contains(daSavlare)) {
            getPrenotazioni().remove(daSavlare);
        }
        getPrenotazioni().add(daSavlare);
        setPrenotazione(new Prenotazione());
        return "/views/list";
    }
[...]    
    
}}}
/%

/%



/%
/%