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.category;
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.category.Category;
33 import org.mxchange.pizzaapplication.database.category.PizzaCategoryDatabaseConstants;
36 * Stores and retrieves Contact instances
38 * @author Roland Haeder
40 public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implements CategoryFrontend {
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 PizzaCategoryDatabaseFrontend () throws UnsupportedDatabaseBackendException, SQLException {
49 this.getLogger().trace("CALLED!"); //NOI18N
52 this.setTableName("category"); //NOI18N
59 * Shuts down the database layer
60 * @throws java.sql.SQLException If an SQL error occurs
61 * @throws java.io.IOException If any IO error occurs
64 public void doShutdown () throws SQLException, IOException {
66 this.getLogger().trace("CALLED!"); //NOI18N
69 this.getBackend().doShutdown();
72 this.getLogger().trace("EXIT!"); //NOI18N
76 @SuppressWarnings ("unchecked")
77 public Iterator<Category> getCategories () throws IOException, BadTokenException, SQLException {
79 this.getLogger().trace("CALLED!"); //NOI18N
81 // Instance search criteria
82 SearchableCritera critera = new SearchCriteria();
85 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
88 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
91 Iterator<?> iterator = result.iterator();
94 this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N
96 // Get iterator and return it
97 return (Iterator<Category>) iterator;
101 * Gets a Result back from given ResultSet instance
103 * @param resultSet ResultSet instance from SQL driver
104 * @return A typorized Result instance
105 * @throws java.sql.SQLException If any SQL error occurs
108 public Result<? extends Storeable> getResultFromSet (final ResultSet resultSet) throws SQLException {
110 this.getLogger().trace(MessageFormat.format("resultSet={0} - CALLED!", resultSet));
112 // Init result instance
113 Result<? extends Storeable> result = new DatabaseResult();
115 // Reset result set before first row
116 resultSet.beforeFirst();
118 // "Walk" through all entries
119 while (resultSet.next()) {
120 // Unwrap whole object
121 Category category = resultSet.unwrap(Category.class);
124 this.getLogger().debug(MessageFormat.format("category={0}", category));
127 result.add(category);
131 this.getLogger().trace(MessageFormat.format("result({0})={1} - EXIT!", result.size(), result));
138 * Parses given line from database backend into a Storeable instance. Please
139 * note that not all backends need this.
141 * @param line Line from database backend
142 * @return A Storeable instance
145 public Storeable parseLineToStoreable (final String line) throws BadTokenException {
147 this.getLogger().trace(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N
150 Category category = this.parseLineToCategory(line);
153 this.getLogger().trace(MessageFormat.format("category={0} - EXIT!", category)); //NOI18N
160 * Parses given line to a Category instance
162 * @param line Raw, decoded line from a file-based backend
163 * @return A Category instance from given line
165 private Category parseLineToCategory (final String line) {
166 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N
170 * Checks if given category title is already used
172 * @param title Title to check
173 * @return Whether the title has been used
176 public boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException {
178 this.getLogger().trace(MessageFormat.format("title={0} - CALLED!", title));
180 // Get search criteria
181 SearchableCritera criteria = new SearchCriteria();
184 criteria.addCriteria(PizzaCategoryDatabaseConstants.COLUMN_TITLE, title);
186 // Only one entry is find
187 criteria.setLimit(1);
190 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
193 this.getLogger().debug(MessageFormat.format("result({0}={1}", result, result.size()));
195 // Now check size of the result
196 boolean isFound = (result.size() == 1);
199 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));