1+
12package data .databaseAccessObjects .mappers ;
23
34import data .databaseAccessObjects .DBConnector ;
1112
1213/**
1314 *
14- * @author
15+ * @author
1516 */
16- public class MaterialMapper
17- {
17+ public class MaterialMapper {
1818
1919 private static MaterialMapper materialMapper ;
2020
21- private MaterialMapper ()
22- {
23-
21+ private MaterialMapper () {
2422 }
2523
26- public static MaterialMapper getInstance ()
27- {
28- if (materialMapper == null )
29- {
24+ public static MaterialMapper getInstance () {
25+ if (materialMapper == null ) {
3026 materialMapper = new MaterialMapper ();
3127 }
3228 return materialMapper ;
@@ -40,25 +36,22 @@ public static MaterialMapper getInstance()
4036 * @return name of the category.
4137 * @throws LoginException Should most likely throw something else.
4238 */
43- public String getCategory (int id ) throws LoginException
44- {
39+ public String getCategory (int id ) throws LoginException {
4540 String SQL = "SELECT `category`.`category_name`\n "
4641 + "FROM `carportdb`.`category`\n "
4742 + "WHERE `category`.`id_category` = ?;" ;
48- // Using try-with resources, so they automatically close afterwards.
49- try ( Connection con = DBConnector . connection ();
50- PreparedStatement ps = con . prepareStatement ( SQL );)
51- {
43+
44+ try {
45+ Connection con = DBConnector . connection ();
46+ PreparedStatement ps = con . prepareStatement ( SQL );
5247 String category = "" ;
5348 ps .setInt (1 , id );
5449 ResultSet rs = ps .executeQuery ();
55- while (rs .next ())
56- {
50+ while (rs .next ()) {
5751 category = rs .getString ("category_name" );
5852 }
5953 return category ;
60- } catch (SQLException ex )
61- {
54+ } catch (SQLException ex ) {
6255 // Should most likely be another exception.
6356 throw new LoginException (ex .getMessage ()); // ex.getMessage() Should not be in production.
6457 }
@@ -73,23 +66,24 @@ public String getCategory(int id) throws LoginException
7366 * @return MaterialModel
7467 * @throws LoginException Should probably be something else later on.
7568 */
76- public MaterialModel getMaterial (int id ) throws LoginException
77- {
69+ public MaterialModel getMaterial (int id ) throws LoginException {
7870 MaterialModel material = new MaterialModel ();
7971
80- String SQL = "SELECT `description`, height, width, length, cost_price, unit, category_name "
81- + "FROM materials INNER JOIN `category` ON `materials`.`id_category` = `category`.`id_category`;" ;
82- // Using try-with resources, so they automatically close afterwards.
83- try (Connection con = DBConnector .connection ();
84- PreparedStatement ps = con .prepareStatement (SQL );)
85- {
72+ String SQL = "SELECT `description`, height, width, length, cost_price, unit, category_name \n "
73+ + "FROM materials \n "
74+ + "INNER JOIN `category` \n "
75+ + "ON `materials`.`id_category` = `category`.`id_category` \n "
76+ + "WHERE `materials`.`id_material` = ?;" ;
77+
78+ try {
79+ Connection con = DBConnector .connection ();
80+ PreparedStatement ps = con .prepareStatement (SQL );
8681 material .setID (id );
8782
8883 ps .setInt (1 , id );
8984 ResultSet rs = ps .executeQuery ();
9085
91- while (rs .next ())
92- {
86+ while (rs .next ()) {
9387 String description = rs .getString ("description" );
9488 material .setDescription (description );
9589
@@ -109,12 +103,10 @@ public MaterialModel getMaterial(int id) throws LoginException
109103 material .setUnit (unit );
110104
111105 String categoryname = rs .getString ("category_name" );
112- material .setCategory (categoryname );
106+ material .setCategory (categoryname );
113107
114-
115108 }
116- } catch (SQLException ex )
117- {
109+ } catch (SQLException ex ) {
118110 // Should most likely be another exception.
119111 throw new LoginException (ex .getMessage ()); // ex.getMessage() Should not be in production.
120112 }
@@ -131,25 +123,21 @@ public MaterialModel getMaterial(int id) throws LoginException
131123 * @return name of the category.
132124 * @throws LoginException Should most likely throw something else.
133125 */
134- public String getOrderDetailsCategory (int id ) throws LoginException
135- {
126+ public String getOrderDetailsCategory (int id ) throws LoginException {
136127 String SQL = "SELECT `order_details_category`.`details_category_name`\n "
137128 + "FROM `carportdb`.`order_details_category`\n "
138129 + "WHERE `order_details_category`.`id_order_details_category` = ?;" ;
139- // Using try-with resources, so they automatically close afterwards.
140- try (Connection con = DBConnector .connection ();
141- PreparedStatement ps = con .prepareStatement (SQL );)
142- {
130+ try {
131+ Connection con = DBConnector .connection ();
132+ PreparedStatement ps = con .prepareStatement (SQL );
143133 String category = "" ;
144134 ps .setInt (1 , id );
145135 ResultSet rs = ps .executeQuery ();
146- while (rs .next ())
147- {
136+ while (rs .next ()) {
148137 category = rs .getString ("details_category_name" );
149138 }
150139 return category ;
151- } catch (SQLException ex )
152- {
140+ } catch (SQLException ex ) {
153141 // Should most likely be another exception.
154142 throw new LoginException (ex .getMessage ()); // ex.getMessage() Should not be in production.
155143 }
@@ -159,30 +147,27 @@ public String getOrderDetailsCategory(int id) throws LoginException
159147 // <editor-fold defaultstate="collapsed" desc="Get all Materials for an Order Details">
160148 /**
161149 * Get a List of Materials.
150+ *
162151 * @param id of the Order Details.
163152 * @return List of MaterialModel.
164153 * @throws LoginException Should most likely throw something else.
165154 */
166- public PartslistModel getMaterials (int id ) throws LoginException
167- {
155+ public PartslistModel getMaterials (int id ) throws LoginException {
168156 PartslistModel materials = new PartslistModel ();
169157 String SQL = "SELECT `order_details`.`id_material`\n "
170158 + "FROM `carportdb`.`order_details`\n "
171159 + "WHERE `order_details`.`id_order_details` = ?;" ;
172160
173- // Using try-with resources, so they automatically close afterwards.
174- try (Connection con = DBConnector .connection ();
175- PreparedStatement ps = con .prepareStatement (SQL );)
176- {
161+ try {
162+ Connection con = DBConnector .connection ();
163+ PreparedStatement ps = con .prepareStatement (SQL );
177164 ps .setInt (1 , id );
178165 ResultSet rs = ps .executeQuery ();
179- while (rs .next ())
180- {
166+ while (rs .next ()) {
181167 MaterialModel material = getMaterial (rs .getInt ("id_material" ));
182168 materials .addMaterial (material );
183169 }
184- } catch (SQLException ex )
185- {
170+ } catch (SQLException ex ) {
186171 // Should most likely be another exception.
187172 throw new LoginException (ex .getMessage ()); // ex.getMessage() Should not be in production.
188173 }
0 commit comments