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