You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by ek...@apache.org on 2005/04/13 01:14:06 UTC
svn commit: r161136 [1/3] - in incubator/beehive/trunk/samples/petstoreWeb:
./ WEB-INF/src/ WEB-INF/src/org/apache/beehive/samples/petstore/controller/
WEB-INF/src/org/apache/beehive/samples/petstore/controls/
WEB-INF/src/org/apache/beehive/samples/petstore/controls/data/
WEB-INF/src/org/apache/beehive/samples/petstore/controls/exceptions/
WEB-INF/src/org/apache/beehive/samples/petstore/controls/internal/
WEB-INF/src/org/apache/beehive/samples/petstore/forms/
WEB-INF/src/org/apache/beehive/samples/petstore/model/
WEB-INF/src/org/apache/beehive/samples/petstore/resources/ WEB-INF/src/sql/
WEB-INF/tags/beehive/petstore/ account/addresses/ account/create/
account/edit/ auth/ checkout/ search/ shop/ site/ webappRoot/
Author: ekoneil
Date: Tue Apr 12 16:13:55 2005
New Revision: 161136
URL: http://svn.apache.org/viewcvs?view=rev&rev=161136
Log:
petstoreWeb work from Scott Semyan.
This changes the petstore to use the JdbcControl from ControlHaus using Derby as the default database.
Also contains some general cleanup.
BB: self
DRT: Beehive pass
Test: the app builds, deploys, and is navigable around the catalog. users can also login.
Added:
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AddressControl.java (with props)
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AddressControlImpl.jcs (with props)
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/data/
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/data/AccountDBControl.jcx (with props)
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/data/AddressDBControl.jcx (with props)
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/data/CatalogDBControl.jcx (with props)
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/data/DBProperties.java (with props)
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/data/OrderDBControl.jcx (with props)
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/exceptions/DataStoreException.java (with props)
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/exceptions/NoSuchAddressException.java (with props)
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/internal/
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/forms/AddressForm.java (with props)
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/model/OrderItem.java (with props)
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/sql/
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/sql/initDB.sql
incubator/beehive/trunk/samples/petstoreWeb/account/addresses/
incubator/beehive/trunk/samples/petstoreWeb/account/addresses/Controller.jpf (with props)
incubator/beehive/trunk/samples/petstoreWeb/account/addresses/createUpdate.jsp (with props)
incubator/beehive/trunk/samples/petstoreWeb/account/addresses/listAddresses.jsp (with props)
Removed:
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/SequenceControl.java
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/SequenceControlImpl.jcs
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/exceptions/NoSuchSequenceException.java
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/model/Sequence.java
Modified:
incubator/beehive/trunk/samples/petstoreWeb/Controller.jpf
incubator/beehive/trunk/samples/petstoreWeb/README
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/PetstoreInventoryManager.jws
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/build.xml
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controller/AccountController.java
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AccountControl.java
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AccountControlImpl.jcs
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/CatalogControl.java
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/CatalogControlImpl.jcs
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/OrderControl.java
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/OrderControlImpl.jcs
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/forms/AccountForm.java
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/forms/CheckoutForm.java
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/forms/OrderForm.java
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/model/Account.java
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/model/Address.java
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/model/Item.java
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/model/Order.java
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/resources/account.properties
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/resources/order.properties
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/resources/view.properties
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/tags/beehive/petstore/accountFields.tag
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/tags/beehive/petstore/footer.tag
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/tags/beehive/petstore/headerMenu.tag
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/tags/beehive/petstore/orderSummary.tag
incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/tags/beehive/petstore/productFavorites.tag
incubator/beehive/trunk/samples/petstoreWeb/account/create/Controller.jpf
incubator/beehive/trunk/samples/petstoreWeb/account/create/create.jsp
incubator/beehive/trunk/samples/petstoreWeb/account/edit/Controller.jpf
incubator/beehive/trunk/samples/petstoreWeb/account/edit/edit.jsp
incubator/beehive/trunk/samples/petstoreWeb/account/edit/listOrders.jsp
incubator/beehive/trunk/samples/petstoreWeb/auth/Controller.jpf
incubator/beehive/trunk/samples/petstoreWeb/auth/login.jsp
incubator/beehive/trunk/samples/petstoreWeb/checkout/Controller.jpf
incubator/beehive/trunk/samples/petstoreWeb/checkout/confirm.jsp
incubator/beehive/trunk/samples/petstoreWeb/checkout/newOrder.jsp
incubator/beehive/trunk/samples/petstoreWeb/checkout/viewCart.jsp
incubator/beehive/trunk/samples/petstoreWeb/checkout/viewOrder.jsp
incubator/beehive/trunk/samples/petstoreWeb/error.jsp
incubator/beehive/trunk/samples/petstoreWeb/index.jsp
incubator/beehive/trunk/samples/petstoreWeb/search/Controller.jpf
incubator/beehive/trunk/samples/petstoreWeb/shop/Controller.jpf
incubator/beehive/trunk/samples/petstoreWeb/shop/cart.jsp
incubator/beehive/trunk/samples/petstoreWeb/shop/item.jsp
incubator/beehive/trunk/samples/petstoreWeb/shop/product.jsp
incubator/beehive/trunk/samples/petstoreWeb/site/template.jsp
incubator/beehive/trunk/samples/petstoreWeb/webappRoot/SharedFlow.jpfs
Modified: incubator/beehive/trunk/samples/petstoreWeb/Controller.jpf
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/petstoreWeb/Controller.jpf?view=diff&r1=161135&r2=161136
==============================================================================
--- incubator/beehive/trunk/samples/petstoreWeb/Controller.jpf (original)
+++ incubator/beehive/trunk/samples/petstoreWeb/Controller.jpf Tue Apr 12 16:13:55 2005
@@ -15,9 +15,24 @@
*
* $Header:$
*/
+import java.io.BufferedReader;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.beehive.controls.api.bean.Control;
import org.apache.beehive.netui.pageflow.PageFlowController;
import org.apache.beehive.netui.pageflow.Forward;
import org.apache.beehive.netui.pageflow.annotations.Jpf;
+import org.apache.beehive.samples.petstore.controls.CatalogControl;
+import org.apache.beehive.samples.petstore.controls.data.DBProperties;
+import org.apache.beehive.samples.petstore.controls.exceptions.DataStoreException;
+
+import webappRoot.SharedFlow;
@Jpf.Controller(
sharedFlowRefs={
@@ -27,9 +42,14 @@
public class Controller
extends PageFlowController
{
+ private static final String SQL_FILE = "sql/initDB.sql";
+
@Jpf.SharedFlowField(name="rootSharedFlow")
- private webappRoot.SharedFlow _sharedFlow;
+ private SharedFlow _sharedFlow;
+ @Control()
+ private CatalogControl _catalogControl;
+
@Jpf.Action(
forwards = {
@Jpf.Forward(name = "shop", path = "/shop/Controller.jpf"),
@@ -40,6 +60,72 @@
{
if (_sharedFlow.isUserLoggedIn())
return new Forward("shop");
- else return new Forward("index");
+ else
+ return new Forward("index");
}
+
+ @Jpf.Action(
+ forwards = {
+ @Jpf.Forward(name = "success", path = "index.jsp")
+ }
+ )
+ public Forward stopDB() throws Exception
+ {
+ try {
+ java.sql.DriverManager.getConnection("jdbc:derby:" + DBProperties.dbLocation + ";shutdown=true");
+ } catch (Exception e) {}
+ getRequest().setAttribute("message", "DB Stopped.");
+ return new Forward("success");
+ }
+
+ @Jpf.Action(
+ forwards = {
+ @Jpf.Forward(name = "index", path = "index.jsp")
+ }
+ )
+ protected Forward initDB() throws DataStoreException, Exception
+ {
+ List<String> dbInit = getSqlStatements(SQL_FILE);
+
+ for(String sql : dbInit)
+ {
+ try {
+ _catalogControl.initDB(sql);
+ } catch (DataStoreException e) {
+ // Ignore errors on the drop action since the table may not exist
+ if (!sql.startsWith("drop"))
+ throw e;
+ }
+ }
+ getRequest().setAttribute("message", "DB Initialized.");
+
+ return new Forward("index");
+ }
+
+ private List<String> getSqlStatements(String sqlFile) throws Exception
+ {
+ InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(sqlFile);
+ BufferedReader fs = new BufferedReader(new InputStreamReader(is));
+ List<String> statements = new ArrayList<String>();
+ try {
+ String line = null;
+ while((line = fs.readLine()) != null)
+ {
+ // Ignore comments and empty lines
+ if (line.endsWith(";") && !(line.startsWith("--")))
+ statements.add(line.replace(';', ' '));
+ }
+ } catch(IOException io) {
+ throw new RuntimeException("Unable to read input stream: " + sqlFile);
+ } finally {
+ try {
+ if(fs != null) fs.close();
+ } catch(Exception ignore) {}
+ }
+ for(String s : statements) {
+ System.out.println(s + "\n");
+ }
+
+ return statements;
+ }
}
Modified: incubator/beehive/trunk/samples/petstoreWeb/README
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/petstoreWeb/README?view=diff&r1=161135&r2=161136
==============================================================================
--- incubator/beehive/trunk/samples/petstoreWeb/README (original)
+++ incubator/beehive/trunk/samples/petstoreWeb/README Tue Apr 12 16:13:55 2005
@@ -32,27 +32,36 @@
3) J2EE Container
- A J2EE Container is required. Use your existing container.
+ A J2EE container is required; since the petstoreWeb is a J2EE web application
+it will run fine on Tomcat 5.x.
+4) Derby 10.0
+
+ Download: http://incubator.apache.org/derby/derby_downloads.html
+
+ Copy derby.jar to the WEB-INF\lib directory of the petstoreWeb sample.
+
+5) Derby DB Location
+
+ Modify WEB-INF/src/org/apache/beehive/samples/petstore/controls/data/DBProperties.java
+in the petstoreWeb to set the location for the Derby petstore database. By default, this location
+is: "d:/Apache/Derby/petDB". Most users will want to change this.
+
Building
========
-In the descriptions below, <BeehiveRoot> refers to the root of the
-Beehive distribution. For example, if the distribution was unpacked
-to c:\beehive, then that is what you should substitute for <BeehiveRoot>.
+The petstoreWeb stores its Ant build files in the WEB-INF/src directory. The steps
+below expect to be run from this directory.
The first time you build the Petstore web application, you will need to
pull in the Beehive runtime libraries. Use the following target once:
-ant -f <BeehiveRoot>\ant\webappRuntimeCore.xml
- -Dwebapp.dir=<BeehiveRoot>\samples\petstoreWeb
- deploy.beehive.webapp.runtime
-
-To build the Petstore web application, run the following target:
-
-ant -f <BeehiveRoot>\ant\buildWebapp.xml
- -Dwebapp.dir=<BeehiveRoot>\samples\petstoreWeb
- build
+ant deploy-beehive
+
+To build the Petstore web application, run the following target from the petstore's WEB-INF/src
+directory:
+
+ant build
Deploying and Running
=====================
@@ -66,11 +75,10 @@
http://<host>:<port>/petstoreWeb/Controller.jpf
For example, if you are using Tomcat 5.0.25 for you J2EE container, you
-may deploy using the following:
+can deploy using the following:
cd <BeehiveRoot>\samples\petstoreWeb
-jar cf %CATALINA_HOME%\webapps\petstoreWeb.war *
+jar -cf %CATALINA_HOME%\webapps\petstoreWeb.war *
-Check the Beehive Wiki for information about deploying to other
-containers.
+Check the Beehive Wiki for information about deploying to other application containers.
Modified: incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/PetstoreInventoryManager.jws
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/PetstoreInventoryManager.jws?view=diff&r1=161135&r2=161136
==============================================================================
--- incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/PetstoreInventoryManager.jws (original)
+++ incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/PetstoreInventoryManager.jws Tue Apr 12 16:13:55 2005
@@ -1,221 +1,215 @@
-/*
- * 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 java.util.Date;
-import javax.imageio.ImageIO;
-
-import javax.jws.Oneway;
-import javax.jws.WebMethod;
-import javax.jws.WebParam;
-import javax.jws.WebResult;
-import javax.jws.WebService;
-import javax.jws.soap.SOAPBinding;
-import javax.jws.soap.SOAPMessageHandler;
-import javax.jws.soap.SOAPMessageHandlers;
-
-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.CatalogControlBean;
-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.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 CatalogControlBean 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 CatalogControlBean getCatalogBean()
- {
- try {
- if (catalogBean == null) {
- catalogBean = (CatalogControlBean)java.beans.Beans
- .instantiate(getClass().getClassLoader(),
- CatalogControlBean.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)
- {
- CatalogControlBean 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)
- {
- 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 org.apache.beehive.samples.petstore.controls.exceptions.InvalidIdentifierException
- {
- CatalogControlBean 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 org.apache.beehive.samples.petstore.controls.exceptions.InvalidIdentifierException
- {
-
- Item i = getCatalogBean().getItem(itemId);
- if (i != null) {
- i.setListPrice(listPrice);
- }
- else {
- throw new org.apache.beehive.samples.petstore.controls.exceptions.
- InvalidIdentifierException
- ("A price cannot be set for a null Item");
- }
- }
+/*
+ * 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");
+ }
+ }
}
Modified: incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/build.xml
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/build.xml?view=diff&r1=161135&r2=161136
==============================================================================
--- incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/build.xml (original)
+++ incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/build.xml Tue Apr 12 16:13:55 2005
@@ -45,11 +45,16 @@
<!-- ========================================= -->
<target name="build" description="Build the webapp">
+ <copy todir="${basedir}/WEB-INF/lib" file="${beehive.home}/controlhaus/jdbc/trunk/build/dist/jdbc-control.jar"/>
<deploy-netui webappDir="${basedir}"/>
<deploy-wsm webappDir="${basedir}"/>
<build-webapp webappDir="${basedir}"/>
+ <copy todir="${basedir}/WEB-INF/classes">
+ <fileset dir="${basedir}/WEB-INF/src">
+ <include name="sql/**"/>
+ </fileset>
+ </copy>
</target>
-
<!-- ========================================= -->
<!-- clean - clean the webapp -->
Modified: incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controller/AccountController.java
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controller/AccountController.java?view=diff&r1=161135&r2=161136
==============================================================================
--- incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controller/AccountController.java (original)
+++ incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controller/AccountController.java Tue Apr 12 16:13:55 2005
@@ -24,8 +24,7 @@
protected static final String[] LANGUAGES;
- static {
+ static {
LANGUAGES = new String[]{"English", "Japanese"};
}
-
}
Modified: incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AccountControl.java
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AccountControl.java?view=diff&r1=161135&r2=161136
==============================================================================
--- incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AccountControl.java (original)
+++ incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AccountControl.java Tue Apr 12 16:13:55 2005
@@ -20,6 +20,7 @@
import org.apache.beehive.controls.api.bean.ControlInterface;
import org.apache.beehive.samples.petstore.model.Account;
+import org.apache.beehive.samples.petstore.controls.exceptions.DataStoreException;
import org.apache.beehive.samples.petstore.controls.exceptions.InvalidIdentifierException;
import org.apache.beehive.samples.petstore.controls.exceptions.AccountAlreadyExistsException;
import org.apache.beehive.samples.petstore.controls.exceptions.NoSuchAccountException;
@@ -27,15 +28,15 @@
@ControlInterface
public interface AccountControl {
- public Account getAccount(String userId);
+ public Account getAccount(String userId) throws InvalidIdentifierException, DataStoreException;
public void insertAccount(Account account)
- throws InvalidIdentifierException, AccountAlreadyExistsException;
+ throws InvalidIdentifierException, AccountAlreadyExistsException, DataStoreException;
public void updateAccount(Account account)
- throws InvalidIdentifierException, NoSuchAccountException;
+ throws InvalidIdentifierException, NoSuchAccountException, DataStoreException;
- public boolean checkAccountExists(String userId);
+ public boolean checkAccountExists(String userId) throws DataStoreException;
- public Account[] getAccounts();
+ //public Account[] getAccounts();
}
Modified: incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AccountControlImpl.jcs
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AccountControlImpl.jcs?view=diff&r1=161135&r2=161136
==============================================================================
--- incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AccountControlImpl.jcs (original)
+++ incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AccountControlImpl.jcs Tue Apr 12 16:13:55 2005
@@ -17,79 +17,58 @@
*/
package org.apache.beehive.samples.petstore.controls;
+import org.apache.beehive.controls.api.bean.Control;
import org.apache.beehive.controls.api.bean.ControlImplementation;
import org.apache.beehive.samples.petstore.controls.AccountControl;
+import org.apache.beehive.samples.petstore.controls.data.AccountDBControl;
import org.apache.beehive.samples.petstore.controls.exceptions.AccountAlreadyExistsException;
+import org.apache.beehive.samples.petstore.controls.exceptions.DataStoreException;
import org.apache.beehive.samples.petstore.controls.exceptions.InvalidIdentifierException;
import org.apache.beehive.samples.petstore.controls.exceptions.NoSuchAccountException;
-import org.apache.beehive.samples.petstore.controls.internal.InMemoryObjectStore;
import org.apache.beehive.samples.petstore.model.Account;
-import org.apache.beehive.samples.petstore.model.Address;
-
-import java.util.HashMap;
+import java.sql.SQLException;
@ControlImplementation
public class AccountControlImpl
implements AccountControl, java.io.Serializable
{
- private static final String BANNER_NAMESPACE = "banner";
- private static final String ACCOUNT_NAMESPACE = "account";
-
- static
- {
- InMemoryObjectStore.store(BANNER_NAMESPACE, "FISH", "banner_fish.gif");
- InMemoryObjectStore.store(BANNER_NAMESPACE, "CATS", "banner_cats.gif");
- InMemoryObjectStore.store(BANNER_NAMESPACE, "DOGS", "banner_dogs.gif");
- InMemoryObjectStore.store(BANNER_NAMESPACE, "REPTILES", "banner_reptiles.gif");
- InMemoryObjectStore.store(BANNER_NAMESPACE, "BIRDS", "banner_birds.gif");
-
- Account account = new Account();
- account.setUserId("beehive");
- account.setPassword("beehive");
- account.setEmail("yourname@yourdomain.com");
- Address address = new Address();
- address.setAddr1("901 San Antonio Road");
- address.setAddr2("MS UCUP02-206");
- address.setCity("Palo Alto");
- address.setCountry("USA");
- account.setFirstName("Joe");
- account.setLastName("User");
- account.setPhone("555-555-5555");
- address.setState("CA");
- account.setStatus("OK");
- address.setZip("94303");
- account.setAddress(address);
-
- account.setBannerOpt(true);
- account.setLangPref("english");
- account.setMyListOpt(true);
- account.setFavCategory("DOGS");
- account.setBannerName((String)InMemoryObjectStore.find(BANNER_NAMESPACE, account.getFavCategory()));
-
- InMemoryObjectStore.store(ACCOUNT_NAMESPACE, account.getUserId(), account);
- }
-
- public Account[] getAccounts()
- {
- return (Account[])InMemoryObjectStore.toArray(ACCOUNT_NAMESPACE, Account.class);
- }
-
- public boolean checkAccountExists(String key)
- {
- return InMemoryObjectStore.find(ACCOUNT_NAMESPACE, key) != null;
- }
+ @Control()
+ private AccountDBControl _accountDBControl;
- /**
- */
- public Account getAccount(String key)
+ public boolean checkAccountExists(String key) throws DataStoreException
{
- return (Account)InMemoryObjectStore.find(ACCOUNT_NAMESPACE, key);
+ boolean ret = false;
+ try {
+ if (_accountDBControl.checkAccountExists(key) > 0)
+ ret = true;
+ } catch (SQLException e) {
+
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+ return ret;
+ }
+
+ public Account getAccount(String key) throws InvalidIdentifierException, DataStoreException
+ {
+ Account acct = new Account();
+ try {
+ acct = _accountDBControl.getAccountById(key);
+ if (acct == null)
+ throw new InvalidIdentifierException("Account: " + key + " not found!");
+
+ } catch (SQLException e) {
+
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+ return acct;
}
- /**
- */
- public void updateAccount(Account account)
- throws InvalidIdentifierException, NoSuchAccountException
+ public void updateAccount(Account account)
+ throws InvalidIdentifierException, NoSuchAccountException, DataStoreException
{
String userId = account.getUserId();
if (userId == null || userId.length()==0)
@@ -101,10 +80,8 @@
doUpdateAccount(account);
}
- /**
- */
public void insertAccount(Account account)
- throws InvalidIdentifierException, AccountAlreadyExistsException
+ throws InvalidIdentifierException, AccountAlreadyExistsException, DataStoreException
{
String userId = account.getUserId();
if (userId == null || userId.length() == 0)
@@ -118,13 +95,27 @@
doInsertAccount(account);
}
- private void doUpdateAccount(Account account)
- {
- InMemoryObjectStore.store(ACCOUNT_NAMESPACE, account.getUserId(), account);
- }
-
- private void doInsertAccount(Account account)
+ private void doUpdateAccount(Account account) throws DataStoreException
{
- InMemoryObjectStore.store(ACCOUNT_NAMESPACE, account.getUserId(), account);
+ try {
+ _accountDBControl.updateAccount(account);
+ } catch (SQLException e) {
+
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+ }
+
+ private void doInsertAccount(Account account) throws DataStoreException
+ {
+ try {
+ _accountDBControl.insertAccount(account);
+ } catch (SQLException e) {
+
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
}
}
Added: incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AddressControl.java
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AddressControl.java?view=auto&rev=161136
==============================================================================
--- incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AddressControl.java (added)
+++ incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AddressControl.java Tue Apr 12 16:13:55 2005
@@ -0,0 +1,44 @@
+/*
+ * Copyright 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.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.samples.petstore.controls;
+
+import org.apache.beehive.controls.api.bean.ControlInterface;
+
+import org.apache.beehive.samples.petstore.model.Address;
+import org.apache.beehive.samples.petstore.controls.exceptions.DataStoreException;
+import org.apache.beehive.samples.petstore.controls.exceptions.InvalidIdentifierException;
+import org.apache.beehive.samples.petstore.controls.exceptions.NoSuchAddressException;
+
+@ControlInterface
+public interface AddressControl {
+
+ public Address getAddress(int addressId) throws InvalidIdentifierException, DataStoreException;
+
+ public void insertAddress(Address address)
+ throws InvalidIdentifierException, DataStoreException;
+
+ public void updateAddress(Address address)
+ throws InvalidIdentifierException, NoSuchAddressException, DataStoreException;
+
+ public boolean checkAddressExists(int addressId, String userId) throws DataStoreException;
+
+ public Address[] getUserAddresses(String userId) throws DataStoreException;
+
+ public void deleteAddress(int addressId, String userId) throws InvalidIdentifierException, DataStoreException, NoSuchAddressException;
+
+}
\ No newline at end of file
Propchange: incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AddressControl.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AddressControlImpl.jcs
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AddressControlImpl.jcs?view=auto&rev=161136
==============================================================================
--- incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AddressControlImpl.jcs (added)
+++ incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AddressControlImpl.jcs Tue Apr 12 16:13:55 2005
@@ -0,0 +1,142 @@
+/*
+ * Copyright 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.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.samples.petstore.controls;
+
+import org.apache.beehive.controls.api.bean.Control;
+import org.apache.beehive.controls.api.bean.ControlImplementation;
+import org.apache.beehive.samples.petstore.controls.data.AddressDBControl;
+import org.apache.beehive.samples.petstore.controls.exceptions.DataStoreException;
+import org.apache.beehive.samples.petstore.controls.exceptions.InvalidIdentifierException;
+import org.apache.beehive.samples.petstore.controls.exceptions.NoSuchAddressException;
+import org.apache.beehive.samples.petstore.model.Address;
+
+import java.sql.SQLException;
+
+@ControlImplementation
+public class AddressControlImpl
+ implements AddressControl, java.io.Serializable
+{
+ @Control()
+ private AddressDBControl _addressDBControl;
+
+ public boolean checkAddressExists(int key, String userKey) throws DataStoreException
+ {
+ boolean ret = false;
+ try {
+ if (_addressDBControl.checkAddressExists(key, userKey) > 0)
+ ret = true;
+ } catch (SQLException e) {
+
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+ return ret;
+ }
+
+ public Address[] getUserAddresses(String key) throws DataStoreException
+ {
+ Address[] addresses;
+ try {
+ addresses = _addressDBControl.getUserAddresses(key);
+ } catch (SQLException e) {
+
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+ return addresses;
+ }
+
+ public Address getAddress(int key) throws InvalidIdentifierException, DataStoreException
+ {
+ Address add = new Address();
+ try {
+ add = _addressDBControl.getAddress(key);
+ if (add == null)
+ throw new InvalidIdentifierException("Address: " + key + " not found!");
+
+ } catch (SQLException e) {
+
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+ return add;
+ }
+
+ public void updateAddress(Address address)
+ throws InvalidIdentifierException, NoSuchAddressException, DataStoreException
+ {
+ String userId = address.getUserId();
+ int addressId = address.getAddressId();
+
+ if (userId == null || userId.length() == 0 || addressId == -1)
+ throw new InvalidIdentifierException("cannot update Address with null or empty userId or addressId of -1");
+
+ if (!checkAddressExists(addressId, userId))
+ throw new NoSuchAddressException("no Address found for userId: " + userId + " and addressId " + addressId);
+
+ try {
+ _addressDBControl.updateAddress(address);
+ } catch (SQLException e) {
+
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+ }
+
+ public void insertAddress(Address address)
+ throws InvalidIdentifierException, DataStoreException
+ {
+ String userId = address.getUserId();
+ if (userId == null || userId.length() == 0)
+ {
+ throw new InvalidIdentifierException("cannot insert Address with null userId");
+ }
+
+ try {
+ _addressDBControl.insertAddress(address);
+ } catch (SQLException e) {
+
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+ }
+
+ public void deleteAddress(int addressId, String userId)
+ throws InvalidIdentifierException, DataStoreException, NoSuchAddressException
+ {
+ if (userId == null || userId.length() == 0 || addressId == -1)
+ throw new InvalidIdentifierException("cannot delete Address with null or empty userId or addressId of -1");
+
+ if (!checkAddressExists(addressId, userId))
+ throw new NoSuchAddressException("no Address found for userId: " + userId + " and addressId " + addressId);
+
+ try {
+ _addressDBControl.deleteAddress(addressId, userId);
+ } catch (SQLException e) {
+
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+ }
+}
Propchange: incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/AddressControlImpl.jcs
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/CatalogControl.java
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/CatalogControl.java?view=diff&r1=161135&r2=161136
==============================================================================
--- incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/CatalogControl.java (original)
+++ incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/CatalogControl.java Tue Apr 12 16:13:55 2005
@@ -21,25 +21,29 @@
import org.apache.beehive.samples.petstore.model.Item;
import org.apache.beehive.samples.petstore.model.Product;
import org.apache.beehive.samples.petstore.model.Category;
+import org.apache.beehive.samples.petstore.controls.exceptions.DataStoreException;
import org.apache.beehive.samples.petstore.controls.exceptions.InvalidIdentifierException;
@ControlInterface
public interface CatalogControl {
- public Category[] getCategoryList();
+ public Category[] getCategoryList() throws DataStoreException;
- public Category getCategory(String categoryId);
+ public Category getCategory(String categoryId) throws InvalidIdentifierException, DataStoreException;
- public Product getProduct(String productId);
+ public Product getProduct(String productId) throws InvalidIdentifierException, DataStoreException;
- public Product[] getProductListByCategory(String categoryId);
+ public Product[] getProductListByCategory(String categoryId) throws DataStoreException;
- public Product[] searchProductList(String searchString);
+ public Product[] searchProductList(String searchString) throws DataStoreException;
- public Item getItem(String itemId);
+ public Item getItem(String itemId) throws InvalidIdentifierException, DataStoreException;
- public Item[] getItemListByProduct(String categoryId);
+ public Item[] getItemListByProduct(String categoryId) throws DataStoreException;
public void updateItemQuantity(Item item, int quantity)
- throws InvalidIdentifierException;
+ throws InvalidIdentifierException, DataStoreException;
+
+ public int checkDB() throws DataStoreException;
+ public void initDB(String initString) throws DataStoreException;
}
Modified: incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/CatalogControlImpl.jcs
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/CatalogControlImpl.jcs?view=diff&r1=161135&r2=161136
==============================================================================
--- incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/CatalogControlImpl.jcs (original)
+++ incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/CatalogControlImpl.jcs Tue Apr 12 16:13:55 2005
@@ -17,255 +17,169 @@
*/
package org.apache.beehive.samples.petstore.controls;
+import org.apache.beehive.controls.api.bean.Control;
import org.apache.beehive.controls.api.bean.ControlImplementation;
-import org.apache.beehive.samples.petstore.controls.internal.InMemoryObjectStore;
import org.apache.beehive.samples.petstore.model.Item;
import org.apache.beehive.samples.petstore.model.Category;
import org.apache.beehive.samples.petstore.model.Product;
+import org.apache.beehive.samples.petstore.controls.data.CatalogDBControl;
+import org.apache.beehive.samples.petstore.controls.exceptions.DataStoreException;
import org.apache.beehive.samples.petstore.controls.exceptions.InvalidIdentifierException;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.ArrayList;
-import java.math.BigDecimal;
+import java.sql.SQLException;
@ControlImplementation
public class CatalogControlImpl
implements CatalogControl, java.io.Serializable
{
- private static final String ITEM_NAMESPACE = "items";
- private static final String CATEGORY_NAMESPACE= "categories";
- private static final String PRODUCTS_NAMESPACE = "products";
-
- // @todo: would be nice to return an XMLBean for the Catalog object here
- static
- {
- Integer one = new Integer(1);
- Integer ten_thousand = new Integer(10000);
- int col = 0;
- Object[] row;
-
- // itemId, productId, unitPrice, unitCost, supplierId, status, attribute1, quantity
- Object[][] dataArray = new Object[][]
- {
- {"EST-1", "FI-SW-01", new BigDecimal(16.50), new BigDecimal(10.00), one, "P", "Large", ten_thousand},
- {"EST-2", "FI-SW-01", new BigDecimal(16.50), new BigDecimal(10.00), one, "P", "Small", ten_thousand},
- {"EST-3", "FI-SW-02", new BigDecimal(18.50), new BigDecimal(12.00), one, "P", "Toothless", ten_thousand},
- {"EST-4", "FI-FW-01", new BigDecimal(18.50), new BigDecimal(12.00), one, "P", "Spotted", ten_thousand},
- {"EST-5", "FI-FW-01", new BigDecimal(18.50), new BigDecimal(12.00), one, "P", "Spotless", ten_thousand},
- {"EST-6", "K9-BD-01", new BigDecimal(18.50), new BigDecimal(12.00), one, "P", "Male Adult", ten_thousand},
- {"EST-7", "K9-BD-01", new BigDecimal(18.50), new BigDecimal(12.00), one, "P", "Female Puppy", ten_thousand},
- {"EST-8", "K9-PO-02", new BigDecimal(18.50), new BigDecimal(12.00), one, "P", "Male Puppy", ten_thousand},
- {"EST-9", "K9-DL-01", new BigDecimal(18.50), new BigDecimal(12.00), one, "P", "Spotless Male Puppy", ten_thousand},
- {"EST-10", "K9-DL-01", new BigDecimal(18.50), new BigDecimal(12.00), one, "P", "Spotted Adult Female", ten_thousand},
- {"EST-11", "RP-SN-01", new BigDecimal(18.50), new BigDecimal(12.00), one, "P", "Venomless", ten_thousand},
- {"EST-12", "RP-SN-01", new BigDecimal(18.50), new BigDecimal(12.00), one, "P", "Rattleless", ten_thousand},
- {"EST-13", "RP-LI-02", new BigDecimal(18.50), new BigDecimal(12.00), one, "P", "Green Adult", ten_thousand},
- {"EST-14", "FL-DSH-01", new BigDecimal(58.50), new BigDecimal(12.00), one, "P", "Tailless", ten_thousand},
- {"EST-15", "FL-DSH-01", new BigDecimal(23.50), new BigDecimal(12.00), one, "P", "With tail", ten_thousand},
- {"EST-16", "FL-DLH-02", new BigDecimal(93.50), new BigDecimal(12.00), one, "P", "Adult Female", ten_thousand},
- {"EST-17", "FL-DLH-02", new BigDecimal(93.50), new BigDecimal(12.00), one, "P", "Adult Male", ten_thousand},
- {"EST-18", "AV-CB-01", new BigDecimal(193.50), new BigDecimal(92.00), one, "P", "Adult Male", ten_thousand},
- {"EST-19", "AV-SB-02", new BigDecimal(15.50), new BigDecimal(2.00), one, "P", "Adult Male", ten_thousand},
- {"EST-20", "FI-FW-02", new BigDecimal(5.50), new BigDecimal(2.00), one, "P", "Adult Male", ten_thousand},
- {"EST-21", "FI-FW-02", new BigDecimal(5.29), new BigDecimal(1.00), one, "P", "Adult Female", ten_thousand},
- {"EST-22", "K9-RT-02", new BigDecimal(135.50), new BigDecimal(100.00), one, "P", "Adult Male", ten_thousand},
- {"EST-23", "K9-RT-02", new BigDecimal(145.49), new BigDecimal(100.00), one, "P", "Adult Female", ten_thousand},
- {"EST-24", "K9-RT-02", new BigDecimal(255.50), new BigDecimal(92.00), one, "P", "Adult Male", ten_thousand},
- {"EST-25", "K9-RT-02", new BigDecimal(325.29), new BigDecimal(90.00), one, "P", "Adult Female", ten_thousand},
- {"EST-26", "K9-CW-01", new BigDecimal(125.50), new BigDecimal(92.00), one, "P", "Adult Male", ten_thousand},
- {"EST-27", "K9-CW-01", new BigDecimal(155.29), new BigDecimal(90.00), one, "P", "Adult Female", ten_thousand},
- {"EST-28", "K9-RT-01", new BigDecimal(155.29), new BigDecimal(90.00), one, "P", "Adult Female", ten_thousand}
- };
-
- // item data
- for(int i = 0; i < dataArray.length; i++)
- {
- col = 0;
-
- row = dataArray[i];
- Item item = new Item();
- item.setItemId((String)row[col++]);
- item.setProductId((String)row[col++]);
- item.setListPrice((BigDecimal)row[col++]);
- item.setUnitCost((BigDecimal)row[col++]);
- item.setSupplier(((Integer)row[col++]).intValue());
- item.setStatus((String)row[col++]);
- item.setAttr1((String)row[col++]);
- item.setQty(((Integer)row[col++]).intValue());
-
- InMemoryObjectStore.store(ITEM_NAMESPACE, item.getItemId(), item);
- }
-
- // catId, catName, catImage, catDescription
- // @todo: need to whack the image names here --
- // these are icons of the text "Fish" and so on; clearly these don't
- // work for i18n. <g>
- // -- ekoneil
- String[][] categoryDataArray = new String[][]
- {
- {"FISH", "Fish", "fish_icon.gif", "Saltwater, Freshwater"},
- {"DOGS", "Dogs", "dogs_icon.gif", "Various Breeds"},
- {"REPTILES", "Reptiles", "reptiles_icon.gif", "Lizards, Turtles, Snakes"},
- {"CATS", "Cats", "cats_icon.gif", "Various Breeds, Exotic Varieties"},
- {"BIRDS", "Birds", "birds_icon.gif", "Exotic Varieties"}
- };
-
- for(int i = 0; i < categoryDataArray.length; i++)
- {
- col = 0;
-
- row = categoryDataArray[i];
- Category category = new Category();
- category.setCatId((String) row[col++]);
- category.setName((String) row[col++]);
- category.setImage((String) row[col++]);
- category.setDescription((String) row[col++]);
-
- InMemoryObjectStore.store(CATEGORY_NAMESPACE, category.getCatId(), category);
- }
-
- // productId, categoryId, productName, productImage, productDescription
- String[][] productDataArray = new String[][]
- {
- {"AV-CB-01", "BIRDS", "Amazon Parrot", "bird2.jpg", "Great companion for up to 75 years"},
- {"AV-SB-02", "BIRDS", "Finch", "bird1.jpg", "Great stress reliever"},
- {"FI-FW-01", "FISH", "Koi", "fish3.jpg", "Fresh Water fish from Japan"},
- {"FI-FW-02", "FISH", "Goldfish", "fish2.jpg", "Fresh Water fish from China"},
- {"FI-SW-01", "FISH", "Angelfish", "fish1.jpg", "Salt Water fish from Australia"},
- {"FI-SW-02", "FISH", "Tiger Shark", "fish4.jpg", "Salt Water fish from Australia"},
- {"FL-DLH-02", "CATS", "Persian", "cat1.jpg", "Friendly house cat, doubles as a princess"},
- {"FL-DSH-01", "CATS", "Manx", "cat2.jpg", "Great for reducing mouse populations"},
- {"K9-BD-01", "DOGS", "Bulldog", "dog1.jpg", "Friendly dog from England"},
- {"K9-CW-01", "DOGS", "Chihuahua", "dog2.jpg", "Great companion dog"},
- {"K9-DL-01", "DOGS", "Dalmation", "dog3.jpg", "Great dog for a Fire Station"},
- {"K9-PO-02", "DOGS", "Poodle", "dog6.jpg", "Cute dog from France"},
- {"K9-RT-01", "DOGS", "Golden Retriever", "dog4.jpg", "Great family dog"},
- {"K9-RT-02", "DOGS", "Labrador Retriever", "dog5.jpg", "Great hunting dog"},
- {"RP-LI-02", "REPTILES", "Iguana", "lizard1.jpg", "Friendly green friend"},
- {"RP-SN-01", "REPTILES", "Rattlesnake", "snake1.jpg", "Doubles as a watch dog"}
- };
-
- for(int i = 0; i < productDataArray.length; i++)
- {
- col = 0;
-
- row = productDataArray[i];
- Product product = new Product();
- product.setProductId((String) row[col++]);
- product.setCategory((String) row[col++]);
- product.setName((String) row[col++]);
- product.setImage((String) row[col++]);
- product.setDescription((String) row[col++]);
+ @Control()
+ private CatalogDBControl _petsDBControl;
- InMemoryObjectStore.store(PRODUCTS_NAMESPACE, product.getProductId(), product);
- }
- }
-
- public Category[] getCategoryList()
+ public Category[] getCategoryList() throws DataStoreException
{
// todo: bad form -- should return an immutable array here
- return (Category[])InMemoryObjectStore.toArray(CATEGORY_NAMESPACE, Category.class);
- }
-
- public Category getCategory(String key)
- {
- return (Category)InMemoryObjectStore.find(CATEGORY_NAMESPACE, key);
+ Category[] categories;
+ try {
+ categories = _petsDBControl.getCategoryList();
+ } catch (SQLException e) {
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+ return categories;
+ }
+
+ public Category getCategory(String key) throws InvalidIdentifierException, DataStoreException
+ {
+ Category cat = new Category();
+ try {
+ cat = _petsDBControl.getCategory(key);
+ if (cat == null)
+ throw new InvalidIdentifierException("Category: " + key + " not found!");
+ } catch (SQLException e) {
+
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+ return cat;
}
/**
*/
- public Item getItem(String key)
+ public Item getItem(String key) throws InvalidIdentifierException, DataStoreException
{
- return (Item)InMemoryObjectStore.find(ITEM_NAMESPACE, key);
+ Item item = new Item();
+ try {
+ item = _petsDBControl.getItem(key);
+ if (item == null)
+ throw new InvalidIdentifierException("Item: " + key + " not found!");
+ } catch (SQLException e) {
+
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+ return item;
}
/**
*/
- public Item[] getItemListByProduct(String key)
+ public Item[] getItemListByProduct(String key) throws DataStoreException
{
- Item[] items = getItemList();
- Item item = null;
- ArrayList result = new ArrayList();
- for(int i = 0; i < items.length; i++)
- {
- item = items[i];
- if (item.getProductId().hashCode() == key.hashCode())
- {
- result.add(item);
- }
- }
-
- Item[] a = new Item[result.size()];
- result.toArray(a);
- return a;
+ Item[] items;
+ try {
+ items = _petsDBControl.getItemListByProduct(key);
+ } catch (SQLException e) {
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+ return items;
}
// todo: this doesn't handle the case where something goes "out of stock", ie a negative quantity
public void updateItemQuantity(Item item, int quantity)
- throws InvalidIdentifierException
+ throws InvalidIdentifierException, DataStoreException
{
if(item == null)
throw new InvalidIdentifierException("cannot update Item inventory with null or empty itemId");
- item.setQty(item.getQty() - quantity);
- }
-
- public Product getProduct(String key)
- {
- return (Product)InMemoryObjectStore.find(PRODUCTS_NAMESPACE, key);
- }
-
- public Product[] getProductListByCategory(String key)
- {
- Product[] products = getProductList();
- ArrayList result = new ArrayList();
- Product product = null;
- for(int i = 0; i < products.length; i++)
- {
- product = (Product)products[i];
- if (product.getCategory().hashCode() == key.hashCode())
- {
- result.add(product);
- }
- }
- Product[] a = new Product[result.size()];
- result.toArray(a);
- return a;
- }
-
+ item.setQty(item.getQty() - quantity);
- /**
- */
- public Product[] searchProductList(String searchTerm)
- {
- Product[] products = getProductList();
- ArrayList result = new ArrayList();
- Product product = null;
- for(int i = 0; i < products.length; i++)
- {
- product = products[i];
- if (product.getName().toLowerCase().indexOf(searchTerm) >= 0 ||
- product.getCategory().toLowerCase().indexOf(searchTerm) >= 0 ||
- product.getDescription().toLowerCase().indexOf(searchTerm) >= 0)
- {
- result.add(product);
- }
- }
- Product[] a = new Product[result.size()];
- result.toArray(a);
- return a;
- }
-
- private Item[] getItemList()
- {
- // todo: bad form -- should return an immutable array here
- return (Item[])InMemoryObjectStore.toArray(ITEM_NAMESPACE, Item.class);
- }
-
- private Product[] getProductList()
- {
- // todo: bad form -- should return an immutable array here
- return (Product[])InMemoryObjectStore.toArray(PRODUCTS_NAMESPACE, Product.class);
- }
+ try {
+ _petsDBControl.updateItemQuantity(item.getItemId(), item.getQty());
+ } catch (SQLException e) {
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+ }
+
+ public Product getProduct(String key) throws InvalidIdentifierException, DataStoreException
+ {
+ Product prod = new Product();
+ try {
+ prod = _petsDBControl.getProduct(key);
+ if (prod == null)
+ throw new InvalidIdentifierException("Product: " + key + " not found!");
+ } catch (SQLException e) {
+
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+ return prod;
+
+ }
+
+ public Product[] getProductListByCategory(String key) throws DataStoreException
+ {
+ Product[] products;
+ try {
+ products = _petsDBControl.getProductListByCategory(key);
+ } catch (SQLException e) {
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+ return products;
+ }
+
+
+ public Product[] searchProductList(String searchTerm) throws DataStoreException
+ {
+ Product[] products;
+ try {
+ products = _petsDBControl.searchProductList("%" + searchTerm + "%");
+ } catch (SQLException e) {
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+ return products;
+ }
+
+ public void initDB(String initString) throws DataStoreException
+ {
+ try {
+ _petsDBControl.initDB(initString);
+ } catch (SQLException e) {
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+ }
+ public int checkDB() throws DataStoreException
+ {
+ int nRet = 0;
+ try {
+ nRet = _petsDBControl.checkDB();
+ } catch (SQLException e) {
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+ return nRet;
+ }
}
Modified: incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/OrderControl.java
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/OrderControl.java?view=diff&r1=161135&r2=161136
==============================================================================
--- incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/OrderControl.java (original)
+++ incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/OrderControl.java Tue Apr 12 16:13:55 2005
@@ -20,7 +20,10 @@
import java.util.List;
import org.apache.beehive.controls.api.bean.ControlInterface;
+import org.apache.beehive.samples.petstore.model.Cart;
import org.apache.beehive.samples.petstore.model.Order;
+import org.apache.beehive.samples.petstore.model.OrderItem;
+import org.apache.beehive.samples.petstore.controls.exceptions.DataStoreException;
import org.apache.beehive.samples.petstore.controls.exceptions.NoSuchOrderException;
import org.apache.beehive.samples.petstore.controls.exceptions.OrderAlreadyExistsException;
import org.apache.beehive.samples.petstore.controls.exceptions.InvalidIdentifierException;
@@ -30,13 +33,15 @@
public Order createOrder();
- public void commitOrder(Order order)
- throws OrderAlreadyExistsException, NoSuchOrderException, InvalidIdentifierException;
+ public int commitOrder(Order order, Cart cart)
+ throws OrderAlreadyExistsException, NoSuchOrderException, InvalidIdentifierException, DataStoreException;
- public Order getOrder(String key)
- throws NoSuchOrderException;
+ public Order getOrder(String userId, int key)
+ throws NoSuchOrderException, DataStoreException;
- public Order[] getOrdersByUserId(String key);
+ public Order[] getOrdersByUserId(String key) throws DataStoreException;
public List getCreditCards();
+
+ public OrderItem[] getOrderItemsByOrderId(int orderId) throws DataStoreException;
}
Modified: incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/OrderControlImpl.jcs
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/OrderControlImpl.jcs?view=diff&r1=161135&r2=161136
==============================================================================
--- incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/OrderControlImpl.jcs (original)
+++ incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/OrderControlImpl.jcs Tue Apr 12 16:13:55 2005
@@ -17,22 +17,20 @@
*/
package org.apache.beehive.samples.petstore.controls;
-import java.util.Date;
-import java.util.HashMap;
+import java.sql.SQLException;
import java.util.Iterator;
import java.util.ArrayList;
-import java.math.BigDecimal;
import java.util.List;
-import org.apache.beehive.samples.petstore.controls.internal.InMemoryObjectStore;
+import org.apache.beehive.samples.petstore.model.Cart;
import org.apache.beehive.samples.petstore.model.Order;
import org.apache.beehive.samples.petstore.model.LineItem;
-import org.apache.beehive.samples.petstore.model.Product;
-import org.apache.beehive.samples.petstore.model.Sequence;
import org.apache.beehive.samples.petstore.model.Item;
+import org.apache.beehive.samples.petstore.model.OrderItem;
+import org.apache.beehive.samples.petstore.controls.data.OrderDBControl;
+import org.apache.beehive.samples.petstore.controls.exceptions.DataStoreException;
import org.apache.beehive.samples.petstore.controls.exceptions.OrderAlreadyExistsException;
import org.apache.beehive.samples.petstore.controls.exceptions.NoSuchOrderException;
-import org.apache.beehive.samples.petstore.controls.exceptions.NoSuchSequenceException;
import org.apache.beehive.samples.petstore.controls.exceptions.InvalidIdentifierException;
import org.apache.beehive.controls.api.bean.ControlImplementation;
@@ -40,21 +38,14 @@
public class OrderControlImpl
implements OrderControl, java.io.Serializable
{
- private static final String ORDER_SEQUENCE_NAME = "order";
- private static final String LINE_ITEM_SEQUENCE_NAME = "lineitem";
-
- private static final String ORDER_NAMESPACE = "orders";
- private static final String LINE_ITEM_NAMESPACE = "lineitems";
-
@org.apache.beehive.controls.api.bean.Control()
private CatalogControl _catalogControl;
@org.apache.beehive.controls.api.bean.Control()
- private SequenceControl _sequenceControl;
-
+ private OrderDBControl _orderDBControl;
+
private static final ArrayList<String> CARD_TYPE_LIST;
- // todo: add sample order data for a customer
static
{
CARD_TYPE_LIST = new ArrayList<String>();
@@ -65,74 +56,87 @@
public Order createOrder()
{
- try
- {
- int id = _sequenceControl.getNextSequenceId(ORDER_SEQUENCE_NAME);
- Order order = new Order(id);
- return order;
- }
- catch(Exception e)
- {
- assert e instanceof NoSuchSequenceException || e instanceof InvalidIdentifierException;
- e.printStackTrace();
-
- return null;
- }
- }
+ Order order = new Order();
+ return order;
+ }
- /**
- */
- public void commitOrder(Order order)
- throws OrderAlreadyExistsException, NoSuchOrderException, InvalidIdentifierException
+ public int commitOrder(Order order, Cart cart)
+ throws OrderAlreadyExistsException, NoSuchOrderException, InvalidIdentifierException, DataStoreException
{
- Integer integerOrderId = order.getOrderId();
-
- Order o = lookupOrder(integerOrderId);
- if (o != null)
- throw new OrderAlreadyExistsException("attempted to insert Order with duplicate orderId: " + integerOrderId);
-
- Item item = null;
- Iterator i = order.getCart().getLineItems().iterator();
+ int orderId = -1;
+
+ // TODO: the following should be in a transaction
+ // Add order to DB
+ try {
+ _orderDBControl.addOrder(order);
+ orderId = _orderDBControl.getLastOrderIdForUser(order.getUserId());
+ } catch (SQLException e) {
+
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+
+ // Add the cart items and update the quantities in the DB
+ Item item = null;
+ Iterator i = cart.getLineItems().iterator();
while (i.hasNext())
{
LineItem lineItem = (LineItem) i.next();
updateItemQuantity(lineItem.getItem(), lineItem.getQuantity());
+ addItemToOrder(orderId, lineItem.getItem(), lineItem.getQuantity());
}
-
- InMemoryObjectStore.store(ORDER_NAMESPACE, integerOrderId, order);
+ return orderId;
}
/**
*/
- public Order getOrder(String key)
- throws NoSuchOrderException
+ public Order getOrder(String userId, int key)
+ throws NoSuchOrderException, DataStoreException
{
- Integer orderId = convertStringToInteger(key);
-
- Order order = lookupOrder(orderId);
- if(order == null)
- throw new NoSuchOrderException("No order could be found with order id \"" + key + "\"");
-
+ Order order = new Order();
+ try {
+ order = _orderDBControl.getOrder(key, userId);
+ if(order.getOrderId() == -1)
+ throw new NoSuchOrderException("No order could be found with order id: " + key + " for user: " + userId);
+ } catch (SQLException e) {
+
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+
return order;
}
- /**
- */
- public Order[] getOrdersByUserId(String userId)
+ public Order[] getOrdersByUserId(String userId) throws DataStoreException
{
- ArrayList result = new ArrayList();
- Order order = null;
- Order[] orders = getOrders();
- for(int i = 0; i < orders.length; i++)
- {
- order = orders[i];
- if (order.getAccount().getUserId().equals(userId))
- result.add(order);
- }
-
- Order[] a = new Order[result.size()];
- result.toArray(a);
- return a;
+ Order[] orders;
+ try {
+ orders = _orderDBControl.getOrderByUserId(userId);
+ } catch (SQLException e) {
+
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+
+ return orders;
+ }
+
+ public OrderItem[] getOrderItemsByOrderId(int orderId) throws DataStoreException
+ {
+ OrderItem[] orderItems;
+ try {
+ orderItems = _orderDBControl.getOrderItems(orderId);
+ } catch (SQLException e) {
+
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+
+ return orderItems;
}
public List getCreditCards()
@@ -141,30 +145,21 @@
}
private void updateItemQuantity(Item item, int quantity)
- throws InvalidIdentifierException
+ throws InvalidIdentifierException, DataStoreException
{
_catalogControl.updateItemQuantity(item, quantity);
}
-
- private Order lookupOrder(Integer orderId)
- {
- return (Order)InMemoryObjectStore.find(ORDER_NAMESPACE, orderId);
- }
-
- private Order[] getOrders()
- {
- return (Order[])InMemoryObjectStore.toArray(ORDER_NAMESPACE, Order.class);
- }
-
- private LineItem[] getLineItems()
- {
- return (LineItem[])InMemoryObjectStore.toArray(LINE_ITEM_NAMESPACE, LineItem.class);
- }
-
- private static Integer convertStringToInteger(String i)
- {
- Integer integer = new Integer(0);
- integer = Integer.valueOf(i);
- return integer;
- }
+
+ private void addItemToOrder(int orderId, Item item, int quantity)
+ throws InvalidIdentifierException, DataStoreException
+ {
+ try {
+ _orderDBControl.addOrderItem(orderId, item.getItemId(), quantity);
+ } catch (SQLException e) {
+
+ // Print the stack to std out and bubble a friendly error
+ e.printStackTrace();
+ throw new DataStoreException("Database Error: " + e.getMessage());
+ }
+ }
}
Added: incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/data/AccountDBControl.jcx
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/data/AccountDBControl.jcx?view=auto&rev=161136
==============================================================================
--- incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/data/AccountDBControl.jcx (added)
+++ incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/data/AccountDBControl.jcx Tue Apr 12 16:13:55 2005
@@ -0,0 +1,66 @@
+/*
+ * Copyright 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.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.samples.petstore.controls.data;
+
+import java.sql.SQLException;
+
+import org.apache.beehive.samples.petstore.model.Account;
+import org.controlhaus.jdbc.JdbcControl;
+
+/**
+ * This control contains access to the pets database
+ */
+
+@org.apache.beehive.controls.api.bean.ControlExtension
+//PostgreSQL
+//@JdbcControl.ConnectionDataSource(jndiName="java:comp/env/jdbc/TestDB")
+
+//Derby
+@JdbcControl.ConnectionDriver(databaseDriverClass="org.apache.derby.jdbc.EmbeddedDriver",
+ databaseURL="jdbc:derby:" + DBProperties.dbLocation + "/petStoreDB;create=true")
+
+public interface AccountDBControl extends JdbcControl {
+
+ // getAccounts
+ @SQL(statement="select userId, password, email, firstname, lastname, status, favCategory, " +
+ "langPref, bannerData, myListOpt as myListState, bannerOpt as bannerOptState from Accounts")
+ public Account[] getAccounts() throws SQLException;
+
+ // checkAccountExists
+ @SQL(statement="select count(*) from Accounts where userId = {userId}")
+ public int checkAccountExists(String userId) throws SQLException;
+
+ // getAccountById
+ @SQL(statement="select userId, password, email, firstname, lastname, status, favCategory, " +
+ "langPref, bannerData, myListOpt as myListState, bannerOpt as bannerOptState from Accounts " +
+ "where userId = {userId}")
+ public Account getAccountById(String userId) throws SQLException;
+
+ // updateAccount
+ @SQL(statement="update Accounts set password = {account.password}, email = {account.email}, " +
+ "firstName = {account.firstName}, lastName = {account.lastName}, status = {account.status}, " +
+ "favCategory = {account.favCategory}, langPref = {account.langPref}, bannerData = {account.bannerData}, " +
+ "myListOpt = {account.myListState}, bannerOpt = {account.bannerOptState} where userId = {account.userId}")
+ public void updateAccount(Account account) throws SQLException;
+
+ // insertAccount
+ @SQL(statement="insert into Accounts values ({account.userId}, {account.password}, {account.email}, {account.firstName}, " +
+ "{account.lastName}, {account.status}, {account.favCategory}, {account.langPref}, " +
+ "{account.bannerData}, {account.myListState}, {account.bannerOptState})")
+ public void insertAccount(Account account) throws SQLException;
+}
\ No newline at end of file
Propchange: incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/data/AccountDBControl.jcx
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/data/AddressDBControl.jcx
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/data/AddressDBControl.jcx?view=auto&rev=161136
==============================================================================
--- incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/data/AddressDBControl.jcx (added)
+++ incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/data/AddressDBControl.jcx Tue Apr 12 16:13:55 2005
@@ -0,0 +1,71 @@
+/*
+ * Copyright 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.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.samples.petstore.controls.data;
+
+import java.sql.SQLException;
+
+import org.apache.beehive.samples.petstore.model.Address;
+import org.controlhaus.jdbc.JdbcControl;
+
+/**
+ * This control contains access to the pets database
+ */
+
+@org.apache.beehive.controls.api.bean.ControlExtension
+//PostgreSQL
+//@JdbcControl.ConnectionDataSource(jndiName="java:comp/env/jdbc/TestDB")
+
+//Derby
+@JdbcControl.ConnectionDriver(databaseDriverClass="org.apache.derby.jdbc.EmbeddedDriver",
+ databaseURL="jdbc:derby:" + DBProperties.dbLocation + "/petStoreDB;create=true")
+
+public interface AddressDBControl extends JdbcControl {
+
+ // checkAddressExists
+ @SQL(statement="select count(*) from Addresses where addressId = {addressId} and userId = {userId}")
+ public int checkAddressExists(int addressId, String userId) throws SQLException;
+
+ // getUserAddresses
+ @SQL(statement="select addressId, userId, name, phone, addr1, addr2, " +
+ "city, state, zip, country from Addresses where userId = {userId}")
+ public Address[] getUserAddresses(String userId) throws SQLException;
+
+ // getAddress
+ @SQL(statement="select addressId, userId, name, phone, addr1, addr2, " +
+ "city, state, zip, country from Addresses where addressId = {addressId}")
+ public Address getAddress(int addressId) throws SQLException;
+
+ // updateAddress
+ @SQL(statement="update Addresses set name = {address.name}, " +
+ "phone = {address.phone}, addr1 = {address.addr1}, addr2 = {address.addr2}, " +
+ "city = {address.city}, state = {address.state}, zip = {address.zip}, country = {address.country} " +
+ "where addressId = {address.addressId}")
+ public void updateAddress(Address address) throws SQLException;
+
+ // insertAddress
+ @SQL(statement="insert into Addresses(userId, name, phone, addr1, addr2, " +
+ "city, state, zip, country) values ({address.userId}, {address.name}, " +
+ "{address.phone}, {address.addr1}, {address.addr2}, " +
+ "{address.city}, {address.state}, {address.zip}, {address.country})")
+ public void insertAddress(Address address) throws SQLException;
+
+ // deleteAddress
+ @SQL(statement="delete from Addresses where addressId = {addressId} and userId = {userId}")
+ public void deleteAddress(int addressId, String userId) throws SQLException;
+
+}
\ No newline at end of file
Propchange: incubator/beehive/trunk/samples/petstoreWeb/WEB-INF/src/org/apache/beehive/samples/petstore/controls/data/AddressDBControl.jcx
------------------------------------------------------------------------------
svn:eol-style = native