Wiki

Imprimir Propiedades
Bookshop_business

Some operations need to be added to the domain elements that were defined previously. They will be included in their corresponding classes:
 - Cart.java: A checkOut method will be included that will mark all of its products as "sold". Afterwards, the cart will be emptied.
 - Order.java: acceptProducts and rejectProducts methods will be included in order to allow warehouse employees to accept and reject orders. When accepting an order, its products will be included in the warehouse. In both methods, the order will change its state into "processed".
 - Product.java: An addToCart method is included in order to allow clients to pick products that they are about to buy.

In all cases, notice how changing an object requires notifying the persistence aspect to update its corresponding database row. In Product.java, notice how the Cart object is retrieved from the session data by using the session aspect.

Cart.java:

public class Cart {
  ...
    public void checkOut() {
        for (Product p: getProducts()) {
            PersistenceAspect db = ObjectContext.getInstance().getComponent(PersistenceAspect.class);
            QueryByExample query = new QueryByExample(Product.class, p);

            Product product = (Product)db.query(query).get(0);
            product.setAvailability(Product.SOLD);
            db.updateObject(product);
        }
        setProducts(new ArrayList<Product>());
    }
}


Order.java:

public class Order {
  ...
    @ViewAction(visible=AnnotationConstants.FALSE)
    public void acceptProducts() {
        if (getState().equals(PENDING)) {
            for (Product p: getProducts()) p.setAvailability(Product.ON_SALE);
            setState(PROCESSED);
            ObjectContext.getInstance().getComponent(PersistenceAspect.class).updateObjects(getProducts().toArray());
            ObjectContext.getInstance().getComponent(PersistenceAspect.class).updateObject(this);
        }
    }
    @ViewAction(visible=AnnotationConstants.FALSE)
    public void rejectProducts() {
        if (getState().equals(PENDING)) {
            setState(PROCESSED);
            ObjectContext.getInstance().getComponent(PersistenceAspect.class).updateObject(this);
        }
    }
}


Product.java:
public class Order {
  ...
    public void addToCart() {
        SessionAspect sessionAspect = ObjectContext.getInstance().getComponent(SessionAspect.class);
        Cart cart = (Cart)sessionAspect.getProperty("cart");
        if (cart == null) cart = new Cart();
       
        cart.getProducts().add(this);
       
        sessionAspect.setProperty("cart", cart);
        ObjectContext.getInstance().show(new HomePageClient());
    }
}
 

965 Accesos, 0 Ficheros adjuntos 0 Ficheros adjuntos

  • Comentarios