En esta lección, creará la clase MyOrderItemAddCmdImpl que implementa al interfaz que se define dentro de la clase OrderItemAddCmd. La nueva clase MyOrderItemAddCmdImpl debe incluir el código para garantizar que un carro de la compra de cliente incluya cinco elementos, o menos.
Procedimiento
-
En vista Enterprise Explorer, expanda .
-
Dentro del directorio src, cree el paquete com.ibm.commerce.sample.commands.
-
Haga clic en el botón derecho del ratón en el directorio src y seleccione .
-
Escriba com.ibm.commerce.sample.commands en el campo Nombre.
-
Haga clic en Finalizar.
-
Cree una clase MyOrderItemAddCmdImpl.java:
-
Haga clic en el botón derecho del ratón en el nuevo paquete com.ibm.commerce.sample.commands y seleccione .
-
En el asistente Clase Java nueva, escriba MyOrderItemAddCmdImpl en el campo Nombre.
-
Haga clic en el botón Examinar a la derecha del campo Superclase.
-
Escriba
OrderItemAddCmdImpl
y Haga clic en Aceptar.
-
Haga clic en Añadir y, a continuación, escriba
OrderItemAddCmd
y Haga clic en Aceptar.
-
Haga clic en Finalizar.
La clase
MyOrderItemAddCmdImpl.java nueva puede parecerse al siguiente código:
package com.ibm.commerce.sample.commands;
import com.ibm.commerce.orderitems.commands.OrderItemAddCmd;
import com.ibm.commerce.orderitems.commands.OrderItemAddCmdImpl;
public class MyOrderItemAddCmdImpl extends OrderItemAddCmdImpl implements
OrderItemAddCmd {
}
-
Añada la lógica de negocio y el manejo de excepciones en la nueva clase.
La lógica de negocio y el manejo de excepciones determinan si hay cinco o más artículos en el carro de la compra. Si el carro contiene cinco o más artículos, al cliente no le está permitido añadir más artículos al mismo. Para añadir lógica de negocio y manejo de excepciones:
-
Dentro de la nueva clase, añada el código siguiente antes de la última llave de cierre (
}
) que indica el final de la clase:
public void performExecute() throws ECException {
// Get a list of order ids
String[] orderIds = getOrderId();
// Check to make sure that an id exists at all
// if order ID exists then get number of items in the order
// else if no order ID exists then execute normal code
if (orderIds != null && orderIds.length > 0) {
// An exception should be thrown when trying to add a sixth item
// to the shopping cart. This code runs before an item is added and
// throws an exception if there are 5 or more items in the cart.
if (itemsInOrder(orderIds[0]) >= 5) {
throw new ECApplicationException(
MyNewMessages._ERR_TOO_MANY_ITEMS,
this.getClass().getName(),
"performExecute");
}
//else perform normal flow
}
super.performExecute();
}
//get number of items in the order
protected int itemsInOrder(String orderId) throws ECException {
try {
OrderAccessBean order = new OrderAccessBean();
order.setInitKey_orderId(orderId);
order.refreshCopyHelper();
return order.getOrderItems().length;
} catch (javax.ejb.FinderException e) {
throw new ECSystemException(
ECMessage._ERR_FINDER_EXCEPTION,
this.getClass().getName(),
"itemsInOrder");
} catch (javax.naming.NamingException e) {
throw new ECSystemException(
ECMessage._ERR_NAMING_EXCEPTION,
this.getClass().getName(),
"itemsInOrder");
} catch (java.rmi.RemoteException e) {
throw new ECSystemException(
ECMessage._ERR_REMOTE_EXCEPTION,
this.getClass().getName(),
"itemsInOrder");
} catch (javax.ejb.CreateException e) {
throw new ECSystemException(
ECMessage._ERR_CREATE_EXCEPTION,
this.getClass().getName(),
"itemsInOrder");
}
}
Este código añade un método performExecute para comprobar el número de artículos del carro de la compra. Si el número de artículos del carro de la compra es inferior a cinco, el método llama al método performExecute de la superclase (OrderItemAddCmdImpl) como lo hace habitualmente. Si el carro de la compra contiene cinco o más artículos, se genera una excepción y el usuario no puede añadir más artículos al mismo.
-
En el menú Origen, seleccione Organizar importaciones para añadir las sentencias de importación a la clase.
La clase se ha completado puede parecerse al siguiente código:
package com.ibm.commerce.sample.commands;
import com.ibm.commerce.exception.ECApplicationException;
import com.ibm.commerce.exception.ECException;
import com.ibm.commerce.exception.ECSystemException;
import com.ibm.commerce.order.objects.OrderAccessBean;
import com.ibm.commerce.orderitems.commands.OrderItemAddCmd;
import com.ibm.commerce.orderitems.commands.OrderItemAddCmdImpl;
import com.ibm.commerce.ras.ECMessage;
import com.ibm.commerce.sample.messages.MyNewMessages;
public class MyOrderItemAddCmdImpl extends OrderItemAddCmdImpl implements
OrderItemAddCmd {
public void performExecute() throws ECException {
// Get a list of order ids
String[] orderIds = getOrderId();
// Check to make sure that an id exists at all
// if order ID exists then get number of items in the order
// else if no order ID exists then execute normal code
if (orderIds != null && orderIds.length > 0) {
// An exception should be thrown when trying to add a sixth item
// to the shopping cart. This code runs before an item is added and
// throws an exception if there are 5 or more items in the cart.
if (itemsInOrder(orderIds[0]) >= 5) {
throw new ECApplicationException(
MyNewMessages._ERR_TOO_MANY_ITEMS, this.getClass()
.getName(), "performExecute");
}
// else perform normal flow
}
super.performExecute();
}
// get number of items in the order
protected int itemsInOrder(String orderId) throws ECException {
try {
OrderAccessBean order = new OrderAccessBean();
order.setInitKey_orderId(orderId);
order.refreshCopyHelper();
return order.getOrderItems().length;
} catch (javax.ejb.FinderException e) {
throw new ECSystemException(ECMessage._ERR_FINDER_EXCEPTION, this
.getClass().getName(), "itemsInOrder");
} catch (javax.naming.NamingException e) {
throw new ECSystemException(ECMessage._ERR_NAMING_EXCEPTION, this
.getClass().getName(), "itemsInOrder");
} catch (java.rmi.RemoteException e) {
throw new ECSystemException(ECMessage._ERR_REMOTE_EXCEPTION, this
.getClass().getName(), "itemsInOrder");
} catch (javax.ejb.CreateException e) {
throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION, this
.getClass().getName(), "itemsInOrder");
}
}
}
-
Guarde el trabajo.