+ this.abortProgramWithException(ex);
+ }
+
+ // Init instance
+ Iterator<Product> iterator = null;
+
+ try {
+ // Get iterator
+ iterator = this.getAvailableProducts();
+ } catch (final ServletException ex) {
+ this.abortProgramWithException(ex);
+ }
+
+ // "Walk" over all products
+ while ((iterator instanceof Iterator) && (iterator.hasNext())) {
+ // Get next product
+ Product product = iterator.next();
+
+ // Output data
+ this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getId(), product.getTitle(), product.getPrice())); //NOI18N
+ }
+
+ // Generate fake Customer instance
+ Customer customer = new PizzaServiceCustomer();
+
+ /*
+ * Need a least a gender ... :( See, that is why I don't like default
+ * constructors, you can easily miss something important and bam! You
+ * get an NPE. The fix here is, to have construtors (or factories) which
+ * requires all required instances that needs to be set to get a
+ * consitent object back.
+ */
+
+ // Gender is MALE now
+ customer.setGender(Gender.MALE);
+
+ // Init iterator
+ Iterator<Map.Entry<Field, Object>> it = null;
+
+ try {
+ // Get iterator on all its fields
+ it = customer.iterator();
+ } catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
+ this.abortProgramWithException(ex);
+ }
+
+ // Output it
+ while ((it instanceof Iterator) && (it.hasNext())) {
+ Map.Entry<Field, Object> entry = it.next();
+ this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N
+ }
+ }
+
+ /**
+ * Adds given category data from request to database
+ *
+ * @param request Request instance
+ */
+ @Override
+ public void doAdminAddCategory (final HttpServletRequest request) throws ServletException, CategoryTitleAlreadyUsedException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
+
+ // request must not be null
+ if (request == null) {
+ // Is null
+ throw new NullPointerException("request is null"); //NOI18N
+ }
+
+ // Get all fields
+ String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE);
+ String parent = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT);
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("title={0},parent={1}", title, parent)); //NOI18N
+
+ // Init variables for casting
+ Integer id = 0;
+
+ // Check all fields
+ if (title == null) {
+ // "title" not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
+ } else if (title.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
+ } else if ((parent != null) && (!parent.isEmpty())) {
+ // "parent" is set, so check it
+ try {
+ id = Integer.parseInt(parent);
+ } catch (final NumberFormatException e) {
+ // Not valid number
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ try {
+ // Try to check if title is used already
+ if (this.isCategoryTitleUsed(title)) {
+ // Title already used
+ throw new CategoryTitleAlreadyUsedException(request);
+ }
+ } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
+ throw new ServletException(ex);
+ }
+
+ try {
+ // The category is not found, so add it to database
+ this.categoryFrontend.addCategory(title, id);
+ } catch (final SQLException | IOException ex) {
+ // Continue to throw it
+ throw new ServletException(ex);
+ }
+
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
+ }
+
+ /**
+ * Adds given product data from request to database
+ *
+ * @param request Request instance
+ */
+ @Override
+ public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
+
+ // request must not be null
+ if (request == null) {
+ // Is null
+ throw new NullPointerException("request is null"); //NOI18N
+ }
+
+ // Get title, price and category id
+ String title = request.getParameter(PizzaProductDatabaseConstants.COLUMN_TITLE);
+ String price = request.getParameter(PizzaProductDatabaseConstants.COLUMN_PRICE);
+ String category = request.getParameter(PizzaProductDatabaseConstants.COLUMN_CATEGORY);
+ String available = request.getParameter(PizzaProductDatabaseConstants.COLUMN_AVAILABLE);
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("title={0},price={1},category={2},available={3}", title, price, category, available)); //NOI18N
+
+ // Variables for converting
+ Long id = null;
+ Float p = null;
+
+ // Check all fields
+ if (title == null) {
+ // "title" not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
+ } else if (title.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
+ } else if (price == null) {
+ // "price" not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_PRICE)); //NOI18N
+ } else if (price.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_PRICE)); //NOI18N
+ } else if (category == null) {
+ // "title" not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_CATEGORY)); //NOI18N
+ } else if (category.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_CATEGORY)); //NOI18N
+ } else if (available == null) {
+ // "title" not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_AVAILABLE)); //NOI18N
+ } else if (available.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_AVAILABLE)); //NOI18N
+ } else if ((!"true".equals(available)) && (!"false".equals(available))) { //NOI18N
+ // Invalid value
+ throw new IllegalArgumentException(MessageFormat.format("{0} is invalid: {1}", PizzaProductDatabaseConstants.COLUMN_AVAILABLE, available)); //NOI18N
+ }
+
+ // Parse numbers
+ try {
+ id = Long.parseLong(category);
+ p = Float.parseFloat(price);
+ } catch (final NumberFormatException e) {
+ // Not valid number
+ throw new IllegalArgumentException(e);
+ }
+
+ // Parse boolean
+ Boolean a = Boolean.parseBoolean(available);
+
+ // Test on product title
+ try {
+ // Try to check if title is used already
+ if (this.isProductTitleUsed(title)) {
+ // Title already used
+ throw new ProductTitleAlreadyUsedException(request);
+ }
+ } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
+ throw new ServletException(ex);
+ }
+
+ try {
+ // The product is not found, so add it to database
+ this.productFrontend.addProduct(title, p, id, a);
+ } catch (final SQLException | IOException ex) {
+ // Continue to throw it
+ throw new ServletException(ex);
+ }
+
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
+ }
+
+ /**
+ * Generates link HTML code for given category's parent id, if set. This
+ * link then points to products.jsp?category_id=x
+ *
+ * @param category Category instance
+ * @return HTML code
+ */
+ @Override
+ public String generateLinkForParent (final Category category) {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
+
+ // category must not be null
+ if (category == null) {
+ // Is null
+ throw new NullPointerException("category is null"); //NOI18N
+ }
+
+ // Get parent id
+ Long parent = category.getParent();
+
+ // Is the id set?
+ if (parent > 0) {
+ // Product HTML code for link
+ throw new UnsupportedOperationException(MessageFormat.format("parent={0} - Unfinished!", parent)); //NOI18N
+ }
+
+ // No parent set
+ return "Keine";
+ }
+
+ @Override
+ public String getPrintableProduktCategory (final Product product) throws ServletException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
+
+ // product must not be null
+ if (product == null) {