Skip to content

Commit

Permalink
Added order list for the products
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristof Ryheul committed Aug 21, 2009
1 parent 66d607a commit c94af0c
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 0 deletions.
55 changes: 55 additions & 0 deletions lunch_wicket/src/main/java/com/melexis/DetachableModelOrder.java
Original file line number Diff line number Diff line change
@@ -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<ModelOrder> {

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;
}

}
47 changes: 47 additions & 0 deletions lunch_wicket/src/main/java/com/melexis/ModelOrder.java
Original file line number Diff line number Diff line change
@@ -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;
}





}
32 changes: 32 additions & 0 deletions lunch_wicket/src/main/java/com/melexis/OrderProduct.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!--
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.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Order Lunch</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<table wicket:id="table">
<tr>
<td><input type="text" wicket:id="quantity" /></td>
<td><span wicket:id="name" /></td>
<td><span wicket:id="price" /></td>
</tr>
</table>
</body>
</html>
76 changes: 76 additions & 0 deletions lunch_wicket/src/main/java/com/melexis/OrderProduct.java
Original file line number Diff line number Diff line change
@@ -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<DetachableModelOrder> refreshingView = new RefreshingView<DetachableModelOrder>("table") {

@Override
protected Iterator<IModel<DetachableModelOrder>> getItemModels() {
List<Product> products = productRepository.findAvailableProducts();
List<IModel<DetachableModelOrder>> models = new ArrayList<IModel<DetachableModelOrder>>();

for (Product p:products)
{
DetachableModelOrder model = new DetachableModelOrder(productRepository, p.getId());
Model<DetachableModelOrder> m = new Model<DetachableModelOrder>(model);

models.add(m);
}

return models.iterator();
}

@Override
protected void populateItem(Item<DetachableModelOrder> 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);
}
}

0 comments on commit c94af0c

Please sign in to comment.