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 // @todo Nothing is done yet!
101 Result<? extends Storeable> result = this.doInsertDataSet();
104 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
107 this.getLogger().trace("EXIT!"); //NOI18N
111 * Shuts down the database layer
113 * @throws java.sql.SQLException If an SQL error occurs
114 * @throws java.io.IOException If any IO error occurs
117 public void doShutdown () throws SQLException, IOException {
119 this.getLogger().trace("CALLED!"); //NOI18N
122 this.getBackend().doShutdown();
125 this.getLogger().trace("EXIT!"); //NOI18N
129 * Depending on column, an empty value may be converted to null
131 * @param key Key to check
132 * @param value Value to check
133 * @return Possible previous value or null
136 public Object emptyStringToNull (final String key, final Object value) {
138 this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value)); //NOI18N
143 // Is the value empty?
144 if ((value instanceof String) && ("".equals(value))) { //NOI18N
145 // This value may need to be changed
151 this.getLogger().trace(MessageFormat.format("v={0} - EXIT!", v)); //NOI18N
158 * An iterator on all products
160 * @return Iterator on all products
161 * @throws java.io.IOException If any IO error occurs
162 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
163 * @throws java.sql.SQLException If any SQL errors occur
166 @SuppressWarnings ("unchecked")
167 public Iterator<Product> getAllProducts () throws IOException, BadTokenException, SQLException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
169 this.getLogger().trace("CALLED!"); //NOI18N
171 // Instance search criteria
172 SearchableCriteria critera = new SearchCriteria();
175 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
178 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
181 Iterator<?> iterator = result.iterator();
184 this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N
186 // Get iterator and return it
187 return (Iterator<Product>) iterator;
191 @SuppressWarnings ("unchecked")
192 public Iterator<Product> getAvailableProducts () throws IOException, BadTokenException, SQLException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
194 this.getLogger().trace("CALLED!"); //NOI18N
196 // Instance search criteria
197 SearchableCriteria critera = new SearchCriteria();
200 critera.addCriteria(PizzaProductDatabaseConstants.COLUMN_AVAILABLE, true);
203 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
206 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
209 Iterator<?> iterator = result.iterator();
212 this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N
214 // Get iterator and return it
215 return (Iterator<Product>) iterator;
219 public String getIdName () {
221 return PizzaProductDatabaseConstants.COLUMN_ID;
225 * Gets a Result back from given ResultSet instance
227 * @param resultSet ResultSet instance from SQL driver
228 * @return A typorized Result instance
229 * @throws java.sql.SQLException If any SQL error occurs
232 public Result<? extends Storeable> getResultFromSet (final ResultSet resultSet) throws SQLException {
234 this.getLogger().trace(MessageFormat.format("resultSet={0} - CALLED!", resultSet)); //NOI18N
236 // Init result instance
237 Result<? extends Storeable> result = new DatabaseResult();
239 // Reset result set before first row
240 resultSet.beforeFirst();
242 // "Walk" through all entries
243 while (resultSet.next()) {
244 // Get id, title and parent id
245 Long id = resultSet.getLong(PizzaProductDatabaseConstants.COLUMN_ID);
246 String title = resultSet.getString(PizzaProductDatabaseConstants.COLUMN_TITLE);
247 Float price = resultSet.getFloat(PizzaProductDatabaseConstants.COLUMN_PRICE);
248 Long category = resultSet.getLong(PizzaProductDatabaseConstants.COLUMN_CATEGORY);
249 Boolean available = resultSet.getBoolean(PizzaProductDatabaseConstants.COLUMN_AVAILABLE);
252 this.getLogger().debug(MessageFormat.format("id={0},title={1},category={2},available={3}", id, title, category, available)); //NOI18N
254 // Instance new object
255 Product product = new PizzaProduct(id, title, price, category, available);
258 this.getLogger().debug(MessageFormat.format("product={0}", product)); //NOI18N
265 this.getLogger().trace(MessageFormat.format("result({0})={1} - EXIT!", result.size(), result)); //NOI18N
272 public Storeable getStoreableAtRow (final int rowIndex) {
273 throw new UnsupportedOperationException("Not supported yet: rowIndex=" + rowIndex);
277 * Checks wether the given product title is already used.
279 * @param title Product title
280 * @return Whether the product title is already used
281 * @throws java.io.IOException If any IO error occurs
282 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
283 * @throws java.sql.SQLException If any SQL errors occur
286 public boolean isProductTitleUsed (String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
288 this.getLogger().trace(MessageFormat.format("title={0} - CALLED!", title)); //NOI18N
290 // Get search criteria
291 SearchableCriteria criteria = new SearchCriteria();
294 criteria.addCriteria(PizzaProductDatabaseConstants.COLUMN_TITLE, title);
296 // Only one entry is find
297 criteria.setLimit(1);
300 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
303 this.getLogger().debug(MessageFormat.format("result({0}={1}", result, result.size())); //NOI18N
305 // Now check size of the result
306 boolean isFound = (result.size() == 1);
309 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); //NOI18N
316 * Converts the given map into a Storeable instance, depending on which class implements it. All
317 * keys are being interpreted as class fields/attributes and their respective setters are being searched for. As
318 * this method may fail to find one or access it, this method throws some exception.
320 * @param map Map instance to convert to Storeable
321 * @return An instance of a Storeable implementation
324 public Storeable toStoreable (final Map<String, String> map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
326 this.getLogger().trace(MessageFormat.format("map={0} - CALLED!", map)); //NOI18N
331 throw new NullPointerException("map is null"); //NOI18N
332 } else if (map.isEmpty()) {
334 throw new IllegalArgumentException("map is empty."); //NOI18N
338 this.getLogger().debug(MessageFormat.format("Has to handle {0} entries", map.size())); //NOI18N
340 // Get iterator on all entries
341 Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
343 // Init object instance
344 Storeable instance = new PizzaProduct();
347 while (iterator.hasNext()) {
349 Map.Entry<String, String> entry = iterator.next();
352 this.getLogger().debug(MessageFormat.format("entry:{0}={1}", entry.getKey(), entry.getValue())); //NOI18N
355 instance.setValueFromColumn(entry.getKey(), entry.getValue());
359 this.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N