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;
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;
36 * Stores and retrieves Contact instances
38 * @author Roland Haeder
40 public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implements ProductFrontend {
44 * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the configured backend is not supported
45 * @throws java.sql.SQLException If any SQL error occurs
47 public PizzaProductDatabaseFrontend () throws UnsupportedDatabaseBackendException, SQLException {
49 this.getLogger().trace("CALLED!"); //NOI18N
52 this.setTableName("products"); //NOI18N
59 * Shuts down the database layer
61 * @throws java.sql.SQLException If an SQL error occurs
62 * @throws java.io.IOException If any IO error occurs
65 public void doShutdown () throws SQLException, IOException {
67 this.getLogger().trace("CALLED!"); //NOI18N
70 this.getBackend().doShutdown();
73 this.getLogger().trace("EXIT!"); //NOI18N
77 * Depending on column, an empty value may be converted to null
79 * @param key Key to check
80 * @param value Value to check
81 * @return Possible previous value or null
84 public Object emptyStringToNull (final String key, final Object value) {
86 this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value));
91 // Is the value empty?
92 if ((value instanceof String) && ("".equals(value))) {
93 // This value may need to be changed
99 this.getLogger().trace(MessageFormat.format("v={0} - EXIT!", v));
106 @SuppressWarnings ("unchecked")
107 public Iterator<Product> getProducts () throws IOException, BadTokenException, SQLException {
109 this.getLogger().trace("CALLED!"); //NOI18N
111 // Instance search criteria
112 SearchableCritera critera = new SearchCriteria();
115 critera.addCriteria(PizzaProductDatabaseConstants.COLUMN_AVAILABLE, true);
118 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
121 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
124 Iterator<?> iterator = result.iterator();
127 this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N
129 // Get iterator and return it
130 return (Iterator<Product>) iterator;
134 * Gets a Result back from given ResultSet instance
136 * @param resultSet ResultSet instance from SQL driver
137 * @return A typorized Result instance
138 * @throws java.sql.SQLException If any SQL error occurs
141 public Result<? extends Storeable> getResultFromSet (final ResultSet resultSet) throws SQLException {
143 this.getLogger().trace(MessageFormat.format("resultSet={0} - CALLED!", resultSet));
145 // Init result instance
146 Result<? extends Storeable> result = new DatabaseResult();
148 // Reset result set before first row
149 resultSet.beforeFirst();
151 // "Walk" through all entries
152 while (resultSet.next()) {
153 // Unwrap whole object
154 Product product = resultSet.unwrap(Product.class);
157 this.getLogger().debug(MessageFormat.format("product={0}", product));
164 this.getLogger().trace(MessageFormat.format("result({0})={1} - EXIT!", result.size(), result));
171 * Parses given line from database backend into a Storeable instance. Please
172 * note that not all backends need this.
174 * @param line Line from database backend
175 * @return A Storeable instance
178 public Storeable parseLineToStoreable (final String line) throws BadTokenException {
180 this.getLogger().trace(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N
183 Product product = this.parseLineToProduct(line);
186 this.getLogger().trace(MessageFormat.format("product={0} - EXIT!", product)); //NOI18N
193 * Parses given line to a Product instance
196 * @return A Product instance from given line
198 private Product parseLineToProduct (final String line) {
199 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N