Skip to content
Snippets Groups Projects
Commit 465be5ce authored by Thiemo Belmega's avatar Thiemo Belmega
Browse files

Load supercategory and subcategories from DB, when a category is loaded

parent 415b2963
No related branches found
No related tags found
No related merge requests found
......@@ -19,7 +19,8 @@ public class DatabaseController {
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 final String COLUMN_NAME_UUID = "uuid";
private static final String COLUMN_SUFFIX_UUID = "_" + COLUMN_NAME_UUID;
private static DatabaseController instance;
private SqlStatementFactory sqlStatementFactory;
......@@ -79,11 +80,11 @@ public class DatabaseController {
ResultSet res = prepQuery.executeQuery();
return getRelatedIdsFromResultSet(targetBusinessObjectType + COLUMN_SUFFIX_UUID,
return getAllIdsFromResultSet(targetBusinessObjectType + COLUMN_SUFFIX_UUID,
res);
}
private Set<String> getRelatedIdsFromResultSet(
private Set<String> getAllIdsFromResultSet(
String resultColumnName, ResultSet res) throws SQLException {
Set<String> relatedIds = new HashSet<>();
while (res.next()) {
......@@ -109,6 +110,15 @@ public class DatabaseController {
}
public Collection<String> loadSubCategories(String uuid) throws SQLException {
PreparedStatement prepQuery = sqlStatementFactory.getSelectSubcategories();
prepQuery.setString(1, uuid);
ResultSet res = prepQuery.executeQuery();
return getAllIdsFromResultSet(COLUMN_NAME_UUID, res);
}
public boolean checkIfExistsInDB(BusinessObject aBusinessObject) throws SQLException, InvalidIdException {
return checkIfExistsInDB(aBusinessObject.getUuid());
}
......
......@@ -7,6 +7,8 @@ import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;
import static de.unipotsdam.cs.toolup.database.DatabaseController.TABLE_NAME_CATEGORY;
public class SqlStatementFactory {
public static final String SQL_STATEMENTS_FILENAME = "/SQL_Statements.xml";
......@@ -67,4 +69,7 @@ public class SqlStatementFactory {
}
public PreparedStatement getSelectSubcategories() throws SQLException {
return getCustomizedStatement("selectSubCategories", TABLE_NAME_CATEGORY);
}
}
......@@ -15,6 +15,7 @@ import static de.unipotsdam.cs.toolup.model.BusinessObject.*;
public class BusinessObjectFactory {
private static final String KEY_UUID = "uuid";
private static final String KEY_SUPERCATEGORY = "supercategory";
private static final String TABLENAME_FEATURE = "feature";
private static final String TABLENAME_CATEGORY = "category";
private static final String TABLENAME_APPLICATION = "application";
......@@ -29,22 +30,7 @@ public class BusinessObjectFactory {
}
}
private static BusinessObject createBusinessObjectWithLoadedRelations(String id, String title,
String description) throws SQLException, InvalidIdException {
int indexOfFirstSlash = id.indexOf(BusinessObject.ID_DELIMITER_CHAR);
String className = id.substring(0, indexOfFirstSlash);
switch (className.toLowerCase()) {
case TABLENAME_APPLICATION:
return new Application(id, title, description, db.loadRelatedCategoriesForApp(id), db.loadRelatedFeaturesForApp(id));
case TABLENAME_CATEGORY:
return new Category(id, title, description, db.loadRelatedApplicationsForCat(id));
case TABLENAME_FEATURE:
return new Feature(id, title, description, db.loadRelatedApplicationsForFeat(id));
default:
throw new UnsupportedOperationException("No class defined for this prefix:" + className);
}
}
/**
* @param res the result of a database query
......@@ -77,17 +63,40 @@ public class BusinessObjectFactory {
}
private static BusinessObject createBusinessObjectFromResult(ResultSet res) throws SQLException {
String id = res.getString(KEY_UUID);
String title = res.getString(JSON_KEY_TITLE);
String description = res.getString(JSON_KEY_DESCRIPTION);
try {
return createBusinessObjectWithLoadedRelations(id, title, description);
} catch (Exception e) {
return createBusinessObjectWithLoadedRelations(res);
} catch (InvalidIdException e) {
throw new RuntimeException("This should never happen. ID is loaded from DB and cannot be invalid", e);
}
}
private static BusinessObject createBusinessObjectWithLoadedRelations(ResultSet res) throws SQLException, InvalidIdException {
BusinessObject loadedBO;
String id = res.getString(KEY_UUID);
int indexOfFirstSlash = id.indexOf(BusinessObject.ID_DELIMITER_CHAR);
String className = id.substring(0, indexOfFirstSlash);
switch (className.toLowerCase()) {
case TABLENAME_APPLICATION:
loadedBO = new Application(id, null, null, db.loadRelatedCategoriesForApp(id), db.loadRelatedFeaturesForApp(id));
break;
case TABLENAME_CATEGORY:
loadedBO = new Category(id, null, null, db.loadRelatedApplicationsForCat(id));
((Category) loadedBO).setSuperCategory(res.getString(KEY_SUPERCATEGORY));
((Category) loadedBO).addSubCategories(db.loadSubCategories(id));
break;
case TABLENAME_FEATURE:
loadedBO = new Feature(id, null, null, db.loadRelatedApplicationsForFeat(id));
break;
default:
throw new UnsupportedOperationException("No class defined for this prefix:" + className);
}
loadedBO.setTitle(res.getString(JSON_KEY_TITLE));
loadedBO.setDescription(res.getString(JSON_KEY_DESCRIPTION));
return loadedBO;
}
public static BusinessObject createInstance(String id) throws InvalidIdException {
String type = getTableNameFromId(id);
......
......@@ -3,6 +3,7 @@
<properties>
<entry key="selectBO">SELECT * FROM TABLE_NAME WHERE uuid = ?;</entry>
<entry key="selectAll">SELECT * FROM TABLE_NAME;</entry>
<entry key="selectSubCategories">SELECT uuid FROM TABLE_NAME WHERE supercategory = ?;</entry>
<entry key="insertBO">INSERT INTO TABLE_NAME VALUES (?,?,?);</entry>
<entry key="insertCategory">INSERT INTO TABLE_NAME VALUES (?,?,?,NULL);</entry>
<entry key="updateBO">UPDATE TABLE_NAME SET title = ?, description = ? WHERE uuid = ?;</entry>
......
package de.unipotsdam.cs.toolup.database;
import de.unipotsdam.cs.toolup.model.Category;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.Collection;
import static de.unipotsdam.cs.toolup.model.BusinessObjectTest.*;
import static de.unipotsdam.cs.toolup.util.AssertionUtil.assertCollectionEquals;
import static org.testng.Assert.assertEquals;
public class DatabaseControllerForCategoryTest extends AbstractDatabaseTest {
@Test
public void testThatSuperCategoryIsLoadedFromDB() throws Exception {
//arrange
//act
Category cat = (Category) db.load(CATEGORY_TEST_ID_11);
//assert
assertEquals(CATEGORY_TEST_ID_13, cat.getSuperCategory());
}
@Test
public void testThatSubCategoriesAreLoadedFromDB() throws Exception {
//arrange
Collection<String> expectedSubCategories = Arrays.asList(CATEGORY_TEST_ID_11, CATEGORY_TEST_ID_12);
//act
Category cat = (Category) db.load(CATEGORY_TEST_ID_13);
//assert
assertCollectionEquals(expectedSubCategories, cat.getSubCategories());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment