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