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 * Adds product to database by given title, price and category id
64 * @param title Product title
65 * @param price Product price
66 * @param category Product category id
67 * @param available Availability of product (selectable by customer)
68 * @throws java.sql.SQLException If any SQL errors occur
71 public void addProduct (final String title, final Float price, final Long category, final Boolean available) throws SQLException, IOException {
73 this.getLogger().trace(MessageFormat.format("title={0},price={1},category={2} - CALLED!", title, price, category)); //NOI18N
75 // Title should not be null
78 throw new NullPointerException("title is null"); //NOI18N
79 } else if (price == null) {
81 throw new NullPointerException("price is null"); //NOI18N
82 } else if (category == null) {
84 throw new NullPointerException("category is null"); //NOI18N
85 } else if (available == null) {
87 throw new NullPointerException("available is null"); //NOI18N
90 // Clear dataset from previous usage
93 // Add title and parent
94 this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_TITLE, title);
95 this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_PRICE, price);
96 this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_CATEGORY, category);
97 this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_AVAILABLE, available);
99 // Handle this over to the backend
100 Result<? extends Storeable> result = this.doInsertDataSet();
103 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
106 this.getLogger().trace("EXIT!"); //NOI18N
110 * Shuts down the database layer
112 * @throws java.sql.SQLException If an SQL error occurs
113 * @throws java.io.IOException If any IO error occurs
116 public void doShutdown () throws SQLException, IOException {
118 this.getLogger().trace("CALLED!"); //NOI18N
121 this.getBackend().doShutdown();
124 this.getLogger().trace("EXIT!"); //NOI18N
128 * Depending on column, an empty value may be converted to null
130 * @param key Key to check
131 * @param value Value to check
132 * @return Possible previous value or null
135 public Object emptyStringToNull (final String key, final Object value) {
137 this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value)); //NOI18N
142 // Is the value empty?
143 if ((value instanceof String) && ("".equals(value))) { //NOI18N
144 // This value may need to be changed
150 this.getLogger().trace(MessageFormat.format("v={0} - EXIT!", v)); //NOI18N
157 * An iterator on all products
159 * @return Iterator on all products
160 * @throws java.io.IOException If any IO error occurs
161 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
162 * @throws java.sql.SQLException If any SQL errors occur
165 @SuppressWarnings ("unchecked")
166 public Iterator<Product> getAllProducts () throws IOException, BadTokenException, SQLException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
168 this.getLogger().trace("CALLED!"); //NOI18N
170 // Instance search criteria
171 SearchableCriteria critera = new SearchCriteria();
174 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
177 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
180 Iterator<?> iterator = result.iterator();
183 this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N
185 // Get iterator and return it
186 return (Iterator<Product>) iterator;
190 @SuppressWarnings ("unchecked")
191 public Iterator<Product> getAvailableProducts () throws IOException, BadTokenException, SQLException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
193 this.getLogger().trace("CALLED!"); //NOI18N
195 // Instance search criteria
196 SearchableCriteria critera = new SearchCriteria();
199 critera.addCriteria(PizzaProductDatabaseConstants.COLUMN_AVAILABLE, true);
202 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
205 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
208 Iterator<?> iterator = result.iterator();
211 this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N
213 // Get iterator and return it
214 return (Iterator<Product>) iterator;
218 public String getIdName () {
220 return PizzaProductDatabaseConstants.COLUMN_ID;
224 * Gets a Result back from given ResultSet instance
226 * @param resultSet ResultSet instance from SQL driver
227 * @return A typorized Result instance
228 * @throws java.sql.SQLException If any SQL error occurs
231 public Result<? extends Storeable> getResultFromSet (final ResultSet resultSet) throws SQLException {
233 this.getLogger().trace(MessageFormat.format("resultSet={0} - CALLED!", resultSet)); //NOI18N
235 // Init result instance
236 Result<? extends Storeable> result = new DatabaseResult();
238 // Reset result set before first row
239 resultSet.beforeFirst();
241 // "Walk" through all entries
242 while (resultSet.next()) {
243 // Get id, title and parent id
244 Long id = resultSet.getLong(PizzaProductDatabaseConstants.COLUMN_ID);
245 String title = resultSet.getString(PizzaProductDatabaseConstants.COLUMN_TITLE);
246 Float price = resultSet.getFloat(PizzaProductDatabaseConstants.COLUMN_PRICE);
247 Long category = resultSet.getLong(PizzaProductDatabaseConstants.COLUMN_CATEGORY);
248 Boolean available = resultSet.getBoolean(PizzaProductDatabaseConstants.COLUMN_AVAILABLE);
251 this.getLogger().debug(MessageFormat.format("id={0},title={1},category={2},available={3}", id, title, category, available)); //NOI18N
253 // Instance new object
254 Product product = new PizzaProduct(id, title, price, category, available);
257 this.getLogger().debug(MessageFormat.format("product={0}", product)); //NOI18N
264 this.getLogger().trace(MessageFormat.format("result({0})={1} - EXIT!", result.size(), result)); //NOI18N
271 * Checks wether the given product title is already used.
273 * @param title Product title
274 * @return Whether the product title is already used
275 * @throws java.io.IOException If any IO error occurs
276 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
277 * @throws java.sql.SQLException If any SQL errors occur
280 public boolean isProductTitleUsed (String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
282 this.getLogger().trace(MessageFormat.format("title={0} - CALLED!", title)); //NOI18N
284 // Get search criteria
285 SearchableCriteria criteria = new SearchCriteria();
288 criteria.addCriteria(PizzaProductDatabaseConstants.COLUMN_TITLE, title);
290 // Only one entry is find
291 criteria.setLimit(1);
294 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
297 this.getLogger().debug(MessageFormat.format("result({0}={1}", result, result.size())); //NOI18N
299 // Now check size of the result
300 boolean isFound = (result.size() == 1);
303 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); //NOI18N
310 * Converts the given map into a Storeable instance, depending on which class implements it. All
311 * keys are being interpreted as class fields/attributes and their respective setters are being searched for. As
312 * this method may fail to find one or access it, this method throws some exception.
314 * @param map Map instance to convert to Storeable
315 * @return An instance of a Storeable implementation
318 public Storeable toStoreable (final Map<String, String> map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
320 this.getLogger().trace(MessageFormat.format("map={0} - CALLED!", map)); //NOI18N
325 throw new NullPointerException("map is null"); //NOI18N
326 } else if (map.isEmpty()) {
328 throw new IllegalArgumentException("map is empty."); //NOI18N
332 this.getLogger().debug(MessageFormat.format("Has to handle {0} entries", map.size())); //NOI18N
334 // Get iterator on all entries
335 Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
337 // Init object instance
338 Storeable instance = new PizzaProduct();
341 while (iterator.hasNext()) {
343 Map.Entry<String, String> entry = iterator.next();
346 this.getLogger().debug(MessageFormat.format("entry:{0}={1}", entry.getKey(), entry.getValue())); //NOI18N
349 instance.setValueFromColumn(entry.getKey(), entry.getValue());
353 this.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N