Return-Path: X-Original-To: apmail-flex-commits-archive@www.apache.org Delivered-To: apmail-flex-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 529C618DB2 for ; Tue, 4 Aug 2015 09:51:39 +0000 (UTC) Received: (qmail 24699 invoked by uid 500); 4 Aug 2015 09:51:39 -0000 Delivered-To: apmail-flex-commits-archive@flex.apache.org Received: (qmail 24668 invoked by uid 500); 4 Aug 2015 09:51:39 -0000 Mailing-List: contact commits-help@flex.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@flex.apache.org Delivered-To: mailing list commits@flex.apache.org Received: (qmail 24659 invoked by uid 99); 4 Aug 2015 09:51:39 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 04 Aug 2015 09:51:39 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 18A61E00B2; Tue, 4 Aug 2015 09:51:39 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: cdutz@apache.org To: commits@flex.apache.org Date: Tue, 04 Aug 2015 09:51:39 -0000 Message-Id: <28a952e270454f25ab877306f062e0a4@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [01/51] [partial] flex-blazeds git commit: - Moved old stuff to an "attic" directory - Changed the directory structure to comply to a default maven directory structure Repository: flex-blazeds Updated Branches: refs/heads/4.8.0 95f7f472c -> 752297593 http://git-wip-us.apache.org/repos/asf/flex-blazeds/blob/75229759/attic/apps/samples/WEB-INF/src/flex/samples/dcd/product/ProductService.java ---------------------------------------------------------------------- diff --git a/attic/apps/samples/WEB-INF/src/flex/samples/dcd/product/ProductService.java b/attic/apps/samples/WEB-INF/src/flex/samples/dcd/product/ProductService.java new file mode 100755 index 0000000..8288767 --- /dev/null +++ b/attic/apps/samples/WEB-INF/src/flex/samples/dcd/product/ProductService.java @@ -0,0 +1,197 @@ +/* + * 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 flex.samples.dcd.product; + +import java.util.ArrayList; +import java.util.List; +import java.sql.*; +import flex.samples.ConnectionHelper; +import flex.samples.DAOException; +import java.util.Iterator; + +public class ProductService { + + public Product[] getProducts() throws DAOException { + + List list = new ArrayList(); + Connection c = null; + + try { + c = ConnectionHelper.getConnection(); + Statement s = c.createStatement(); + ResultSet rs = s.executeQuery("SELECT * FROM product ORDER BY name"); + while (rs.next()) { + list.add(new Product(rs.getInt("product_id"), + rs.getString("name"), + rs.getString("description"), + rs.getString("image"), + rs.getString("category"), + rs.getDouble("price"), + rs.getInt("qty_in_stock"))); + } + } catch (SQLException e) { + e.printStackTrace(); + throw new DAOException(e); + } finally { + ConnectionHelper.close(c); + } + Product[] products = new Product[list.size()]; + Iterator i = list.iterator(); + int index = 0; + while(i.hasNext()) { + products[index] = (Product)i.next(); + index++; + } + return products; + } + + public List getProductsByName(String name) throws DAOException { + + List list = new ArrayList(); + Connection c = null; + + try { + c = ConnectionHelper.getConnection(); + PreparedStatement ps = c.prepareStatement("SELECT * FROM product WHERE UPPER(name) LIKE ? ORDER BY name"); + ps.setString(1, "%" + name.toUpperCase() + "%"); + ResultSet rs = ps.executeQuery(); + while (rs.next()) { + list.add(new Product(rs.getInt("product_id"), + rs.getString("name"), + rs.getString("description"), + rs.getString("image"), + rs.getString("category"), + rs.getDouble("price"), + rs.getInt("qty_in_stock"))); + } + } catch (SQLException e) { + e.printStackTrace(); + throw new DAOException(e); + } finally { + ConnectionHelper.close(c); + } + return list; + + } + + public Product getProduct(int productId) throws DAOException { + + Product product = new Product(); + Connection c = null; + + try { + c = ConnectionHelper.getConnection(); + PreparedStatement ps = c.prepareStatement("SELECT * FROM product WHERE product_id=?"); + ps.setInt(1, productId); + ResultSet rs = ps.executeQuery(); + if (rs.next()) { + product = new Product(); + product.setProductId(rs.getInt("product_id")); + product.setName(rs.getString("name")); + product.setDescription(rs.getString("description")); + product.setImage(rs.getString("image")); + product.setCategory(rs.getString("category")); + product.setPrice(rs.getDouble("price")); + product.setQtyInStock(rs.getInt("qty_in_stock")); + } + } catch (Exception e) { + e.printStackTrace(); + throw new DAOException(e); + } finally { + ConnectionHelper.close(c); + } + return product; + } + + public Product createProduct(Product product) throws DAOException { + + Connection c = null; + PreparedStatement ps = null; + try { + c = ConnectionHelper.getConnection(); + ps = c.prepareStatement("INSERT INTO product (name, description, image, category, price, qty_in_stock) VALUES (?, ?, ?, ?, ?, ?)"); + ps.setString(1, product.getName()); + ps.setString(2, product.getDescription()); + ps.setString(3, product.getImage()); + ps.setString(4, product.getCategory()); + ps.setDouble(5, product.getPrice()); + ps.setInt(6, product.getQtyInStock()); + ps.executeUpdate(); + Statement s = c.createStatement(); + // HSQLDB Syntax to get the identity (company_id) of inserted row + ResultSet rs = s.executeQuery("CALL IDENTITY()"); + // MySQL Syntax to get the identity (product_id) of inserted row + // ResultSet rs = s.executeQuery("SELECT LAST_INSERT_ID()"); + rs.next(); + // Update the id in the returned object. This is important as this value must get returned to the client. + product.setProductId(rs.getInt(1)); + } catch (Exception e) { + e.printStackTrace(); + throw new DAOException(e); + } finally { + ConnectionHelper.close(c); + } + return product; + } + + public void updateProduct(Product product) throws DAOException { + + Connection c = null; + + try { + c = ConnectionHelper.getConnection(); + PreparedStatement ps = c.prepareStatement("UPDATE product SET name=?, description=?, image=?, category=?, price=?, qty_in_stock=? WHERE product_id=?"); + ps.setString(1, product.getName()); + ps.setString(2, product.getDescription()); + ps.setString(3, product.getImage()); + ps.setString(4, product.getCategory()); + ps.setDouble(5, product.getPrice()); + ps.setInt(6, product.getQtyInStock()); + ps.setInt(7, product.getProductId()); + ps.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + throw new DAOException(e); + } finally { + ConnectionHelper.close(c); + } + + } + + private boolean remove(Product product) throws DAOException { + + Connection c = null; + + try { + c = ConnectionHelper.getConnection(); + PreparedStatement ps = c.prepareStatement("DELETE FROM product WHERE product_id=?"); + ps.setInt(1, product.getProductId()); + int count = ps.executeUpdate(); + return (count == 1); + } catch (Exception e) { + e.printStackTrace(); + throw new DAOException(e); + } finally { + ConnectionHelper.close(c); + } + } + + public boolean deleteProduct(Product product) throws DAOException { + return remove(product); + } + +} \ No newline at end of file