/*
* petstore.jws
*
* Copyright 2001-2004 The Apache Software Foundation.
*
*
* 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.
*
*
* Original author: Jonathan Colwell
*/
import java.awt.Image;
import java.net.URL;
import java.math.BigDecimal;
import javax.imageio.ImageIO;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.servlet.ServletContext;
import javax.xml.rpc.server.ServletEndpointContext;
import javax.xml.rpc.server.ServiceLifecycle;
import org.apache.beehive.controls.api.bean.Control;
import org.apache.beehive.samples.petstore.controls.CatalogControl;
import org.apache.beehive.samples.petstore.model.Category;
import org.apache.beehive.samples.petstore.model.Item;
import org.apache.beehive.samples.petstore.model.Product;
import org.apache.beehive.samples.petstore.controls.exceptions.DataStoreException;
import org.apache.beehive.samples.petstore.controls.exceptions.InvalidIdentifierException;
/*******************************************************************************
* A WebService implementation of the Pet Store sample
*
* @author Jonathan Colwell
*/
@WebService(name = "PetstoreInventoryManager",
serviceName = "PetstoreInventoryManagementService",
targetNamespace =
"http://beehive.apache.org/petstore")
//@SOAPBinding(style = SOAPBinding.Style.RPC, use = SOAPBinding.Use.ENCODED)
public class PetstoreInventoryManager implements ServiceLifecycle {
@Control
public CatalogControl catalogBean;
private ServletEndpointContext mSec;
public void init(Object sec)
{
mSec = (ServletEndpointContext)sec;
}
public void destroy()
{
mSec = null;
}
/*
* Made public to show that @WebMethod really controls which
* methods are exposed as opposed to all public methods in
* standard AXIS .jws files.
*/
public CatalogControl getCatalogBean()
{
try {
if (catalogBean == null) {
catalogBean = (CatalogControl)java.beans.Beans
.instantiate(getClass().getClassLoader(),
CatalogControl.class.getName());
}
}
catch (Exception e) {
e.printStackTrace();
}
return catalogBean;
}
private Image obtainImage(String imagePath)
{
if (null == mSec) {
System.out.println("null ServletEndpointContext");
}
else {
try {
ServletContext sc = mSec.getServletContext();
if (null == sc) {
System.out.println("null ServletContext");
}
else {
/*
* FIXME jcolwell@bea.com 2004-Oct-05 -- shouldn't hard
* code "/images/" into the path, change to an init param.
*/
URL imageURL = sc.getResource("/images/" + imagePath);
if (null != imageURL) {
return ImageIO.read(imageURL);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
@WebMethod(action = "listProducts")
@WebResult(name = "ProductList")
public org.apache.beehive.samples.petstore.model.ws.Product[] listProducts
(@WebParam(name = "categoryId")String categoryId) throws DataStoreException
{
CatalogControl ccb = getCatalogBean();
Product[] prods = ccb.getProductListByCategory(categoryId);
org.apache.beehive.samples.petstore.model.ws.Product[] outProds =
new org.apache.beehive.samples.petstore.model.ws.Product[prods.length];
for (int j = 0; j < prods.length ; j++) {
// NOTE jcolwell@bea.com 2004-Oct-08 -- adding images as attachments
Image img = obtainImage(prods[j].getImage());
outProds[j] = new org.apache.beehive.samples.petstore.model.ws.Product(prods[j],
img);
}
return outProds;
}
@WebMethod(action = "listItems")
@WebResult(name = "ItemList")
public Item[] listItems(@WebParam(name = "productId") String productId) throws DataStoreException
{
Item[] items = getCatalogBean().getItemListByProduct(productId);
if (items == null) {
items = new Item[0];
}
return items;
}
@WebMethod(action = "listCategories")
@WebResult(name = "CategoryList")
public org.apache.beehive.samples.petstore.model.ws.Category[] listCategories()
{
try {
Category[] categories = getCatalogBean().getCategoryList();
if (categories != null && categories.length > 0) {
org.apache.beehive.samples.petstore.model.ws.Category[] outCats =
new org.apache.beehive.samples.petstore.model.ws.Category[categories.length];
for (int j = 0; j < categories.length ; j++) {
// NOTE jcolwell@bea.com 2004-Oct-08 -- adding inline images
Image img = obtainImage(categories[j].getImage());
outCats[j] = new org.apache.beehive.samples.petstore.model.ws.Category
(categories[j], img);
}
return outCats;
}
}
catch (Throwable e) {
e.printStackTrace();
}
return new org.apache.beehive.samples.petstore.model.ws.Category[0];
}
@WebMethod(action = "restockItem")
public void restockItem(@WebParam(name = "itemId") String itemId,
@WebParam(name = "quantity") int quantity)
throws InvalidIdentifierException, DataStoreException
{
CatalogControl ccb = getCatalogBean();
ccb.updateItemQuantity(ccb.getItem(itemId), -quantity);
}
/*
* NOTE jcolwell@bea.com 2004-Oct-05 -- a set price method should probably
* be added to the catalog control for the web service to call rather than
* having the web service directly manipulate the Item.
*/
@WebMethod(action = "setItemListPrice")
public void setItemListPrice
(@WebParam(name = "itemId") String itemId,
@WebParam(name = "listPrice") BigDecimal listPrice)
throws InvalidIdentifierException, DataStoreException
{
Item i = getCatalogBean().getItem(itemId);
if (i != null) {
i.setListPrice(listPrice);
}
else {
throw new InvalidIdentifierException
("A price cannot be set for a null Item");
}
}
}