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.jshopcore.exceptions.CategoryTitleAlreadyUsedException;
27 import org.mxchange.jshopcore.exceptions.ProductTitleAlreadyUsedException;
28 import org.mxchange.jshopcore.model.category.Category;
29 import org.mxchange.jshopcore.model.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
95 @SuppressWarnings ("unchecked")
96 public Iterator<Product> getAvailableProductsIterator () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
98 this.getLogger().trace("CALLED!"); //NOI18N
100 // Ask frontend for a list of products
101 return this.productFrontend.getAvailableProductsIterator();
105 @SuppressWarnings ("unchecked")
106 public Iterator<Product> getAllProductsIterator () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
108 this.getLogger().trace("CALLED!"); //NOI18N
110 // Ask frontend for a list of products
111 return this.productFrontend.getAllProductsIterator();
115 @SuppressWarnings ("unchecked")
116 public Iterator<Category> getAllCategoriesIterator () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
118 this.getLogger().trace("CALLED!"); //NOI18N
120 // Ask frontend for a list of categories
121 return this.categoryFrontend.getAllCategoriesIterator();
125 * Checks whether given category title is already used
127 * @param title Title of category to check
128 * @return Whether it has been found
130 private boolean isCategoryTitleUsed(final String title) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
132 this.getLogger().trace("title=" + title + " - CALLED!"); //NOI18N
134 // Delegate to frontend
135 return this.categoryFrontend.isCategoryTitleUsed(title);
139 public void doAdminAddCategory (final Category category) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, CategoryTitleAlreadyUsedException {
141 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
143 // category must not be null
144 if (null == category) {
146 throw new NullPointerException("category is null"); //NOI18N
150 String title = category.getTitle();
151 Long parentId = category.getParentId();
154 this.getLogger().debug(MessageFormat.format("title={0},parentId={1}", title, parentId)); //NOI18N
156 // Init variables for casting
162 throw new IllegalArgumentException("title is not set."); //NOI18N
163 } else if (title.isEmpty()) {
165 throw new IllegalArgumentException("title is empty."); //NOI18N
166 } else if ((parentId == null) || (parentId == 0)) {
168 throw new IllegalArgumentException("parentId is empty."); //NOI18N
171 // Try to check if title is used already
172 if (this.isCategoryTitleUsed(title)) {
173 // Title already used
174 throw new CategoryTitleAlreadyUsedException(category);
177 // The category is not found, so add it to database
178 this.categoryFrontend.addCategory(title, id);
181 this.getLogger().trace("EXIT!"); //NOI18N
185 public void doAdminAddProduct (final Product product) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, ProductTitleAlreadyUsedException {
187 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
189 // product must not be null
190 if (null == product) {
192 throw new NullPointerException("product is null"); //NOI18N
195 // Get title, price and category id
196 String title = product.getTitle();
197 Float price = product.getPrice();
198 Long categoryId = product.getCategoryId();
199 Boolean available = product.getAvailable();
202 this.getLogger().debug(MessageFormat.format("title={0},price={1},categoryId={2},available={3}", title, price, categoryId, available)); //NOI18N
207 throw new IllegalArgumentException("product title is not set"); //NOI18N
208 } else if (title.isEmpty()) {
210 throw new IllegalArgumentException("product title is empty"); //NOI18N
211 } else if (null == price) {
213 throw new IllegalArgumentException("product price is not set."); //NOI18N
214 } else if (null == categoryId) {
216 throw new IllegalArgumentException("product category id is not set."); //NOI18N
217 } else if (categoryId == 0) {
219 throw new IllegalArgumentException("product category id is zero."); //NOI18N
220 } else if (null == available) {
222 throw new IllegalArgumentException("product availability not set."); //NOI18N
225 // Try to check if title is used already
226 if (this.isProductTitleUsed(product)) {
227 // Title already used
228 throw new ProductTitleAlreadyUsedException(product);
231 // The product is not found, so add it to database
232 this.productFrontend.addProduct(title, price, categoryId, available);
235 this.getLogger().trace("EXIT!"); //NOI18N
239 * Checks if product's title is already used.
241 * @param product Product instance
242 * @return Whether the product title is already used
243 * @throws java.io.IOException If any IO error occurs
244 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
245 * @throws java.sql.SQLException If any SQL error occurs
246 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
247 * @throws java.lang.NoSuchMethodException If a method was not found
248 * @throws java.lang.IllegalAccessException If the method cannot be accessed
249 * @throws java.lang.reflect.InvocationTargetException Any other problems?
251 private boolean isProductTitleUsed (final Product product) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
253 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", product)); //NOI18N
256 String title = product.getTitle();
258 // category must not be null and "title" must be found and non-empty
259 if (null == product) {
261 throw new NullPointerException("category is null"); //NOI18N
262 } else if (null == title) {
264 throw new IllegalArgumentException("product title is not set"); //NOI18N
265 } else if (title.isEmpty()) {
267 throw new IllegalArgumentException("product title is empty"); //NOI18N
270 // Default is not used
272 throw new UnsupportedOperationException("not finished yet.");
275 //this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
282 * Checks if category's title is already used.
284 * @param category Category instance
285 * @return Whether the product title is already used
286 * @throws java.io.IOException If any IO error occurs
287 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
288 * @throws java.sql.SQLException If any SQL error occurs
289 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
290 * @throws java.lang.NoSuchMethodException If a method was not found
291 * @throws java.lang.IllegalAccessException If the method cannot be accessed
292 * @throws java.lang.reflect.InvocationTargetException Any other problems?
294 private boolean isCategoryTitleUsed (final Category category) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
296 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
299 String title = category.getTitle();
301 // category must not be null and "title" must be found and non-empty
302 if (null == category) {
304 throw new NullPointerException("category is null"); //NOI18N
305 } else if (null == title) {
307 throw new IllegalArgumentException("category title is not set."); //NOI18N
308 } else if (title.isEmpty()) {
310 throw new IllegalArgumentException("category title is empty."); //NOI18N
313 // Default is not used
314 boolean isUsed = this.isCategoryTitleUsed(title);
317 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N