]> git.mxchange.org Git - pizzaservice-swing.git/blob - src/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java
updated jshop-ee-lib.jar + removed deprecated methods and which are old and lost...
[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         @SuppressWarnings ("unchecked")
96         public Iterator<Product> getAvailableProductsIterator () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
97                 // Trace message
98                 this.getLogger().trace("CALLED!"); //NOI18N
99
100                 // Ask frontend for a list of products
101                 return this.productFrontend.getAvailableProductsIterator();
102         }
103
104         @Override
105         @SuppressWarnings ("unchecked")
106         public Iterator<Product> getAllProductsIterator () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
107                 // Trace message
108                 this.getLogger().trace("CALLED!"); //NOI18N
109
110                 // Ask frontend for a list of products
111                 return this.productFrontend.getAllProductsIterator();
112         }
113
114         @Override
115         @SuppressWarnings ("unchecked")
116         public Iterator<Category> getAllCategoriesIterator () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
117                 // Trace message
118                 this.getLogger().trace("CALLED!"); //NOI18N
119
120                 // Ask frontend for a list of categories
121                 return this.categoryFrontend.getAllCategoriesIterator();
122         }
123
124         /**
125          * Checks whether given category title is already used
126          *
127          * @param title Title of category to check
128          * @return Whether it has been found
129          */
130         private boolean isCategoryTitleUsed(final String title) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
131                 // Trace message
132                 this.getLogger().trace("title=" + title + " - CALLED!"); //NOI18N
133
134                 // Delegate to frontend
135                 return this.categoryFrontend.isCategoryTitleUsed(title);
136         }
137
138         @Override
139         public void doAdminAddCategory (final Category category) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, CategoryTitleAlreadyUsedException {
140                 // Trace message
141                 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
142
143                 // category must not be null
144                 if (null == category) {
145                         // Is null
146                         throw new NullPointerException("category is null"); //NOI18N
147                 }
148
149                 // Get all fields
150                 String title = category.getTitle();
151                 Long parentId = category.getParentId();
152
153                 // Debug message
154                 this.getLogger().debug(MessageFormat.format("title={0},parentId={1}", title, parentId)); //NOI18N
155
156                 // Init variables for casting
157                 Integer id = 0;
158
159                 // Check all fields
160                 if (null == title) {
161                         // "title" not set
162                         throw new IllegalArgumentException("title is not set."); //NOI18N
163                 } else if (title.isEmpty()) {
164                         // Is left empty
165                         throw new IllegalArgumentException("title is empty."); //NOI18N
166                 } else if ((parentId == null) || (parentId == 0)) {
167                         // Is left empty
168                         throw new IllegalArgumentException("parentId is empty."); //NOI18N
169                 }
170
171                 // Try to check if title is used already
172                 if (this.isCategoryTitleUsed(title)) {
173                         // Title already used
174                         throw new CategoryTitleAlreadyUsedException(category);
175                 }
176
177                 // The category is not found, so add it to database
178                 this.categoryFrontend.addCategory(title, id);
179
180                 // Trace message
181                 this.getLogger().trace("EXIT!"); //NOI18N
182         }
183
184         @Override
185         public void doAdminAddProduct (final Product product) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, ProductTitleAlreadyUsedException {
186                 // Trace message
187                 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
188
189                 // product must not be null
190                 if (null == product) {
191                         // Is null
192                         throw new NullPointerException("product is null"); //NOI18N
193                 }
194
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();
200
201                 // Debug message
202                 this.getLogger().debug(MessageFormat.format("title={0},price={1},categoryId={2},available={3}", title, price, categoryId, available)); //NOI18N
203
204                 // Check all fields
205                 if (null == title) {
206                         // "title" not set
207                         throw new IllegalArgumentException("product title is not set"); //NOI18N
208                 } else if (title.isEmpty()) {
209                         // Is left empty
210                         throw new IllegalArgumentException("product title is empty"); //NOI18N
211                 } else if (null == price) {
212                         // "price" not set
213                         throw new IllegalArgumentException("product price is not set."); //NOI18N
214                 } else if (null == categoryId) {
215                         // "title" not set
216                         throw new IllegalArgumentException("product category id is not set."); //NOI18N
217                 } else if (categoryId == 0) {
218                         // "title" not set
219                         throw new IllegalArgumentException("product category id is zero."); //NOI18N
220                 } else if (null == available) {
221                         // "title" not set
222                         throw new IllegalArgumentException("product availability not set."); //NOI18N
223                 }
224
225                 // Try to check if title is used already
226                 if (this.isProductTitleUsed(product)) {
227                         // Title already used
228                         throw new ProductTitleAlreadyUsedException(product);
229                 }
230
231                 // The product is not found, so add it to database
232                 this.productFrontend.addProduct(title, price, categoryId, available);
233
234                 // Trace message
235                 this.getLogger().trace("EXIT!"); //NOI18N
236         }
237
238         /**
239          * Checks if product's title is already used.
240          * 
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?
250          */
251         private boolean isProductTitleUsed (final Product product) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
252                 // Trace message
253                 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", product)); //NOI18N
254
255                 // Init title
256                 String title = product.getTitle();
257
258                 // category must not be null and "title" must be found and non-empty
259                 if (null == product) {
260                         // Abort here
261                         throw new NullPointerException("category is null"); //NOI18N
262                 } else if (null == title) {
263                         // title is not set
264                         throw new IllegalArgumentException("product title is not set"); //NOI18N
265                 } else if (title.isEmpty()) {
266                         // Is left empty
267                         throw new IllegalArgumentException("product title is empty"); //NOI18N
268                 }
269
270                 // Default is not used
271                 // TODO Call backend
272                 throw new UnsupportedOperationException("not finished yet.");
273
274                 // Trace message
275                 //this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
276
277                 // Return it
278                 //return isUsed;
279         }
280
281         /**
282          * Checks if category's title is already used.
283          * 
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?
293          */
294         private boolean isCategoryTitleUsed (final Category category) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
295                 // Trace message
296                 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
297
298                 // Init title
299                 String title = category.getTitle();
300
301                 // category must not be null and "title" must be found and non-empty
302                 if (null == category) {
303                         // Abort here
304                         throw new NullPointerException("category is null"); //NOI18N
305                 } else if (null == title) {
306                         // title is not set
307                         throw new IllegalArgumentException("category title is not set."); //NOI18N
308                 } else if (title.isEmpty()) {
309                         // Is left empty
310                         throw new IllegalArgumentException("category title is empty."); //NOI18N
311                 }
312
313                 // Default is not used
314                 boolean isUsed = this.isCategoryTitleUsed(title);
315
316                 // Trace message
317                 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
318
319                 // Return it
320                 return isUsed;
321         }
322 }