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.sql.ResultSet;
21 import java.sql.SQLException;
22 import java.text.MessageFormat;
23 import java.util.Iterator;
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;
39 * Stores and retrieves Contact instances
41 * @author Roland Haeder
43 public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implements ProductFrontend {
47 * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the configured backend is not supported
48 * @throws java.sql.SQLException If any SQL error occurs
50 public PizzaProductDatabaseFrontend () throws UnsupportedDatabaseBackendException, SQLException {
52 this.getLogger().trace("CALLED!"); //NOI18N
55 this.setTableName("products"); //NOI18N
62 * Shuts down the database layer
64 * @throws java.sql.SQLException If an SQL error occurs
65 * @throws java.io.IOException If any IO error occurs
68 public void doShutdown () throws SQLException, IOException {
70 this.getLogger().trace("CALLED!"); //NOI18N
73 this.getBackend().doShutdown();
76 this.getLogger().trace("EXIT!"); //NOI18N
80 * Depending on column, an empty value may be converted to null
82 * @param key Key to check
83 * @param value Value to check
84 * @return Possible previous value or null
87 public Object emptyStringToNull (final String key, final Object value) {
89 this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value));
94 // Is the value empty?
95 if ((value instanceof String) && ("".equals(value))) {
96 // This value may need to be changed
102 this.getLogger().trace(MessageFormat.format("v={0} - EXIT!", v));
109 @SuppressWarnings ("unchecked")
110 public Iterator<Product> getAvailableProducts () throws IOException, BadTokenException, SQLException, CorruptedDatabaseFileException {
112 this.getLogger().trace("CALLED!"); //NOI18N
114 // Instance search criteria
115 SearchableCritera critera = new SearchCriteria();
118 critera.addCriteria(PizzaProductDatabaseConstants.COLUMN_AVAILABLE, true);
121 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
124 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
127 Iterator<?> iterator = result.iterator();
130 this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N
132 // Get iterator and return it
133 return (Iterator<Product>) iterator;
137 * An iterator on all products
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
145 @SuppressWarnings ("unchecked")
146 public Iterator<Product> getAllProducts () throws IOException, BadTokenException, SQLException, CorruptedDatabaseFileException {
148 this.getLogger().trace("CALLED!"); //NOI18N
150 // Instance search criteria
151 SearchableCritera critera = new SearchCriteria();
154 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
157 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
160 Iterator<?> iterator = result.iterator();
163 this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N
165 // Get iterator and return it
166 return (Iterator<Product>) iterator;
170 * Gets a Result back from given ResultSet instance
172 * @param resultSet ResultSet instance from SQL driver
173 * @return A typorized Result instance
174 * @throws java.sql.SQLException If any SQL error occurs
177 public Result<? extends Storeable> getResultFromSet (final ResultSet resultSet) throws SQLException {
179 this.getLogger().trace(MessageFormat.format("resultSet={0} - CALLED!", resultSet));
181 // Init result instance
182 Result<? extends Storeable> result = new DatabaseResult();
184 // Reset result set before first row
185 resultSet.beforeFirst();
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);
197 this.getLogger().debug(MessageFormat.format("id={0},title={1},category={2},available={3}", id, title, category, available));
199 // Instance new object
200 Product product = new PizzaProduct(id, title, price, category, available);
203 this.getLogger().debug(MessageFormat.format("product={0}", product));
210 this.getLogger().trace(MessageFormat.format("result({0})={1} - EXIT!", result.size(), result));
217 * Checks wether the given product title is already used.
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
226 public boolean isProductTitleUsed (String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException {
228 this.getLogger().trace(MessageFormat.format("title={0} - CALLED!", title));
230 // Get search criteria
231 SearchableCritera criteria = new SearchCriteria();
234 criteria.addCriteria(PizzaProductDatabaseConstants.COLUMN_TITLE, title);
236 // Only one entry is find
237 criteria.setLimit(1);
240 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
243 this.getLogger().debug(MessageFormat.format("result({0}={1}", result, result.size()));
245 // Now check size of the result
246 boolean isFound = (result.size() == 1);
249 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
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
264 public void addProduct (final String title, final Float price, final Long category, final Boolean available) throws SQLException, IOException {
266 this.getLogger().trace(MessageFormat.format("title={0},price={1},category={2} - CALLED!", title, price, category));
268 // Title should not be null
271 throw new NullPointerException("title is null");
272 } else if (price == null) {
274 throw new NullPointerException("price is null");
275 } else if (category == null) {
277 throw new NullPointerException("category is null");
278 } else if (available == null) {
280 throw new NullPointerException("available is null");
283 // Clear dataset from previous usage
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);
292 // Handle this over to the backend
293 Result<? extends Storeable> result = this.doInsertDataSet();
296 this.getLogger().debug(MessageFormat.format("result={0}", result));
299 this.getLogger().trace("EXIT!");
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.
307 * @param map Map instance to convert to Storeable
308 * @return An instance of a Storeable implementation
311 public Storeable toStoreable (final Map<String, String> map) {
312 throw new UnsupportedOperationException("Not supported yet: map=" + map);