Skip to content

Commit 2b6bf69

Browse files
authored
Merge pull request #52 from HrBjarup/developer
Master merge sprint 2
2 parents c990ba7 + 3d5808c commit 2b6bf69

36 files changed

+3076
-351
lines changed
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package data;
22

3+
import data.exceptions.LoginException;
34
import data.models.MaterialModel;
45
import data.models.OrderModel;
56
import data.models.PartslistModel;
67

78
public interface DataFacade
89
{
9-
public MaterialModel getMaterial();
10+
public MaterialModel getMaterial(int id) throws LoginException;
1011

11-
public OrderModel getOrder();
12+
public OrderModel getOrder(int id) throws LoginException;
1213

1314
public PartslistModel getBOM();
15+
16+
public PartslistModel getOrderDetails(int id) throws LoginException;
17+
18+
public void createOrder(OrderModel order) throws LoginException;
1419
}
Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
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-
*/
61
package data;
72

3+
import data.databaseAccessObjects.mappers.MaterialMapper;
4+
import data.databaseAccessObjects.mappers.OrderMapper;
5+
import data.exceptions.LoginException;
86
import data.models.MaterialModel;
97
import data.models.OrderModel;
108
import data.models.PartslistModel;
119

1210
/**
1311
*
14-
* @author Camilla
12+
* @author
1513
*/
1614
public class DataFacadeImpl implements DataFacade
1715
{
16+
17+
private static DataFacadeImpl instance = null;
18+
19+
public synchronized static DataFacadeImpl getInstance() {
20+
if (instance == null) {
21+
instance = new DataFacadeImpl();
22+
}
23+
return instance;
24+
}
25+
1826

1927
@Override
20-
public MaterialModel getMaterial()
28+
public MaterialModel getMaterial(int id) throws LoginException
2129
{
22-
// skal udviddes
23-
return new MaterialModel();
30+
return MaterialMapper.getInstance().getMaterial(id);
2431
}
2532

2633
@Override
27-
public OrderModel getOrder()
34+
public OrderModel getOrder(int id) throws LoginException
2835
{
29-
// skal udviddes
30-
return new OrderModel();
36+
return OrderMapper.getInstance().getOrder(id);
3137
}
3238

3339
@Override
@@ -36,4 +42,16 @@ public PartslistModel getBOM()
3642
return new PartslistModel();
3743
}
3844

45+
@Override
46+
public PartslistModel getOrderDetails(int id) throws LoginException
47+
{
48+
return MaterialMapper.getInstance().getMaterials(id);
49+
}
50+
51+
@Override
52+
public void createOrder(OrderModel order) throws LoginException
53+
{
54+
OrderMapper.getInstance().createOrder(order);
55+
}
56+
3957
}

FogCarport/src/main/java/data/databaseAccessObjects/DBConnector.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
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-
*/
61
package data.databaseAccessObjects;
72

83
import java.sql.Connection;
@@ -12,16 +7,16 @@
127

138
/**
149
*
15-
* @author Camilla
10+
* @author
1611
*/
1712
public class DBConnector
1813
{
1914

2015
private static final String IP = "207.154.233.238";
2116
private static final String PORT = "3306";
22-
public static final String DATABASE = "NAME"; // CHANGE to name
23-
private static final String USERNAME = "admin"; // CHANGE if wrong
24-
private static final String PASSWORD = "1234"; // CHANGE if wrong
17+
public static final String DATABASE = "carportdb";
18+
private static final String USERNAME = "admin";
19+
private static final String PASSWORD = "1234";
2520

2621
private static Connection singleton;
2722

@@ -30,9 +25,9 @@ public static void setConnection(Connection con)
3025
singleton = con;
3126
}
3227

33-
public static Connection connection() throws ClassNotFoundException, SQLException
28+
public static Connection connection() throws SQLException
3429
{
35-
if (singleton == null)
30+
if (singleton == null || singleton.isClosed())
3631
{
3732
try
3833
{
@@ -50,11 +45,11 @@ public static Connection connection() throws ClassNotFoundException, SQLExceptio
5045
props.put("serverTimezone", "CET");
5146
singleton = DriverManager.getConnection(url, props);
5247
System.out.println("Connection correctly established to the database: " + DATABASE);
53-
} catch (InstantiationException | IllegalAccessException ex)
48+
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException ex)
5449
{
5550
ex.printStackTrace();
5651
throw new SQLException(ex.getMessage());
57-
}
52+
}
5853
}
5954
return singleton;
6055
}
Lines changed: 177 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,192 @@
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-
*/
61
package data.databaseAccessObjects.mappers;
72

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+
812
/**
913
*
10-
* @author Camilla
14+
* @author
1115
*/
12-
public class MaterialMapper {
16+
public class MaterialMapper
17+
{
18+
1319
private static MaterialMapper materialMapper;
14-
15-
private MaterialMapper() {
20+
21+
private MaterialMapper()
22+
{
1623

1724
}
1825

19-
public static MaterialMapper getInstance() {
20-
if (materialMapper == null) {
26+
public static MaterialMapper getInstance()
27+
{
28+
if (materialMapper == null)
29+
{
2130
materialMapper = new MaterialMapper();
2231
}
2332
return materialMapper;
2433
}
25-
}
2634

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);
27104

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

Comments
 (0)