]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java
Continued with project:
[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
34 /**
35  * Stores and retrieves Contact instances
36  *
37  * @author Roland Haeder
38  */
39 public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implements CategoryFrontend {
40
41         /**
42          * Default constrcutor
43          * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the configured backend is not supported
44          * @throws java.sql.SQLException If any SQL error occurs
45          */
46         public PizzaCategoryDatabaseFrontend () throws UnsupportedDatabaseBackendException, SQLException {
47                 // Trace message
48                 this.getLogger().trace("CALLED!"); //NOI18N
49
50                 // Set "table" name
51                 this.setTableName("category"); //NOI18N
52
53                 // Initalize backend
54                 this.initBackend();
55         }
56
57         /**
58          * Shuts down the database layer
59          * @throws java.sql.SQLException If an SQL error occurs
60          * @throws java.io.IOException If any IO error occurs
61          */
62         @Override
63         public void doShutdown () throws SQLException, IOException {
64                 // Trace message
65                 this.getLogger().trace("CALLED!"); //NOI18N
66
67                 // Shutdown backend
68                 this.getBackend().doShutdown();
69
70                 // Trace message
71                 this.getLogger().trace("EXIT!"); //NOI18N
72         }
73
74         @Override
75         @SuppressWarnings ("unchecked")
76         public Iterator<Category> getCategories () throws IOException, BadTokenException, SQLException {
77                 // Trace message
78                 this.getLogger().trace("CALLED!"); //NOI18N
79
80                 // Instance search criteria
81                 SearchableCritera critera = new SearchCriteria();
82
83                 // Run the query
84                 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
85
86                 // Debug message
87                 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
88
89                 // Get iterator
90                 Iterator<?> iterator = result.iterator();
91
92                 // Trace message
93                 this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N
94
95                 // Get iterator and return it
96                 return (Iterator<Category>) iterator;
97         }
98
99         /**
100          * Gets a Result back from given ResultSet instance
101          *
102          * @param resultSet ResultSet instance from SQL driver
103          * @return A typorized Result instance
104          * @throws java.sql.SQLException If any SQL error occurs
105          */
106         @Override
107         public Result<? extends Storeable> getResultFromSet (final ResultSet resultSet) throws SQLException {
108                 // Trace message
109                 this.getLogger().trace(MessageFormat.format("resultSet={0} - CALLED!", resultSet));
110
111                 // Init result instance
112                 Result<? extends Storeable> result = new DatabaseResult();
113
114                 // Reset result set before first row
115                 resultSet.beforeFirst();
116
117                 // "Walk" through all entries
118                 while (resultSet.next()) {
119                         // Unwrap whole object
120                         Category category = resultSet.unwrap(Category.class);
121
122                         // Debug log
123                         this.getLogger().debug(MessageFormat.format("category={0}", category));
124
125                         // Add it to result
126                         result.add(category);
127                 }
128
129                 // Trace message
130                 this.getLogger().trace(MessageFormat.format("result({0})={1} - EXIT!", result.size(), result));
131
132                 // Return result
133                 return result;
134         }
135
136         /**
137          * Parses given line from database backend into a Storeable instance. Please
138          * note that not all backends need this.
139          *
140          * @param line Line from database backend
141          * @return A Storeable instance
142          */
143         @Override
144         public Storeable parseLineToStoreable (final String line) throws BadTokenException {
145                 // Trace message
146                 this.getLogger().trace(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N
147
148                 // Call inner method
149                 Category category = this.parseLineToCategory(line);
150
151                 // Debug message
152                 this.getLogger().trace(MessageFormat.format("category={0} - EXIT!", category)); //NOI18N
153
154                 // Return it
155                 return category;
156         }
157
158         /**
159          * Parses given line to a Category instance
160          *
161          * @param line Raw, decoded line from a file-based backend
162          * @return A Category instance from given line
163          */
164         private Category parseLineToCategory (final String line) {
165                 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N
166         }
167 }