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