CcmNG: Custom JSON converters for ComponentModels
git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@5663 8810af33-2d31-482b-a856-94f89814c4dfpull/2/head
parent
c32c442234
commit
ac5436c299
|
|
@ -50,7 +50,7 @@ public class CategorizedItemComponentJsonBuilder
|
||||||
}
|
}
|
||||||
|
|
||||||
final CategorizedItemComponent component
|
final CategorizedItemComponent component
|
||||||
= (CategorizedItemComponent) componentModel;
|
= (CategorizedItemComponent) componentModel;
|
||||||
|
|
||||||
convertBasePropertiesToJson(component, objectBuilder);
|
convertBasePropertiesToJson(component, objectBuilder);
|
||||||
convertContentItemComponentPropertiesToJson(component, objectBuilder);
|
convertContentItemComponentPropertiesToJson(component, objectBuilder);
|
||||||
|
|
@ -59,16 +59,21 @@ public class CategorizedItemComponentJsonBuilder
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ComponentModel fromJson(final JsonObject jsonObject) {
|
public void fromJson(final JsonObject jsonObject,
|
||||||
|
final ComponentModel componentModel) {
|
||||||
|
|
||||||
Objects.requireNonNull(jsonObject);
|
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);
|
readBasePropertiesFromJson(jsonObject, component);
|
||||||
readContentItemComponentPropertiesFromJson(jsonObject, component);
|
readContentItemComponentPropertiesFromJson(jsonObject, component);
|
||||||
|
|
||||||
return component;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -61,21 +61,30 @@ public class CategoryTreeComponentJsonConverter
|
||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ComponentModel fromJson(final JsonObject jsonObject) {
|
public void fromJson(final JsonObject jsonObject,
|
||||||
|
final ComponentModel componentModel) {
|
||||||
|
|
||||||
Objects.requireNonNull(jsonObject);
|
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);
|
readBasePropertiesFromJson(jsonObject, categoryTree);
|
||||||
|
|
||||||
if (!jsonObject.isNull(SHOW_FULL_TREE)) {
|
if (!jsonObject.isNull(SHOW_FULL_TREE)) {
|
||||||
categoryTree.setShowFullTree(jsonObject.getBoolean(SHOW_FULL_TREE));
|
categoryTree.setShowFullTree(jsonObject.getBoolean(SHOW_FULL_TREE));
|
||||||
}
|
}
|
||||||
|
|
||||||
return categoryTree;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -72,12 +72,18 @@ public class FixedContentItemComponentJsonConverter
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ComponentModel fromJson(final JsonObject jsonObject) {
|
public void fromJson(final JsonObject jsonObject,
|
||||||
|
final ComponentModel componentModel) {
|
||||||
|
|
||||||
Objects.requireNonNull(jsonObject);
|
Objects.requireNonNull(jsonObject);
|
||||||
|
|
||||||
|
if (!(componentModel instanceof FixedContentItemComponent)) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"This converter only processes FixedContentItemComponents.");
|
||||||
|
}
|
||||||
|
|
||||||
final FixedContentItemComponent component
|
final FixedContentItemComponent component
|
||||||
= new FixedContentItemComponent();
|
= (FixedContentItemComponent) componentModel;
|
||||||
|
|
||||||
readBasePropertiesFromJson(jsonObject, component);
|
readBasePropertiesFromJson(jsonObject, component);
|
||||||
readContentItemComponentPropertiesFromJson(jsonObject, component);
|
readContentItemComponentPropertiesFromJson(jsonObject, component);
|
||||||
|
|
@ -90,12 +96,9 @@ public class FixedContentItemComponentJsonConverter
|
||||||
.setContentItem(itemRepo
|
.setContentItem(itemRepo
|
||||||
.findByUuid(uuid)
|
.findByUuid(uuid)
|
||||||
.orElseThrow(() -> new UnexpectedErrorException(
|
.orElseThrow(() -> new UnexpectedErrorException(
|
||||||
String.format("No ContentItem with UUID \"%s\" exists.",
|
String.format("No ContentItem with UUID \"%s\" exists.",
|
||||||
uuid))));
|
uuid))));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return component;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ public class GreetingItemComponentJsonConverter
|
||||||
}
|
}
|
||||||
|
|
||||||
final GreetingItemComponent component
|
final GreetingItemComponent component
|
||||||
= (GreetingItemComponent) componentModel;
|
= (GreetingItemComponent) componentModel;
|
||||||
|
|
||||||
final JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
|
final JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
|
||||||
|
|
||||||
|
|
@ -59,16 +59,21 @@ public class GreetingItemComponentJsonConverter
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ComponentModel fromJson(final JsonObject jsonObject) {
|
public void fromJson(final JsonObject jsonObject,
|
||||||
|
final ComponentModel componentModel) {
|
||||||
|
|
||||||
Objects.requireNonNull(jsonObject);
|
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);
|
readBasePropertiesFromJson(jsonObject, component);
|
||||||
readContentItemComponentPropertiesFromJson(jsonObject, component);
|
readContentItemComponentPropertiesFromJson(jsonObject, component);
|
||||||
|
|
||||||
return component;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -78,11 +78,17 @@ public class ItemListComponentJsonConverter
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ComponentModel fromJson(final JsonObject jsonObject) {
|
public void fromJson(final JsonObject jsonObject,
|
||||||
|
final ComponentModel componentModel) {
|
||||||
|
|
||||||
Objects.requireNonNull(jsonObject);
|
Objects.requireNonNull(jsonObject);
|
||||||
|
|
||||||
final ItemListComponent itemList = new ItemListComponent();
|
if (!(componentModel instanceof ItemListComponent)) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"This implementation does only handle ItemListComponents.");
|
||||||
|
}
|
||||||
|
|
||||||
|
final ItemListComponent itemList = (ItemListComponent) componentModel;
|
||||||
readBasePropertiesFromJson(jsonObject, itemList);
|
readBasePropertiesFromJson(jsonObject, itemList);
|
||||||
|
|
||||||
if (!jsonObject.isNull(DESCENDING)) {
|
if (!jsonObject.isNull(DESCENDING)) {
|
||||||
|
|
@ -107,8 +113,6 @@ public class ItemListComponentJsonConverter
|
||||||
.collect(Collectors.toList()));
|
.collect(Collectors.toList()));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return itemList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,16 +31,19 @@ public interface ComponentModelJsonConverter {
|
||||||
* Convert a {@link ComponentModel} to JSON.
|
* Convert a {@link ComponentModel} to JSON.
|
||||||
*
|
*
|
||||||
* @param componentModel The {@link ComponentModel} to convert.
|
* @param componentModel The {@link ComponentModel} to convert.
|
||||||
|
*
|
||||||
* @return The JSON representation of the provided {@link ComponentModel}.
|
* @return The JSON representation of the provided {@link ComponentModel}.
|
||||||
*/
|
*/
|
||||||
JsonObject toJson(ComponentModel componentModel);
|
JsonObject toJson(ComponentModel componentModel);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read the values of a {@link ComponentModel} from a JSON object.
|
* 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 jsonObject The JSON object with the values.
|
||||||
* @return The {@link ComponentModel}.
|
* @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