Hi,
It is my first experience with Wicket.
I try to do a e-commerce site, based on cheese shop from Wicket in Action Book.
I have problem to implement shopping cart to set the quantity of each item that 
the user choose.
As you can see in this screenshot, I put a TextField for quantity in a ListView 
of all items that the user put in the shopping cart.
At beginning all quantity value are set to 1.
The problem is that it works correctly only if I have only one item. If I have 
two or more items, I can set correctly only the last quantity.
Foe example, If I try to set quantity of the first item, the value that will be 
assigned to it will be the value of the last item.
Thank you for help!

http://sacredspirit.altervista.org/images/screenshot.jpg

QuantFUORI=1
QuantFUORI=1
QuantSTR=1
QuantFORM=2
Quantity Value changed
QuantFUORI=1
QuantFUORI=2
QuantSTR=1
QuantFORM=2
Quantity Value changed
QuantFUORI=2
QuantFUORI=2

This is the complete source code of Checkout.java

package org.wicket.example.pages;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;

import java.io.IOException;
import java.io.Serializable;
import java.text.NumberFormat;

import java.io.Serializable;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.behavior.AbstractBehavior;
import org.apache.wicket.extensions.yui.calendar.DateField;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.RefreshingView;
import org.apache.wicket.markup.repeater.ReuseIfModelsEqualStrategy;
import org.apache.wicket.markup.repeater.util.ModelIteratorAdapter;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;


import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.Radio;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.markup.html.form.RadioGroup;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.behavior.AbstractBehavior;
import org.apache.wicket.extensions.yui.calendar.DateField;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.RefreshingView;
import org.apache.wicket.markup.repeater.ReuseIfModelsEqualStrategy;
import org.apache.wicket.markup.repeater.util.ModelIteratorAdapter;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.validation.validator.EmailAddressValidator;

import org.wicket.example.Address;
import org.wicket.example.Pagamento;
import org.wicket.example.Cart;
import org.wicket.example.C;
import org.wicket.example.pages.CheesrPage;
import org.wicket.example.MainApplication;

