Return-Path: Delivered-To: apmail-geronimo-scm-archive@www.apache.org Received: (qmail 97229 invoked from network); 26 Mar 2008 07:16:47 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 26 Mar 2008 07:16:47 -0000 Received: (qmail 20046 invoked by uid 500); 26 Mar 2008 07:16:45 -0000 Delivered-To: apmail-geronimo-scm-archive@geronimo.apache.org Received: (qmail 20000 invoked by uid 500); 26 Mar 2008 07:16:45 -0000 Mailing-List: contact scm-help@geronimo.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: dev@geronimo.apache.org List-Id: Delivered-To: mailing list scm@geronimo.apache.org Received: (qmail 19991 invoked by uid 99); 26 Mar 2008 07:16:45 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 26 Mar 2008 00:16:45 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 26 Mar 2008 07:15:52 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id E659F1A9832; Wed, 26 Mar 2008 00:16:09 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r641180 - in /geronimo/samples/branches/1.0/migration-hibernate: hibernate/ solutions/com/ solutions/org/ solutions/org/apache/ solutions/org/apache/geronimo/ solutions/org/apache/geronimo/demo/ solutions/org/apache/geronimo/demo/hibernate/... Date: Wed, 26 Mar 2008 07:16:06 -0000 To: scm@geronimo.apache.org From: ecraig@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080326071609.E659F1A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: ecraig Date: Wed Mar 26 00:16:01 2008 New Revision: 641180 URL: http://svn.apache.org/viewvc?rev=641180&view=rev Log: GERONIMO-3910 migration-hibernate cleaned up Added: geronimo/samples/branches/1.0/migration-hibernate/solutions/org/ geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/ geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/ geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/ geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/ geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/bo/ geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/bo/Stock.java geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/bo/User.java geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/bo/UserStock.java geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/dao/ geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/dao/TradeDAO.java geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/exception/ geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/exception/DBException.java geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/servlet/ geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/servlet/TradeDispatcherServlet.java geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/util/ geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/util/HibernateUtil.java geronimo/samples/branches/1.0/migration-hibernate/src/org/ geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/ geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/ geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/ geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/ geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/bo/ geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/bo/Stock.java geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/bo/User.java geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/bo/UserStock.java geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/dao/ geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/dao/TradeDAO.java geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/exception/ geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/exception/DBException.java geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/servlet/ geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/servlet/TradeDispatcherServlet.java geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/util/ geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/util/HibernateUtil.java Removed: geronimo/samples/branches/1.0/migration-hibernate/solutions/com/ geronimo/samples/branches/1.0/migration-hibernate/src/com/ Modified: geronimo/samples/branches/1.0/migration-hibernate/hibernate/Stock.hbm.xml geronimo/samples/branches/1.0/migration-hibernate/hibernate/User.hbm.xml geronimo/samples/branches/1.0/migration-hibernate/hibernate/UserStock.hbm.xml geronimo/samples/branches/1.0/migration-hibernate/hibernate/hibernate.cfg.xml geronimo/samples/branches/1.0/migration-hibernate/web/descriptors/web.xml geronimo/samples/branches/1.0/migration-hibernate/web/jsps/register.jsp geronimo/samples/branches/1.0/migration-hibernate/web/jsps/stocks.jsp geronimo/samples/branches/1.0/migration-hibernate/web/jsps/userstocks.jsp Modified: geronimo/samples/branches/1.0/migration-hibernate/hibernate/Stock.hbm.xml URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/hibernate/Stock.hbm.xml?rev=641180&r1=641179&r2=641180&view=diff ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/hibernate/Stock.hbm.xml (original) +++ geronimo/samples/branches/1.0/migration-hibernate/hibernate/Stock.hbm.xml Wed Mar 26 00:16:01 2008 @@ -20,7 +20,7 @@ "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> - + Modified: geronimo/samples/branches/1.0/migration-hibernate/hibernate/User.hbm.xml URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/hibernate/User.hbm.xml?rev=641180&r1=641179&r2=641180&view=diff ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/hibernate/User.hbm.xml (original) +++ geronimo/samples/branches/1.0/migration-hibernate/hibernate/User.hbm.xml Wed Mar 26 00:16:01 2008 @@ -20,7 +20,7 @@ "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> - + Modified: geronimo/samples/branches/1.0/migration-hibernate/hibernate/UserStock.hbm.xml URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/hibernate/UserStock.hbm.xml?rev=641180&r1=641179&r2=641180&view=diff ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/hibernate/UserStock.hbm.xml (original) +++ geronimo/samples/branches/1.0/migration-hibernate/hibernate/UserStock.hbm.xml Wed Mar 26 00:16:01 2008 @@ -20,7 +20,7 @@ "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> - + Modified: geronimo/samples/branches/1.0/migration-hibernate/hibernate/hibernate.cfg.xml URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/hibernate/hibernate.cfg.xml?rev=641180&r1=641179&r2=641180&view=diff ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/hibernate/hibernate.cfg.xml (original) +++ geronimo/samples/branches/1.0/migration-hibernate/hibernate/hibernate.cfg.xml Wed Mar 26 00:16:01 2008 @@ -44,9 +44,9 @@ - + Added: geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/bo/Stock.java URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/bo/Stock.java?rev=641180&view=auto ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/bo/Stock.java (added) +++ geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/bo/Stock.java Wed Mar 26 00:16:01 2008 @@ -0,0 +1,58 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.geronimo.demo.hibernate.bo; + +public class Stock { + private String id = null; + private String name = null; + private float price = 0.0f; + + + public Stock(String id, String name, float price) { + super(); + // TODO Auto-generated constructor stub + this.id = id; + this.name = name; + this.price = price; + } + + public Stock() { + super(); + // TODO Auto-generated constructor stub + } + + public String getId() { + return id; + } + public void setId(String id) { + this.id = id; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public float getPrice() { + return price; + } + public void setPrice(float price) { + this.price = price; + } + + +} Added: geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/bo/User.java URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/bo/User.java?rev=641180&view=auto ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/bo/User.java (added) +++ geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/bo/User.java Wed Mar 26 00:16:01 2008 @@ -0,0 +1,80 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.geronimo.demo.hibernate.bo; + +import java.util.HashSet; +import java.util.Set; + +public class User { + + private String userId = null; + private String name = null; + private String password = null; + private String address = null; + private float cash = 0f; + + + public User() { + super(); + // TODO Auto-generated constructor stub + } + + public User(String userId,String name,String password,String address,float cash){ + this.userId = userId; + this.name = name; + this.password = password; + this.address = address; + this.cash = cash; + + } + + public String getAddress() { + return address; + } + public void setAddress(String address) { + this.address = address; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getPassword() { + return password; + } + public void setPassword(String password) { + this.password = password; + } + public String getUserId() { + return userId; + } + public void setUserId(String userId) { + this.userId = userId; + } + + public float getCash() { + return cash; + } + + public void setCash(float cash) { + this.cash = cash; + } + + + +} Added: geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/bo/UserStock.java URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/bo/UserStock.java?rev=641180&view=auto ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/bo/UserStock.java (added) +++ geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/bo/UserStock.java Wed Mar 26 00:16:01 2008 @@ -0,0 +1,92 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.geronimo.demo.hibernate.bo; + +import java.io.Serializable; + +import org.apache.geronimo.demo.hibernate.bo.UserStock; + +public class UserStock implements Serializable{ + private String id = null; + private String name = null; + private float price = 0.0f; + private int quantity = 0; + private String userId = null; + + public UserStock(String id, String name, float price, int quantity,String userId) { + super(); + // TODO Auto-generated constructor stub + this.id = id; + this.name = name; + this.price = price; + this.quantity = quantity; + this.userId = userId; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public UserStock() { + super(); + // TODO Auto-generated constructor stub + } + + public String getId() { + return id; + } + public void setId(String id) { + this.id = id; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public float getPrice() { + return price; + } + public void setPrice(float price) { + this.price = price; + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public boolean equals(Object obj) { + if(!(obj instanceof UserStock)) return false; + UserStock stock = (UserStock)obj; + if(this.id.equals(stock.getId()) && this.userId.equals(stock.getUserId())) + return true; + return false; + } + + public int hashCode() { + return this.id.hashCode() ^ this.userId.hashCode(); + } + +} Added: geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/dao/TradeDAO.java URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/dao/TradeDAO.java?rev=641180&view=auto ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/dao/TradeDAO.java (added) +++ geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/dao/TradeDAO.java Wed Mar 26 00:16:01 2008 @@ -0,0 +1,210 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.geronimo.demo.hibernate.dao; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import org.apache.geronimo.demo.hibernate.bo.Stock; +import org.apache.geronimo.demo.hibernate.bo.User; +import org.apache.geronimo.demo.hibernate.bo.UserStock; +import org.apache.geronimo.demo.hibernate.exception.DBException; +import org.apache.geronimo.demo.hibernate.util.HibernateUtil; +import org.hibernate.Query; +import org.hibernate.Session; + + +public class TradeDAO { + + + Session session = null; + + public TradeDAO() throws Exception { + + } + + public User getUserByUserId(String userId) throws DBException { + + session = HibernateUtil.getCurrentSession(); + Query q = session.createQuery("from User u where u.userId=:userId"); + q.setString("userId", userId); + return (User) q.uniqueResult(); + + } + + public List getUserStocks(String userId) throws DBException { + + session = HibernateUtil.getCurrentSession(); + Query q = session.createQuery("from UserStock u where u.userId=:userId"); + q.setString("userId", userId); + List list = q.list(); + return list; + + } + + public List getStocks() throws DBException { + + session = HibernateUtil.getCurrentSession(); + Query q = session.createQuery("from Stock"); + List stocks = q.list(); + return stocks; + + } + + public float getUserCash(String userId) throws DBException { + + User user = null; + session = HibernateUtil.getCurrentSession(); + Query q = session.createQuery("from User u where u.userId=:userId"); + q.setString("userId", userId); + return ((User) q.uniqueResult()).getCash(); + + } + + public boolean buyStock(String userId, String stockId, int quantity) + throws DBException { + float cashAvailable = getUserCash(userId); + float costOfStock = getStockPrice(stockId); + float totalCost = costOfStock * quantity; + int availableStock = getStockQuantityForUser(userId, stockId); + cashAvailable = cashAvailable - totalCost; + + if (cashAvailable >= 0 && (availableStock != 0)) { + availableStock = availableStock + quantity; + setUserCash(userId, cashAvailable); + setUserStock(userId, stockId, availableStock); + + } else if (cashAvailable >= 0 && (availableStock == 0)) { + availableStock = availableStock + quantity; + setUserCash(userId, cashAvailable); + addUserStock(userId, stockId, availableStock); + + } else { + return false; + } + return true; + } + + public boolean sellStock(String userId, String stockId, int quantity) + throws DBException { + float cashAvailable = getUserCash(userId); + float costOfStock = getStockPrice(stockId); + float totalCost = costOfStock * quantity; + int availableStock = getStockQuantityForUser(userId, stockId); + cashAvailable = cashAvailable + totalCost; + availableStock = availableStock - quantity; + if (availableStock >= 0) { + + setUserCash(userId, cashAvailable); + setUserStock(userId, stockId, availableStock); + + } else { + return false; + } + return true; + } + + public float getStockPrice(String stockId) throws DBException { + + session = HibernateUtil.getCurrentSession(); + Query q = session.createQuery("from Stock s where s.id=:id"); + q.setString("id", stockId); + Stock stock = (Stock) q.uniqueResult(); + return stock.getPrice(); + + } + + private boolean setUserCash(String userId, float cash) throws DBException { + + session = HibernateUtil.getCurrentSession(); + Query q = session.createQuery("from User u where u.userId=:userId"); + q.setString("userId", userId); + User user = (User) q.uniqueResult(); + user.setCash(cash); + session.save(user); + return true; + + } + + // buy = true sell = false + private boolean setUserStock(String userId, String stockId, int quantity) + throws DBException { + + session = HibernateUtil.getCurrentSession(); + Query q = session.createQuery("from UserStock u where u.userId=:userId and u.id=:id"); + q.setString("userId", userId); + q.setString("id", stockId); + UserStock userStock = (UserStock) q.uniqueResult(); + userStock.setQuantity(quantity); + if(quantity != 0){ + session.save(userStock); + }else{ + session.delete(userStock); + } + return true; + + } + + private boolean addUserStock(String userId, String stockId, int quantity) + throws DBException { + + session = HibernateUtil.getCurrentSession(); + Query q = session.createQuery("from Stock s where s.id=:id"); + q.setString("id", stockId); + Stock stock = (Stock) q.uniqueResult(); + UserStock userStock = new UserStock(stock.getId(), stock.getName(), stock.getPrice(),quantity,userId); + session.save(userStock); + return true; + + } + + public int getStockQuantityForUser(String userId, String stockId) + throws DBException { + + session = HibernateUtil.getCurrentSession(); + Query q = session.createQuery("from UserStock u where u.userId=:userId and u.id=:id"); + q.setString("userId", userId); + q.setString("id", stockId); + UserStock userStock = (UserStock) q.uniqueResult(); + if(userStock == null){ + return 0; + } + return userStock.getQuantity(); + } + + public boolean addUser(String userId, String name, String password, + String address, float cash) throws DBException { + session = HibernateUtil.getCurrentSession(); + User user = new User(); + user.setAddress(address); + user.setCash(cash); + user.setName(name); + user.setPassword(password); + user.setUserId(userId); + session.save(user); + return true; + } + + public void remove() throws DBException { + + } + +} Added: geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/exception/DBException.java URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/exception/DBException.java?rev=641180&view=auto ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/exception/DBException.java (added) +++ geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/exception/DBException.java Wed Mar 26 00:16:01 2008 @@ -0,0 +1,26 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.geronimo.demo.hibernate.exception; + +public class DBException extends Exception { +public DBException(){ +super(); +} +public DBException(String message){ + super(message); + } +} Added: geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/servlet/TradeDispatcherServlet.java URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/servlet/TradeDispatcherServlet.java?rev=641180&view=auto ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/servlet/TradeDispatcherServlet.java (added) +++ geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/servlet/TradeDispatcherServlet.java Wed Mar 26 00:16:01 2008 @@ -0,0 +1,195 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.geronimo.demo.hibernate.servlet; + +import java.io.IOException; +import java.util.List; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import org.apache.geronimo.demo.hibernate.bo.User; +import org.apache.geronimo.demo.hibernate.dao.TradeDAO; +import org.apache.geronimo.demo.hibernate.exception.DBException; +import org.apache.geronimo.demo.hibernate.util.HibernateUtil; +import org.hibernate.Transaction; + + +public class TradeDispatcherServlet extends HttpServlet { + + private TradeDAO tradeDAO = null; + + private HttpSession session = null; + + public void init() throws ServletException { + // TODO Auto-generated method stub + super.init(); + try { + tradeDAO = new TradeDAO(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + protected void doGet(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + Transaction tr = HibernateUtil.getCurrentSession().getTransaction(); + tr.begin(); + System.out.println("****************************Starting Transaction*****************"); + try { + session = request.getSession(true); + session.removeAttribute("status"); + String page = request.getServletPath(); + if ("/login".equals(page)) { + String userId = (String) request.getParameter("user"); + String password = (String) request.getParameter("password"); + if (userId != null && password != null) { + User user = tradeDAO.getUserByUserId(userId); + session.setAttribute("user", user); + if (user != null && password.equals(user.getPassword())) { + page = "/stocks"; + session.setAttribute("userId", userId); + session.setAttribute("password", password); + List stocks = tradeDAO.getStocks(); + session.setAttribute("stocks", stocks); + } else { + page = "/login"; + session.setAttribute("status", + "Logon Failed Please Try Again!"); + } + } else { + page = "/login"; + } + + } else if ("/stocks".equals(page)) { + List stocks = tradeDAO.getStocks(); + session.setAttribute("stocks", stocks); + } else if ("/userstocks".equals(page)) { + String userId = (String) session.getAttribute("userId"); + List userStocks = tradeDAO.getUserStocks(userId); + session.setAttribute("userStocks", userStocks); + } else if ("/register".equals(page)) { + String userId = request.getParameter("user"); + String name = request.getParameter("name"); + String password = request.getParameter("password"); + String address = request.getParameter("address"); + float cash = Float + .parseFloat((request.getParameter("cash") == null) ? "0" + : request.getParameter("cash")); + if (userId != null && name != null && password != null + && address != null && cash != 0) { + User user = tradeDAO.getUserByUserId(userId); + if (user == null) { + boolean status = tradeDAO.addUser(userId, name, + password, address, cash); + if (status) { + page = "/login"; + } else { + page = "/register"; + session.setAttribute("status", + "Registration Failed Please Try Again!"); + } + } else { + page = "/register"; + session.setAttribute("status", + "Registration Failed User Already Exists!"); + } + + }else if (userId != null && name != null && password != null + && address != null && cash == 0) { + session.setAttribute("status", + "Registration Failed Not enough Cash!"); + page = "/register"; + + }else { + page = "/register"; + } + + } else if ("/sell".equals(page)) { + page = "/userstocks"; + String userId = (String) session.getAttribute("userId"); + String stockId = request.getParameter("select"); + int quantity = Integer.parseInt(request + .getParameter("sellQuantity")); + boolean status = tradeDAO.sellStock(userId, stockId, quantity); + if (!status) { + session + .setAttribute("status", + "Not Enough Stocks or Operation Failed Please Try Again!"); + } + List userStocks = tradeDAO.getUserStocks(userId); + session.setAttribute("userStocks", userStocks); + User user = tradeDAO.getUserByUserId(userId); + session.setAttribute("user", user); + } else if ("/buy".equals(page)) { + page = "/stocks"; + String userId = (String) session.getAttribute("userId"); + String stockId = request.getParameter("select"); + int quantity = Integer.parseInt(request + .getParameter("buyQuantity")); + boolean status = tradeDAO.buyStock(userId, stockId, quantity); + if (!status) { + session + .setAttribute("status", + "Not Enough Money or Operation Failed Please Try Again!"); + } + List userStocks = tradeDAO.getUserStocks(userId); + session.setAttribute("userStocks", userStocks); + User user = tradeDAO.getUserByUserId(userId); + session.setAttribute("user", user); + } + System.out.println("****************************Before End Transaction*****************"); + tr.commit(); + System.out.println("****************************After End Transaction*****************"); + request.getRequestDispatcher(page + ".jsp").forward(request, + response); + } catch (DBException e) { + //HibernateUtil.getCurrentSession().getTransaction().rollback(); + throw new ServletException(e.getMessage()); + + } catch (Exception ex) { + //HibernateUtil.getCurrentSession().getTransaction().rollback(); + ex.printStackTrace(System.out); + //throw new ServletException(ex.getMessage()); + } + + } + + protected void doPost(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + // TODO Auto-generated method stub + + doGet(request, response); + } + + public void destroy() { + // TODO Auto-generated method stub + super.destroy(); + try { + tradeDAO.remove(); + } catch (DBException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + +} Added: geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/util/HibernateUtil.java URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/util/HibernateUtil.java?rev=641180&view=auto ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/util/HibernateUtil.java (added) +++ geronimo/samples/branches/1.0/migration-hibernate/solutions/org/apache/geronimo/demo/hibernate/util/HibernateUtil.java Wed Mar 26 00:16:01 2008 @@ -0,0 +1,86 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.geronimo.demo.hibernate.util; + +import org.hibernate.HibernateException; +import org.hibernate.Session; +import org.hibernate.cfg.Configuration; + +/** + * Configures and provides access to Hibernate sessions, tied to the + * current thread of execution. Follows the Thread Local Session + * pattern, see {@link http://hibernate.org/42.html}. + */ +public class HibernateUtil { + + /** location of the Hibernate Configuration File */ + private static String CONFIG_FILE_LOCATION = "hibernate.cfg.xml"; + + /** Holds a single instance of Session */ + private static final ThreadLocal threadLocal = new ThreadLocal(); + + /** The single instance of hibernate configuration */ + private static final Configuration cfg = new Configuration(); + + /** The single instance of hibernate SessionFactory */ + private static org.hibernate.SessionFactory sessionFactory; + + /** + * Returns the ThreadLocal Session instance. Lazy initialize + * the SessionFactory if needed. + * + * @return Session + * @throws HibernateException + */ + public static Session getCurrentSession() throws HibernateException { + Session session = (Session) threadLocal.get(); + + if (session == null || ! session.isConnected()) { + if (sessionFactory == null) { + try { + cfg.configure(CONFIG_FILE_LOCATION); + sessionFactory = cfg.buildSessionFactory(); + } + catch (Exception e) { + System.err.println("%%%% Error Creating SessionFactory %%%%"); + e.printStackTrace(); + } + } + session = sessionFactory.openSession(); + threadLocal.set(session); + } + + return session; + } + + /** + * Close the single hibernate session instance. + * + * @throws HibernateException + */ + public static void closeSession() throws HibernateException { + Session session = (Session) threadLocal.get(); + + + if (session != null) { + session.close(); + } + } + + + +} \ No newline at end of file Added: geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/bo/Stock.java URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/bo/Stock.java?rev=641180&view=auto ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/bo/Stock.java (added) +++ geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/bo/Stock.java Wed Mar 26 00:16:01 2008 @@ -0,0 +1,58 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.geronimo.demo.hibernate.bo; + +public class Stock { + private String id = null; + private String name = null; + private float price = 0.0f; + + + public Stock(String id, String name, float price) { + super(); + // TODO Auto-generated constructor stub + this.id = id; + this.name = name; + this.price = price; + } + + public Stock() { + super(); + // TODO Auto-generated constructor stub + } + + public String getId() { + return id; + } + public void setId(String id) { + this.id = id; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public float getPrice() { + return price; + } + public void setPrice(float price) { + this.price = price; + } + + +} Added: geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/bo/User.java URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/bo/User.java?rev=641180&view=auto ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/bo/User.java (added) +++ geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/bo/User.java Wed Mar 26 00:16:01 2008 @@ -0,0 +1,80 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.geronimo.demo.hibernate.bo; + +import java.util.HashSet; +import java.util.Set; + +public class User { + + private String userId = null; + private String name = null; + private String password = null; + private String address = null; + private float cash = 0f; + + + public User() { + super(); + // TODO Auto-generated constructor stub + } + + public User(String userId,String name,String password,String address,float cash){ + this.userId = userId; + this.name = name; + this.password = password; + this.address = address; + this.cash = cash; + + } + + public String getAddress() { + return address; + } + public void setAddress(String address) { + this.address = address; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getPassword() { + return password; + } + public void setPassword(String password) { + this.password = password; + } + public String getUserId() { + return userId; + } + public void setUserId(String userId) { + this.userId = userId; + } + + public float getCash() { + return cash; + } + + public void setCash(float cash) { + this.cash = cash; + } + + + +} Added: geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/bo/UserStock.java URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/bo/UserStock.java?rev=641180&view=auto ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/bo/UserStock.java (added) +++ geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/bo/UserStock.java Wed Mar 26 00:16:01 2008 @@ -0,0 +1,93 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.geronimo.demo.hibernate.bo; + +import java.io.Serializable; + +import org.apache.geronimo.demo.hibernate.bo.UserStock; + + +public class UserStock implements Serializable{ + private String id = null; + private String name = null; + private float price = 0.0f; + private int quantity = 0; + private String userId = null; + + public UserStock(String id, String name, float price, int quantity,String userId) { + super(); + // TODO Auto-generated constructor stub + this.id = id; + this.name = name; + this.price = price; + this.quantity = quantity; + this.userId = userId; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public UserStock() { + super(); + // TODO Auto-generated constructor stub + } + + public String getId() { + return id; + } + public void setId(String id) { + this.id = id; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public float getPrice() { + return price; + } + public void setPrice(float price) { + this.price = price; + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public boolean equals(Object obj) { + if(!(obj instanceof UserStock)) return false; + UserStock stock = (UserStock)obj; + if(this.id.equals(stock.getId()) && this.userId.equals(stock.getUserId())) + return true; + return false; + } + + public int hashCode() { + return this.id.hashCode() ^ this.userId.hashCode(); + } + +} Added: geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/dao/TradeDAO.java URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/dao/TradeDAO.java?rev=641180&view=auto ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/dao/TradeDAO.java (added) +++ geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/dao/TradeDAO.java Wed Mar 26 00:16:01 2008 @@ -0,0 +1,222 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.geronimo.demo.hibernate.dao; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import org.apache.geronimo.demo.hibernate.bo.Stock; +import org.apache.geronimo.demo.hibernate.bo.User; +import org.apache.geronimo.demo.hibernate.bo.UserStock; +import org.apache.geronimo.demo.hibernate.exception.DBException; +import org.apache.geronimo.demo.hibernate.util.HibernateUtil; +import org.hibernate.Query; +import org.hibernate.Session; +import org.hibernate.SessionFactory; + + +public class TradeDAO { + + SessionFactory factory = null; + Session session = null; + + public TradeDAO() throws Exception { + + try { + InitialContext ctx = new InitialContext(); + factory = (SessionFactory)ctx.lookup("java:hibernate/BrokerageSessionFactory"); + + } catch (NamingException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } + + public User getUserByUserId(String userId) throws DBException { + + session = factory.getCurrentSession(); + Query q = session.createQuery("from User u where u.userId=:userId"); + q.setString("userId", userId); + return (User) q.uniqueResult(); + + } + + public List getUserStocks(String userId) throws DBException { + + session = factory.getCurrentSession(); + Query q = session.createQuery("from UserStock u where u.userId=:userId"); + q.setString("userId", userId); + List list = q.list(); + return list; + + } + + public List getStocks() throws DBException { + + session = factory.getCurrentSession(); + Query q = session.createQuery("from Stock"); + List stocks = q.list(); + return stocks; + + } + + public float getUserCash(String userId) throws DBException { + + User user = null; + session = factory.getCurrentSession(); + Query q = session.createQuery("from User u where u.userId=:userId"); + q.setString("userId", userId); + return ((User) q.uniqueResult()).getCash(); + + } + + public boolean buyStock(String userId, String stockId, int quantity) + throws DBException { + float cashAvailable = getUserCash(userId); + float costOfStock = getStockPrice(stockId); + float totalCost = costOfStock * quantity; + int availableStock = getStockQuantityForUser(userId, stockId); + cashAvailable = cashAvailable - totalCost; + + if (cashAvailable >= 0 && (availableStock != 0)) { + availableStock = availableStock + quantity; + setUserCash(userId, cashAvailable); + setUserStock(userId, stockId, availableStock); + + } else if (cashAvailable >= 0 && (availableStock == 0)) { + availableStock = availableStock + quantity; + setUserCash(userId, cashAvailable); + addUserStock(userId, stockId, availableStock); + + } else { + return false; + } + return true; + } + + public boolean sellStock(String userId, String stockId, int quantity) + throws DBException { + float cashAvailable = getUserCash(userId); + float costOfStock = getStockPrice(stockId); + float totalCost = costOfStock * quantity; + int availableStock = getStockQuantityForUser(userId, stockId); + cashAvailable = cashAvailable + totalCost; + availableStock = availableStock - quantity; + if (availableStock >= 0) { + + setUserCash(userId, cashAvailable); + setUserStock(userId, stockId, availableStock); + + } else { + return false; + } + return true; + } + + public float getStockPrice(String stockId) throws DBException { + + session = factory.getCurrentSession(); + Query q = session.createQuery("from Stock s where s.id=:id"); + q.setString("id", stockId); + Stock stock = (Stock) q.uniqueResult(); + return stock.getPrice(); + + } + + private boolean setUserCash(String userId, float cash) throws DBException { + + session = factory.getCurrentSession(); + Query q = session.createQuery("from User u where u.userId=:userId"); + q.setString("userId", userId); + User user = (User) q.uniqueResult(); + user.setCash(cash); + session.save(user); + return true; + + } + + // buy = true sell = false + private boolean setUserStock(String userId, String stockId, int quantity) + throws DBException { + + session = factory.getCurrentSession(); + Query q = session.createQuery("from UserStock u where u.userId=:userId and u.id=:id"); + q.setString("userId", userId); + q.setString("id", stockId); + UserStock userStock = (UserStock) q.uniqueResult(); + userStock.setQuantity(quantity); + if(quantity != 0){ + session.save(userStock); + }else{ + session.delete(userStock); + } + return true; + + } + + private boolean addUserStock(String userId, String stockId, int quantity) + throws DBException { + + session = factory.getCurrentSession(); + Query q = session.createQuery("from Stock s where s.id=:id"); + q.setString("id", stockId); + Stock stock = (Stock) q.uniqueResult(); + UserStock userStock = new UserStock(stock.getId(), stock.getName(), stock.getPrice(),quantity,userId); + session.save(userStock); + return true; + + } + + public int getStockQuantityForUser(String userId, String stockId) + throws DBException { + + session = factory.getCurrentSession(); + Query q = session.createQuery("from UserStock u where u.userId=:userId and u.id=:id"); + q.setString("userId", userId); + q.setString("id", stockId); + UserStock userStock = (UserStock) q.uniqueResult(); + if(userStock == null){ + return 0; + } + return userStock.getQuantity(); + } + + public boolean addUser(String userId, String name, String password, + String address, float cash) throws DBException { + session = factory.getCurrentSession(); + User user = new User(); + user.setAddress(address); + user.setCash(cash); + user.setName(name); + user.setPassword(password); + user.setUserId(userId); + session.save(user); + return true; + } + + public void remove() throws DBException { + + } + +} Added: geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/exception/DBException.java URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/exception/DBException.java?rev=641180&view=auto ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/exception/DBException.java (added) +++ geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/exception/DBException.java Wed Mar 26 00:16:01 2008 @@ -0,0 +1,26 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.geronimo.demo.hibernate.exception; + +public class DBException extends Exception { +public DBException(){ +super(); +} +public DBException(String message){ + super(message); + } +} Added: geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/servlet/TradeDispatcherServlet.java URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/servlet/TradeDispatcherServlet.java?rev=641180&view=auto ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/servlet/TradeDispatcherServlet.java (added) +++ geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/servlet/TradeDispatcherServlet.java Wed Mar 26 00:16:01 2008 @@ -0,0 +1,209 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.geronimo.demo.hibernate.servlet; + +import java.io.IOException; +import java.util.List; + +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import org.apache.geronimo.demo.hibernate.bo.User; +import org.apache.geronimo.demo.hibernate.dao.TradeDAO; +import org.apache.geronimo.demo.hibernate.exception.DBException; +import org.apache.geronimo.demo.hibernate.util.HibernateUtil; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.Transaction; + + +public class TradeDispatcherServlet extends HttpServlet { + + private TradeDAO tradeDAO = null; + + private HttpSession session = null; + + public void init() throws ServletException { + // TODO Auto-generated method stub + super.init(); + try { + tradeDAO = new TradeDAO(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + protected void doGet(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + + Session hsession = null; + try { + InitialContext ctx = new InitialContext(); + SessionFactory factory = (SessionFactory)ctx.lookup("java:hibernate/BrokerageSessionFactory"); + hsession = factory.openSession(); + } catch (NamingException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + Transaction tr = hsession.beginTransaction(); + System.out.println("****************************Starting Transaction*****************"); + try { + session = request.getSession(true); + session.removeAttribute("status"); + String page = request.getServletPath(); + if ("/login".equals(page)) { + String userId = (String) request.getParameter("user"); + String password = (String) request.getParameter("password"); + if (userId != null && password != null) { + User user = tradeDAO.getUserByUserId(userId); + session.setAttribute("user", user); + if (user != null && password.equals(user.getPassword())) { + page = "/stocks"; + session.setAttribute("userId", userId); + session.setAttribute("password", password); + List stocks = tradeDAO.getStocks(); + session.setAttribute("stocks", stocks); + } else { + page = "/login"; + session.setAttribute("status", + "Logon Failed Please Try Again!"); + } + } else { + page = "/login"; + } + + } else if ("/stocks".equals(page)) { + List stocks = tradeDAO.getStocks(); + session.setAttribute("stocks", stocks); + } else if ("/userstocks".equals(page)) { + String userId = (String) session.getAttribute("userId"); + List userStocks = tradeDAO.getUserStocks(userId); + session.setAttribute("userStocks", userStocks); + } else if ("/register".equals(page)) { + String userId = request.getParameter("user"); + String name = request.getParameter("name"); + String password = request.getParameter("password"); + String address = request.getParameter("address"); + float cash = Float + .parseFloat((request.getParameter("cash") == null) ? "0" + : request.getParameter("cash")); + if (userId != null && name != null && password != null + && address != null && cash != 0) { + User user = tradeDAO.getUserByUserId(userId); + if (user == null) { + boolean status = tradeDAO.addUser(userId, name, + password, address, cash); + if (status) { + page = "/login"; + } else { + page = "/register"; + session.setAttribute("status", + "Registration Failed Please Try Again!"); + } + } else { + page = "/register"; + session.setAttribute("status", + "Registration Failed User Already Exists!"); + } + + }else if (userId != null && name != null && password != null + && address != null && cash == 0) { + session.setAttribute("status", + "Registration Failed Not enough Cash!"); + page = "/register"; + + }else { + page = "/register"; + } + + } else if ("/sell".equals(page)) { + page = "/userstocks"; + String userId = (String) session.getAttribute("userId"); + String stockId = request.getParameter("select"); + int quantity = Integer.parseInt(request + .getParameter("sellQuantity")); + boolean status = tradeDAO.sellStock(userId, stockId, quantity); + if (!status) { + session + .setAttribute("status", + "Not Enough Stocks or Operation Failed Please Try Again!"); + } + List userStocks = tradeDAO.getUserStocks(userId); + session.setAttribute("userStocks", userStocks); + User user = tradeDAO.getUserByUserId(userId); + session.setAttribute("user", user); + } else if ("/buy".equals(page)) { + page = "/stocks"; + String userId = (String) session.getAttribute("userId"); + String stockId = request.getParameter("select"); + int quantity = Integer.parseInt(request + .getParameter("buyQuantity")); + boolean status = tradeDAO.buyStock(userId, stockId, quantity); + if (!status) { + session + .setAttribute("status", + "Not Enough Money or Operation Failed Please Try Again!"); + } + List userStocks = tradeDAO.getUserStocks(userId); + session.setAttribute("userStocks", userStocks); + User user = tradeDAO.getUserByUserId(userId); + session.setAttribute("user", user); + } + System.out.println("****************************Before End Transaction*****************"); + tr.commit(); + System.out.println("****************************After End Transaction*****************"); + request.getRequestDispatcher(page + ".jsp").forward(request, + response); + } catch (DBException e) { + //HibernateUtil.getCurrentSession().getTransaction().rollback(); + throw new ServletException(e.getMessage()); + + } catch (Exception ex) { + //HibernateUtil.getCurrentSession().getTransaction().rollback(); + ex.printStackTrace(System.out); + //throw new ServletException(ex.getMessage()); + } + + } + + protected void doPost(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + // TODO Auto-generated method stub + + doGet(request, response); + } + + public void destroy() { + // TODO Auto-generated method stub + super.destroy(); + try { + tradeDAO.remove(); + } catch (DBException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + +} Added: geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/util/HibernateUtil.java URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/util/HibernateUtil.java?rev=641180&view=auto ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/util/HibernateUtil.java (added) +++ geronimo/samples/branches/1.0/migration-hibernate/src/org/apache/geronimo/demo/hibernate/util/HibernateUtil.java Wed Mar 26 00:16:01 2008 @@ -0,0 +1,86 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.geronimo.demo.hibernate.util; + +import org.hibernate.HibernateException; +import org.hibernate.Session; +import org.hibernate.cfg.Configuration; + +/** + * Configures and provides access to Hibernate sessions, tied to the + * current thread of execution. Follows the Thread Local Session + * pattern, see {@link http://hibernate.org/42.html}. + */ +public class HibernateUtil { + + /** location of the Hibernate Configuration File */ + private static String CONFIG_FILE_LOCATION = "hibernate.cfg.xml"; + + /** Holds a single instance of Session */ + private static final ThreadLocal threadLocal = new ThreadLocal(); + + /** The single instance of hibernate configuration */ + private static final Configuration cfg = new Configuration(); + + /** The single instance of hibernate SessionFactory */ + private static org.hibernate.SessionFactory sessionFactory; + + /** + * Returns the ThreadLocal Session instance. Lazy initialize + * the SessionFactory if needed. + * + * @return Session + * @throws HibernateException + */ + public static Session getCurrentSession() throws HibernateException { + Session session = (Session) threadLocal.get(); + + if (session == null || ! session.isConnected()) { + if (sessionFactory == null) { + try { + cfg.configure(CONFIG_FILE_LOCATION); + sessionFactory = cfg.buildSessionFactory(); + } + catch (Exception e) { + System.err.println("%%%% Error Creating SessionFactory %%%%"); + e.printStackTrace(); + } + } + session = sessionFactory.openSession(); + threadLocal.set(session); + } + + return session; + } + + /** + * Close the single hibernate session instance. + * + * @throws HibernateException + */ + public static void closeSession() throws HibernateException { + Session session = (Session) threadLocal.get(); + + + if (session != null) { + session.close(); + } + } + + + +} \ No newline at end of file Modified: geronimo/samples/branches/1.0/migration-hibernate/web/descriptors/web.xml URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/web/descriptors/web.xml?rev=641180&r1=641179&r2=641180&view=diff ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/web/descriptors/web.xml (original) +++ geronimo/samples/branches/1.0/migration-hibernate/web/descriptors/web.xml Wed Mar 26 00:16:01 2008 @@ -20,7 +20,7 @@ Trade-Dispatcher TradeDispatcher -com.dev.trade.servlet.TradeDispatcherServlet +org.apache.geronimo.demo.hibernate.servlet.TradeDispatcherServlet TradeDispatcher Modified: geronimo/samples/branches/1.0/migration-hibernate/web/jsps/register.jsp URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/web/jsps/register.jsp?rev=641180&r1=641179&r2=641180&view=diff ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/web/jsps/register.jsp (original) +++ geronimo/samples/branches/1.0/migration-hibernate/web/jsps/register.jsp Wed Mar 26 00:16:01 2008 @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. --%> -<%@page language="java" import="com.dev.trade.bo.*" %> +<%@page language="java" import="org.apache.geronimo.demo.hibernate.bo.*" %> Trader Registration Modified: geronimo/samples/branches/1.0/migration-hibernate/web/jsps/stocks.jsp URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/web/jsps/stocks.jsp?rev=641180&r1=641179&r2=641180&view=diff ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/web/jsps/stocks.jsp (original) +++ geronimo/samples/branches/1.0/migration-hibernate/web/jsps/stocks.jsp Wed Mar 26 00:16:01 2008 @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. --%> -<%@page language="java" import="java.util.List,com.dev.trade.bo.Stock" %> +<%@page language="java" import="java.util.List,org.apache.geronimo.demo.hibernate.bo.Stock" %> Available Stocks Modified: geronimo/samples/branches/1.0/migration-hibernate/web/jsps/userstocks.jsp URL: http://svn.apache.org/viewvc/geronimo/samples/branches/1.0/migration-hibernate/web/jsps/userstocks.jsp?rev=641180&r1=641179&r2=641180&view=diff ============================================================================== --- geronimo/samples/branches/1.0/migration-hibernate/web/jsps/userstocks.jsp (original) +++ geronimo/samples/branches/1.0/migration-hibernate/web/jsps/userstocks.jsp Wed Mar 26 00:16:01 2008 @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. --%> -<%@page language="java" import="java.util.List,com.dev.trade.bo.*" %> +<%@page language="java" import="java.util.List,org.apache.geronimo.demo.hibernate.bo.*" %> Portfolio