http://git-wip-us.apache.org/repos/asf/flex-blazeds/blob/012fad7c/attic/apps/samples/WEB-INF/src/flex/samples/crm/employee/EmployeeDAO.java ---------------------------------------------------------------------- diff --git a/attic/apps/samples/WEB-INF/src/flex/samples/crm/employee/EmployeeDAO.java b/attic/apps/samples/WEB-INF/src/flex/samples/crm/employee/EmployeeDAO.java index 273dee5..c165016 100755 --- a/attic/apps/samples/WEB-INF/src/flex/samples/crm/employee/EmployeeDAO.java +++ b/attic/apps/samples/WEB-INF/src/flex/samples/crm/employee/EmployeeDAO.java @@ -1,300 +1,300 @@ -/* - * 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.crm.employee; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.Statement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Types; -import java.util.ArrayList; -import java.util.List; - -import flex.samples.ConnectionHelper; -import flex.samples.DAOException; -import flex.samples.crm.ConcurrencyException; -import flex.samples.crm.company.Company; - -public class EmployeeDAO -{ - public List getEmployees() throws DAOException - { - List list = new ArrayList(); - Connection c = null; - try - { - c = ConnectionHelper.getConnection(); - Statement s = c.createStatement(); - ResultSet rs = s.executeQuery("SELECT * FROM employee ORDER BY last_name"); - Employee employee; - while (rs.next()) - { - employee = new Employee(); - employee.setEmployeeId(rs.getInt("employee_id")); - employee.setFirstName(rs.getString("first_name")); - employee.setLastName(rs.getString("last_name")); - employee.setTitle(rs.getString("title")); - employee.setEmail(rs.getString("email")); - employee.setPhone(rs.getString("phone")); - Company company = new Company(); - company.setCompanyId(rs.getInt("company_id")); - employee.setCompany(company); - list.add(employee); - } - } - catch (SQLException e) - { - e.printStackTrace(); - throw new DAOException(e); - } - finally - { - ConnectionHelper.close(c); - } - return list; - } - - public List findEmployeesByCompany(Integer companyId) throws DAOException - { - List list = new ArrayList(); - Connection c = null; - try - { - Company company = new Company(); - company.setCompanyId(companyId.intValue()); - c = ConnectionHelper.getConnection(); - PreparedStatement ps = c.prepareStatement("SELECT * FROM employee WHERE company_id = ? ORDER BY last_name"); - ps.setInt(1, companyId.intValue()); - ResultSet rs = ps.executeQuery(); - while (rs.next()) - { - Employee employee = new Employee(); - employee.setEmployeeId(rs.getInt("employee_id")); - employee.setFirstName(rs.getString("first_name")); - employee.setLastName(rs.getString("last_name")); - employee.setTitle(rs.getString("title")); - employee.setEmail(rs.getString("email")); - employee.setPhone(rs.getString("phone")); - employee.setCompany(company); - list.add(employee); - } - } - catch (SQLException e) - { - e.printStackTrace(); - throw new DAOException(e); - } - finally - { - ConnectionHelper.close(c); - } - return list; - } - - public List findEmployeesByName(String name) throws DAOException - { - List list = new ArrayList(); - Connection c = null; - - try - { - c = ConnectionHelper.getConnection(); - PreparedStatement ps = c.prepareStatement("SELECT * FROM employee WHERE first_name LIKE ? OR last_name LIKE ? ORDER BY last_name"); - ps.setString(1, "%" + name + "%"); - ps.setString(2, "%" + name + "%"); - ResultSet rs = ps.executeQuery(); - - Employee employee; - while (rs.next()) - { - employee = new Employee(); - employee.setEmployeeId(rs.getInt("employee_id")); - employee.setFirstName(rs.getString("first_name")); - employee.setLastName(rs.getString("last_name")); - employee.setTitle(rs.getString("title")); - employee.setEmail(rs.getString("email")); - employee.setPhone(rs.getString("phone")); - Company company = new Company(); - company.setCompanyId(rs.getInt("company_id")); - - list.add(employee); - } - } - catch (SQLException e) - { - e.printStackTrace(); - throw new DAOException(e); - } - finally - { - ConnectionHelper.close(c); - } - return list; - } - - public Employee getEmployee(int employeeId) throws DAOException - { - Employee employee = null; - Connection c = null; - - try - { - c = ConnectionHelper.getConnection(); - PreparedStatement ps = c.prepareStatement("SELECT * FROM employee WHERE employee_id= ?"); - ps.setInt(1, employeeId); - ResultSet rs = ps.executeQuery(); - - if (rs.next()) - { - employee = new Employee(); - employee.setEmployeeId(rs.getInt("employee_id")); - employee.setFirstName(rs.getString("first_name")); - employee.setLastName(rs.getString("last_name")); - employee.setTitle(rs.getString("title")); - employee.setEmail(rs.getString("email")); - employee.setPhone(rs.getString("phone")); - } - } - catch (SQLException e) - { - e.printStackTrace(); - throw new DAOException(e.getMessage()); - } - finally - { - ConnectionHelper.close(c); - } - return employee; - } - - public Employee createEmployee(Employee employee) throws DAOException - { - Connection c = null; - PreparedStatement ps = null; - try - { - c = ConnectionHelper.getConnection(); - ps = c.prepareStatement("INSERT INTO employee (first_name, last_name, title, email, phone, company_id) VALUES (?, ?, ?, ?, ?, ?)"); - ps.setString(1, employee.getFirstName()); - ps.setString(2, employee.getLastName()); - ps.setString(3, employee.getTitle()); - ps.setString(4, employee.getEmail()); - ps.setString(5, employee.getPhone()); - if (employee.getCompany() != null) - ps.setInt(6, employee.getCompany().getCompanyId()); - else - ps.setNull(6, Types.INTEGER); - ps.execute(); - ps.close(); - Statement s = c.createStatement(); - // HSQLDB Syntax to get the identity (employee_id) of inserted row - ResultSet rs = s.executeQuery("CALL IDENTITY()"); - rs.next(); - // Update the id in the returned object. This is important as this - // value must get returned to the client. - employee.setEmployeeId(rs.getInt(1)); - } - catch (SQLException e) - { - e.printStackTrace(); - throw new DAOException(e); - } - finally - { - ConnectionHelper.close(c); - } - return employee; - } - - - public void updateEmployee(Employee newVersion, Employee previousVersion, List changes) throws DAOException, ConcurrencyException - { - Connection c = null; - try - { - c = ConnectionHelper.getConnection(); - PreparedStatement ps = c.prepareStatement("UPDATE employee SET first_name=?, last_name=?, title=?, email=?, phone=?, company_id=? WHERE employee_id=? AND first_name=? AND last_name=? AND title=? AND email=? AND phone=? AND company_id=?"); - ps.setString(1, newVersion.getFirstName()); - ps.setString(2, newVersion.getLastName()); - ps.setString(3, newVersion.getTitle()); - ps.setString(4, newVersion.getEmail()); - ps.setString(5, newVersion.getPhone()); - if (newVersion.getCompany() != null) - ps.setInt(6, newVersion.getCompany().getCompanyId()); - else - ps.setNull(6,Types.INTEGER); - ps.setInt(7, newVersion.getEmployeeId()); - ps.setString(8, previousVersion.getFirstName()); - ps.setString(9, previousVersion.getLastName()); - ps.setString(10, previousVersion.getTitle()); - ps.setString(11, previousVersion.getEmail()); - ps.setString(12, previousVersion.getPhone()); - if (previousVersion.getCompany() != null) - ps.setInt(13, previousVersion.getCompany().getCompanyId()); - else - ps.setNull(13, Types.INTEGER); - if (ps.executeUpdate() == 0) - { - throw new ConcurrencyException("Item not found"); - } - } - catch (SQLException e) - { - e.printStackTrace(); - throw new DAOException(e.getMessage()); - } - finally - { - ConnectionHelper.close(c); - } - } - - public void deleteEmployee(Employee employee) throws DAOException, ConcurrencyException - { - Connection c = null; - try - { - c = ConnectionHelper.getConnection(); - PreparedStatement ps = c.prepareStatement("DELETE FROM employee WHERE employee_id=? AND first_name=? AND last_name=? AND title=? AND email=? AND phone=? AND company_id=?"); - ps.setInt(1, employee.getEmployeeId()); - ps.setString(2, employee.getFirstName()); - ps.setString(3, employee.getLastName()); - ps.setString(4, employee.getTitle()); - ps.setString(5, employee.getEmail()); - ps.setString(6, employee.getPhone()); - if (employee.getCompany() != null) - ps.setInt(7, employee.getCompany().getCompanyId()); - else - ps.setNull(7, Types.INTEGER); - if (ps.executeUpdate() == 0) - { - throw new ConcurrencyException("Item not found"); - } - } - catch (SQLException e) - { - e.printStackTrace(); - throw new DAOException(e.getMessage()); - } - finally - { - ConnectionHelper.close(c); - } - } - -} +/* + * 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.crm.employee; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.Statement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Types; +import java.util.ArrayList; +import java.util.List; + +import flex.samples.ConnectionHelper; +import flex.samples.DAOException; +import flex.samples.crm.ConcurrencyException; +import flex.samples.crm.company.Company; + +public class EmployeeDAO +{ + public List getEmployees() throws DAOException + { + List list = new ArrayList(); + Connection c = null; + try + { + c = ConnectionHelper.getConnection(); + Statement s = c.createStatement(); + ResultSet rs = s.executeQuery("SELECT * FROM employee ORDER BY last_name"); + Employee employee; + while (rs.next()) + { + employee = new Employee(); + employee.setEmployeeId(rs.getInt("employee_id")); + employee.setFirstName(rs.getString("first_name")); + employee.setLastName(rs.getString("last_name")); + employee.setTitle(rs.getString("title")); + employee.setEmail(rs.getString("email")); + employee.setPhone(rs.getString("phone")); + Company company = new Company(); + company.setCompanyId(rs.getInt("company_id")); + employee.setCompany(company); + list.add(employee); + } + } + catch (SQLException e) + { + e.printStackTrace(); + throw new DAOException(e); + } + finally + { + ConnectionHelper.close(c); + } + return list; + } + + public List findEmployeesByCompany(Integer companyId) throws DAOException + { + List list = new ArrayList(); + Connection c = null; + try + { + Company company = new Company(); + company.setCompanyId(companyId.intValue()); + c = ConnectionHelper.getConnection(); + PreparedStatement ps = c.prepareStatement("SELECT * FROM employee WHERE company_id = ? ORDER BY last_name"); + ps.setInt(1, companyId.intValue()); + ResultSet rs = ps.executeQuery(); + while (rs.next()) + { + Employee employee = new Employee(); + employee.setEmployeeId(rs.getInt("employee_id")); + employee.setFirstName(rs.getString("first_name")); + employee.setLastName(rs.getString("last_name")); + employee.setTitle(rs.getString("title")); + employee.setEmail(rs.getString("email")); + employee.setPhone(rs.getString("phone")); + employee.setCompany(company); + list.add(employee); + } + } + catch (SQLException e) + { + e.printStackTrace(); + throw new DAOException(e); + } + finally + { + ConnectionHelper.close(c); + } + return list; + } + + public List findEmployeesByName(String name) throws DAOException + { + List list = new ArrayList(); + Connection c = null; + + try + { + c = ConnectionHelper.getConnection(); + PreparedStatement ps = c.prepareStatement("SELECT * FROM employee WHERE first_name LIKE ? OR last_name LIKE ? ORDER BY last_name"); + ps.setString(1, "%" + name + "%"); + ps.setString(2, "%" + name + "%"); + ResultSet rs = ps.executeQuery(); + + Employee employee; + while (rs.next()) + { + employee = new Employee(); + employee.setEmployeeId(rs.getInt("employee_id")); + employee.setFirstName(rs.getString("first_name")); + employee.setLastName(rs.getString("last_name")); + employee.setTitle(rs.getString("title")); + employee.setEmail(rs.getString("email")); + employee.setPhone(rs.getString("phone")); + Company company = new Company(); + company.setCompanyId(rs.getInt("company_id")); + + list.add(employee); + } + } + catch (SQLException e) + { + e.printStackTrace(); + throw new DAOException(e); + } + finally + { + ConnectionHelper.close(c); + } + return list; + } + + public Employee getEmployee(int employeeId) throws DAOException + { + Employee employee = null; + Connection c = null; + + try + { + c = ConnectionHelper.getConnection(); + PreparedStatement ps = c.prepareStatement("SELECT * FROM employee WHERE employee_id= ?"); + ps.setInt(1, employeeId); + ResultSet rs = ps.executeQuery(); + + if (rs.next()) + { + employee = new Employee(); + employee.setEmployeeId(rs.getInt("employee_id")); + employee.setFirstName(rs.getString("first_name")); + employee.setLastName(rs.getString("last_name")); + employee.setTitle(rs.getString("title")); + employee.setEmail(rs.getString("email")); + employee.setPhone(rs.getString("phone")); + } + } + catch (SQLException e) + { + e.printStackTrace(); + throw new DAOException(e.getMessage()); + } + finally + { + ConnectionHelper.close(c); + } + return employee; + } + + public Employee createEmployee(Employee employee) throws DAOException + { + Connection c = null; + PreparedStatement ps = null; + try + { + c = ConnectionHelper.getConnection(); + ps = c.prepareStatement("INSERT INTO employee (first_name, last_name, title, email, phone, company_id) VALUES (?, ?, ?, ?, ?, ?)"); + ps.setString(1, employee.getFirstName()); + ps.setString(2, employee.getLastName()); + ps.setString(3, employee.getTitle()); + ps.setString(4, employee.getEmail()); + ps.setString(5, employee.getPhone()); + if (employee.getCompany() != null) + ps.setInt(6, employee.getCompany().getCompanyId()); + else + ps.setNull(6, Types.INTEGER); + ps.execute(); + ps.close(); + Statement s = c.createStatement(); + // HSQLDB Syntax to get the identity (employee_id) of inserted row + ResultSet rs = s.executeQuery("CALL IDENTITY()"); + rs.next(); + // Update the id in the returned object. This is important as this + // value must get returned to the client. + employee.setEmployeeId(rs.getInt(1)); + } + catch (SQLException e) + { + e.printStackTrace(); + throw new DAOException(e); + } + finally + { + ConnectionHelper.close(c); + } + return employee; + } + + + public void updateEmployee(Employee newVersion, Employee previousVersion, List changes) throws DAOException, ConcurrencyException + { + Connection c = null; + try + { + c = ConnectionHelper.getConnection(); + PreparedStatement ps = c.prepareStatement("UPDATE employee SET first_name=?, last_name=?, title=?, email=?, phone=?, company_id=? WHERE employee_id=? AND first_name=? AND last_name=? AND title=? AND email=? AND phone=? AND company_id=?"); + ps.setString(1, newVersion.getFirstName()); + ps.setString(2, newVersion.getLastName()); + ps.setString(3, newVersion.getTitle()); + ps.setString(4, newVersion.getEmail()); + ps.setString(5, newVersion.getPhone()); + if (newVersion.getCompany() != null) + ps.setInt(6, newVersion.getCompany().getCompanyId()); + else + ps.setNull(6,Types.INTEGER); + ps.setInt(7, newVersion.getEmployeeId()); + ps.setString(8, previousVersion.getFirstName()); + ps.setString(9, previousVersion.getLastName()); + ps.setString(10, previousVersion.getTitle()); + ps.setString(11, previousVersion.getEmail()); + ps.setString(12, previousVersion.getPhone()); + if (previousVersion.getCompany() != null) + ps.setInt(13, previousVersion.getCompany().getCompanyId()); + else + ps.setNull(13, Types.INTEGER); + if (ps.executeUpdate() == 0) + { + throw new ConcurrencyException("Item not found"); + } + } + catch (SQLException e) + { + e.printStackTrace(); + throw new DAOException(e.getMessage()); + } + finally + { + ConnectionHelper.close(c); + } + } + + public void deleteEmployee(Employee employee) throws DAOException, ConcurrencyException + { + Connection c = null; + try + { + c = ConnectionHelper.getConnection(); + PreparedStatement ps = c.prepareStatement("DELETE FROM employee WHERE employee_id=? AND first_name=? AND last_name=? AND title=? AND email=? AND phone=? AND company_id=?"); + ps.setInt(1, employee.getEmployeeId()); + ps.setString(2, employee.getFirstName()); + ps.setString(3, employee.getLastName()); + ps.setString(4, employee.getTitle()); + ps.setString(5, employee.getEmail()); + ps.setString(6, employee.getPhone()); + if (employee.getCompany() != null) + ps.setInt(7, employee.getCompany().getCompanyId()); + else + ps.setNull(7, Types.INTEGER); + if (ps.executeUpdate() == 0) + { + throw new ConcurrencyException("Item not found"); + } + } + catch (SQLException e) + { + e.printStackTrace(); + throw new DAOException(e.getMessage()); + } + finally + { + ConnectionHelper.close(c); + } + } + +}
http://git-wip-us.apache.org/repos/asf/flex-blazeds/blob/012fad7c/attic/apps/samples/WEB-INF/src/flex/samples/dcd/product/Product.java ---------------------------------------------------------------------- diff --git a/attic/apps/samples/WEB-INF/src/flex/samples/dcd/product/Product.java b/attic/apps/samples/WEB-INF/src/flex/samples/dcd/product/Product.java index be6d992..d46544e 100755 --- a/attic/apps/samples/WEB-INF/src/flex/samples/dcd/product/Product.java +++ b/attic/apps/samples/WEB-INF/src/flex/samples/dcd/product/Product.java @@ -1,89 +1,89 @@ -/* - * 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.io.Serializable; - -public class Product implements Serializable { - - static final long serialVersionUID = 103844514947365244L; - - private int productId; - private String name; - private String description; - private String image; - private String category; - private double price; - private int qtyInStock; - - public Product() { - - } - - public Product(int productId, String name, String description, String image, String category, double price, int qtyInStock) { - this.productId = productId; - this.name = name; - this.description = description; - this.image = image; - this.category = category; - this.price = price; - this.qtyInStock = qtyInStock; - } - - public String getCategory() { - return category; - } - public void setCategory(String category) { - this.category = category; - } - public String getDescription() { - return description; - } - public void setDescription(String description) { - this.description = description; - } - public String getImage() { - return image; - } - public void setImage(String image) { - this.image = image; - } - public String getName() { - return name; - } - public void setName(String name) { - this.name = name; - } - public double getPrice() { - return price; - } - public void setPrice(double price) { - this.price = price; - } - public int getProductId() { - return productId; - } - public void setProductId(int productId) { - this.productId = productId; - } - public int getQtyInStock() { - return qtyInStock; - } - public void setQtyInStock(int qtyInStock) { - this.qtyInStock = qtyInStock; - } - +/* + * 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.io.Serializable; + +public class Product implements Serializable { + + static final long serialVersionUID = 103844514947365244L; + + private int productId; + private String name; + private String description; + private String image; + private String category; + private double price; + private int qtyInStock; + + public Product() { + + } + + public Product(int productId, String name, String description, String image, String category, double price, int qtyInStock) { + this.productId = productId; + this.name = name; + this.description = description; + this.image = image; + this.category = category; + this.price = price; + this.qtyInStock = qtyInStock; + } + + public String getCategory() { + return category; + } + public void setCategory(String category) { + this.category = category; + } + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } + public String getImage() { + return image; + } + public void setImage(String image) { + this.image = image; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public double getPrice() { + return price; + } + public void setPrice(double price) { + this.price = price; + } + public int getProductId() { + return productId; + } + public void setProductId(int productId) { + this.productId = productId; + } + public int getQtyInStock() { + return qtyInStock; + } + public void setQtyInStock(int qtyInStock) { + this.qtyInStock = qtyInStock; + } + } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/flex-blazeds/blob/012fad7c/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 index 8288767..dcf1ba9 100755 --- 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 @@ -1,197 +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); - } - +/* + * 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 http://git-wip-us.apache.org/repos/asf/flex-blazeds/blob/012fad7c/attic/apps/samples/WEB-INF/src/flex/samples/feed/Feed.java ---------------------------------------------------------------------- diff --git a/attic/apps/samples/WEB-INF/src/flex/samples/feed/Feed.java b/attic/apps/samples/WEB-INF/src/flex/samples/feed/Feed.java index caf5fc2..5f31a28 100755 --- a/attic/apps/samples/WEB-INF/src/flex/samples/feed/Feed.java +++ b/attic/apps/samples/WEB-INF/src/flex/samples/feed/Feed.java @@ -1,85 +1,85 @@ -/* - * 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.feed; - -import java.util.*; -import flex.messaging.MessageBroker; -import flex.messaging.messages.AsyncMessage; -import flex.messaging.util.UUIDUtils; - -public class Feed { - private static FeedThread thread; - - public Feed() { - } - - public void start() { - if (thread == null) { - thread = new FeedThread(); - thread.start(); - } - } - - public void stop() { - thread.running = false; - thread = null; - } - - public static class FeedThread extends Thread { - - public boolean running = true; - - public void run() { - MessageBroker msgBroker = MessageBroker.getMessageBroker(null); - String clientID = UUIDUtils.createUUID(); - - Random random = new Random(); - double initialValue = 35; - double currentValue = 35; - double maxChange = initialValue * 0.005; - - while (running) { - double change = maxChange - random.nextDouble() * maxChange * 2; - double newValue = currentValue + change; - - if (currentValue < initialValue + initialValue * 0.15 - && currentValue > initialValue - initialValue * 0.15) { - currentValue = newValue; - } else { - currentValue -= change; - } - - AsyncMessage msg = new AsyncMessage(); - msg.setDestination("feed"); - msg.setClientId(clientID); - msg.setMessageId(UUIDUtils.createUUID()); - msg.setTimestamp(System.currentTimeMillis()); - msg.setBody(new Double(currentValue)); - msgBroker.routeMessageToService(msg, null); - - System.out.println("" + currentValue); - - try { - Thread.sleep(300); - } catch (InterruptedException e) { - } - - } - } - } - +/* + * 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.feed; + +import java.util.*; +import flex.messaging.MessageBroker; +import flex.messaging.messages.AsyncMessage; +import flex.messaging.util.UUIDUtils; + +public class Feed { + private static FeedThread thread; + + public Feed() { + } + + public void start() { + if (thread == null) { + thread = new FeedThread(); + thread.start(); + } + } + + public void stop() { + thread.running = false; + thread = null; + } + + public static class FeedThread extends Thread { + + public boolean running = true; + + public void run() { + MessageBroker msgBroker = MessageBroker.getMessageBroker(null); + String clientID = UUIDUtils.createUUID(); + + Random random = new Random(); + double initialValue = 35; + double currentValue = 35; + double maxChange = initialValue * 0.005; + + while (running) { + double change = maxChange - random.nextDouble() * maxChange * 2; + double newValue = currentValue + change; + + if (currentValue < initialValue + initialValue * 0.15 + && currentValue > initialValue - initialValue * 0.15) { + currentValue = newValue; + } else { + currentValue -= change; + } + + AsyncMessage msg = new AsyncMessage(); + msg.setDestination("feed"); + msg.setClientId(clientID); + msg.setMessageId(UUIDUtils.createUUID()); + msg.setTimestamp(System.currentTimeMillis()); + msg.setBody(new Double(currentValue)); + msgBroker.routeMessageToService(msg, null); + + System.out.println("" + currentValue); + + try { + Thread.sleep(300); + } catch (InterruptedException e) { + } + + } + } + } + } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/flex-blazeds/blob/012fad7c/attic/apps/samples/WEB-INF/src/flex/samples/marketdata/Feed.java ---------------------------------------------------------------------- diff --git a/attic/apps/samples/WEB-INF/src/flex/samples/marketdata/Feed.java b/attic/apps/samples/WEB-INF/src/flex/samples/marketdata/Feed.java index 65cf6d0..19e7247 100755 --- a/attic/apps/samples/WEB-INF/src/flex/samples/marketdata/Feed.java +++ b/attic/apps/samples/WEB-INF/src/flex/samples/marketdata/Feed.java @@ -1,121 +1,121 @@ -/* - * 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.marketdata; - -import java.util.*; - -import flex.messaging.MessageBroker; -import flex.messaging.messages.AsyncMessage; -import flex.messaging.util.UUIDUtils; - -public class Feed { - - public static void main(String args[]) { - Feed feed = new Feed(); - feed.start(); - } - - private static FeedThread thread; - - public Feed() { - } - - public void start() { - if (thread == null) { - thread = new FeedThread(); - thread.start(); - } - } - - public void stop() { - thread.running = false; - thread = null; - } - - public static class FeedThread extends Thread { - - public boolean running = true; - - private Random random; - - public void run() { - - MessageBroker msgBroker = MessageBroker.getMessageBroker(null); - String clientID = UUIDUtils.createUUID(); - - Portfolio portfolio = new Portfolio(); - List stocks = portfolio.getStocks(); - int size = stocks.size(); - int index = 0; - - random = new Random(); - - Stock stock; - - while (running) { - - stock = (Stock) stocks.get(index); - simulateChange(stock); - - index++; - if (index >= size) { - index = 0; - } - - AsyncMessage msg = new AsyncMessage(); - msg.setDestination("market-data-feed"); - msg.setHeader("DSSubtopic", stock.getSymbol()); - msg.setClientId(clientID); - msg.setMessageId(UUIDUtils.createUUID()); - msg.setTimestamp(System.currentTimeMillis()); - msg.setBody(stock); - msgBroker.routeMessageToService(msg, null); - - try { - Thread.sleep(20); - } catch (InterruptedException e) { - } - - } - } - - private void simulateChange(Stock stock) { - - double maxChange = stock.open * 0.005; - double change = maxChange - random.nextDouble() * maxChange * 2; - stock.change = change; - double last = stock.last + change; - - if (last < stock.open + stock.open * 0.15 - && last > stock.open - stock.open * 0.15) { - stock.last = last; - } else { - stock.last = stock.last - change; - } - - if (stock.last > stock.high) { - stock.high = stock.last; - } else if (stock.last < stock.low) { - stock.low = stock.last; - } - stock.date = new Date(); - - } - - } - -} +/* + * 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.marketdata; + +import java.util.*; + +import flex.messaging.MessageBroker; +import flex.messaging.messages.AsyncMessage; +import flex.messaging.util.UUIDUtils; + +public class Feed { + + public static void main(String args[]) { + Feed feed = new Feed(); + feed.start(); + } + + private static FeedThread thread; + + public Feed() { + } + + public void start() { + if (thread == null) { + thread = new FeedThread(); + thread.start(); + } + } + + public void stop() { + thread.running = false; + thread = null; + } + + public static class FeedThread extends Thread { + + public boolean running = true; + + private Random random; + + public void run() { + + MessageBroker msgBroker = MessageBroker.getMessageBroker(null); + String clientID = UUIDUtils.createUUID(); + + Portfolio portfolio = new Portfolio(); + List stocks = portfolio.getStocks(); + int size = stocks.size(); + int index = 0; + + random = new Random(); + + Stock stock; + + while (running) { + + stock = (Stock) stocks.get(index); + simulateChange(stock); + + index++; + if (index >= size) { + index = 0; + } + + AsyncMessage msg = new AsyncMessage(); + msg.setDestination("market-data-feed"); + msg.setHeader("DSSubtopic", stock.getSymbol()); + msg.setClientId(clientID); + msg.setMessageId(UUIDUtils.createUUID()); + msg.setTimestamp(System.currentTimeMillis()); + msg.setBody(stock); + msgBroker.routeMessageToService(msg, null); + + try { + Thread.sleep(20); + } catch (InterruptedException e) { + } + + } + } + + private void simulateChange(Stock stock) { + + double maxChange = stock.open * 0.005; + double change = maxChange - random.nextDouble() * maxChange * 2; + stock.change = change; + double last = stock.last + change; + + if (last < stock.open + stock.open * 0.15 + && last > stock.open - stock.open * 0.15) { + stock.last = last; + } else { + stock.last = stock.last - change; + } + + if (stock.last > stock.high) { + stock.high = stock.last; + } else if (stock.last < stock.low) { + stock.low = stock.last; + } + stock.date = new Date(); + + } + + } + +} http://git-wip-us.apache.org/repos/asf/flex-blazeds/blob/012fad7c/attic/apps/samples/WEB-INF/src/flex/samples/marketdata/Portfolio.java ---------------------------------------------------------------------- diff --git a/attic/apps/samples/WEB-INF/src/flex/samples/marketdata/Portfolio.java b/attic/apps/samples/WEB-INF/src/flex/samples/marketdata/Portfolio.java index 134c5aa..250dc10 100755 --- a/attic/apps/samples/WEB-INF/src/flex/samples/marketdata/Portfolio.java +++ b/attic/apps/samples/WEB-INF/src/flex/samples/marketdata/Portfolio.java @@ -1,75 +1,75 @@ -/* - * 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.marketdata; - -import java.io.*; -import java.net.URLDecoder; - -import javax.xml.parsers.*; -import org.w3c.dom.*; -import java.util.List; -import java.util.ArrayList; - -public class Portfolio { - - public static void main(String[] args) { - Portfolio stockFeed = new Portfolio(); - stockFeed.getStocks(); - } - - public List getStocks() { - - List list = new ArrayList(); - - try { - InputStream is = getClass().getClassLoader().getResourceAsStream("flex/samples/marketdata/portfolio.xml"); - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setValidating(false); - Document doc = factory.newDocumentBuilder().parse(is); - NodeList stockNodes = doc.getElementsByTagName("stock"); - int length = stockNodes.getLength(); - Stock stock; - Node stockNode; - for (int i=0; i<length; i++) { - stockNode = stockNodes.item(i); - stock = new Stock(); - stock.setSymbol( getStringValue(stockNode, "symbol") ); - stock.setName( getStringValue(stockNode, "company") ); - stock.setLast( getDoubleValue(stockNode, "last") ); - stock.setHigh( stock.getLast() ); - stock.setLow( stock.getLast() ); - stock.setOpen( stock.getLast() ); - stock.setChange( 0 ); - list.add(stock); - System.out.println(stock.getSymbol()); - } - } catch (Exception e) { - e.printStackTrace(); - } - - return list; - } - - private String getStringValue(Node node, String name) { - return ((Element) node).getElementsByTagName(name).item(0).getFirstChild().getNodeValue(); - } - - private double getDoubleValue(Node node, String name) { - return Double.parseDouble( getStringValue(node, name) ); - } - +/* + * 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.marketdata; + +import java.io.*; +import java.net.URLDecoder; + +import javax.xml.parsers.*; +import org.w3c.dom.*; +import java.util.List; +import java.util.ArrayList; + +public class Portfolio { + + public static void main(String[] args) { + Portfolio stockFeed = new Portfolio(); + stockFeed.getStocks(); + } + + public List getStocks() { + + List list = new ArrayList(); + + try { + InputStream is = getClass().getClassLoader().getResourceAsStream("flex/samples/marketdata/portfolio.xml"); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setValidating(false); + Document doc = factory.newDocumentBuilder().parse(is); + NodeList stockNodes = doc.getElementsByTagName("stock"); + int length = stockNodes.getLength(); + Stock stock; + Node stockNode; + for (int i=0; i<length; i++) { + stockNode = stockNodes.item(i); + stock = new Stock(); + stock.setSymbol( getStringValue(stockNode, "symbol") ); + stock.setName( getStringValue(stockNode, "company") ); + stock.setLast( getDoubleValue(stockNode, "last") ); + stock.setHigh( stock.getLast() ); + stock.setLow( stock.getLast() ); + stock.setOpen( stock.getLast() ); + stock.setChange( 0 ); + list.add(stock); + System.out.println(stock.getSymbol()); + } + } catch (Exception e) { + e.printStackTrace(); + } + + return list; + } + + private String getStringValue(Node node, String name) { + return ((Element) node).getElementsByTagName(name).item(0).getFirstChild().getNodeValue(); + } + + private double getDoubleValue(Node node, String name) { + return Double.parseDouble( getStringValue(node, name) ); + } + } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/flex-blazeds/blob/012fad7c/attic/apps/samples/WEB-INF/src/flex/samples/marketdata/Stock.java ---------------------------------------------------------------------- diff --git a/attic/apps/samples/WEB-INF/src/flex/samples/marketdata/Stock.java b/attic/apps/samples/WEB-INF/src/flex/samples/marketdata/Stock.java index ad271ec..6e4b9ee 100755 --- a/attic/apps/samples/WEB-INF/src/flex/samples/marketdata/Stock.java +++ b/attic/apps/samples/WEB-INF/src/flex/samples/marketdata/Stock.java @@ -1,84 +1,84 @@ -/* - * 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.marketdata; - -import java.util.Date; -import java.io.Serializable; - -public class Stock implements Serializable { - - private static final long serialVersionUID = -8334804402463267285L; - - protected String symbol; - protected String name; - protected double low; - protected double high; - protected double open; - protected double last; - protected double change; - protected Date date; - - public double getChange() { - return change; - } - public void setChange(double change) { - this.change = change; - } - public double getHigh() { - return high; - } - public void setHigh(double high) { - this.high = high; - } - public double getLast() { - return last; - } - public void setLast(double last) { - this.last = last; - } - public double getLow() { - return low; - } - public void setLow(double low) { - this.low = low; - } - public String getName() { - return name; - } - public void setName(String name) { - this.name = name; - } - public double getOpen() { - return open; - } - public void setOpen(double open) { - this.open = open; - } - public String getSymbol() { - return symbol; - } - public void setSymbol(String symbol) { - this.symbol = symbol; - } - public Date getDate() { - return date; - } - public void setDate(Date date) { - this.date = date; - } - -} +/* + * 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.marketdata; + +import java.util.Date; +import java.io.Serializable; + +public class Stock implements Serializable { + + private static final long serialVersionUID = -8334804402463267285L; + + protected String symbol; + protected String name; + protected double low; + protected double high; + protected double open; + protected double last; + protected double change; + protected Date date; + + public double getChange() { + return change; + } + public void setChange(double change) { + this.change = change; + } + public double getHigh() { + return high; + } + public void setHigh(double high) { + this.high = high; + } + public double getLast() { + return last; + } + public void setLast(double last) { + this.last = last; + } + public double getLow() { + return low; + } + public void setLow(double low) { + this.low = low; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public double getOpen() { + return open; + } + public void setOpen(double open) { + this.open = open; + } + public String getSymbol() { + return symbol; + } + public void setSymbol(String symbol) { + this.symbol = symbol; + } + public Date getDate() { + return date; + } + public void setDate(Date date) { + this.date = date; + } + +} http://git-wip-us.apache.org/repos/asf/flex-blazeds/blob/012fad7c/attic/apps/samples/WEB-INF/src/flex/samples/marketdata/portfolio.xml ---------------------------------------------------------------------- diff --git a/attic/apps/samples/WEB-INF/src/flex/samples/marketdata/portfolio.xml b/attic/apps/samples/WEB-INF/src/flex/samples/marketdata/portfolio.xml index a5efecc..b464290 100755 --- a/attic/apps/samples/WEB-INF/src/flex/samples/marketdata/portfolio.xml +++ b/attic/apps/samples/WEB-INF/src/flex/samples/marketdata/portfolio.xml @@ -16,126 +16,126 @@ See the License for the specific language governing permissions and limitations under the License. --> -<portfolio> - - <stock> - <symbol>XOM</symbol> - <company>Exxon Mobile Corp</company> - <last>61.56</last> - </stock> - - <stock> - <symbol>WMT</symbol> - <company>Wal-Mart Stores</company> - <last>45.62</last> - </stock> - - <stock> - <symbol>GM</symbol> - <company>General Motors Corporation</company> - <last>19.8</last> - </stock> - - <stock> - <symbol>CVX</symbol> - <company>Chevron Corp New</company> - <last>58.95</last> - </stock> - - <stock> - <symbol>COP</symbol> - <company>Conocophillips</company> - <last>67.14</last> - </stock> - - <stock> - <symbol>GE</symbol> - <company>General Electric Company</company> - <last>33.61</last> - </stock> - - <stock> - <symbol>C</symbol> - <company>Citigroup Inc</company> - <last>48.18</last> - </stock> - - <stock> - <symbol>AIG</symbol> - <company>American International Group Inc</company> - <last>62.92</last> - </stock> - - <stock> - <symbol>GOOG</symbol> - <company>Google Inc</company> - <last>417.93</last> - </stock> - - <stock> - <symbol>ADBE</symbol> - <company>Adobe Systems Incorporated</company> - <last>39.20</last> - </stock> - - <stock> - <symbol>JBLU</symbol> - <company>JetBlue Airways Corporation</company> - <last>10.57</last> - </stock> - - <stock> - <symbol>COKE</symbol> - <company>Coca-Cola Bottling Co. Consolidated</company> - <last>48.20</last> - </stock> - - <stock> - <symbol>GENZ</symbol> - <company>Genzyme Corporation</company> - <last>61.16</last> - </stock> - - <stock> - <symbol>YHOO</symbol> - <company>Yahoo Inc.</company> - <last>32.78</last> - </stock> - - <stock> - <symbol>IBM</symbol> - <company>International Business Machines Corp.</company> - <last>82.34</last> - </stock> - - <stock> - <symbol>BA</symbol> - <company>Boeing Company</company> - <last>83.45</last> - </stock> - - <stock> - <symbol>SAP</symbol> - <company>SAP AG</company> - <last>54.63</last> - </stock> - - <stock> - <symbol>MOT</symbol> - <company>Motorola, Inc.</company> - <last>21.35</last> - </stock> - - <stock> - <symbol>VZ</symbol> - <company>Verizon Communications</company> - <last>33.03</last> - </stock> - - <stock> - <symbol>MCD</symbol> - <company>McDonald's Corporation</company> - <last>34.57</last> - </stock> - +<portfolio> + + <stock> + <symbol>XOM</symbol> + <company>Exxon Mobile Corp</company> + <last>61.56</last> + </stock> + + <stock> + <symbol>WMT</symbol> + <company>Wal-Mart Stores</company> + <last>45.62</last> + </stock> + + <stock> + <symbol>GM</symbol> + <company>General Motors Corporation</company> + <last>19.8</last> + </stock> + + <stock> + <symbol>CVX</symbol> + <company>Chevron Corp New</company> + <last>58.95</last> + </stock> + + <stock> + <symbol>COP</symbol> + <company>Conocophillips</company> + <last>67.14</last> + </stock> + + <stock> + <symbol>GE</symbol> + <company>General Electric Company</company> + <last>33.61</last> + </stock> + + <stock> + <symbol>C</symbol> + <company>Citigroup Inc</company> + <last>48.18</last> + </stock> + + <stock> + <symbol>AIG</symbol> + <company>American International Group Inc</company> + <last>62.92</last> + </stock> + + <stock> + <symbol>GOOG</symbol> + <company>Google Inc</company> + <last>417.93</last> + </stock> + + <stock> + <symbol>ADBE</symbol> + <company>Adobe Systems Incorporated</company> + <last>39.20</last> + </stock> + + <stock> + <symbol>JBLU</symbol> + <company>JetBlue Airways Corporation</company> + <last>10.57</last> + </stock> + + <stock> + <symbol>COKE</symbol> + <company>Coca-Cola Bottling Co. Consolidated</company> + <last>48.20</last> + </stock> + + <stock> + <symbol>GENZ</symbol> + <company>Genzyme Corporation</company> + <last>61.16</last> + </stock> + + <stock> + <symbol>YHOO</symbol> + <company>Yahoo Inc.</company> + <last>32.78</last> + </stock> + + <stock> + <symbol>IBM</symbol> + <company>International Business Machines Corp.</company> + <last>82.34</last> + </stock> + + <stock> + <symbol>BA</symbol> + <company>Boeing Company</company> + <last>83.45</last> + </stock> + + <stock> + <symbol>SAP</symbol> + <company>SAP AG</company> + <last>54.63</last> + </stock> + + <stock> + <symbol>MOT</symbol> + <company>Motorola, Inc.</company> + <last>21.35</last> + </stock> + + <stock> + <symbol>VZ</symbol> + <company>Verizon Communications</company> + <last>33.03</last> + </stock> + + <stock> + <symbol>MCD</symbol> + <company>McDonald's Corporation</company> + <last>34.57</last> + </stock> + </portfolio> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/flex-blazeds/blob/012fad7c/attic/apps/samples/WEB-INF/src/flex/samples/product/Product.java ---------------------------------------------------------------------- diff --git a/attic/apps/samples/WEB-INF/src/flex/samples/product/Product.java b/attic/apps/samples/WEB-INF/src/flex/samples/product/Product.java index 28b70d3..7e3c87d 100755 --- a/attic/apps/samples/WEB-INF/src/flex/samples/product/Product.java +++ b/attic/apps/samples/WEB-INF/src/flex/samples/product/Product.java @@ -1,89 +1,89 @@ -/* - * 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.product; -import java.io.Serializable; - -public class Product implements Serializable { - - static final long serialVersionUID = 103844514947365244L; - - private int productId; - private String name; - private String description; - private String image; - private String category; - private double price; - private int qtyInStock; - - public Product() { - - } - - public Product(int productId, String name, String description, String image, String category, double price, int qtyInStock) { - this.productId = productId; - this.name = name; - this.description = description; - this.image = image; - this.category = category; - this.price = price; - this.qtyInStock = qtyInStock; - } - - public String getCategory() { - return category; - } - public void setCategory(String category) { - this.category = category; - } - public String getDescription() { - return description; - } - public void setDescription(String description) { - this.description = description; - } - public String getImage() { - return image; - } - public void setImage(String image) { - this.image = image; - } - public String getName() { - return name; - } - public void setName(String name) { - this.name = name; - } - public double getPrice() { - return price; - } - public void setPrice(double price) { - this.price = price; - } - public int getProductId() { - return productId; - } - public void setProductId(int productId) { - this.productId = productId; - } - public int getQtyInStock() { - return qtyInStock; - } - public void setQtyInStock(int qtyInStock) { - this.qtyInStock = qtyInStock; - } - +/* + * 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.product; +import java.io.Serializable; + +public class Product implements Serializable { + + static final long serialVersionUID = 103844514947365244L; + + private int productId; + private String name; + private String description; + private String image; + private String category; + private double price; + private int qtyInStock; + + public Product() { + + } + + public Product(int productId, String name, String description, String image, String category, double price, int qtyInStock) { + this.productId = productId; + this.name = name; + this.description = description; + this.image = image; + this.category = category; + this.price = price; + this.qtyInStock = qtyInStock; + } + + public String getCategory() { + return category; + } + public void setCategory(String category) { + this.category = category; + } + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } + public String getImage() { + return image; + } + public void setImage(String image) { + this.image = image; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public double getPrice() { + return price; + } + public void setPrice(double price) { + this.price = price; + } + public int getProductId() { + return productId; + } + public void setProductId(int productId) { + this.productId = productId; + } + public int getQtyInStock() { + return qtyInStock; + } + public void setQtyInStock(int qtyInStock) { + this.qtyInStock = qtyInStock; + } + } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/flex-blazeds/blob/012fad7c/attic/apps/samples/WEB-INF/src/flex/samples/product/ProductService.java ---------------------------------------------------------------------- diff --git a/attic/apps/samples/WEB-INF/src/flex/samples/product/ProductService.java b/attic/apps/samples/WEB-INF/src/flex/samples/product/ProductService.java index f63ce16..8e0a816 100755 --- a/attic/apps/samples/WEB-INF/src/flex/samples/product/ProductService.java +++ b/attic/apps/samples/WEB-INF/src/flex/samples/product/ProductService.java @@ -1,191 +1,191 @@ -/* - * 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.product; - -import java.util.ArrayList; -import java.util.List; -import java.sql.*; - -import flex.samples.ConnectionHelper; -import flex.samples.DAOException; - -public class ProductService { - - public List 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); - } - return list; - - } - - 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 create(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 boolean update(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()); - return (ps.executeUpdate() == 1); - } catch (SQLException e) { - e.printStackTrace(); - throw new DAOException(e); - } finally { - ConnectionHelper.close(c); - } - - } - - public 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 delete(Product product) throws DAOException { - return remove(product); - } - +/* + * 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.product; + +import java.util.ArrayList; +import java.util.List; +import java.sql.*; + +import flex.samples.ConnectionHelper; +import flex.samples.DAOException; + +public class ProductService { + + public List 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); + } + return list; + + } + + 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 create(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 boolean update(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()); + return (ps.executeUpdate() == 1); + } catch (SQLException e) { + e.printStackTrace(); + throw new DAOException(e); + } finally { + ConnectionHelper.close(c); + } + + } + + public 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 delete(Product product) throws DAOException { + return remove(product); + } + } \ No newline at end of file
