Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package de.unipotsdam.cs.toolup.database;
import de.unipotsdam.cs.toolup.exceptions.InvalidIdException;
import de.unipotsdam.cs.toolup.model.*;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
public class DatabaseController {
public static final String TABLE_NAME_FEATURE = "feature";
public static final String TABLE_NAME_CATEGORY = "category";
public static final String TABLE_NAME_APPLICATION = "application";
public static final String TABLE_NAME_APPLICATION_FEATURE = "application_has_feature";
public static final String TABLE_NAME_APPLICATION_CATEGORY = "application_belongs_to_category";
private static final String COLUMN_SUFFIX_UUID = "_uuid";
private static DatabaseController instance;
private SqlStatementFactory sqlStatementFactory;
protected DatabaseController() throws IOException, SQLException {
sqlStatementFactory = new SqlStatementFactory();
}
public static DatabaseController getInstance() throws IOException, SQLException {
if (instance == null) {
instance = new DatabaseController();
}
return instance;
}
public BusinessObject load(String uuid) throws SQLException, InvalidIdException {
String tableName = BusinessObject.getTableNameFromId(uuid);
PreparedStatement prepQuery = sqlStatementFactory.getSelectAllFrom(tableName);
prepQuery.setString(1, uuid);
ResultSet res = prepQuery.executeQuery();
return BusinessObjectFactory.createBusinessObjectFromSingleResult(res);
}
public Set<String> loadRelatedBusinessObjectsForId(String tableName, String targetBusinessObjectType, String id) throws SQLException, InvalidIdException {
String criteriaBusinessObjectType = BusinessObject.getTableNameFromId(id);
PreparedStatement prepQuery = sqlStatementFactory.getSelectRelation(tableName, criteriaBusinessObjectType + COLUMN_SUFFIX_UUID);
prepQuery.setString(1, id);
ResultSet res = prepQuery.executeQuery();
return getRelatedIdsFromResultSet(targetBusinessObjectType + COLUMN_SUFFIX_UUID,
res);
}
private Set<String> getRelatedIdsFromResultSet(
String resultColumnName, ResultSet res) throws SQLException {
Set<String> relatedIds = new HashSet<>();
while (res.next()) {
relatedIds.add(res.getString(resultColumnName));
}
return relatedIds;
}
public Set<String> loadRelatedCategoriesForApp(String id) throws SQLException, InvalidIdException {
return loadRelatedBusinessObjectsForId(TABLE_NAME_APPLICATION_CATEGORY, TABLE_NAME_CATEGORY, id);
}
public Set<String> loadRelatedApplicationsForCat(String id) throws SQLException, InvalidIdException {
return loadRelatedBusinessObjectsForId(TABLE_NAME_APPLICATION_CATEGORY, TABLE_NAME_APPLICATION, id);
}
public Set<String> loadRelatedFeaturesForApp(String id) throws SQLException, InvalidIdException {
return loadRelatedBusinessObjectsForId(TABLE_NAME_APPLICATION_FEATURE, TABLE_NAME_FEATURE, id);
}
public Collection<String> loadRelatedApplicationsForFeat(String id) throws SQLException, InvalidIdException {
return loadRelatedBusinessObjectsForId(TABLE_NAME_APPLICATION_FEATURE, TABLE_NAME_APPLICATION, id);
}
public boolean checkIfExistsInDB(BusinessObject aBusinessObject) throws SQLException, InvalidIdException {
return checkIfExistsInDB(aBusinessObject.getUuid());
}
public boolean checkIfExistsInDB(String id) throws SQLException, InvalidIdException {
BusinessObject objectFromDB = load(id);
return !(objectFromDB instanceof NullBusinessObject);
}
public void storeToDatabase(BusinessObject aBusinessObject) throws SQLException, InvalidIdException {
boolean exists = checkIfExistsInDB(aBusinessObject);
if (exists) {
updateDatabase(aBusinessObject);
} else {
insertIntoDatabase(aBusinessObject);
}
}
private void insertIntoDatabase(BusinessObject aBusinessObject)
throws SQLException, InvalidIdException {
String tableName = BusinessObject.getTableNameFromId(aBusinessObject.getUuid());
insertBO(aBusinessObject, tableName);
insertRelations(aBusinessObject, tableName);
}
private void insertRelations(BusinessObject aBusinessObject,
String tableName) throws SQLException {
if (aBusinessObject.getRelatedBOs().isEmpty()) return;
switch (tableName) {
case TABLE_NAME_APPLICATION: {
Application app = (Application) aBusinessObject;
insertSingleRelationInto(TABLE_NAME_APPLICATION_FEATURE, app.getUuid(), 1, app.getRelatedFeatures());
insertSingleRelationInto(TABLE_NAME_APPLICATION_CATEGORY, app.getUuid(), 1, app.getRelatedCategories());
break;
}
case TABLE_NAME_CATEGORY: {
Category cat = (Category) aBusinessObject;
insertSingleRelationInto(TABLE_NAME_APPLICATION_CATEGORY, cat.getUuid(), 2, cat.getRelatedApplications());
break;
}
case TABLE_NAME_FEATURE: {
Feature feat = (Feature) aBusinessObject;
insertSingleRelationInto(TABLE_NAME_APPLICATION_FEATURE, feat.getUuid(), 2, feat.getRelatedApplications());
break;
}
default:
}
}
/**
* @param tableName the name of the relation table
* @param uuid the BO thats relations are to be stored
* @param uuidColumnNumber column number of the BOs id in the table. 1 if BO is Application, 2 otherwise.
* @param relatedIds a collection of the uuid's of related BOs
* @throws SQLException
*/
private void insertSingleRelationInto(String tableName, String uuid,
int uuidColumnNumber, Collection<String> relatedIds) throws SQLException {
for (String foreignKey : relatedIds) {
PreparedStatement prepQuery;
prepQuery = sqlStatementFactory.getInsertRelation(tableName);
prepQuery.setString(uuidColumnNumber, uuid);
prepQuery.setString((uuidColumnNumber % 2) + 1, foreignKey);
prepQuery.executeUpdate();
}
}
private void updateSingleRelation(
String tableName, String uuid, int uuidColumnNumber,
Collection<String> relatedIds) throws SQLException {
for (String foreignKey : relatedIds) {
PreparedStatement prepQuery;
prepQuery = sqlStatementFactory.getInsertRelation(tableName);
prepQuery.setString(uuidColumnNumber, uuid);
prepQuery.setString((uuidColumnNumber % 2) + 1, foreignKey);
prepQuery.executeUpdate();
}
}
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
private void insertBO(BusinessObject aBusinessObject,
String tableName) throws SQLException {
PreparedStatement prepQuery;
prepQuery = sqlStatementFactory.getInsertInto(tableName);
prepQuery.setString(1, aBusinessObject.getUuid());
prepQuery.setString(2, aBusinessObject.getTitle());
prepQuery.setString(3, aBusinessObject.getDescription());
prepQuery.executeUpdate();
}
private void updateDatabase(BusinessObject aBusinessObject)
throws SQLException, InvalidIdException {
String tableName = BusinessObject.getTableNameFromId(aBusinessObject.getUuid());
updateBO(aBusinessObject, tableName);
updateRelations(aBusinessObject, tableName);
}
private void updateRelations(BusinessObject aBusinessObject,
String tableName) throws SQLException {
if (aBusinessObject.getRelatedBOs().isEmpty()) return;
switch (tableName) {
case TABLE_NAME_APPLICATION: {
Application app = (Application) aBusinessObject;
updateSingleRelation(TABLE_NAME_APPLICATION_FEATURE, app.getUuid(), 1, app.getRelatedFeatures());
updateSingleRelation(TABLE_NAME_APPLICATION_CATEGORY, app.getUuid(), 1, app.getRelatedCategories());
break;
}
case TABLE_NAME_CATEGORY: {
Category cat = (Category) aBusinessObject;
updateSingleRelation(TABLE_NAME_APPLICATION_CATEGORY, cat.getUuid(), 2, cat.getRelatedApplications());
break;
}
case TABLE_NAME_FEATURE: {
Feature feat = (Feature) aBusinessObject;
updateSingleRelation(TABLE_NAME_APPLICATION_FEATURE, feat.getUuid(), 2, feat.getRelatedApplications());
break;
}
default:
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
}
}
private void updateBO(BusinessObject aBusinessObject,
String tableName) throws SQLException {
PreparedStatement prepQuery;
prepQuery = sqlStatementFactory.getUpdate(tableName);
prepQuery.setString(1, aBusinessObject.getTitle());
prepQuery.setString(2, aBusinessObject.getDescription());
prepQuery.setString(3, aBusinessObject.getUuid());
prepQuery.executeUpdate();
}
public void deleteFromDatabase(String id) throws SQLException, InvalidIdException {
String tableName = BusinessObject.getTableNameFromId(id);
PreparedStatement prepQuery = sqlStatementFactory.getDeleteFrom(tableName);
prepQuery.setString(1, id);
prepQuery.executeUpdate();
}
}