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