CCM NG: PageModelsEditor

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@5662 8810af33-2d31-482b-a856-94f89814c4df
ccm-docs
jensp 2018-08-06 17:35:57 +00:00
parent 58ef5c27c6
commit a4865fadf3
11 changed files with 829 additions and 64 deletions

View File

@ -0,0 +1,59 @@
/*
* Copyright (C) 2018 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.librecms.pagemodel;
import org.libreccm.pagemodel.AbstractComponentModelJsonConverter;
import java.util.Objects;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public abstract class AbstractContentItemComponentJsonConverter
extends AbstractComponentModelJsonConverter {
private static final String MODE = "mode";
protected void convertContentItemComponentPropertiesToJson(
final ContentItemComponent component,
final JsonObjectBuilder objectBuilder) {
Objects.requireNonNull(component);
Objects.requireNonNull(objectBuilder);
objectBuilder.add(MODE, component.getMode());
}
protected void readContentItemComponentPropertiesFromJson(
final JsonObject jsonObject, final ContentItemComponent component) {
Objects.requireNonNull(jsonObject);
Objects.requireNonNull(component);
if (!jsonObject.isNull(MODE)) {
component.setMode(jsonObject.getString(MODE));
}
}
}

View File

@ -0,0 +1,74 @@
/*
* Copyright (C) 2018 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.librecms.pagemodel;
import org.libreccm.pagemodel.ComponentModel;
import org.libreccm.pagemodel.ConvertsComponentModel;
import java.util.Objects;
import javax.enterprise.context.RequestScoped;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
@ConvertsComponentModel(componentModel = CategorizedItemComponent.class)
public class CategorizedItemComponentJsonBuilder
extends AbstractContentItemComponentJsonConverter {
@Override
public JsonObject toJson(final ComponentModel componentModel) {
Objects.requireNonNull(componentModel);
final JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
if (!(componentModel instanceof CategorizedItemComponent)) {
throw new IllegalArgumentException(
"This converter only processes CategorizedItemComponents.");
}
final CategorizedItemComponent component
= (CategorizedItemComponent) componentModel;
convertBasePropertiesToJson(component, objectBuilder);
convertContentItemComponentPropertiesToJson(component, objectBuilder);
return objectBuilder.build();
}
@Override
public ComponentModel fromJson(final JsonObject jsonObject) {
Objects.requireNonNull(jsonObject);
final CategorizedItemComponent component = new CategorizedItemComponent();
readBasePropertiesFromJson(jsonObject, component);
readContentItemComponentPropertiesFromJson(jsonObject, component);
return component;
}
}

View File

@ -0,0 +1,81 @@
/*
* Copyright (C) 2018 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.librecms.pagemodel;
import org.libreccm.pagemodel.AbstractComponentModelJsonConverter;
import org.libreccm.pagemodel.ComponentModel;
import org.libreccm.pagemodel.ConvertsComponentModel;
import java.util.Objects;
import javax.enterprise.context.RequestScoped;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
@ConvertsComponentModel(componentModel = CategoryTreeComponent.class)
public class CategoryTreeComponentJsonConverter
extends AbstractComponentModelJsonConverter {
private static final String SHOW_FULL_TREE = "showFullTree";
@Override
public JsonObject toJson(final ComponentModel componentModel) {
Objects.requireNonNull(componentModel);
if (!(componentModel instanceof CategoryTreeComponent)) {
throw new IllegalArgumentException(
"This converter only processes CategoryTreeComponents.");
}
final CategoryTreeComponent categoryTree
= (CategoryTreeComponent) componentModel;
final JsonObjectBuilder builder = Json.createObjectBuilder();
convertBasePropertiesToJson(categoryTree, builder);
builder.add(SHOW_FULL_TREE, categoryTree.isShowFullTree());
return builder.build();
}
@Override
public ComponentModel fromJson(final JsonObject jsonObject) {
Objects.requireNonNull(jsonObject);
final CategoryTreeComponent categoryTree = new CategoryTreeComponent();
readBasePropertiesFromJson(jsonObject, categoryTree);
if (!jsonObject.isNull(SHOW_FULL_TREE)) {
categoryTree.setShowFullTree(jsonObject.getBoolean(SHOW_FULL_TREE));
}
return categoryTree;
}
}

View File

@ -0,0 +1,101 @@
/*
* Copyright (C) 2018 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.librecms.pagemodel;
import org.libreccm.core.UnexpectedErrorException;
import org.libreccm.pagemodel.ComponentModel;
import org.libreccm.pagemodel.ConvertsComponentModel;
import org.librecms.contentsection.ContentItemRepository;
import java.util.Objects;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
@ConvertsComponentModel(componentModel = FixedContentItemComponent.class)
public class FixedContentItemComponentJsonConverter
extends AbstractContentItemComponentJsonConverter {
private static final String CONTENT_ITEM = "contentItem";
@Inject
private ContentItemRepository itemRepo;
@Override
public JsonObject toJson(final ComponentModel componentModel) {
Objects.requireNonNull(componentModel);
if (!(componentModel instanceof FixedContentItemComponent)) {
throw new IllegalArgumentException(
"This converter only processes FixedContentItemComponents.");
}
final FixedContentItemComponent component
= (FixedContentItemComponent) componentModel;
final JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
convertBasePropertiesToJson(component, objectBuilder);
convertContentItemComponentPropertiesToJson(component, objectBuilder);
if (component.getContentItem() != null) {
objectBuilder.add(CONTENT_ITEM,
component.getContentItem().getUuid());
}
return objectBuilder.build();
}
@Override
public ComponentModel fromJson(final JsonObject jsonObject) {
Objects.requireNonNull(jsonObject);
final FixedContentItemComponent component
= new FixedContentItemComponent();
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))));
}
return component;
}
}

View File

@ -0,0 +1,74 @@
/*
* Copyright (C) 2018 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.librecms.pagemodel;
import org.libreccm.pagemodel.ComponentModel;
import org.libreccm.pagemodel.ConvertsComponentModel;
import java.util.Objects;
import javax.enterprise.context.RequestScoped;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
@ConvertsComponentModel(componentModel = GreetingItemComponent.class)
public class GreetingItemComponentJsonConverter
extends AbstractContentItemComponentJsonConverter {
@Override
public JsonObject toJson(final ComponentModel componentModel) {
Objects.requireNonNull(componentModel);
if (!(componentModel instanceof GreetingItemComponent)) {
throw new IllegalArgumentException(
"This converter only processes GreetingItemComponents.");
}
final GreetingItemComponent component
= (GreetingItemComponent) componentModel;
final JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
convertBasePropertiesToJson(component, objectBuilder);
convertContentItemComponentPropertiesToJson(component, objectBuilder);
return objectBuilder.build();
}
@Override
public ComponentModel fromJson(final JsonObject jsonObject) {
Objects.requireNonNull(jsonObject);
final GreetingItemComponent component = new GreetingItemComponent();
readBasePropertiesFromJson(jsonObject, component);
readContentItemComponentPropertiesFromJson(jsonObject, component);
return component;
}
}

View File

@ -0,0 +1,114 @@
/*
* Copyright (C) 2018 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.librecms.pagemodel;
import org.libreccm.pagemodel.AbstractComponentModelJsonConverter;
import org.libreccm.pagemodel.ComponentModel;
import org.libreccm.pagemodel.ConvertsComponentModel;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.enterprise.context.RequestScoped;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
@ConvertsComponentModel(componentModel = ItemListComponent.class)
public class ItemListComponentJsonConverter
extends AbstractComponentModelJsonConverter {
private static final String LIST_ORDER = "listOrder";
private static final String PAGE_SIZE = "pageSize";
private static final String LIMIT_TO_TYPE = "limitToType";
private static final String DESCENDING = "descending";
@Override
public JsonObject toJson(final ComponentModel componentModel) {
Objects.requireNonNull(componentModel);
if (!(componentModel instanceof ItemListComponent)) {
throw new IllegalArgumentException(
"This implementation does only handle ItemListComponents.");
}
final ItemListComponent itemList = (ItemListComponent) componentModel;
final JsonObjectBuilder builder = Json.createObjectBuilder();
convertBasePropertiesToJson(itemList, builder);
builder.add(DESCENDING, itemList.isDescending());
builder.add(LIMIT_TO_TYPE, itemList.getLimitToType());
builder.add(PAGE_SIZE, itemList.getPageSize());
final JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
if (itemList.getListOrder() != null) {
itemList
.getListOrder()
.stream()
.forEach(value -> arrayBuilder.add(value));
}
builder.add(LIST_ORDER, arrayBuilder.build());
return builder.build();
}
@Override
public ComponentModel fromJson(final JsonObject jsonObject) {
Objects.requireNonNull(jsonObject);
final ItemListComponent itemList = new ItemListComponent();
readBasePropertiesFromJson(jsonObject, itemList);
if (!jsonObject.isNull(DESCENDING)) {
itemList.setDescending(jsonObject.getBoolean(DESCENDING));
}
if (!jsonObject.isNull(LIMIT_TO_TYPE)) {
itemList.setLimitToType(jsonObject.getString(LIMIT_TO_TYPE));
}
if (!jsonObject.isNull(PAGE_SIZE)) {
itemList.setPageSize(jsonObject.getInt(PAGE_SIZE));
}
if (!jsonObject.isNull(LIST_ORDER)) {
itemList.setListOrder(
jsonObject
.getJsonArray(LIST_ORDER)
.stream()
.map(value -> value.toString())
.collect(Collectors.toList()));
}
return itemList;
}
}

View File

@ -0,0 +1,107 @@
/*
* Copyright (C) 2018 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.libreccm.pagemodel;
import java.util.Objects;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
/**
* Converter for the basic properties of a {
*
* @ComponentModel}. Can be used as base for implementations.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public abstract class AbstractComponentModelJsonConverter
implements ComponentModelJsonConverter {
/**
* Converts the basic properties of a {@link ComponentModel} to JSON.
*
* @param componentModel The {@link ComponentModel}.
* @param objectBuilder The {@link JsonObjectBuilder} to use.
*/
protected void convertBasePropertiesToJson(
final ComponentModel componentModel,
final JsonObjectBuilder objectBuilder) {
Objects.requireNonNull(componentModel);
Objects.requireNonNull(objectBuilder);
objectBuilder
.add("componentModelId",
Long.toString(componentModel.getComponentModelId()))
.add("uuid", componentModel.getUuid())
.add("modelUuid", componentModel.getModelUuid())
.add("key", componentModel.getKey())
.add("type", componentModel.getClass().getName());
if (componentModel.getIdAttribute() != null) {
objectBuilder.add("idAttribute",
componentModel.getIdAttribute());
}
if (componentModel.getClassAttribute() != null) {
objectBuilder.add("classAttribute",
componentModel.getClassAttribute());
}
if (componentModel.getStyleAttribute() != null) {
objectBuilder.add("styleAttribute",
componentModel.getStyleAttribute());
}
}
/**
* Read the basic properties of a {@link ComponentModel} from a
* {@link JsonObject}.
*
* @param jsonObject The {@link JsonObject}.
* @param componentModel The {@link ComponentModel}.
*/
protected void readBasePropertiesFromJson(
final JsonObject jsonObject, final ComponentModel componentModel) {
Objects.requireNonNull(jsonObject);
Objects.requireNonNull(componentModel);
componentModel.setComponentModelId(
jsonObject.getInt("componentModelId"));
componentModel.setUuid(jsonObject.getString("uuid"));
componentModel.setModelUuid(jsonObject.getString("modelUuid"));
componentModel.setKey(jsonObject.getString("key"));
if (jsonObject.getString("idAttribute") != null) {
componentModel.setIdAttribute(jsonObject.getString("idAttribute"));
}
if (jsonObject.getString("classAttribute") != null) {
componentModel
.setClassAttribute(jsonObject.getString("classAttribute"));
}
if (jsonObject.getString("styleAttribute") != null) {
componentModel
.setStyleAttribute(jsonObject.getString("styleAttribute"));
}
}
}

