2 * Copyright (C) 2015 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.pizzaapplication.database.frontend.product;
19 import java.io.IOException;
20 import java.lang.reflect.InvocationTargetException;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.text.MessageFormat;
24 import java.util.Iterator;
26 import org.mxchange.jcore.criteria.searchable.SearchCriteria;
27 import org.mxchange.jcore.criteria.searchable.SearchableCriteria;
28 import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend;
29 import org.mxchange.jcore.database.result.DatabaseResult;
30 import org.mxchange.jcore.database.result.Result;
31 import org.mxchange.jcore.database.storage.Storeable;
32 import org.mxchange.jcore.exceptions.BadTokenException;
33 import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
34 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
35 import org.mxchange.pizzaapplication.database.product.PizzaProductDatabaseConstants;
36 import org.mxchange.pizzaapplication.product.Product;
37 import org.mxchange.pizzaapplication.product.pizza.PizzaProduct;
40 * Stores and retrieves Contact instances
42 * @author Roland Haeder
44 public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implements ProductFrontend {
48 * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the configured backend is not supported
49 * @throws java.sql.SQLException If any SQL error occurs
51 public PizzaProductDatabaseFrontend () throws UnsupportedDatabaseBackendException, SQLException {
53 this.getLogger().trace("CALLED!"); //NOI18N
56 this.setTableName("products"); //NOI18N
63 * Shuts down the database layer
65 * @throws java.sql.SQLException If an SQL error occurs
66 * @throws java.io.IOException If any IO error occurs
69 public void doShutdown () throws SQLException, IOException {
71 this.getLogger().trace("CALLED!"); //NOI18N
74 this.getBackend().doShutdown();
77 this.getLogger().trace("EXIT!"); //NOI18N
81 * Depending on column, an empty value may be converted to null
83 * @param key Key to check
84 * @param value Value to check
85 * @return Possible previous value or null
88 public Object emptyStringToNull (final String key, final Object value) {
90 this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value));
95 // Is the value empty?
96 if ((value instanceof String) && ("".equals(value))) {
97 // This value may need to be changed
103 this.getLogger().trace(MessageFormat.format("v={0} - EXIT!", v));
110 @SuppressWarnings ("unchecked")
111 public Iterator<Product> getAvailableProducts () throws IOException, BadTokenException, SQLException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
113 this.getLogger().trace("CALLED!"); //NOI18N
115 // Instance search criteria
116 SearchableCriteria critera = new SearchCriteria();
119 critera.addCriteria(PizzaProductDatabaseConstants.COLUMN_AVAILABLE, true);
122 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
125 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
128 Iterator<?> iterator = result.iterator();
131 this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N
133 // Get iterator and return it
134 return (Iterator<Product>) iterator;
138 * An iterator on all products
140 * @return Iterator on all products
141 * @throws java.io.IOException If any IO error occurs
142 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
143 * @throws java.sql.SQLException If any SQL errors occur
146 @SuppressWarnings ("unchecked")
147 public Iterator<Product> getAllProducts () throws IOException, BadTokenException, SQLException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
149 this.getLogger().trace("CALLED!"); //NOI18N
151 // Instance search criteria
152 SearchableCriteria critera = new SearchCriteria();
155 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
158 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
161 Iterator<?> iterator = result.iterator();
164 this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N
166 // Get iterator and return it
167 return (Iterator<Product>) iterator;
171 * Gets a Result back from given ResultSet instance
173 * @param resultSet ResultSet instance from SQL driver
174 * @return A typorized Result instance
175 * @throws java.sql.SQLException If any SQL error occurs
178 public Result<? extends Storeable> getResultFromSet (final ResultSet resultSet) throws SQLException {
180 this.getLogger().trace(MessageFormat.format("resultSet={0} - CALLED!", resultSet));
182 // Init result instance
183 Result<? extends Storeable> result = new DatabaseResult();
185 // Reset result set before first row
186 resultSet.beforeFirst();
188 // "Walk" through all entries
189 while (resultSet.next()) {
190 // Get id, title and parent id
191 Long id = resultSet.getLong(PizzaProductDatabaseConstants.COLUMN_ID);
192 String title = resultSet.getString(PizzaProductDatabaseConstants.COLUMN_TITLE);
193 Float price = resultSet.getFloat(PizzaProductDatabaseConstants.COLUMN_PRICE);
194 Long category = resultSet.getLong(PizzaProductDatabaseConstants.COLUMN_CATEGORY);
195 Boolean available = resultSet.getBoolean(PizzaProductDatabaseConstants.COLUMN_AVAILABLE);
198 this.getLogger().debug(MessageFormat.format("id={0},title={1},category={2},available={3}", id, title, category, available));
200 // Instance new object
201 Product product = new PizzaProduct(id, title, price, category, available);
204 this.getLogger().debug(MessageFormat.format("product={0}", product));
211 this.getLogger().trace(MessageFormat.format("result({0})={1} - EXIT!", result.size(), result));
218 * Checks wether the given product title is already used.
220 * @param title Product title
221 * @return Whether the product title is already used
222 * @throws java.io.IOException If any IO error occurs
223 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
224 * @throws java.sql.SQLException If any SQL errors occur
227 public boolean isProductTitleUsed (String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
229 this.getLogger().trace(MessageFormat.format("title={0} - CALLED!", title));
231 // Get search criteria
232 SearchableCriteria criteria = new SearchCriteria();
235 criteria.addCriteria(PizzaProductDatabaseConstants.COLUMN_TITLE, title);
237 // Only one entry is find
238 criteria.setLimit(1);
241 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
244 this.getLogger().debug(MessageFormat.format("result({0}={1}", result, result.size()));
246 // Now check size of the result
247 boolean isFound = (result.size() == 1);
250 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
257 * Adds product to database by given title, price and category id
258 * @param title Product title
259 * @param price Product price
260 * @param category Product category id
261 * @param available Availability of product (selectable by customer)
262 * @throws java.sql.SQLException If any SQL errors occur
265 public void addProduct (final String title, final Float price, final Long category, final Boolean available) throws SQLException, IOException {
267 this.getLogger().trace(MessageFormat.format("title={0},price={1},category={2} - CALLED!", title, price, category));
269 // Title should not be null
272 throw new NullPointerException("title is null");
273 } else if (price == null) {
275 throw new NullPointerException("price is null");
276 } else if (category == null) {
278 throw new NullPointerException("category is null");
279 } else if (available == null) {
281 throw new NullPointerException("available is null");
284 // Clear dataset from previous usage
287 // Add title and parent
288 this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_TITLE, title);
289 this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_PRICE, price);
290 this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_CATEGORY, category);
291 this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_AVAILABLE, available);
293 // Handle this over to the backend
294 Result<? extends Storeable> result = this.doInsertDataSet();
297 this.getLogger().debug(MessageFormat.format("result={0}", result));
300 this.getLogger().trace("EXIT!");
304 * Converts the given map into a Storeable instance, depending on which class implements it. All
305 * keys are being interpreted as class fields/attributes and their respective setters are being searched for. As
306 * this method may fail to find one or access it, this method throws some exception.
308 * @param map Map instance to convert to Storeable
309 * @return An instance of a Storeable implementation
312 public Storeable toStoreable (final Map<String, String> map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
314 this.getLogger().trace("map=" + map + " - CALLED!");
319 throw new NullPointerException("map is null");
320 } else if (map.isEmpty()) {
322 throw new IllegalArgumentException("map is empty.");
326 this.getLogger().debug("Has to handle " + map.size() + " entries");
328 // Get iterator on all entries
329 Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
331 // Init object instance
332 Storeable instance = new PizzaProduct();
335 while (iterator.hasNext()) {
337 Map.Entry<String, String> entry = iterator.next();
340 this.getLogger().debug("entry:" + entry.getKey() + "=" + entry.getValue());
343 instance.setValueFromColumn(entry.getKey(), entry.getValue());
347 this.getLogger().trace("instance=" + instance + " - EXIT!");
354 public String getIdName () {
356 return PizzaProductDatabaseConstants.COLUMN_ID;