CcmNG: Custom JSON converters for ComponentModels
git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@5663 8810af33-2d31-482b-a856-94f89814c4df
parent
9b962fdc9c
commit
b7e4e9e0d2
|
|
@ -50,8 +50,8 @@ public class CategorizedItemComponentJsonBuilder
|
|||
}
|
||||
|
||||
final CategorizedItemComponent component
|
||||
= (CategorizedItemComponent) componentModel;
|
||||
|
||||
= (CategorizedItemComponent) componentModel;
|
||||
|
||||
convertBasePropertiesToJson(component, objectBuilder);
|
||||
convertContentItemComponentPropertiesToJson(component, objectBuilder);
|
||||
|
||||
|
|
@ -59,16 +59,21 @@ public class CategorizedItemComponentJsonBuilder
|
|||
}
|
||||
|
||||
@Override
|
||||
public ComponentModel fromJson(final JsonObject jsonObject) {
|
||||
|
||||
public void fromJson(final JsonObject jsonObject,
|
||||
final ComponentModel componentModel) {
|
||||
|
||||
Objects.requireNonNull(jsonObject);
|
||||
|
||||
final CategorizedItemComponent component = new CategorizedItemComponent();
|
||||
|
||||
|
||||
if (!(componentModel instanceof CategorizedItemComponent)) {
|
||||
throw new IllegalArgumentException(
|
||||
"This converter only processes CategorizedItemComponents.");
|
||||
}
|
||||
|
||||
final CategorizedItemComponent component
|
||||
= (CategorizedItemComponent) componentModel;
|
||||
|
||||
readBasePropertiesFromJson(jsonObject, component);
|
||||
readContentItemComponentPropertiesFromJson(jsonObject, component);
|
||||
|
||||
return component;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ import javax.json.JsonObjectBuilder;
|
|||
@ConvertsComponentModel(componentModel = CategoryTreeComponent.class)
|
||||
public class CategoryTreeComponentJsonConverter
|
||||
extends AbstractComponentModelJsonConverter {
|
||||
|
||||
|
||||
private static final String SHOW_FULL_TREE = "showFullTree";
|
||||
|
||||
@Override
|
||||
|
|
@ -57,25 +57,34 @@ public class CategoryTreeComponentJsonConverter
|
|||
convertBasePropertiesToJson(categoryTree, builder);
|
||||
|
||||
builder.add(SHOW_FULL_TREE, categoryTree.isShowFullTree());
|
||||
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ComponentModel fromJson(final JsonObject jsonObject) {
|
||||
|
||||
public void fromJson(final JsonObject jsonObject,
|
||||
final ComponentModel componentModel) {
|
||||
|
||||
Objects.requireNonNull(jsonObject);
|
||||
|
||||
final CategoryTreeComponent categoryTree = new CategoryTreeComponent();
|
||||
|
||||
|
||||
if (!(componentModel instanceof CategoryTreeComponent)) {
|
||||
throw new IllegalArgumentException(
|
||||
"This converter only processes CategoryTreeComponents.");
|
||||
}
|
||||
|
||||
final CategoryTreeComponent categoryTree
|
||||
= (CategoryTreeComponent) componentModel;
|
||||
|
||||
if (!(componentModel instanceof CategoryTreeComponent)) {
|
||||
throw new IllegalArgumentException(
|
||||
"This converter only processes CategoryTreeComponents.");
|
||||
}
|
||||
|
||||
readBasePropertiesFromJson(jsonObject, categoryTree);
|
||||
|
||||
|
||||
if (!jsonObject.isNull(SHOW_FULL_TREE)) {
|
||||
categoryTree.setShowFullTree(jsonObject.getBoolean(SHOW_FULL_TREE));
|
||||
}
|
||||
|
||||
return categoryTree;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ public class FixedContentItemComponentJsonConverter
|
|||
extends AbstractContentItemComponentJsonConverter {
|
||||
|
||||
private static final String CONTENT_ITEM = "contentItem";
|
||||
|
||||
|
||||
@Inject
|
||||
private ContentItemRepository itemRepo;
|
||||
|
||||
|
|
@ -72,30 +72,33 @@ public class FixedContentItemComponentJsonConverter
|
|||
}
|
||||
|
||||
@Override
|
||||
public ComponentModel fromJson(final JsonObject jsonObject) {
|
||||
public void fromJson(final JsonObject jsonObject,
|
||||
final ComponentModel componentModel) {
|
||||
|
||||
Objects.requireNonNull(jsonObject);
|
||||
|
||||
if (!(componentModel instanceof FixedContentItemComponent)) {
|
||||
throw new IllegalArgumentException(
|
||||
"This converter only processes FixedContentItemComponents.");
|
||||
}
|
||||
|
||||
final FixedContentItemComponent component
|
||||
= new FixedContentItemComponent();
|
||||
= (FixedContentItemComponent) componentModel;
|
||||
|
||||
readBasePropertiesFromJson(jsonObject, component);
|
||||
readContentItemComponentPropertiesFromJson(jsonObject, component);
|
||||
|
||||
if (!jsonObject.isNull(CONTENT_ITEM)) {
|
||||
|
||||
|
||||
final String uuid = jsonObject.getString(CONTENT_ITEM);
|
||||
|
||||
|
||||
component
|
||||
.setContentItem(itemRepo
|
||||
.findByUuid(uuid)
|
||||
.orElseThrow(() -> new UnexpectedErrorException(
|
||||
String.format("No ContentItem with UUID \"%s\" exists.",
|
||||
uuid))));
|
||||
|
||||
String.format("No ContentItem with UUID \"%s\" exists.",
|
||||
uuid))));
|
||||
}
|
||||
|
||||
return component;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ public class GreetingItemComponentJsonConverter
|
|||
}
|
||||
|
||||
final GreetingItemComponent component
|
||||
= (GreetingItemComponent) componentModel;
|
||||
= (GreetingItemComponent) componentModel;
|
||||
|
||||
final JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
|
||||
|
||||
|
|
@ -59,16 +59,21 @@ public class GreetingItemComponentJsonConverter
|
|||
}
|
||||
|
||||
@Override
|
||||
public ComponentModel fromJson(final JsonObject jsonObject) {
|
||||
|
||||
public void fromJson(final JsonObject jsonObject,
|
||||
final ComponentModel componentModel) {
|
||||
|
||||
Objects.requireNonNull(jsonObject);
|
||||
|
||||
final GreetingItemComponent component = new GreetingItemComponent();
|
||||
|
||||
|
||||
if (!(componentModel instanceof GreetingItemComponent)) {
|
||||
throw new IllegalArgumentException(
|
||||
"This converter only processes GreetingItemComponents.");
|
||||
}
|
||||
|
||||
final GreetingItemComponent component
|
||||
= (GreetingItemComponent) componentModel;
|
||||
|
||||
readBasePropertiesFromJson(jsonObject, component);
|
||||
readContentItemComponentPropertiesFromJson(jsonObject, component);
|
||||
|
||||
return component;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -78,11 +78,17 @@ public class ItemListComponentJsonConverter
|
|||
}
|
||||
|
||||
@Override
|
||||
public ComponentModel fromJson(final JsonObject jsonObject) {
|
||||
public void fromJson(final JsonObject jsonObject,
|
||||
final ComponentModel componentModel) {
|
||||
|
||||
Objects.requireNonNull(jsonObject);
|
||||
|
||||
if (!(componentModel instanceof ItemListComponent)) {
|
||||
throw new IllegalArgumentException(
|
||||
"This implementation does only handle ItemListComponents.");
|
||||
}
|
||||
|
||||
final ItemListComponent itemList = new ItemListComponent();
|
||||
final ItemListComponent itemList = (ItemListComponent) componentModel;
|
||||
readBasePropertiesFromJson(jsonObject, itemList);
|
||||
|
||||
if (!jsonObject.isNull(DESCENDING)) {
|
||||
|
|
@ -107,8 +113,6 @@ public class ItemListComponentJsonConverter
|
|||
.collect(Collectors.toList()));
|
||||
|
||||
}
|
||||
|
||||
return itemList;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,21 +26,24 @@ import javax.json.JsonObject;
|
|||
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||
*/
|
||||
public interface ComponentModelJsonConverter {
|
||||
|
||||
|
||||
/**
|
||||
* Convert a {@link ComponentModel} to JSON.
|
||||
*
|
||||
*
|
||||
* @param componentModel The {@link ComponentModel} to convert.
|
||||
*
|
||||
* @return The JSON representation of the provided {@link ComponentModel}.
|
||||
*/
|
||||
JsonObject toJson(ComponentModel componentModel);
|
||||
|
||||
|
||||
/**
|
||||
* Read the values of a {@link ComponentModel} from a JSON object.
|
||||
*
|
||||
* @param jsonObject The JSON object with the values.
|
||||
* @return The {@link ComponentModel}.
|
||||
* Read the values of a {@link ComponentModel} from a JSON object and set
|
||||
* them on the provided component model.
|
||||
*
|
||||
* @param jsonObject The JSON object with the values.
|
||||
* @param componentModel The {@link ComponentModel} on which the values are
|
||||
* set.
|
||||
*/
|
||||
ComponentModel fromJson(JsonObject jsonObject);
|
||||
|
||||
void fromJson(JsonObject jsonObject, ComponentModel componentModel);
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue