]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java
d491d03102baf3b5049bd5460cb22159e23acdf8
[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 org.mxchange.pizzaapplication.category.product.ProductCategory;
20 import java.io.IOException;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.text.MessageFormat;
24 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.UnsupportedDatabaseBackendException;
33 import org.mxchange.pizzaapplication.category.Category;
34 import org.mxchange.pizzaapplication.database.category.PizzaCategoryDatabaseConstants;
35
36 /**
37  * Stores and retrieves Contact instances
38  *
39  * @author Roland Haeder
40  */
41 public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implements CategoryFrontend {
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          * Adds given category title as new category, parent may be null if not selected.
60          *
61          * @param title Title of category
62          * @param parent Parent id or null if not selected
63          */
64         @Override
65         public void addCategory (final String title, final Integer parent) throws SQLException {
66                 // Trace message
67                 this.getLogger().trace(MessageFormat.format("title={0},parent={1} - CALLED!", title, parent));
68
69                 // Title should not be null
70                 if (title == null) {
71                         // Abort here
72                         throw new NullPointerException("title is null");
73                 }
74
75                 // Clear dataset from previous usage
76                 this.clearDataSet();
77
78                 // Add title and parent
79                 this.addToDataSet(PizzaCategoryDatabaseConstants.COLUMN_TITLE, title);
80                 this.addToDataSet(PizzaCategoryDatabaseConstants.COLUMN_PARENT, parent);
81
82                 // Handle this over to the backend
83                 Result<? extends Storeable> result = this.doInsertDataSet();
84
85                 // Debug message
86                 this.getLogger().debug(MessageFormat.format("result={0}", result));
87
88                 // Trace message
89                 this.getLogger().trace("EXIT!");
90         }
91
92         /**
93          * Shuts down the database layer
94          * @throws java.sql.SQLException If an SQL error occurs
95          * @throws java.io.IOException If any IO error occurs
96          */
97         @Override
98         public void doShutdown () throws SQLException, IOException {
99                 // Trace message
100                 this.getLogger().trace("CALLED!"); //NOI18N
101
102                 // Shutdown backend
103                 this.getBackend().doShutdown();
104
105                 // Trace message
106                 this.getLogger().trace("EXIT!"); //NOI18N
107         }
108
109         /**
110          * Depending on column, an empty value may be converted to null
111          *
112          * @param key Key to check
113          * @param value Value to check
114          * @return Possible previous value or null
115          */
116         @Override
117         public Object emptyStringToNull (final String key, final Object value) {
118                 // Trace message
119                 this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value));
120
121                 // Copy value
122                 Object v = value;
123
124                 // Is the value empty?
125                 if ((value instanceof String) && ("".equals(value))) {
126                         // This value may need to be changed
127                         switch (key) {
128                                 case PizzaCategoryDatabaseConstants.COLUMN_PARENT: // Convert this
129                                         v = null;
130                                         break;
131                         }
132                 }
133
134                 // Trace message
135                 this.getLogger().trace(MessageFormat.format("v={0} - EXIT!", v));
136
137                 // Return it
138                 return v;
139         }
140
141         @Override
142         @SuppressWarnings ("unchecked")
143         public Iterator<Category> getCategories () throws IOException, BadTokenException, SQLException {
144                 // Trace message
145                 this.getLogger().trace("CALLED!"); //NOI18N
146
147                 // Instance search criteria
148                 SearchableCritera critera = new SearchCriteria();
149
150                 // Run the query
151                 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
152
153                 // Debug message
154                 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
155
156                 // Get iterator
157                 Iterator<?> iterator = result.iterator();
158
159                 // Trace message
160                 this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N
161
162                 // Get iterator and return it
163                 return (Iterator<Category>) iterator;
164         }
165
166         /**
167          * Gets a Result back from given ResultSet instance
168          *
169          * @param resultSet ResultSet instance from SQL driver
170          * @return A typorized Result instance
171          * @throws java.sql.SQLException If any SQL error occurs
172          */
173         @Override
174         public Result<? extends Storeable> getResultFromSet (final ResultSet resultSet) throws SQLException {
175                 // Trace message
176                 this.getLogger().trace(MessageFormat.format("resultSet={0} - CALLED!", resultSet));
177
178                 // Init result instance
179                 Result<? extends Storeable> result = new DatabaseResult();
180
181                 // Reset result set before first row
182                 resultSet.beforeFirst();
183
184                 // "Walk" through all entries
185                 while (resultSet.next()) {
186                         // Get id, title and parent id
187                         Integer id = resultSet.getInt(PizzaCategoryDatabaseConstants.COLUMN_ID);
188                         String title = resultSet.getString(PizzaCategoryDatabaseConstants.COLUMN_TITLE);
189                         Integer parent = resultSet.getInt(PizzaCategoryDatabaseConstants.COLUMN_PARENT);
190
191                         // Debug message
192                         this.getLogger().debug(MessageFormat.format("id={0},title={1},parent={2}", id, title, parent));
193
194                         // Instance new object
195                         Category category = new ProductCategory(id, title, parent);
196
197                         // Debug log
198                         this.getLogger().debug(MessageFormat.format("category={0}", category));
199
200                         // Add it to result
201                         result.add(category);
202                 }
203
204                 // Trace message
205                 this.getLogger().trace(MessageFormat.format("result({0})={1} - EXIT!", result.size(), result));
206
207                 // Return result
208                 return result;
209         }
210
211         /**
212          * Checks if given category title is already used
213          *
214          * @param title Title to check
215          * @return Whether the title has been used
216          */
217         @Override
218         public boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException {
219                 // Trace message
220                 this.getLogger().trace(MessageFormat.format("title={0} - CALLED!", title));
221                 
222                 // Get search criteria
223                 SearchableCritera criteria = new SearchCriteria();
224                 
225                 // Add criteria
226                 criteria.addCriteria(PizzaCategoryDatabaseConstants.COLUMN_TITLE, title);
227                 
228                 // Only one entry is find
229                 criteria.setLimit(1);
230                 
231                 // Run it on backend
232                 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
233                 
234                 // Debug log
235                 this.getLogger().debug(MessageFormat.format("result({0}={1}", result, result.size()));
236                 
237                 // Now check size of the result
238                 boolean isFound = (result.size() == 1);
239                 
240                 // Trace message
241                 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
242                 
243                 // Return it
244                 return isFound;
245         }
246
247         /**
248          * Parses given line from database backend into a Storeable instance. Please
249          * note that not all backends need this.
250          *
251          * @param line Line from database backend
252          * @return A Storeable instance
253          */
254         @Override
255         public Storeable parseLineToStoreable (final String line) throws BadTokenException {
256                 // Trace message
257                 this.getLogger().trace(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N
258
259                 // Call inner method
260                 Category category = this.parseLineToCategory(line);
261
262                 // Debug message
263                 this.getLogger().trace(MessageFormat.format("category={0} - EXIT!", category)); //NOI18N
264
265                 // Return it
266                 return category;
267         }
268
269         /**
270          * Parses given line to a Category instance
271          *
272          * @param line Raw, decoded line from a file-based backend
273          * @return A Category instance from given line
274          */
275         private Category parseLineToCategory (final String line) {
276                 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N
277         }
278 }