Skip to content
Snippets Groups Projects
Commit db7bd4a8 authored by t.belmega@gmx.de's avatar t.belmega@gmx.de
Browse files

Use template method pattern for creating business objects from json

Recognize super and sub categories when creating a Category object from json
parent af07bb90
No related branches found
No related tags found
No related merge requests found
package de.unipotsdam.cs.toolup.model;
import de.unipotsdam.cs.toolup.exceptions.InvalidIdException;
import org.json.JSONObject;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
......@@ -19,6 +22,12 @@ public class Application extends BusinessObject {
this(uuid, "", "", new HashSet<String>(), new HashSet<String>());
}
@Override
protected void buildSubClassSpecificAttributes(JSONObject jsonRepresentation) throws InvalidIdException {
addRelationFromJson(jsonRepresentation, JSON_KEY_FEATURES);
addRelationFromJson(jsonRepresentation, JSON_KEY_CATEGORIES);
}
public Collection<String> getRelatedCategories() {
return new HashSet<>(this.relations.get(TABLE_NAME_CATEGORY));
}
......
......@@ -21,8 +21,8 @@ public abstract class BusinessObject {
public static final String JSON_KEY_FEATURES = "features";
public static final String JSON_KEY_CATEGORIES = "categories";
public static final String JSON_KEY_APPLICATIONS = "applications";
static Map<String, String> keyMappingSqlJson = new HashMap<>(); //TODO Move to config file
static Map<String, String> keyMappingSqlJson = new HashMap<>(); //TODO Move to config file
static {
keyMappingSqlJson.put(TABLE_NAME_APPLICATION, JSON_KEY_APPLICATIONS);
keyMappingSqlJson.put(TABLE_NAME_CATEGORY, JSON_KEY_CATEGORIES);
......@@ -77,18 +77,19 @@ public abstract class BusinessObject {
newlyCreatedBO.title = jsonRepresentation.getString(JSON_KEY_TITLE);
newlyCreatedBO.description = jsonRepresentation.getString(JSON_KEY_DESCRIPTION);
addRelationFromJson(newlyCreatedBO, jsonRepresentation, JSON_KEY_APPLICATIONS);
addRelationFromJson(newlyCreatedBO, jsonRepresentation, JSON_KEY_FEATURES);
addRelationFromJson(newlyCreatedBO, jsonRepresentation, JSON_KEY_CATEGORIES);
newlyCreatedBO.buildSubClassSpecificAttributes(jsonRepresentation);
return newlyCreatedBO;
}
private static void addRelationFromJson(BusinessObject newlyCreatedBO,
JSONObject jsonRepresentation, String relationKey) throws JSONException, InvalidIdException {
protected abstract void buildSubClassSpecificAttributes(JSONObject jsonRepresentation) throws InvalidIdException;
protected void addRelationFromJson(JSONObject jsonRepresentation, String relationKey) throws JSONException, InvalidIdException {
if (jsonRepresentation.has(relationKey)) {
JSONArray relationElements = jsonRepresentation.getJSONArray(relationKey);
addIdsOfAllElements(newlyCreatedBO, relationElements);
addIdsOfAllElements(this, relationElements);
}
}
......
package de.unipotsdam.cs.toolup.model;
import de.unipotsdam.cs.toolup.exceptions.InvalidIdException;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
......@@ -8,6 +12,8 @@ import static de.unipotsdam.cs.toolup.database.DatabaseController.TABLE_NAME_APP
public class Category extends BusinessObject {
private static final String JSON_KEY_SUPERCATEGORY = "supercategory";
private static final String JSON_KEY_SUBCATEGORIES = "subcategories";
private String superCategory = "";
private Collection<String> subCategories = new HashSet<>();
......@@ -20,6 +26,20 @@ public class Category extends BusinessObject {
this(uuid, "", "", new HashSet<String>());
}
@Override
protected void buildSubClassSpecificAttributes(JSONObject jsonRepresentation) throws InvalidIdException {
addRelationFromJson(jsonRepresentation, JSON_KEY_APPLICATIONS);
this.superCategory = jsonRepresentation.getString(JSON_KEY_SUPERCATEGORY);
JSONArray subcategories = jsonRepresentation.getJSONArray(JSON_KEY_SUBCATEGORIES);
for (int i = 0; i < subcategories.length(); i++) {
JSONObject subCat = subcategories.getJSONObject(i);
this.subCategories.add(subCat.getString(JSON_KEY_ID));
}
}
public Collection<String> getRelatedApplications() {
return new HashSet<>(this.relations.get(TABLE_NAME_APPLICATION));
}
......
package de.unipotsdam.cs.toolup.model;
import de.unipotsdam.cs.toolup.exceptions.InvalidIdException;
import org.json.JSONObject;
import java.util.Collection;
import java.util.HashSet;
......@@ -17,6 +20,11 @@ public class Feature extends BusinessObject {
this(uuid, "", "", new HashSet<String>());
}
@Override
protected void buildSubClassSpecificAttributes(JSONObject jsonRepresentation) throws InvalidIdException {
addRelationFromJson(jsonRepresentation, JSON_KEY_APPLICATIONS);
}
public Collection<String> getRelatedApplications() {
return new HashSet<>(this.relations.get(TABLE_NAME_APPLICATION));
}
......
package de.unipotsdam.cs.toolup.model;
import de.unipotsdam.cs.toolup.exceptions.InvalidIdException;
import org.json.JSONObject;
/**
* NullBusinessObject represents the absence of a {@link BusinessObject} according to the NullObjectPattern.
*
......@@ -28,6 +31,10 @@ public class NullBusinessObject extends BusinessObject {
return instance;
}
@Override
protected void buildSubClassSpecificAttributes(JSONObject jsonRepresentation) throws InvalidIdException {
}
@Override
public void addRelation(String string) {
}
......
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