]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java
Continued with project:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / database / frontend / product / PizzaProductDatabaseFrontend.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.database.frontend.product;
18
19 import java.io.IOException;
20 import java.sql.ResultSet;
21 import java.sql.SQLException;
22 import java.text.MessageFormat;
23 import java.util.Iterator;
24 import org.mxchange.jcore.criteria.searchable.SearchCriteria;
25 import org.mxchange.jcore.criteria.searchable.SearchableCritera;
26 import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend;
27 import org.mxchange.jcore.database.result.DatabaseResult;
28 import org.mxchange.jcore.database.result.Result;
29 import org.mxchange.jcore.database.storage.Storeable;
30 import org.mxchange.jcore.exceptions.BadTokenException;
31 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
32 import org.mxchange.pizzaapplication.database.product.PizzaProductDatabaseConstants;
33 import org.mxchange.pizzaapplication.product.PizzaProduct;
34 import org.mxchange.pizzaapplication.product.Product;
35
36 /**
37  * Stores and retrieves Contact instances
38  *
39  * @author Roland Haeder
40  */
41 public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implements ProductFrontend {
42
43         /**
44          * Default constrcutor
45          * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the configured backend is not supported
46          * @throws java.sql.SQLException If any SQL error occurs
47          */
48         public PizzaProductDatabaseFrontend () throws UnsupportedDatabaseBackendException, SQLException {
49                 // Trace message
50                 this.getLogger().trace("CALLED!"); //NOI18N
51
52                 // Set "table" name
53                 this.setTableName("products"); //NOI18N
54
55                 // Initalize backend
56                 this.initBackend();
57         }
58
59         /**
60          * Shuts down the database layer
61          * 
62          * @throws java.sql.SQLException If an SQL error occurs
63          * @throws java.io.IOException If any IO error occurs
64          */
65         @Override
66         public void doShutdown () throws SQLException, IOException {
67                 // Trace message
68                 this.getLogger().trace("CALLED!"); //NOI18N
69
70                 // Shutdown backend
71                 this.getBackend().doShutdown();
72
73                 // Trace message
74                 this.getLogger().trace("EXIT!"); //NOI18N
75         }
76
77         /**
78          * Depending on column, an empty value may be converted to null
79          *
80          * @param key Key to check
81          * @param value Value to check
82          * @return Possible previous value or null
83          */
84         @Override
85         public Object emptyStringToNull (final String key, final Object value) {
86                 // Trace message
87                 this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value));
88
89                 // Copy value
90                 Object v = value;
91
92                 // Is the value empty?
93                 if ((value instanceof String) && ("".equals(value))) {
94                         // This value may need to be changed
95                         switch (key) {
96                         }
97                 }
98
99                 // Trace message
100                 this.getLogger().trace(MessageFormat.format("v={0} - EXIT!", v));
101
102                 // Return it
103                 return v;
104         }
105
106         @Override
107         @SuppressWarnings ("unchecked")
108         public Iterator<Product> getAvailableProducts () throws IOException, BadTokenException, SQLException {
109                 // Trace message
110                 this.getLogger().trace("CALLED!"); //NOI18N
111
112                 // Instance search criteria
113                 SearchableCritera critera = new SearchCriteria();
114
115                 // Add criteria
116                 critera.addCriteria(PizzaProductDatabaseConstants.COLUMN_AVAILABLE, true);
117
118                 // Run the query
119                 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
120
121                 // Debug message
122                 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
123
124                 // Get iterator
125                 Iterator<?> iterator = result.iterator();
126
127                 // Trace message
128                 this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N
129
130                 // Get iterator and return it
131                 return (Iterator<Product>) iterator;
132         }
133
134         /**
135          * An iterator on all products
136          *
137          * @return Iterator on all products
138          * @throws java.io.IOException If any IO error occurs
139          * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
140          * @throws java.sql.SQLException If any SQL errors occur
141          */
142         @Override
143         @SuppressWarnings ("unchecked")
144         public Iterator<Product> getAllProducts () throws IOException, BadTokenException, SQLException {
145                 // Trace message
146                 this.getLogger().trace("CALLED!"); //NOI18N
147
148                 // Instance search criteria
149                 SearchableCritera critera = new SearchCriteria();
150
151                 // Run the query
152                 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
153
154                 // Debug message
155                 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
156
157                 // Get iterator
158                 Iterator<?> iterator = result.iterator();
159
160                 // Trace message
161                 this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N
162
163                 // Get iterator and return it
164                 return (Iterator<Product>) iterator;
165         }
166
167         /**
168          * Gets a Result back from given ResultSet instance
169          *
170          * @param resultSet ResultSet instance from SQL driver
171          * @return A typorized Result instance
172          * @throws java.sql.SQLException If any SQL error occurs
173          */
174         @Override
175         public Result<? extends Storeable> getResultFromSet (final ResultSet resultSet) throws SQLException {
176                 // Trace message
177                 this.getLogger().trace(MessageFormat.format("resultSet={0} - CALLED!", resultSet));
178
179                 // Init result instance
180                 Result<? extends Storeable> result = new DatabaseResult();
181
182                 // Reset result set before first row
183                 resultSet.beforeFirst();
184
185                 // "Walk" through all entries
186                 while (resultSet.next()) {
187                         // Get id, title and parent id
188                         Long id = resultSet.getLong(PizzaProductDatabaseConstants.COLUMN_ID);
189                         String title = resultSet.getString(PizzaProductDatabaseConstants.COLUMN_TITLE);
190                         Float price = resultSet.getFloat(PizzaProductDatabaseConstants.COLUMN_PRICE);
191                         Long category = resultSet.getLong(PizzaProductDatabaseConstants.COLUMN_CATEGORY);
192                         Boolean available = resultSet.getBoolean(PizzaProductDatabaseConstants.COLUMN_AVAILABLE);
193
194                         // Debug message
195                         this.getLogger().debug(MessageFormat.format("id={0},title={1},category={2},available={3}", id, title, category, available));
196
197                         // Instance new object
198                         Product product = new PizzaProduct(id, title, price, category, available);
199
200                         // Debug log
201                         this.getLogger().debug(MessageFormat.format("product={0}", product));
202
203                         // Add it to result
204                         result.add(product);
205                 }
206
207                 // Trace message
208                 this.getLogger().trace(MessageFormat.format("result({0})={1} - EXIT!", result.size(), result));
209
210                 // Return result
211                 return result;
212         }
213
214         /**
215          * Checks wether the given product title is already used.
216          *
217          * @param title Product title
218          * @return Whether the product title is already used
219          * @throws java.io.IOException If any IO error occurs
220          * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
221          * @throws java.sql.SQLException If any SQL errors occur
222          */
223         @Override
224         public boolean isProductTitleUsed (String title) throws IOException, SQLException, BadTokenException {
225                 // Trace message
226                 this.getLogger().trace(MessageFormat.format("title={0} - CALLED!", title));
227                 
228                 // Get search criteria
229                 SearchableCritera criteria = new SearchCriteria();
230                 
231                 // Add criteria
232                 criteria.addCriteria(PizzaProductDatabaseConstants.COLUMN_TITLE, title);
233                 
234                 // Only one entry is find
235                 criteria.setLimit(1);
236                 
237                 // Run it on backend
238                 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
239                 
240                 // Debug log
241                 this.getLogger().debug(MessageFormat.format("result({0}={1}", result, result.size()));
242                 
243                 // Now check size of the result
244                 boolean isFound = (result.size() == 1);
245                 
246                 // Trace message
247                 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
248                 
249                 // Return it
250                 return isFound;
251         }
252
253         /**
254          * Parses given line from database backend into a Storeable instance. Please
255          * note that not all backends need this.
256          *
257          * @param line Line from database backend
258          * @return A Storeable instance
259          */
260         @Override
261         public Storeable parseLineToStoreable (final String line) throws BadTokenException {
262                 // Trace message
263                 this.getLogger().trace(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N
264
265                 // Call inner method
266                 Product product = this.parseLine(line);
267
268                 // Debug message
269                 this.getLogger().trace(MessageFormat.format("product={0} - EXIT!", product)); //NOI18N
270
271                 // Return it
272                 return product;
273         }
274
275         /**
276          * Parses given line to a Product instance
277          *
278          * @param line
279          * @return A Product instance from given line
280          */
281         private Product parseLine (final String line) {
282                 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N
283         }
284
285         /**
286          * Adds product to database by given title, price and category id
287          * @param title Product title
288          * @param price Product price
289          * @param category Product category id
290          * @param available Availability of product (selectable by customer)
291          * @throws java.sql.SQLException If any SQL errors occur
292          */
293         @Override
294         public void addProduct (final String title, final Float price, final Long category, final Boolean available) throws SQLException {
295                 // Trace message
296                 this.getLogger().trace(MessageFormat.format("title={0},price={1},category={2} - CALLED!", title, price, category));
297
298                 // Title should not be null
299                 if (title == null) {
300                         // Abort here
301                         throw new NullPointerException("title is null");
302                 } else if (price == null) {
303                         // Abort here
304                         throw new NullPointerException("price is null");
305                 } else if (category == null) {
306                         // Abort here
307                         throw new NullPointerException("category is null");
308                 } else if (available == null) {
309                         // Abort here
310                         throw new NullPointerException("available is null");
311                 }
312
313                 // Clear dataset from previous usage
314                 this.clearDataSet();
315
316                 // Add title and parent
317                 this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_TITLE, title);
318                 this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_PRICE, price);
319                 this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_CATEGORY, category);
320                 this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_AVAILABLE, available);
321
322                 // Handle this over to the backend
323                 Result<? extends Storeable> result = this.doInsertDataSet();
324
325                 // Debug message
326                 this.getLogger().debug(MessageFormat.format("result={0}", result));
327
328                 // Trace message
329                 this.getLogger().trace("EXIT!");
330         }
331 }