2 * Copyright (C) 2015 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.pizzaapplication.application;
19 import java.io.IOException;
20 import java.lang.reflect.InvocationTargetException;
21 import java.sql.SQLException;
22 import java.text.MessageFormat;
23 import java.util.Deque;
24 import java.util.Iterator;
25 import org.mxchange.jcore.BaseFrameworkSystem;
26 import org.mxchange.jshop.category.Category;
27 import org.mxchange.jshop.exceptions.CategoryTitleAlreadyUsedException;
28 import org.mxchange.jshop.exceptions.ProductTitleAlreadyUsedException;
29 import org.mxchange.jshop.product.Product;
32 * Main application class
34 * @author Roland Haeder
36 public class PizzaServiceApplication extends BaseFrameworkSystem implements PizzaApplication {
40 public PizzaServiceApplication () {
42 this.getLogger().trace("CALLED!"); //NOI18N
46 public Deque<Category> getAllCategories () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
47 // Deligate to frontend
48 return this.categoryFrontend.getAllCategories();
52 public Deque<Product> getAllProducts () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
53 // Deligate to frontend
54 return this.productFrontend.getAllProducts();
58 public Deque<Product> getAvailableProducts () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
59 // Deligate to frontend
60 return this.productFrontend.getAllAvailableProducts();
64 public void init () throws SQLException {
66 this.getLogger().trace("CALLED!"); //NOI18N
68 // Is the bundle initialized?
69 if (!BaseFrameworkSystem.isBundledInitialized()) {
70 // Temporary initialize default bundle
71 // TODO The enum Gender uses this
76 this.getLogger().trace("EXIT!"); //NOI18N
80 public void doBootstrap () {
81 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
85 public void doMainLoop () {
86 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
90 public void doShutdown () {
91 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
96 public String getPrintableProductAvailability (final Product product) {
98 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
101 if (null == product) {
102 // Should not be null
103 throw new NullPointerException("product is null"); //NOI18N
107 if (product.getAvailable() == true) {
111 // Not, not for public
117 @SuppressWarnings ("unchecked")
118 public Iterator<Product> getAvailableProductsIterator () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
120 this.getLogger().trace("CALLED!"); //NOI18N
122 // Ask frontend for a list of products
123 return this.productFrontend.getAvailableProductsIterator();
127 @SuppressWarnings ("unchecked")
128 public Iterator<Product> getAllProductsIterator () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
130 this.getLogger().trace("CALLED!"); //NOI18N
132 // Ask frontend for a list of products
133 return this.productFrontend.getAllProductsIterator();
137 @SuppressWarnings ("unchecked")
138 public Iterator<Category> getAllCategoriesIterator () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
140 this.getLogger().trace("CALLED!"); //NOI18N
142 // Ask frontend for a list of categories
143 return this.categoryFrontend.getAllCategoriesIterator();
147 * Checks whether given category title is already used
149 * @param title Title of category to check
150 * @return Whether it has been found
152 private boolean isCategoryTitleUsed(final String title) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
154 this.getLogger().trace("title=" + title + " - CALLED!"); //NOI18N
156 // Delegate to frontend
157 return this.categoryFrontend.isCategoryTitleUsed(title);
161 public void doAdminAddCategory (final Category category) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, CategoryTitleAlreadyUsedException {
163 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
165 // category must not be null
166 if (null == category) {
168 throw new NullPointerException("category is null"); //NOI18N
172 String title = category.getTitle();
173 Long parentId = category.getParentId();
176 this.getLogger().debug(MessageFormat.format("title={0},parentId={1}", title, parentId)); //NOI18N
178 // Init variables for casting
184 throw new IllegalArgumentException("title is not set."); //NOI18N
185 } else if (title.isEmpty()) {
187 throw new IllegalArgumentException("title is empty."); //NOI18N
188 } else if ((parentId == null) || (parentId == 0)) {
190 throw new IllegalArgumentException("parentId is empty."); //NOI18N
193 // Try to check if title is used already
194 if (this.isCategoryTitleUsed(title)) {
195 // Title already used
196 throw new CategoryTitleAlreadyUsedException(category);
199 // The category is not found, so add it to database
200 this.categoryFrontend.addCategory(title, id);
203 this.getLogger().trace("EXIT!"); //NOI18N
207 public void doAdminAddProduct (final Product product) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, ProductTitleAlreadyUsedException {
209 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
211 // product must not be null
212 if (null == product) {
214 throw new NullPointerException("product is null"); //NOI18N
217 // Get title, price and category id
218 String title = product.getTitle();
219 Float price = product.getPrice();
220 Long categoryId = product.getCategoryId();
221 Boolean available = product.getAvailable();
224 this.getLogger().debug(MessageFormat.format("title={0},price={1},categoryId={2},available={3}", title, price, categoryId, available)); //NOI18N
229 throw new IllegalArgumentException("product title is not set"); //NOI18N
230 } else if (title.isEmpty()) {
232 throw new IllegalArgumentException("product title is empty"); //NOI18N
233 } else if (null == price) {
235 throw new IllegalArgumentException("product price is not set."); //NOI18N
236 } else if (null == categoryId) {
238 throw new IllegalArgumentException("product category id is not set."); //NOI18N
239 } else if (null == available) {
241 throw new IllegalArgumentException("product availability not set."); //NOI18N
244 // Try to check if title is used already
245 if (this.isProductTitleUsed(product)) {
246 // Title already used
247 throw new ProductTitleAlreadyUsedException(product);
250 // The product is not found, so add it to database
251 this.productFrontend.addProduct(title, price, categoryId, available);
254 this.getLogger().trace("EXIT!"); //NOI18N
259 public String generateLinkForParent (final Category category) {
261 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
263 // category must not be null
264 if (null == category) {
266 throw new NullPointerException("category is null"); //NOI18N
270 Long parentId = category.getParentId();
274 // Product HTML code for link
275 throw new UnsupportedOperationException(MessageFormat.format("parentId={0} - Unfinished!", parentId)); //NOI18N
284 public String getPrintableProductCategory (final Product product) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
286 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
288 // product must not be null
289 if (null == product) {
291 throw new NullPointerException("product is null"); //NOI18N
297 // Get Category instance from product over the frontend
298 category = this.categoryFrontend.getCategory(product);
301 this.getLogger().debug(MessageFormat.format("categoryId={0}", category)); //NOI18N
304 String title = category.getDecodedTitle();
307 this.getLogger().trace(MessageFormat.format("title={0} - EXIT!", title)); //NOI18N
314 * Checks if product's title is already used.
316 * @param product Product instance
317 * @return Whether the product title is already used
318 * @throws java.io.IOException If any IO error occurs
319 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
320 * @throws java.sql.SQLException If any SQL error occurs
321 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
322 * @throws java.lang.NoSuchMethodException If a method was not found
323 * @throws java.lang.IllegalAccessException If the method cannot be accessed
324 * @throws java.lang.reflect.InvocationTargetException Any other problems?
326 private boolean isProductTitleUsed (final Product product) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
328 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", product)); //NOI18N
331 String title = product.getTitle();
333 // category must not be null and "title" must be found and non-empty
334 if (null == product) {
336 throw new NullPointerException("category is null"); //NOI18N
337 } else if (null == title) {
339 throw new IllegalArgumentException("product title is not set"); //NOI18N
340 } else if (title.isEmpty()) {
342 throw new IllegalArgumentException("product title is empty"); //NOI18N
345 // Default is not used
347 throw new UnsupportedOperationException("not finished yet.");
350 //this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
358 public void doAdminHandleProductForms () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, ProductTitleAlreadyUsedException {
359 // Deprecated method called
360 throw new UnsupportedOperationException("Deprecated method called.");
365 public void doAdminHandleCategoryForms () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, CategoryTitleAlreadyUsedException {
366 // Deprecated method called
367 throw new UnsupportedOperationException("Deprecated method called.");
371 * Checks if category's title is already used.
373 * @param category Category instance
374 * @return Whether the product title is already used
375 * @throws java.io.IOException If any IO error occurs
376 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
377 * @throws java.sql.SQLException If any SQL error occurs
378 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
379 * @throws java.lang.NoSuchMethodException If a method was not found
380 * @throws java.lang.IllegalAccessException If the method cannot be accessed
381 * @throws java.lang.reflect.InvocationTargetException Any other problems?
383 private boolean isCategoryTitleUsed (final Category category) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
385 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
388 String title = category.getTitle();
390 // category must not be null and "title" must be found and non-empty
391 if (null == category) {
393 throw new NullPointerException("category is null"); //NOI18N
394 } else if (null == title) {
396 throw new IllegalArgumentException("category title is not set."); //NOI18N
397 } else if (title.isEmpty()) {
399 throw new IllegalArgumentException("category title is empty."); //NOI18N
402 // Default is not used
403 boolean isUsed = this.isCategoryTitleUsed(title);
406 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N