]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java
Replace multiple-added strings/objects into MessageFormat.format() call to reduce...
[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.lang.reflect.InvocationTargetException;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.text.MessageFormat;
24 import java.util.Iterator;
25 import java.util.Map;
26 import org.mxchange.jcore.criteria.searchable.SearchCriteria;
27 import org.mxchange.jcore.criteria.searchable.SearchableCriteria;
28 import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend;
29 import org.mxchange.jcore.database.result.DatabaseResult;
30 import org.mxchange.jcore.database.result.Result;
31 import org.mxchange.jcore.database.storage.Storeable;
32 import org.mxchange.jcore.exceptions.BadTokenException;
33 import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
34 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
35 import org.mxchange.pizzaapplication.category.Category;
36 import org.mxchange.pizzaapplication.category.product.ProductCategory;
37 import org.mxchange.pizzaapplication.database.category.PizzaCategoryDatabaseConstants;
38 import org.mxchange.pizzaapplication.product.Product;
39
40 /**
41  * Stores and retrieves Contact instances
42  *
43  * @author Roland Haeder
44  */
45 public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implements CategoryFrontend {
46         /**
47          * Default constrcutor
48          * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the configured backend is not supported
49          * @throws java.sql.SQLException If any SQL error occurs
50          */
51         public PizzaCategoryDatabaseFrontend () throws UnsupportedDatabaseBackendException, SQLException {
52                 // Trace message
53                 this.getLogger().trace("CALLED!"); //NOI18N
54
55                 // Set "table" name
56                 this.setTableName("category"); //NOI18N
57
58                 // Initalize backend
59                 this.initBackend();
60         }
61
62         /**
63          * Adds given category title as new category, parent may be null if not selected.
64          *
65          * @param title Title of category
66          * @param parent Parent id or null if not selected
67          */
68         @Override
69         public void addCategory (final String title, final Integer parent) throws SQLException, IOException {
70                 // Trace message
71                 this.getLogger().trace(MessageFormat.format("title={0},parent={1} - CALLED!", title, parent));
72
73                 // Title should not be null
74                 if (title == null) {
75                         // Abort here
76                         throw new NullPointerException("title is null");
77                 }
78
79                 // Clear dataset from previous usage
80                 this.clearDataSet();
81
82                 // Add title and parent
83                 this.addToDataSet(PizzaCategoryDatabaseConstants.COLUMN_TITLE, title);
84                 this.addToDataSet(PizzaCategoryDatabaseConstants.COLUMN_PARENT, parent);
85
86                 // Handle this over to the backend
87                 Result<? extends Storeable> result = this.doInsertDataSet();
88
89                 // Debug message
90                 this.getLogger().debug(MessageFormat.format("result={0}", result));
91
92                 // Trace message
93                 this.getLogger().trace("EXIT!");
94         }
95
96         /**
97          * Shuts down the database layer
98          * @throws java.sql.SQLException If an SQL error occurs
99          * @throws java.io.IOException If any IO error occurs
100          */
101         @Override
102         public void doShutdown () throws SQLException, IOException {
103                 // Trace message
104                 this.getLogger().trace("CALLED!"); //NOI18N
105
106                 // Shutdown backend
107                 this.getBackend().doShutdown();
108
109                 // Trace message
110                 this.getLogger().trace("EXIT!"); //NOI18N
111         }
112
113         /**
114          * Depending on column, an empty value may be converted to null
115          *
116          * @param key Key to check
117          * @param value Value to check
118          * @return Possible previous value or null
119          */
120         @Override
121         public Object emptyStringToNull (final String key, final Object value) {
122                 // Trace message
123                 this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value));
124
125                 // Copy value
126                 Object v = value;
127
128                 // Is the value empty?
129                 if ((value instanceof String) && ("".equals(value))) {
130                         // This value may need to be changed
131                         switch (key) {
132                                 case PizzaCategoryDatabaseConstants.COLUMN_PARENT: // Convert this
133                                         v = null;
134                                         break;
135                         }
136                 }
137
138                 // Trace message
139                 this.getLogger().trace(MessageFormat.format("v={0} - EXIT!", v));
140
141                 // Return it
142                 return v;
143         }
144
145         @Override
146         @SuppressWarnings ("unchecked")
147         public Iterator<Category> getCategories () throws IOException, BadTokenException, SQLException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
148                 // Trace message
149                 this.getLogger().trace("CALLED!"); //NOI18N
150
151                 // Instance search criteria
152                 SearchableCriteria critera = new SearchCriteria();
153
154                 // Run the query
155                 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
156
157                 // Debug message
158                 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
159
160                 // Get iterator
161                 Iterator<?> iterator = result.iterator();
162
163                 // Trace message
164                 this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N
165
166                 // Get iterator and return it
167                 return (Iterator<Category>) iterator;
168         }
169
170         /**
171          * Gets a Result back from given ResultSet instance
172          *
173          * @param resultSet ResultSet instance from SQL driver
174          * @return A typorized Result instance
175          * @throws java.sql.SQLException If any SQL error occurs
176          */
177         @Override
178         public Result<? extends Storeable> getResultFromSet (final ResultSet resultSet) throws SQLException {
179                 // Trace message
180                 this.getLogger().trace(MessageFormat.format("resultSet={0} - CALLED!", resultSet));
181
182                 // Init result instance
183                 Result<? extends Storeable> result = new DatabaseResult();
184
185                 // Reset result set before first row
186                 resultSet.beforeFirst();
187
188                 // "Walk" through all entries
189                 while (resultSet.next()) {
190                         // Get id, title and parent id
191                         Long id = resultSet.getLong(PizzaCategoryDatabaseConstants.COLUMN_ID);
192                         String title = resultSet.getString(PizzaCategoryDatabaseConstants.COLUMN_TITLE);
193                         Long parent = resultSet.getLong(PizzaCategoryDatabaseConstants.COLUMN_PARENT);
194
195                         // Debug message
196                         this.getLogger().debug(MessageFormat.format("id={0},title={1},parent={2}", id, title, parent));
197
198                         // Instance new object
199                         Category category = new ProductCategory(id, title, parent);
200
201                         // Debug log
202                         this.getLogger().debug(MessageFormat.format("category={0}", category));
203
204                         // Add it to result
205                         result.add(category);
206                 }
207
208                 // Trace message
209                 this.getLogger().trace(MessageFormat.format("result({0})={1} - EXIT!", result.size(), result));
210
211                 // Return result
212                 return result;
213         }
214
215         /**
216          * Checks if given category title is already used
217          *
218          * @param title Title to check
219          * @return Whether the title has been used
220          */
221         @Override
222         public boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
223                 // Trace message
224                 this.getLogger().trace(MessageFormat.format("title={0} - CALLED!", title));
225
226                 // Get search criteria
227                 SearchableCriteria criteria = new SearchCriteria();
228
229                 // Add criteria
230                 criteria.addCriteria(PizzaCategoryDatabaseConstants.COLUMN_TITLE, title);
231
232                 // Only one entry is find
233                 criteria.setLimit(1);
234
235                 // Run it on backend
236                 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
237
238                 // Debug log
239                 this.getLogger().debug(MessageFormat.format("result({0})={1}", result, result.size()));
240
241                 // Now check size of the result
242                 boolean isFound = (result.size() == 1);
243
244                 // Trace message
245                 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
246
247                 // Return it
248                 return isFound;
249         }
250
251         /**
252          * Converts the given map into a Storeable instance, depending on which class implements it. All
253          * keys are being interpreted as class fields/attributes and their respective setters are being searched for. As
254          * this method may fail to find one or access it, this method throws some exception.
255          *
256          * @param map Map instance to convert to Storeable
257          * @return An instance of a Storeable implementation
258          */
259         @Override
260         public Storeable toStoreable (final Map<String, String> map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
261                 // Trace message
262                 this.getLogger().trace(MessageFormat.format("map={0} - CALLED!", map));
263
264                 // Is map null?
265                 if (map == null) {
266                         // Is null
267                         throw new NullPointerException("map is null");
268                 } else if (map.isEmpty()) {
269                         // Map is empty
270                         throw new IllegalArgumentException("map is empty.");
271                 }
272
273                 // Debug message
274                 this.getLogger().debug(MessageFormat.format("Has to handle {0} entries", map.size()));
275
276                 // Get iterator on all entries
277                 Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
278
279                 // Init object instance
280                 Storeable instance = new ProductCategory();
281
282                 // Iterate over all
283                 while (iterator.hasNext()) {
284                         // Get next entry
285                         Map.Entry<String, String> entry = iterator.next();
286
287                         // Debug message
288                         this.getLogger().debug(MessageFormat.format("entry:{0}={1}", entry.getKey(), entry.getValue()));
289
290                         // Try to set value
291                         instance.setValueFromColumn(entry.getKey(), entry.getValue());
292                 }
293
294                 // Trace message
295                 this.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance));
296
297                 // Return it
298                 return instance;
299         }
300
301         @Override
302         public String getIdName () {
303                 // Return column id
304                 return PizzaCategoryDatabaseConstants.COLUMN_ID;
305         }
306
307         @Override
308         public Category getCategory (final Product product) throws IOException, BadTokenException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
309                 // Trace message
310                 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product));
311
312                 // product must not be null
313                 if (product == null) {
314                         // Abort here
315                         throw new NullPointerException("product is null");
316                 }
317
318                 // Get category id from it
319                 Long id = product.getCategory();
320
321                 // Debug message
322                 this.getLogger().debug(MessageFormat.format("id={0}", id));
323
324                 // It should be >0 here
325                 assert(id > 0) : MessageFormat.format("id={0} must be larger zero", id);
326
327                 // Then construct a search instance
328                 SearchableCriteria criteria = new SearchCriteria();
329
330                 // Add id to it
331                 criteria.addCriteria(PizzaCategoryDatabaseConstants.COLUMN_ID, id);
332
333                 // Only one entry is find
334                 criteria.setLimit(1);
335
336                 // Run it on backend
337                 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
338
339                 // Debug log
340                 this.getLogger().debug(MessageFormat.format("result({0})={1}", result, result.size()));
341
342                 // Init category instance
343                 Category category = null;
344
345                 // Is there one entry?
346                 if (result.hasNext()) {
347                         // Read result from it
348                         Storeable storeable = result.next();
349
350                         // Debug message
351                         this.getLogger().debug(MessageFormat.format("storeable={0}", storeable));
352
353                         // Is it instance of Category?
354                         if (storeable instanceof Category) {
355                                 // Then cast it
356                                 category = (Category) storeable;
357                         }
358                 }
359
360                 // Trace message
361                 this.getLogger().trace(MessageFormat.format("category={0} - EXIT!", category));
362
363                 // Return it
364                 return category;
365         }
366 }