More classes for the backport of Pages and Theming from 7.0

git-svn-id: https://svn.libreccm.org/ccm/trunk@5802 8810af33-2d31-482b-a856-94f89814c4df
master
jensp 2019-01-22 15:29:15 +00:00
parent 7212f10705
commit 4f756133e9
5 changed files with 392 additions and 0 deletions

View File

@ -0,0 +1,127 @@
/*
* Copyright (C) 2016 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.HashMap;
import java.util.Map;
import java.util.Optional;
/**
* An abstract base class for implementations of the {@link PageRenderer}
* interface providing some functionality needed by all implementations of the
* {@link PageRenderer} interface.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*
*/
public abstract class AbstractPageRenderer implements PageRenderer {
private ComponentRendererManager componentRendererManager;
/**
* Renders a {@code Page} based on a {@link PageModel}. This implementation
* first calls {@link #renderPage()} to create the page object. After that
* all {@link ComponentModel}s of the {@link PageModel} are processed and
* the component objects created by the {@link ComponentRenderer}s are added
* to the page.
*
* @param pageModel The {@link PageModel} to render.
* @param parameters Parameters provided by application which wants to
* render a {@link PageModel}. The parameters are passed
* the {@link ComponentRenderer}s.
*
* @return A map containing the results from rendering the components of the
* page model.
*/
@Override
public Map<String, Object> renderPage(
final PageModel pageModel, final Map<String, Object> parameters) {
final Map<String, Object> page = renderPage(parameters);
final ContainerModelCollection containers = pageModel
.getContainerModels();
while(containers.next()) {
final ContainerModel containerModel = containers
.getContainerModel();
final Map<String, Object> container = renderContainer(
containerModel, parameters);
page.put(containerModel.getKey(), container);
}
return page;
}
protected Map<String, Object> renderContainer(
final ContainerModel containerModel,
final Map<String, Object> parameters) {
final Map<String, Object> container = new HashMap<>();
container.put("key", containerModel.getKey());
final ComponentModelCollection components = containerModel
.getComponents();
while (components.next()) {
final ComponentModel componentModel = components.getComponentModel();
renderComponent(componentModel,
componentModel.getClass(),
parameters)
.ifPresent(component -> container.put(componentModel.getKey(),
component));
}
return container;
}
/**
* Helper method for rendering the components.
*
* @param <M> Generics variable for the type of rendered
* component
* @param componentModel The {@link ComponentModel} to process.
* @param componentModelClass The class of the {@link ComponentModel}.
* @param parameters Parameters provided by application which wants
* to render a {@link PageModel}. The parameters
* are passed the {@link ComponentRenderer}s.
*
* @return A map containing the results from rendering the components of the
* page model.
*/
protected <M extends ComponentModel> Optional<Object> renderComponent(
final ComponentModel componentModel,
final Class<M> componentModelClass,
final Map<String, Object> parameters) {
final Optional<ComponentRenderer<M>> renderer = componentRendererManager
.findComponentRenderer(componentModelClass);
if (renderer.isPresent()) {
@SuppressWarnings("unchecked")
final M model = (M) componentModel;
return Optional
.of(renderer.get().renderComponent(model, parameters));
} else {
return Optional.empty();
}
}
}

View File

@ -0,0 +1,67 @@
/*
* Copyright (C) 2016 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.Map;
/**
* A {@code ComponentRenderer} transforms a {@link ComponentModel} into a
* component.
*
* An implementation must be annotation with the {@link RendersComponent}
* qualifier annotation.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
* @param <M> Type of the model the component renderer processes.
*/
public interface ComponentRenderer<M extends ComponentModel> {
/**
* Renders a {@link ComponentModel}.
*
* The result of the rendering process is a {@link Map} which uses strings
* as key. The values are either Java primitive types or Collections. More
* exactly the values are objects of one the following types:
*
* <ul>
* <li>{@link Double}</li>
* <li>{@link Float}</li>
* <li>{@link Integer}</li>
* <li>{@link Long}</li>
* <li>{@link Short}</li>
* <li>{@link String}</li>
* <li>{@link List}</li>
* <li>{@link Map}</li>
* </ul>
*
* Other subtypes {@link Collection} are might be supported but there is no
* guarantee for that. The values in a collection must be one of the types
* in the list above. Collections might contain multiple types from the list
* above. The keys for a map should always be strings.
*
* @param componentModel The component model to render.
* @param parameters Parameters provided by the calling
* {@link PageRenderer}.
*
* @return A map representing the rendered component.
*/
Map<String, Object> renderComponent(M componentModel,
Map<String, Object> parameters);
}