public class Checkout extends CheesrPage {
        private Cart cart;
        
        
public Checkout() { 
add(new FeedbackPanel("feedback"));     
add(new ListView("cart", new PropertyModel(this, "cart.cheeses")) { 
        public Model quantitaMOD;
    String cat,articolo;
        @Override
        protected void populateItem(ListItem item) {
            final C cheese = (C) item.getModelObject();

            cat = cheese.getCat1();
            articolo = (cat+cheese.getArt());
            NumberFormat nf = NumberFormat.getCurrencyInstance();
            String prezzopubSTR=cheese.getPrezzopub();
            double prezzopubNUM=Double.parseDouble(prezzopubSTR);

            // immagine piccola dell'oggetto
            ExternalLink elink = new ExternalLink("OggettoLink", 
"http://sacredspirit.dyndns.org/"+cat.toLowerCase()+"/"+articolo.toLowerCase()+"_1.jpg"
 ); 
            final String imageUrl = 
"http://sacredspirit.dyndns.org/"+cat.toLowerCase()+"/"+articolo.toLowerCase()+"_1_piccola.jpg";
  // partnerIconUrl; 
            WebMarkupContainer srcTag = new WebMarkupContainer("OggettoIcon"); 
            IModel mediaSrc = new AbstractReadOnlyModel() { 
                @Override 
                public String getObject() { return imageUrl; } 
                }; 
            srcTag.add(new AttributeModifier("src", mediaSrc));
            elink.add(srcTag); item.add(elink); 

        item.add(new Label("articolo", articolo));
        item.add(new Label("descrizione",cheese.getDescr_it()));
            item.add(new Label("prezzo", 
nf.format(Double.parseDouble(cheese.getPrezzopub()))));
            quantitaMOD = new Model(cheese.getQuantita());
            final String quantitaSTR = (String) quantitaMOD.getObject();
                System.out.println ("QuantFUORI="+quantitaSTR);
            Form fquantita = new Form("fquantita") {
                protected void onSubmit() {
                        String quantitaFORM = (String) quantitaMOD.getObject();
                        //Aggiorna quantita
                        //String quantitaSTR = (String) quantitaMOD.getObject();
                        System.out.println ("QuantSTR="+quantitaSTR);
                        System.out.println ("QuantFORM="+quantitaFORM);
                        //C selected = (C) item.getModelObject();
                        //selected.setQuantita(quantitaFORM);
                        cheese.setQuantita(quantitaFORM);
                        System.out.println ("Quantity Value changed");
                        setResponsePage(Checkout.class);
                        } // OnSubmit
            };  // form
                TextField quantita = new TextField("quantita", quantitaMOD);
            fquantita.add(quantita.setRequired(true));
            item.add(fquantita);        

            item.add(new Label("subtotale", 
nf.format(Double.parseDouble(cheese.getPrezzopub())*Double.parseDouble(cheese.getQuantita()))
 ));           
            item.add(new Label("disp", cheese.getDisp()));
            item.add(removeLink("rimuovi", item));
            }  // populate item
        }); // ListView
        add(new Label("total", new Model() {
                @Override
            public Serializable getObject() { //era  public Object getObject() {
                NumberFormat nf = NumberFormat.getCurrencyInstance();
                return nf.format(getCart().getTotal()+6.98);
                        }
           }));

Form form = new Form("form");
        Address address = getCart().getBillingAddress();
        form.add(new TextField("name", new PropertyModel(address, 
"name")).setRequired(true));
        form.add(new TextField("street", new 
PropertyModel(address,"street")).setRequired(true));
    form.add(new TextField("zipcode", new 
PropertyModel(address,"zipcode")).setRequired(true));
    form.add(new TextField("city", new PropertyModel(address, 
"city")).setRequired(true));
    form.add(new TextField("email", new PropertyModel(address, 
"email")).setRequired(true));
    form.add(new TextField("telefono", new PropertyModel(address, "telefono")) 
);
    form.add(new TextField("data", new PropertyModel(address, "data")) );
    form.add(new TextField("note", new PropertyModel(address, "note")) );

    final Pagamento bean=new Pagamento();
    RadioGroup myInt = new RadioGroup("pagamento", new PropertyModel(bean, 
"myInt"));
    myInt.add(new Radio("1", new Model(1)));
    myInt.add(new Radio("2", new Model(2)));
    myInt.add(new Radio("3", new Model(3)));
    myInt.add(new Radio("4", new Model(4)));
    myInt.add(new Radio("5", new Model(5)));
    form.add(myInt.setRequired(true));
        
form.add(new Button("order") {
        @Override
    public void onSubmit() {
                String pagamentoSTR="Non è stato scelto il pagamento";
        int pagamentoINT=bean.getMyInt();
        System.out.println ("Tipo pagamento:"+pagamentoINT);
        if (pagamentoINT==1) pagamentoSTR="Solo richiesta informazioni";
        if (pagamentoINT==2) pagamentoSTR="Ritiro e pagamento di persona";
        if (pagamentoINT==3) pagamentoSTR="Ricarica Postepay";
        if (pagamentoINT==4) pagamentoSTR="Bonifico Bancario";
        if (pagamentoINT==5) pagamentoSTR="Paypal e carte di credito";          
        System.out.println ("ORDINE:"+getCart().getOrdine());
        // charge customers� credit card
        // ship cheeses to our customer
        // clean out shopping cart
                Cart cart = getCart();
                try  {
             Properties props = System.getProperties ();
             props.put ("mail.smtp.host", "smtp.tiscali.it");
             Session session = Session.getDefaultInstance (props, null);
             MimeMessage message = new MimeMessage (session);
             message.setFrom (new InternetAddress 
(getCart().getBillingAddress().getEmail()));
             message.addRecipient (Message.RecipientType.TO, new 
InternetAddress ("x...@xxxdspirit.it"));
             message.addRecipient (Message.RecipientType.BCC, new 
InternetAddress ("xx...@gmail.com"));
             message.setSubject ("ORDINE DIRETTO dal sito SacredSpirit.it");
             message.setText (getCart().getOrdine()+"\n\nNOME: 
"+getCart().getBillingAddress().getName()+"\nINDIRIZZO: 
"+getCart().getBillingAddress().getStreet()+"\nCAP: 
"+getCart().getBillingAddress().getZipcode()+"\nCITTA': 
"+getCart().getBillingAddress().getCity()+"\nEMAIL: 
"+getCart().getBillingAddress().getEmail()+"\nTELEFONO: 
"+getCart().getBillingAddress().getTelefono()+"\nDATA: 
"+getCart().getBillingAddress().getData()+"\nPAGAMENTO: "+pagamentoSTR+"\nNOTE: 
"+getCart().getBillingAddress().getNote() );
             Transport.send (message);
             setResponsePage(Ok.class);
             }
             catch (Exception e)
             {
                 System.out.println ("ERRORE INVIO MAIL ORDINE: "+e);
                 setResponsePage(Error.class);
             }
        } // Submit
});
add(form);

add(new Link("shop") {
      @Override
      public void onClick() {
                setResponsePage(Home.class);
                }
        }); // add new link

} // public checkout    
} // public class Checkout extends CheesrPage
--------------------------------------------------------

This is the complete source code of Cart.java if you need more, please tell me:

package org.wicket.example;

// package wicket.in.action.chapter03.section_3_1;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

public class Cart implements java.io.Serializable {
  private static final long serialVersionUID = 1L;

  private List<C> cheeses = new ArrayList<C>();

  private Address billingAddress = new Address();

  public List<C> getCheeses() {
    return cheeses;
  }
  
  public void setCheeses(List<C> other) {
    cheeses = other;
  }

  public Address getBillingAddress() {
    return billingAddress;
  }

  public void setBillingAddress(Address other) {
    billingAddress = other;
  }
  public double getTotal() {
    double total = 0;
    for (C cheese : cheeses) {
      total += 
Double.parseDouble(cheese.getPrezzopub())*Double.parseDouble(cheese.getQuantita());
    }
    return total;
  }
  public String getOrdine() {
            double total = 0;
            String ordine="ORDINE DIRETTO DAL SITO:\n";
        NumberFormat nf = NumberFormat.getCurrencyInstance();
            for (C cheese : cheeses) {
              ordine = ordine + "ARTICOLO: "+cheese.getCat1()+cheese.getArt()+" 
 DESCRIZIONE: "+cheese.getDescr_it()+"  PREZZO: "+cheese.getPrezzopub()+"  
QUANTITA': "+cheese.getQuantita()+"  SUBTOT: 
"+nf.format(Double.parseDouble(cheese.getPrezzopub())*Double.parseDouble(cheese.getQuantita()))
 +"  DISP: "+cheese.getDisp()+"\n";
              total += 
Double.parseDouble(cheese.getPrezzopub())*Double.parseDouble(cheese.getQuantita());
                    }
            ordine = ordine + "Totale: "+ nf.format(total+6.98);
            return ordine;
          }

}

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to