]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java
1fc6fe5f760d40694677f2a996f95024de270410
[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.SQLException;
21 import java.text.MessageFormat;
22 import java.util.Iterator;
23 import org.mxchange.jcore.criteria.searchable.SearchCriteria;
24 import org.mxchange.jcore.criteria.searchable.SearchableCritera;
25 import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend;
26 import org.mxchange.jcore.database.result.Result;
27 import org.mxchange.jcore.database.storage.Storeable;
28 import org.mxchange.jcore.exceptions.BadTokenException;
29 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
30 import org.mxchange.pizzaapplication.database.product.PizzaProductDatabaseConstants;
31 import org.mxchange.pizzaapplication.product.Product;
32
33 /**
34  * Stores and retrieves Contact instances
35  *
36  * @author Roland Haeder
37  */
38 public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implements ProductFrontend {
39
40         /**
41          * Default constrcutor
42          * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the configured backend is not supported
43          * @throws java.sql.SQLException If any SQL error occurs
44          */
45         public PizzaProductDatabaseFrontend () throws UnsupportedDatabaseBackendException, SQLException {
46                 // Trace message
47                 this.getLogger().trace("CALLED!"); //NOI18N
48
49                 // Set "table" name
50                 this.setTableName("products"); //NOI18N
51
52                 // Initalize backend
53                 this.initBackend();
54         }
55
56         /**
57          * Shuts down the database layer
58          * 
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         /**
75          * An iterator on all products
76          *
77          * @return Iterator on all products
78          * @throws org.mxchange.jcore.exceptions.BadTokenException
79          * @throws java.io.IOException If any IO error occurs
80          */
81         @Override
82         @SuppressWarnings ("unchecked")
83         public Iterator<Product> getProducts () throws IOException, BadTokenException {
84                 // Trace message
85                 this.getLogger().trace("CALLED!"); //NOI18N
86
87                 // Instance search criteria
88                 SearchableCritera critera = new SearchCriteria();
89
90                 // Add criteria
91                 critera.addCriteria(PizzaProductDatabaseConstants.COLUMN_AVAILABLE, true);
92
93                 // Run the query
94                 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
95
96                 // Debug message
97                 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
98
99                 // Get iterator
100                 Iterator<?> iterator = result.iterator();
101
102                 // Trace message
103                 this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N
104
105                 // Get iterator and return it
106                 return (Iterator<Product>) iterator;
107         }
108
109         /**
110          * Parses given line from database backend into a Storeable instance. Please
111          * note that not all backends need this.
112          *
113          * @param line Line from database backend
114          * @return A Storeable instance
115          */
116         @Override
117         public Storeable parseLineToStoreable (final String line) throws BadTokenException {
118                 // Trace message
119                 this.getLogger().trace(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N
120
121                 // Call inner method
122                 Product product = this.parseLineToProduct(line);
123
124                 // Debug message
125                 this.getLogger().trace(MessageFormat.format("product={0} - EXIT!", product)); //NOI18N
126
127                 // Return it
128                 return product;
129         }
130
131         /**
132          * Parses given line to a Product instance
133          *
134          * @param line
135          * @return A Product instance from given line
136          */
137         private Product parseLineToProduct (final String line) {
138                 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N
139         }
140 }