Trindade
Trindade

Reputation: 3

How to cast a RepositoryItem to an Order on ATG

I'm new to ATG and I'm failing to do something that looks fairly simple.

I'm trying to get an Order in the database by the number of the order. But this number is not the orderId so I can't just use the OrderManager.loadOrder method.

This is the code I have so far:

Repository orderRepository = getOrderManager().getOrderTools().getOrderRepository();
RepositoryView view = orderRepository.getView("order");

RqlStatement statement = RqlStatement.parseRqlStatement("orderNumber EQUALS ?0");
Object params[] = { pOrderNumber };
RepositoryItem items[] = statement.executeQuery(view, params);

RepositoryItem order = null;
if( (items != null) && (items.length > 0) ) {
    order = items[0];
}

//Now I want to convert this order of type "RepositoryItem" to an actual Order object

I can do this by getting the repository ID and calling loadOrder form the OrderManager, but that seems like going back to the database and finding again what I already have in my hands.

Is there another way to get an actual Order object out of this RepositoryItem object?

Upvotes: 0

Views: 4100

Answers (2)

bated
bated

Reputation: 960

It really depends on what you are trying to do.

The question about whether the item is loaded from the database or from the cache depends on your repository settings, combined with the lazy loading settings. The documentation on this can be found here.

If you would like to update the order then you should use OrderManager.loadOrder() as this will ensure that the order is updated correctly and allows you to reprice the order and update other repository items which make up the order such as payment groups and shipping groups (remember to use a transaction wrapper to ensure the order is updated safely).

If you are simply trying to read values then going the repository way will be quicker. I would recommend creating a globally scoped component which your form handler references. Something along the lines of (code below not tested):

OrderTools.properties file:

$class=com.acme.commerce.order.OrderTools
$scope=global
orderRepository=/atg/commerce/order/OrderRepository

OrderTools class:

public class OrderTools extends GenericService


{

private RepositoryView orderView;

private RqlStatement orderStm;

private OrderRepository orderRepository;

private OrderManager orderManager;

public void doStartService() throws ServiceException
{
    try
    {
        orderView = getOrderRepository().getView(CommerceConstants.ORDER);
        orderStm = RqlStatement.parseRqlStatement("uniqueOrderId = ?0");

    } catch (RepositoryException e)
    {
        throw new ServiceException(e);
    }
}

protected RepositoryItem getOrderItem(final String uniqueOrderId) throws RepositoryException
{
    Object params[] = new Object[1];
    params[0] = uniqueOrderId;

    RepositoryItem[] orderItems = orderStm.executeQuery(orderView, params);
    if (orderItems != null)
    {
        return getOrderRepository().getItem(orderItems[0].getRepositoryId(), CommerceConstants.ORDER);
    } else
    {
        return null;
    }
}

/*
 This method demonstrates how to load an order using the OrderManager.loadOrder() method.

 The code includes some basic timing so that a performance comparison can be done with the loadOrderSubItemsRepositoryMethod()
 */

public void loadOrderUsingOrderManager(String orderId) {

    long startTime = System.currentTimeMillis();

    Order order = getOrderManager().loadOrder(orderId);

    long orderLoadTime = System.currentTimeMillis();

    //read your properties here ...

    long totalTime = System.currentTimeMillis();

    if(isLoggingDebug()) {
        logDebug("The order load time was " + (orderLoadTime - startTime) + "ms");
        logDebug("The time to read the list of properties was " + (totalTime - startTime) + "ms");
    }

}

/*

This method shows how to get order items such as payment groups or shipping groups using the repository.

*/

public void loadOrderSubItemsRepositoryMethod(MutableRepositoryItem orderItem) {

    long startTime = System.currentTimeMillis();


    // Example of how to get the payment groups using the repository
   MutableRepositoryItem paymentGroups = (List) orderItem.getPropertyValue("paymentGroups");

    // Put code here to iterate through the list of items you want to read

    // Examploe of how to get the shipping groups
    MutableRepositoryItem shippingGroups = (List) orderItem.getPropertyValue("shippingGroups");

    long totalTime = System.currentTimeMillis();

    if(isLoggingDebug()) {
        logDebug("The order load time was " + (orderLoadTime - startTime) + "ms");
        logDebug("The time to read the list of properties was " + (totalTime - startTime) + "ms");
    }


}


public MutableRepository getOrderRepository()
{
    return orderRepository;
}

public void setOrderRepository(final MutableRepository orderRepository)
{
    this.orderRepository = orderRepository;
}

public OrderManager getOrderManager()
{
    return orderRepository;
}

public void setOrderRepository(final OrderManager orderManager)
{
    this.orderManager = orderManager;
}

}

Hope this helps.

Upvotes: 0

zmcmahon
zmcmahon

Reputation: 431

If you only need properties off of the order item itself, then you can just retrieve them directly from the RepositoryItem using the getPropertyValue methods. If you find that you want to utilize the OrderImpl wrapper and its associated convenience methods, then you should retrieve the Order object instances via the OrderManager.loadOrder() method as you have suggested. While this will require slightly more work by the application to contruct the Order wrapper, it does not necessarily mean another DB call against the order tables. Assuming you have not disabled repository caching for the order item then ATG will utilize the already cached order repository item when it is constructing the OrderImpl wrapper for you. This item would have been cached when you did the RQL lookup for the order by orderNumber, so a redundant DB call will not be performed.

Note that it may require additional DB calls to retrieve related order items if those items have not already been cached (i.e. payment groups, shipping groups, commerce items, etc).

Upvotes: 1

Related Questions