diff --git a/lunch_wicket/src/main/java/com/melexis/DetachableModelOrder.java b/lunch_wicket/src/main/java/com/melexis/DetachableModelOrder.java new file mode 100644 index 0000000..c9e43c7 --- /dev/null +++ b/lunch_wicket/src/main/java/com/melexis/DetachableModelOrder.java @@ -0,0 +1,55 @@ +/* + * Copyright 2009 kry. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * under the License. + */ + +package com.melexis; + +import com.melexis.repository.ProductRepository; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.apache.wicket.model.LoadableDetachableModel; + +/** + * + * @author kry + */ +public class DetachableModelOrder extends LoadableDetachableModel { + + private final ProductRepository productRepository; + private final int id; + + public DetachableModelOrder(ProductRepository productRepository, int id) + { + this.id = id; + this.productRepository = productRepository; + } + + @Override + protected ModelOrder load() { + Product p; + + try { + p = productRepository.findById(id); + + return new ModelOrder(p); + } catch (ProductNotFoundException ex) { + Logger.getLogger(DetachableModelOrder.class.getName()).log(Level.SEVERE, null, ex); + } + + return null; + } + +} diff --git a/lunch_wicket/src/main/java/com/melexis/ModelOrder.java b/lunch_wicket/src/main/java/com/melexis/ModelOrder.java new file mode 100644 index 0000000..32c1541 --- /dev/null +++ b/lunch_wicket/src/main/java/com/melexis/ModelOrder.java @@ -0,0 +1,47 @@ +/* + * Copyright 2009 kry. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * under the License. + */ + +package com.melexis; + +/** + * + * @author kry + */ +public class ModelOrder extends Product { + + private int quantity; + + public ModelOrder(Product p) + { + super(p.getName(), p.getPrice()); + + this.quantity = 0; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + + + + +} diff --git a/lunch_wicket/src/main/java/com/melexis/OrderProduct.html b/lunch_wicket/src/main/java/com/melexis/OrderProduct.html new file mode 100644 index 0000000..b01e8d4 --- /dev/null +++ b/lunch_wicket/src/main/java/com/melexis/OrderProduct.html @@ -0,0 +1,32 @@ + + + + + Order Lunch + + + + + + + + + +
+ + diff --git a/lunch_wicket/src/main/java/com/melexis/OrderProduct.java b/lunch_wicket/src/main/java/com/melexis/OrderProduct.java new file mode 100644 index 0000000..d94a407 --- /dev/null +++ b/lunch_wicket/src/main/java/com/melexis/OrderProduct.java @@ -0,0 +1,76 @@ +/* + * Copyright 2009 kry. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * under the License. + */ +package com.melexis; + +import com.melexis.repository.OrderRepository; +import com.melexis.repository.ProductRepository; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import org.apache.wicket.markup.repeater.Item; +import org.apache.wicket.model.IModel; +import org.apache.wicket.spring.injection.annot.SpringBean; +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.markup.html.basic.Label; +import org.apache.wicket.markup.html.form.TextField; +import org.apache.wicket.markup.repeater.RefreshingView; +import org.apache.wicket.model.Model; +import org.apache.wicket.model.PropertyModel; + +/** + * + * @author kry + */ +public class OrderProduct extends WebPage { + + @SpringBean + private ProductRepository productRepository; + + public OrderProduct() { + + RefreshingView refreshingView = new RefreshingView("table") { + + @Override + protected Iterator> getItemModels() { + List products = productRepository.findAvailableProducts(); + List> models = new ArrayList>(); + + for (Product p:products) + { + DetachableModelOrder model = new DetachableModelOrder(productRepository, p.getId()); + Model m = new Model(model); + + models.add(m); + } + + return models.iterator(); + } + + @Override + protected void populateItem(Item item) { + DetachableModelOrder m = item.getModelObject(); + ModelOrder model = m.getObject(); + + item.add(new TextField("quantity", new PropertyModel(model, "quantity"))); + item.add(new Label("name", model.getName())); + item.add(new Label("price", model.getPrice().toString())); + } + }; + + add(refreshingView); + } +}