View File

@ -0,0 +1,68 @@
package org.libreccm.pagemodel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class ComponentRendererManager {
private static final Logger LOGGER = LogManager
.getLogger(ComponentRendererManager.class);
private static final ComponentRendererManager INSTANCE
= new ComponentRendererManager();
private final Map<Class<?>, ComponentRenderer<?>> componentRenderers;
public ComponentRendererManager() {
componentRenderers = new HashMap<>();
}
public static ComponentRendererManager getInstance() {
return INSTANCE;
}
/**
* Find an implementation of the {@link ComponentRenderer} interface for a
* specific {@link ComponentModel}.
*
* @param <M> Generic variable for the subtype of
* {@link ComponentModel} which is produced by
* the {@link ComponentRenderer} implementation.
* @param componentModelClass The sub class of the {@link ComponentModel}
* for which is processed by the
* {@link ComponentRenderer}.
*
* @return An {@link Optional} containing the implementation of the
* {@link ComponentRenderer} interface for the specified parameters.
* If there is no implementation for the specified parameters an
* empty {@link Optional} is returned.
*/
@SuppressWarnings({"unchecked"})
public <M extends ComponentModel> Optional<ComponentRenderer<M>> findComponentRenderer(
final Class<M> componentModelClass) {
if (componentRenderers.containsKey(componentModelClass)) {
return Optional.of((ComponentRenderer<M>) componentRenderers
.get(componentModelClass));
} else {
return Optional.empty();
}
}
public <M extends ComponentModel> void registerComponentRenderer(
final Class<M> componentClass,
final ComponentRenderer<M> componentRenderer) {
componentRenderers.put(componentClass, componentRenderer);
}
}

View File

@ -0,0 +1,56 @@
/*
* Copyright (C) 2016 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.Date;
import java.util.Objects;
import java.util.UUID;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class PageModelRepository {
private static final PageModelRepository INSTANCE = new PageModelRepository();
private PageModelRepository() {
}
public static final PageModelRepository getInstance() {
return INSTANCE;
}
public void save(final PageModel pageModel) {
Objects.requireNonNull(pageModel);
pageModel.setLastModified(new Date());
if (pageModel.getUuid() == null) {
pageModel.setUuid(UUID.randomUUID().toString());
}
if (pageModel.getModelUuid() == null) {
pageModel.setModelUuid(pageModel.getUuid());
}
}
}

View File

@ -0,0 +1,74 @@
/*
* Copyright (C) 2016 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.Map;
/**
* Interface for page renderers. A page renderer is invoked to render a page of
* specific type. An implementation should be a CDI bean which is annotated with
* the qualifier {@link RendersPageModelType}.
*
* An implementation should add all default components which have to be present
* in page. The {@link PageModel} should only specify
* <strong>additional</strong> components.
*
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public interface PageRenderer {
/**
* Render a page with the default components for a application. An
* implementation of {@link #renderPage(org.libreccm.pagemodel.PageModel)}
* should use this method for creating the default page.
*
* The result of the rendering process is a map with the values of the
* {@link ComponentModel#key} property as key and the result of rendering
* the component as value.
*
* @param parameters Parameters provided by application which wants to
* render a {@link PageModel}. The parameters are passed
* the {@link ComponentRenderer}s.
*
* @return A page with the default components.
*/
Map<String, Object> renderPage(Map<String, Object> parameters);
/**
* Render a page using the provided {@link PageModel}. Implementations
* should call the implementation of {@link #renderPage()} for creating the
* basic page with the default components.
*
* The result of the rendering process is a map with the values of the
* {@link ComponentModel#key} property as key and the result of rendering
* the component as value.
*
*
* @param pageModel The {@link PageModel} from which the page is generated.
* @param parameters Parameters provided by application which wants to
* render a {@link PageModel}. The parameters are passed
* the {@link ComponentRenderer}s.
*
* @return The page generated from the provided {@link PageModel}.
*/
Map<String, Object> renderPage(PageModel pageModel,
Map<String, Object> parameters);
}