/** * Queries the given customer's orders. * * @param alias the customer alias * @param passwordHash the customer password-hash * @return the customer's orders * @throws IllegalStateException if the login data is invalid * @throws SQLException if there is a problem with the underlying JDBC connection */ public SortedSet<Order> queryOrders(final String alias, final byte[] passwordHash) throws SQLException { final SortedSet<Order> orders = new TreeSet<Order>(); final Customer customer = this.queryCustomer(alias, passwordHash); synchronized (this.connection) { try (PreparedStatement statement = this.connection.prepareStatement(SQL_SELECT_PURCHASES)) { statement.setLong(1, customer.getIdentity()); try (ResultSet resultSet = statement.executeQuery()) { while (resultSet.next()) { final Order purchase = new Order(); purchase.setIdentity(resultSet.getLong("identity")); purchase.setCustomerIdentity(resultSet.getLong("customerIdentity")); purchase.setCreationTimestamp(resultSet.getLong("creationTimestamp")); purchase.setTaxRate(resultSet.getDouble("taxRate")); orders.add(purchase); } } } } for (final Order purchase : orders) { this.populateOrderItems(purchase); } return orders; }
/** * Queries the given customer's order with the given identity. * * @param alias the customer alias * @param passwordHash the customer password-hash * @param orderIdentity the order identity * @return the customer's order * @throws IllegalStateException if the login data is invalid * @throws SQLException if there is a problem with the underlying JDBC connection */ public Order queryOrder(final String alias, final byte[] passwordHash, final long orderIdentity) throws SQLException { final Order order = new Order(); final Customer customer = this.queryCustomer(alias, passwordHash); synchronized (this.connection) { try (PreparedStatement statement = this.connection.prepareStatement(SQL_SELECT_PURCHASE)) { statement.setLong(1, orderIdentity); try (ResultSet resultSet = statement.executeQuery()) { if (!resultSet.next()) throw new IllegalStateException("purchase doesn't exist."); if (customer.getIdentity() != resultSet.getLong("customerIdentity")) throw new IllegalStateException("purchase not created by given customer."); order.setIdentity(resultSet.getLong("identity")); order.setCustomerIdentity(resultSet.getLong("customerIdentity")); order.setCreationTimestamp(resultSet.getLong("creationTimestamp")); order.setTaxRate(resultSet.getDouble("taxRate")); } } } this.populateOrderItems(order); return order; }