|
1 | | -/* |
2 | | - * To change this license header, choose License Headers in Project Properties. |
3 | | - * To change this template file, choose Tools | Templates |
4 | | - * and open the template in the editor. |
5 | | - */ |
6 | 1 | package data.databaseAccessObjects.mappers; |
7 | 2 |
|
| 3 | +import data.databaseAccessObjects.DBConnector; |
| 4 | +import data.exceptions.LoginException; |
| 5 | +import data.models.MaterialModel; |
| 6 | +import data.models.PartslistModel; |
| 7 | +import java.sql.Connection; |
| 8 | +import java.sql.PreparedStatement; |
| 9 | +import java.sql.ResultSet; |
| 10 | +import java.sql.SQLException; |
| 11 | + |
8 | 12 | /** |
9 | 13 | * |
10 | | - * @author Camilla |
| 14 | + * @author |
11 | 15 | */ |
12 | | -public class MaterialMapper { |
| 16 | +public class MaterialMapper |
| 17 | +{ |
| 18 | + |
13 | 19 | private static MaterialMapper materialMapper; |
14 | | - |
15 | | - private MaterialMapper() { |
| 20 | + |
| 21 | + private MaterialMapper() |
| 22 | + { |
16 | 23 |
|
17 | 24 | } |
18 | 25 |
|
19 | | - public static MaterialMapper getInstance() { |
20 | | - if (materialMapper == null) { |
| 26 | + public static MaterialMapper getInstance() |
| 27 | + { |
| 28 | + if (materialMapper == null) |
| 29 | + { |
21 | 30 | materialMapper = new MaterialMapper(); |
22 | 31 | } |
23 | 32 | return materialMapper; |
24 | 33 | } |
25 | | -} |
26 | 34 |
|
| 35 | + // <editor-fold defaultstate="collapsed" desc="Get Category of a Material"> |
| 36 | + /** |
| 37 | + * Get Category. |
| 38 | + * |
| 39 | + * @param id of the category. |
| 40 | + * @return name of the category. |
| 41 | + * @throws LoginException Should most likely throw something else. |
| 42 | + */ |
| 43 | + public String getCategory(int id) throws LoginException |
| 44 | + { |
| 45 | + String SQL = "SELECT `category`.`category_name`\n" |
| 46 | + + "FROM `carportdb`.`category`\n" |
| 47 | + + "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 | + { |
| 52 | + String category = ""; |
| 53 | + ps.setInt(1, id); |
| 54 | + ResultSet rs = ps.executeQuery(); |
| 55 | + while (rs.next()) |
| 56 | + { |
| 57 | + category = rs.getString("category_name"); |
| 58 | + } |
| 59 | + return category; |
| 60 | + } catch (SQLException ex) |
| 61 | + { |
| 62 | + // Should most likely be another exception. |
| 63 | + throw new LoginException(ex.getMessage()); // ex.getMessage() Should not be in production. |
| 64 | + } |
| 65 | + } |
| 66 | + // </editor-fold> |
| 67 | + |
| 68 | + // <editor-fold defaultstate="collapsed" desc="Get a Material"> |
| 69 | + /** |
| 70 | + * Get a Material. |
| 71 | + * |
| 72 | + * @param id of the Material. |
| 73 | + * @return MaterialModel |
| 74 | + * @throws LoginException Should probably be something else later on. |
| 75 | + */ |
| 76 | + public MaterialModel getMaterial(int id) throws LoginException |
| 77 | + { |
| 78 | + MaterialModel material = new MaterialModel(); |
| 79 | + |
| 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 | + { |
| 86 | + material.setID(id); |
| 87 | + |
| 88 | + ps.setInt(1, id); |
| 89 | + ResultSet rs = ps.executeQuery(); |
| 90 | + |
| 91 | + while (rs.next()) |
| 92 | + { |
| 93 | + String description = rs.getString("description"); |
| 94 | + material.setDescription(description); |
| 95 | + |
| 96 | + int height = rs.getInt("height"); |
| 97 | + material.setHeight(height); |
| 98 | + |
| 99 | + int width = rs.getInt("width"); |
| 100 | + material.setWidth(width); |
| 101 | + |
| 102 | + int length = rs.getInt("length"); |
| 103 | + material.setLength(length); |
27 | 104 |
|
| 105 | + double price = rs.getDouble("cost_price"); |
| 106 | + material.setPrice(price); |
| 107 | + |
| 108 | + String unit = rs.getString("unit"); |
| 109 | + material.setUnit(unit); |
| 110 | + |
| 111 | + String categoryname = rs.getString("category_name"); |
| 112 | + material.setCategory(categoryname); |
| 113 | + |
| 114 | + |
| 115 | + } |
| 116 | + } catch (SQLException ex) |
| 117 | + { |
| 118 | + // Should most likely be another exception. |
| 119 | + throw new LoginException(ex.getMessage()); // ex.getMessage() Should not be in production. |
| 120 | + } |
| 121 | + |
| 122 | + return material; |
| 123 | + } |
| 124 | + // </editor-fold> |
| 125 | + |
| 126 | + // <editor-fold defaultstate="collapsed" desc="Get Order Details Category"> |
| 127 | + /** |
| 128 | + * Get Order Details Category. |
| 129 | + * |
| 130 | + * @param id of the category. |
| 131 | + * @return name of the category. |
| 132 | + * @throws LoginException Should most likely throw something else. |
| 133 | + */ |
| 134 | + public String getOrderDetailsCategory(int id) throws LoginException |
| 135 | + { |
| 136 | + String SQL = "SELECT `order_details_category`.`details_category_name`\n" |
| 137 | + + "FROM `carportdb`.`order_details_category`\n" |
| 138 | + + "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 | + { |
| 143 | + String category = ""; |
| 144 | + ps.setInt(1, id); |
| 145 | + ResultSet rs = ps.executeQuery(); |
| 146 | + while (rs.next()) |
| 147 | + { |
| 148 | + category = rs.getString("details_category_name"); |
| 149 | + } |
| 150 | + return category; |
| 151 | + } catch (SQLException ex) |
| 152 | + { |
| 153 | + // Should most likely be another exception. |
| 154 | + throw new LoginException(ex.getMessage()); // ex.getMessage() Should not be in production. |
| 155 | + } |
| 156 | + } |
| 157 | + // </editor-fold> |
| 158 | + |
| 159 | + // <editor-fold defaultstate="collapsed" desc="Get all Materials for an Order Details"> |
| 160 | + /** |
| 161 | + * Get a List of Materials. |
| 162 | + * @param id of the Order Details. |
| 163 | + * @return List of MaterialModel. |
| 164 | + * @throws LoginException Should most likely throw something else. |
| 165 | + */ |
| 166 | + public PartslistModel getMaterials(int id) throws LoginException |
| 167 | + { |
| 168 | + PartslistModel materials = new PartslistModel(); |
| 169 | + String SQL = "SELECT `order_details`.`id_material`\n" |
| 170 | + + "FROM `carportdb`.`order_details`\n" |
| 171 | + + "WHERE `order_details`.`id_order_details` = ?;"; |
| 172 | + |
| 173 | + // Using try-with resources, so they automatically close afterwards. |
| 174 | + try (Connection con = DBConnector.connection(); |
| 175 | + PreparedStatement ps = con.prepareStatement(SQL);) |
| 176 | + { |
| 177 | + ps.setInt(1, id); |
| 178 | + ResultSet rs = ps.executeQuery(); |
| 179 | + while (rs.next()) |
| 180 | + { |
| 181 | + MaterialModel material = getMaterial(rs.getInt("id_material")); |
| 182 | + materials.addMaterial(material); |
| 183 | + } |
| 184 | + } catch (SQLException ex) |
| 185 | + { |
| 186 | + // Should most likely be another exception. |
| 187 | + throw new LoginException(ex.getMessage()); // ex.getMessage() Should not be in production. |
| 188 | + } |
| 189 | + return materials; |
| 190 | + } |
| 191 | + // </editor-fold> |
| 192 | +} |
0 commit comments