View File

@ -0,0 +1,46 @@
/*
* Copyright (C) 2018 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.libreccm.pagemodel;
import javax.json.JsonObject;
/**
* Interface which the JSON converters for {@link ComponentModel}s.
*
* @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}.
*/
ComponentModel fromJson(JsonObject jsonObject);
}

View File

@ -71,7 +71,7 @@ public class ComponentRendererManager {
+ "and type \"{}\"...", + "and type \"{}\"...",
componentModelClass.getName()); componentModelClass.getName());
final ComponentModelTypeLiteral literal = new ComponentModelTypeLiteral( final RenderComponentLiteral literal = new RenderComponentLiteral(
componentModelClass); componentModelClass);
final Instance<ComponentRenderer<?>> instance = componentRenderers final Instance<ComponentRenderer<?>> instance = componentRenderers
@ -99,7 +99,7 @@ public class ComponentRendererManager {
/** /**
* Annotation literal for the {@link RendersComponent} annotation. * Annotation literal for the {@link RendersComponent} annotation.
*/ */
private static class ComponentModelTypeLiteral private static class RenderComponentLiteral
extends AnnotationLiteral<RendersComponent> extends AnnotationLiteral<RendersComponent>
implements RendersComponent { implements RendersComponent {
@ -107,7 +107,7 @@ public class ComponentRendererManager {
private final Class<? extends ComponentModel> componentModel; private final Class<? extends ComponentModel> componentModel;
public ComponentModelTypeLiteral( public RenderComponentLiteral(
final Class<? extends ComponentModel> componentModel) { final Class<? extends ComponentModel> componentModel) {
this.componentModel = componentModel; this.componentModel = componentModel;
} }

View File

@ -0,0 +1,42 @@
/*
* Copyright (C) 2018 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.libreccm.pagemodel;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,
ElementType.METHOD,
ElementType.PARAMETER,
ElementType.TYPE})
public @interface ConvertsComponentModel {
Class<? extends ComponentModel> componentModel();
}

View File

@ -19,10 +19,13 @@
package org.libreccm.pagemodel.rs; package org.libreccm.pagemodel.rs;
import org.libreccm.core.CoreConstants; import org.libreccm.core.CoreConstants;
import org.libreccm.core.UnexpectedErrorException;
import org.libreccm.pagemodel.ComponentModel; import org.libreccm.pagemodel.ComponentModel;
import org.libreccm.pagemodel.ComponentModelJsonConverter;
import org.libreccm.pagemodel.ComponentModelRepository; import org.libreccm.pagemodel.ComponentModelRepository;
import org.libreccm.pagemodel.ContainerModel; import org.libreccm.pagemodel.ContainerModel;
import org.libreccm.pagemodel.ContainerModelManager; import org.libreccm.pagemodel.ContainerModelManager;
import org.libreccm.pagemodel.ConvertsComponentModel;
import org.libreccm.pagemodel.PageModel; import org.libreccm.pagemodel.PageModel;
import org.libreccm.security.AuthorizationRequired; import org.libreccm.security.AuthorizationRequired;
import org.libreccm.security.RequiresPrivilege; import org.libreccm.security.RequiresPrivilege;
@ -34,14 +37,17 @@ import java.beans.Introspector;
import java.beans.PropertyDescriptor; import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.enterprise.util.AnnotationLiteral;
import javax.inject.Inject; import javax.inject.Inject;
import javax.json.Json; import javax.json.Json;
import javax.json.JsonArray; import javax.json.JsonArray;
@ -79,6 +85,10 @@ public class Components {
@Inject @Inject
private PageModelsController controller; private PageModelsController controller;
@Inject
@Any
private Instance<ComponentModelJsonConverter> jsonConverters;
/** /**
* Retrieve all {@link ComponentModel} of a {@link ContainerModel}. * Retrieve all {@link ComponentModel} of a {@link ContainerModel}.
* *
@ -285,63 +295,74 @@ public class Components {
private JsonObject mapComponentModelToJson( private JsonObject mapComponentModelToJson(
final ComponentModel componentModel) { final ComponentModel componentModel) {
Objects.requireNonNull(componentModel); final Class<? extends ComponentModel> clazz = Objects
.requireNonNull(componentModel.getClass());
final JsonObjectBuilder objectBuilder = Json final ComponentModelJsonConverter jsonConverter
.createObjectBuilder() = findJsonConverter(clazz)
.add("componentModelId", .orElseThrow(() -> new WebApplicationException(String.format(
Long.toString(componentModel.getComponentModelId())) "No JSON converter available for component model \"%s\".",
.add("uuid", componentModel.getUuid()) clazz.getName())));
.add("modelUuid", componentModel.getModelUuid())
.add("key", componentModel.getKey())
.add("type", componentModel.getClass().getName());
if (componentModel.getIdAttribute() != null) { return jsonConverter.toJson(componentModel);
objectBuilder.add("idAttribute", componentModel.getIdAttribute());
}
if (componentModel.getClassAttribute() != null) { // Objects.requireNonNull(componentModel);
objectBuilder.add("classAttribute", //
componentModel.getClassAttribute()); // final JsonObjectBuilder objectBuilder = Json
} // .createObjectBuilder()
// .add("componentModelId",
if (componentModel.getStyleAttribute() != null) { // Long.toString(componentModel.getComponentModelId()))
objectBuilder.add("styleAttribute", // .add("uuid", componentModel.getUuid())
componentModel.getStyleAttribute()); // .add("modelUuid", componentModel.getModelUuid())
} // .add("key", componentModel.getKey())
// .add("type", componentModel.getClass().getName());
final Class<? extends ComponentModel> clazz = componentModel.getClass(); //
final BeanInfo beanInfo; // if (componentModel.getIdAttribute() != null) {
try { // objectBuilder.add("idAttribute", componentModel.getIdAttribute());
beanInfo = Introspector.getBeanInfo(clazz); // }
} catch (IntrospectionException ex) { //
throw new WebApplicationException(ex); // if (componentModel.getClassAttribute() != null) {
} // objectBuilder.add("classAttribute",
// componentModel.getClassAttribute());
for (final PropertyDescriptor propertyDescriptor // }
: beanInfo.getPropertyDescriptors()) { //
// if (componentModel.getStyleAttribute() != null) {
final Method readMethod = propertyDescriptor.getReadMethod(); // objectBuilder.add("styleAttribute",
final Object value; // componentModel.getStyleAttribute());
try { // }
value = readMethod.invoke(componentModel); //
} catch (IllegalAccessException // final Class<? extends ComponentModel> clazz = componentModel.getClass();
| InvocationTargetException ex) { // final BeanInfo beanInfo;
throw new WebApplicationException(ex); // try {
} // beanInfo = Introspector.getBeanInfo(clazz);
// } catch (IntrospectionException ex) {
final String valueStr; // throw new WebApplicationException(ex);
if (value == null) { // }
valueStr = ""; //
} else { // for (final PropertyDescriptor propertyDescriptor
valueStr = value.toString(); // : beanInfo.getPropertyDescriptors()) {
} //
// final Method readMethod = propertyDescriptor.getReadMethod();
objectBuilder.add(propertyDescriptor.getName(), valueStr); // final Object value;
// try {
} // value = readMethod.invoke(componentModel);
// } catch (IllegalAccessException
return objectBuilder.build(); // | InvocationTargetException ex) {
// throw new WebApplicationException(ex);
// }
//
// final String valueStr;
// if (value == null) {
// valueStr = "";
// } else {
// valueStr = value.toString();
// }
//
// objectBuilder.add(propertyDescriptor.getName(), valueStr);
//
// }
//
// return objectBuilder.build();
} }
/** /**
@ -547,4 +568,50 @@ public class Components {
} }
} }
private Optional<ComponentModelJsonConverter>
findJsonConverter(
final Class<? extends ComponentModel> componentModelClass) {
final ConvertsComponentModelLiteral literal
= new ConvertsComponentModelLiteral(
componentModelClass);
final Instance<ComponentModelJsonConverter> instance = jsonConverters
.select(literal);
if (instance.isUnsatisfied()) {
return Optional.empty();
} else if (instance.isAmbiguous()) {
throw new IllegalStateException(String.format(
"Multiple JSONConverter for \"%s\".",
componentModelClass.getName()));
} else {
final Iterator<ComponentModelJsonConverter> iterator = instance
.iterator();
@SuppressWarnings("unchecked")
final ComponentModelJsonConverter converter = iterator.next();
return Optional.of(converter);
}
}
private static class ConvertsComponentModelLiteral
extends AnnotationLiteral<ConvertsComponentModel>
implements ConvertsComponentModel {
private static final long serialVersionUID = 1L;
private final Class<? extends ComponentModel> componentModel;
public ConvertsComponentModelLiteral(
final Class<? extends ComponentModel> componentModel) {
this.componentModel = componentModel;
}
@Override
public Class<? extends ComponentModel> componentModel() {
return componentModel;
}
}
} }