]> git.mxchange.org Git - pizzaservice-swing.git/blob - src/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java
Added check on zero
[pizzaservice-swing.git] / src / org / mxchange / pizzaapplication / application / PizzaServiceApplication.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.pizzaapplication.application;
18
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;
30
31 /**
32  * Main application class
33  *
34  * @author Roland Haeder
35  */
36 public class PizzaServiceApplication extends BaseFrameworkSystem implements PizzaApplication {
37         /**
38          * Default constructor
39          */
40         public PizzaServiceApplication () {
41                 // Trace message
42                 this.getLogger().trace("CALLED!"); //NOI18N
43         }
44
45         @Override
46         public Deque<Category> getAllCategories () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
47                 // Deligate to frontend
48                 return this.categoryFrontend.getAllCategories();
49         }
50
51         @Override
52         public Deque<Product> getAllProducts () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
53                 // Deligate to frontend
54                 return this.productFrontend.getAllProducts();
55         }
56
57         @Override
58         public Deque<Product> getAvailableProducts () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
59                 // Deligate to frontend
60                 return this.productFrontend.getAllAvailableProducts();
61         }
62
63         @Override
64         public void init () throws SQLException {
65                 // Trace message
66                 this.getLogger().trace("CALLED!"); //NOI18N
67
68                 // Is the bundle initialized?
69                 if (!BaseFrameworkSystem.isBundledInitialized()) {
70                         // Temporary initialize default bundle
71                         // TODO The enum Gender uses this
72                         this.initBundle();
73                 }
74
75                 // Trace message
76                 this.getLogger().trace("EXIT!"); //NOI18N
77         }
78
79         @Override
80         public void doBootstrap () {
81                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
82         }
83
84         @Override
85         public void doMainLoop () {
86                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
87         }
88
89         @Override
90         public void doShutdown () {
91                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
92         }
93
94         @Override
95         @Deprecated
96         public String getPrintableProductAvailability (final Product product) {
97                 // Trace message
98                 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
99
100                 // Is it null?
101                 if (null == product) {
102                         // Should not be null
103                         throw new NullPointerException("product is null"); //NOI18N
104                 }
105
106                 // Get availability
107                 if (product.getAvailable() == true) {
108                         // Is available
109                         return "Ja";
110                 } else {
111                         // Not, not for public
112                         return "Nein";
113                 }
114         }
115
116         @Override
117         @SuppressWarnings ("unchecked")
118         public Iterator<Product> getAvailableProductsIterator () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
119                 // Trace message
120                 this.getLogger().trace("CALLED!"); //NOI18N
121
122                 // Ask frontend for a list of products
123                 return this.productFrontend.getAvailableProductsIterator();
124         }
125
126         @Override
127         @SuppressWarnings ("unchecked")
128         public Iterator<Product> getAllProductsIterator () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
129                 // Trace message
130                 this.getLogger().trace("CALLED!"); //NOI18N
131
132                 // Ask frontend for a list of products
133                 return this.productFrontend.getAllProductsIterator();
134         }
135
136         @Override
137         @SuppressWarnings ("unchecked")
138         public Iterator<Category> getAllCategoriesIterator () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
139                 // Trace message
140                 this.getLogger().trace("CALLED!"); //NOI18N
141
142                 // Ask frontend for a list of categories
143                 return this.categoryFrontend.getAllCategoriesIterator();
144         }
145
146         /**
147          * Checks whether given category title is already used
148          *
149          * @param title Title of category to check
150          * @return Whether it has been found
151          */
152         private boolean isCategoryTitleUsed(final String title) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
153                 // Trace message
154                 this.getLogger().trace("title=" + title + " - CALLED!"); //NOI18N
155
156                 // Delegate to frontend
157                 return this.categoryFrontend.isCategoryTitleUsed(title);
158         }
159
160         @Override
161         public void doAdminAddCategory (final Category category) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, CategoryTitleAlreadyUsedException {
162                 // Trace message
163                 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
164
165                 // category must not be null
166                 if (null == category) {
167                         // Is null
168                         throw new NullPointerException("category is null"); //NOI18N
169                 }
170
171                 // Get all fields
172                 String title = category.getTitle();
173                 Long parentId = category.getParentId();
174
175                 // Debug message
176                 this.getLogger().debug(MessageFormat.format("title={0},parentId={1}", title, parentId)); //NOI18N
177
178                 // Init variables for casting
179                 Integer id = 0;
180
181                 // Check all fields
182                 if (null == title) {
183                         // "title" not set
184                         throw new IllegalArgumentException("title is not set."); //NOI18N
185                 } else if (title.isEmpty()) {
186                         // Is left empty
187                         throw new IllegalArgumentException("title is empty."); //NOI18N
188                 } else if ((parentId == null) || (parentId == 0)) {
189                         // Is left empty
190                         throw new IllegalArgumentException("parentId is empty."); //NOI18N
191                 }
192
193                 // Try to check if title is used already
194                 if (this.isCategoryTitleUsed(title)) {
195                         // Title already used
196                         throw new CategoryTitleAlreadyUsedException(category);
197                 }
198
199                 // The category is not found, so add it to database
200                 this.categoryFrontend.addCategory(title, id);
201
202                 // Trace message
203                 this.getLogger().trace("EXIT!"); //NOI18N
204         }
205
206         @Override
207         public void doAdminAddProduct (final Product product) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, ProductTitleAlreadyUsedException {
208                 // Trace message
209                 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
210
211                 // product must not be null
212                 if (null == product) {
213                         // Is null
214                         throw new NullPointerException("product is null"); //NOI18N
215                 }
216
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();
222
223                 // Debug message
224                 this.getLogger().debug(MessageFormat.format("title={0},price={1},categoryId={2},available={3}", title, price, categoryId, available)); //NOI18N
225
226                 // Check all fields
227                 if (null == title) {
228                         // "title" not set
229                         throw new IllegalArgumentException("product title is not set"); //NOI18N
230                 } else if (title.isEmpty()) {
231                         // Is left empty
232                         throw new IllegalArgumentException("product title is empty"); //NOI18N
233                 } else if (null == price) {
234                         // "price" not set
235                         throw new IllegalArgumentException("product price is not set."); //NOI18N
236                 } else if (null == categoryId) {
237                         // "title" not set
238                         throw new IllegalArgumentException("product category id is not set."); //NOI18N
239                 } else if (categoryId == 0) {
240                         // "title" not set
241                         throw new IllegalArgumentException("product category id is zero."); //NOI18N
242                 } else if (null == available) {
243                         // "title" not set
244                         throw new IllegalArgumentException("product availability not set."); //NOI18N
245                 }
246
247                 // Try to check if title is used already
248                 if (this.isProductTitleUsed(product)) {
249                         // Title already used
250                         throw new ProductTitleAlreadyUsedException(product);
251                 }
252
253                 // The product is not found, so add it to database
254                 this.productFrontend.addProduct(title, price, categoryId, available);
255
256                 // Trace message
257                 this.getLogger().trace("EXIT!"); //NOI18N
258         }
259
260         @Override
261         @Deprecated
262         public String generateLinkForParent (final Category category) {
263                 // Trace message
264                 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
265
266                 // category must not be null
267                 if (null == category) {
268                         // Is null
269                         throw new NullPointerException("category is null"); //NOI18N
270                 }
271
272                 // Get parent id
273                 Long parentId = category.getParentId();
274
275                 // Is the id set?
276                 if (parentId > 0) {
277                         // Product HTML code for link
278                         throw new UnsupportedOperationException(MessageFormat.format("parentId={0} - Unfinished!", parentId)); //NOI18N
279                 }
280
281                 // No parent set
282                 return "Keine";
283         }
284
285         @Override
286         @Deprecated
287         public String getPrintableProductCategory (final Product product) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
288                 // Trace message
289                 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
290
291                 // product must not be null
292                 if (null == product) {
293                         // Abort here
294                         throw new NullPointerException("product is null"); //NOI18N
295                 }
296
297                 // Declare category
298                 Category category = this.categoryFrontend.getCategory(product);
299
300                 // Debug message
301                 this.getLogger().debug(MessageFormat.format("categoryId={0}", category)); //NOI18N
302
303                 // Get decoded title
304                 String title = category.getDecodedTitle();
305
306                 // Trace message
307                 this.getLogger().trace(MessageFormat.format("title={0} - EXIT!", title)); //NOI18N
308
309                 // Return it
310                 return title;
311         }
312
313         /**
314          * Checks if product's title is already used.
315          * 
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?
325          */
326         private boolean isProductTitleUsed (final Product product) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
327                 // Trace message
328                 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", product)); //NOI18N
329
330                 // Init title
331                 String title = product.getTitle();
332
333                 // category must not be null and "title" must be found and non-empty
334                 if (null == product) {
335                         // Abort here
336                         throw new NullPointerException("category is null"); //NOI18N
337                 } else if (null == title) {
338                         // title is not set
339                         throw new IllegalArgumentException("product title is not set"); //NOI18N
340                 } else if (title.isEmpty()) {
341                         // Is left empty
342                         throw new IllegalArgumentException("product title is empty"); //NOI18N
343                 }
344
345                 // Default is not used
346                 // TODO Call backend
347                 throw new UnsupportedOperationException("not finished yet.");
348
349                 // Trace message
350                 //this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
351
352                 // Return it
353                 //return isUsed;
354         }
355
356         @Override
357         @Deprecated
358         public void doAdminHandleProductForms () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, ProductTitleAlreadyUsedException {
359                 // Deprecated method called
360                 throw new UnsupportedOperationException("Deprecated method called.");
361         }
362
363         @Override
364         @Deprecated
365         public void doAdminHandleCategoryForms () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, CategoryTitleAlreadyUsedException {
366                 // Deprecated method called
367                 throw new UnsupportedOperationException("Deprecated method called.");
368         }
369
370         /**
371          * Checks if category's title is already used.
372          * 
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?
382          */
383         private boolean isCategoryTitleUsed (final Category category) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
384                 // Trace message
385                 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
386
387                 // Init title
388                 String title = category.getTitle();
389
390                 // category must not be null and "title" must be found and non-empty
391                 if (null == category) {
392                         // Abort here
393                         throw new NullPointerException("category is null"); //NOI18N
394                 } else if (null == title) {
395                         // title is not set
396                         throw new IllegalArgumentException("category title is not set."); //NOI18N
397                 } else if (title.isEmpty()) {
398                         // Is left empty
399                         throw new IllegalArgumentException("category title is empty."); //NOI18N
400                 }
401
402                 // Default is not used
403                 boolean isUsed = this.isCategoryTitleUsed(title);
404
405                 // Trace message
406                 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
407
408                 // Return it
409                 return isUsed;
410         }
411 }