]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java
36ecbbe8fae0d48a9c6259b33342de74e6b33951
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / database / frontend / category / PizzaCategoryDatabaseFrontend.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.pizzaapplication.database.frontend.category;
18
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;
34
35 /**
36  * Stores and retrieves Contact instances
37  *
38  * @author Roland Haeder
39  */
40 public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implements CategoryFrontend {
41
42         /**
43          * Default constrcutor
44          * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the configured backend is not supported
45          * @throws java.sql.SQLException If any SQL error occurs
46          */
47         public PizzaCategoryDatabaseFrontend () throws UnsupportedDatabaseBackendException, SQLException {
48                 // Trace message
49                 this.getLogger().trace("CALLED!"); //NOI18N
50
51                 // Set "table" name
52                 this.setTableName("category"); //NOI18N
53
54                 // Initalize backend
55                 this.initBackend();
56         }
57
58         /**
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
62          */
63         @Override
64         public void doShutdown () throws SQLException, IOException {
65                 // Trace message
66                 this.getLogger().trace("CALLED!"); //NOI18N
67
68                 // Shutdown backend
69                 this.getBackend().doShutdown();
70
71                 // Trace message
72                 this.getLogger().trace("EXIT!"); //NOI18N
73         }
74
75         @Override
76         @SuppressWarnings ("unchecked")
77         public Iterator<Category> getCategories () throws IOException, BadTokenException, SQLException {
78                 // Trace message
79                 this.getLogger().trace("CALLED!"); //NOI18N
80
81                 // Instance search criteria
82                 SearchableCritera critera = new SearchCriteria();
83
84                 // Run the query
85                 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
86
87                 // Debug message
88                 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
89
90                 // Get iterator
91                 Iterator<?> iterator = result.iterator();
92
93                 // Trace message
94                 this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N
95
96                 // Get iterator and return it
97                 return (Iterator<Category>) iterator;
98         }
99
100         /**
101          * Gets a Result back from given ResultSet instance
102          *
103          * @param resultSet ResultSet instance from SQL driver
104          * @return A typorized Result instance
105          * @throws java.sql.SQLException If any SQL error occurs
106          */
107         @Override
108         public Result<? extends Storeable> getResultFromSet (final ResultSet resultSet) throws SQLException {
109                 // Trace message
110                 this.getLogger().trace(MessageFormat.format("resultSet={0} - CALLED!", resultSet));
111
112                 // Init result instance
113                 Result<? extends Storeable> result = new DatabaseResult();
114
115                 // Reset result set before first row
116                 resultSet.beforeFirst();
117
118                 // "Walk" through all entries
119                 while (resultSet.next()) {
120                         // Unwrap whole object
121                         Category category = resultSet.unwrap(Category.class);
122
123                         // Debug log
124                         this.getLogger().debug(MessageFormat.format("category={0}", category));
125
126                         // Add it to result
127                         result.add(category);
128                 }
129
130                 // Trace message
131                 this.getLogger().trace(MessageFormat.format("result({0})={1} - EXIT!", result.size(), result));
132
133                 // Return result
134                 return result;
135         }
136
137         /**
138          * Parses given line from database backend into a Storeable instance. Please
139          * note that not all backends need this.
140          *
141          * @param line Line from database backend
142          * @return A Storeable instance
143          */
144         @Override
145         public Storeable parseLineToStoreable (final String line) throws BadTokenException {
146                 // Trace message
147                 this.getLogger().trace(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N
148
149                 // Call inner method
150                 Category category = this.parseLineToCategory(line);
151
152                 // Debug message
153                 this.getLogger().trace(MessageFormat.format("category={0} - EXIT!", category)); //NOI18N
154
155                 // Return it
156                 return category;
157         }
158
159         /**
160          * Parses given line to a Category instance
161          *
162          * @param line Raw, decoded line from a file-based backend
163          * @return A Category instance from given line
164          */
165         private Category parseLineToCategory (final String line) {
166                 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N
167         }
168
169         /**
170          * Checks if given category title is already used
171          *
172          * @param title Title to check
173          * @return Whether the title has been used
174          */
175         @Override
176         public boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException {
177                 // Trace message
178                 this.getLogger().trace(MessageFormat.format("title={0} - CALLED!", title));
179
180                 // Get search criteria
181                 SearchableCritera criteria = new SearchCriteria();
182
183                 // Add criteria
184                 criteria.addCriteria(PizzaCategoryDatabaseConstants.COLUMN_TITLE, title);
185
186                 // Only one entry is find
187                 criteria.setLimit(1);
188
189                 // Run it on backend
190                 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
191
192                 // Debug log
193                 this.getLogger().debug(MessageFormat.format("result({0}={1}", result, result.size()));
194
195                 // Now check size of the result
196                 boolean isFound = (result.size() == 1);
197
198                 // Trace message
199                 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
200
201                 // Return it
202                 return isFound;
203         }
204 }