CCM NG: More functions to retrieve PageModel data
git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@5379 8810af33-2d31-482b-a856-94f89814c4df
parent
bd96689595
commit
a4cd8c27da
|
|
@ -35,6 +35,8 @@ import javax.persistence.Inheritance;
|
||||||
import javax.persistence.InheritanceType;
|
import javax.persistence.InheritanceType;
|
||||||
import javax.persistence.JoinColumn;
|
import javax.persistence.JoinColumn;
|
||||||
import javax.persistence.ManyToOne;
|
import javax.persistence.ManyToOne;
|
||||||
|
import javax.persistence.NamedQueries;
|
||||||
|
import javax.persistence.NamedQuery;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -50,6 +52,13 @@ import javax.persistence.Table;
|
||||||
@Entity
|
@Entity
|
||||||
@Inheritance(strategy = InheritanceType.JOINED)
|
@Inheritance(strategy = InheritanceType.JOINED)
|
||||||
@Table(name = "PAGE_MODEL_COMPONENT_MODELS", schema = CoreConstants.DB_SCHEMA)
|
@Table(name = "PAGE_MODEL_COMPONENT_MODELS", schema = CoreConstants.DB_SCHEMA)
|
||||||
|
@NamedQueries({
|
||||||
|
@NamedQuery(name = "ComponentModel.findComponentByContainerAndKey",
|
||||||
|
query = "SELECT c "
|
||||||
|
+ "FROM ComponentModel c "
|
||||||
|
+ "WHERE c.container = :container "
|
||||||
|
+ "AND c.key = :key")
|
||||||
|
})
|
||||||
public class ComponentModel implements Serializable {
|
public class ComponentModel implements Serializable {
|
||||||
|
|
||||||
private static final long serialVersionUID = 8585775139379396806L;
|
private static final long serialVersionUID = 8585775139379396806L;
|
||||||
|
|
|
||||||
|
|
@ -162,4 +162,22 @@ public class ComponentModelRepository
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
|
public Optional<ComponentModel> findComponentByContainerAndKey(
|
||||||
|
final ContainerModel containerModel,
|
||||||
|
final String componentKey) {
|
||||||
|
|
||||||
|
final TypedQuery<ComponentModel> query = getEntityManager()
|
||||||
|
.createNamedQuery("ComponentModel.findComponentByContainerAndKey",
|
||||||
|
ComponentModel.class);
|
||||||
|
query.setParameter("container", containerModel);
|
||||||
|
query.setParameter("key", componentKey);
|
||||||
|
|
||||||
|
try {
|
||||||
|
return Optional.of(query.getSingleResult());
|
||||||
|
} catch(NoResultException ex) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -88,13 +88,14 @@ public class ContainerModelRepository
|
||||||
final String key, final PageModel pageModel) {
|
final String key, final PageModel pageModel) {
|
||||||
|
|
||||||
final TypedQuery<ContainerModel> query = getEntityManager()
|
final TypedQuery<ContainerModel> query = getEntityManager()
|
||||||
.createNamedQuery("ContainerModel.findByKeyAndPage", ContainerModel.class);
|
.createNamedQuery("ContainerModel.findByKeyAndPage",
|
||||||
|
ContainerModel.class);
|
||||||
query.setParameter("key", key);
|
query.setParameter("key", key);
|
||||||
query.setParameter("pageModel", pageModel);
|
query.setParameter("pageModel", pageModel);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return Optional.of(query.getSingleResult());
|
return Optional.of(query.getSingleResult());
|
||||||
} catch(NoResultException ex) {
|
} catch (NoResultException ex) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,136 @@
|
||||||
|
/*
|
||||||
|
* 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.rs;
|
||||||
|
|
||||||
|
import org.libreccm.core.CoreConstants;
|
||||||
|
import org.libreccm.pagemodel.ComponentModel;
|
||||||
|
import org.libreccm.pagemodel.ContainerModel;
|
||||||
|
import org.libreccm.pagemodel.PageModel;
|
||||||
|
import org.libreccm.security.AuthorizationRequired;
|
||||||
|
import org.libreccm.security.RequiresPrivilege;
|
||||||
|
import org.libreccm.web.CcmApplication;
|
||||||
|
|
||||||
|
import javax.enterprise.context.RequestScoped;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.json.Json;
|
||||||
|
import javax.json.JsonArray;
|
||||||
|
import javax.json.JsonArrayBuilder;
|
||||||
|
import javax.json.JsonObject;
|
||||||
|
import javax.json.JsonObjectBuilder;
|
||||||
|
import javax.transaction.Transactional;
|
||||||
|
import javax.ws.rs.GET;
|
||||||
|
import javax.ws.rs.Path;
|
||||||
|
import javax.ws.rs.PathParam;
|
||||||
|
import javax.ws.rs.Produces;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||||
|
*/
|
||||||
|
@RequestScoped
|
||||||
|
@Path("/")
|
||||||
|
public class Components {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private PageModelsController controller;
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Path(PageModelsApp.COMPONENTS_PATH)
|
||||||
|
@Produces("application/json; charset=utf-8")
|
||||||
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
|
@AuthorizationRequired
|
||||||
|
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||||
|
public JsonArray getComponents(
|
||||||
|
@PathParam(PageModelsApp.APP_NAME) final String appPath,
|
||||||
|
@PathParam(PageModelsApp.PAGE_MODEL_NAME) final String pageModelName,
|
||||||
|
@PathParam(PageModelsApp.CONTAINER_KEY) final String containerKey) {
|
||||||
|
|
||||||
|
final CcmApplication app = controller.findCcmApplication(
|
||||||
|
String.format("/%s/", appPath));
|
||||||
|
final PageModel pageModel = controller.findPageModel(app,
|
||||||
|
pageModelName);
|
||||||
|
final ContainerModel container = controller.findContainer(app,
|
||||||
|
pageModel,
|
||||||
|
containerKey);
|
||||||
|
|
||||||
|
final JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
|
||||||
|
container
|
||||||
|
.getComponents()
|
||||||
|
.stream()
|
||||||
|
.map(component -> mapComponentModelToJson(component))
|
||||||
|
.forEach(arrayBuilder::add);
|
||||||
|
|
||||||
|
return arrayBuilder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Path(PageModelsApp.COMPONENT_PATH)
|
||||||
|
@Produces("application/json; charset=utf-8")
|
||||||
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
|
@AuthorizationRequired
|
||||||
|
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||||
|
public JsonObject getComponent(
|
||||||
|
@PathParam(PageModelsApp.APP_NAME) final String appPath,
|
||||||
|
@PathParam(PageModelsApp.PAGE_MODEL_NAME) final String pageModelName,
|
||||||
|
@PathParam(PageModelsApp.CONTAINER_KEY) final String containerKey,
|
||||||
|
@PathParam(PageModelsApp.COMPONENT_KEY) final String componentKey) {
|
||||||
|
|
||||||
|
final CcmApplication app = controller.findCcmApplication(
|
||||||
|
String.format("/%s/", appPath));
|
||||||
|
final PageModel pageModel = controller.findPageModel(app,
|
||||||
|
pageModelName);
|
||||||
|
final ContainerModel container = controller.findContainer(app,
|
||||||
|
pageModel,
|
||||||
|
containerKey);
|
||||||
|
final ComponentModel component = controller
|
||||||
|
.findComponentModel(app, pageModel, container, componentKey);
|
||||||
|
|
||||||
|
return mapComponentModelToJson(component);
|
||||||
|
}
|
||||||
|
|
||||||
|
private JsonObject mapComponentModelToJson(
|
||||||
|
final ComponentModel componentModel) {
|
||||||
|
|
||||||
|
final JsonObjectBuilder objectBuilder = Json
|
||||||
|
.createObjectBuilder()
|
||||||
|
.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());
|
||||||
|
}
|
||||||
|
|
||||||
|
return objectBuilder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,113 @@
|
||||||
|
/*
|
||||||
|
* 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.rs;
|
||||||
|
|
||||||
|
import org.libreccm.core.CoreConstants;
|
||||||
|
import org.libreccm.pagemodel.ContainerModel;
|
||||||
|
import org.libreccm.pagemodel.PageModel;
|
||||||
|
import org.libreccm.security.AuthorizationRequired;
|
||||||
|
import org.libreccm.security.RequiresPrivilege;
|
||||||
|
import org.libreccm.web.CcmApplication;
|
||||||
|
|
||||||
|
import javax.enterprise.context.RequestScoped;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.json.Json;
|
||||||
|
import javax.json.JsonArray;
|
||||||
|
import javax.json.JsonArrayBuilder;
|
||||||
|
import javax.json.JsonObject;
|
||||||
|
import javax.transaction.Transactional;
|
||||||
|
import javax.ws.rs.GET;
|
||||||
|
import javax.ws.rs.Path;
|
||||||
|
import javax.ws.rs.PathParam;
|
||||||
|
import javax.ws.rs.Produces;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||||
|
*/
|
||||||
|
@RequestScoped
|
||||||
|
@Path("/")
|
||||||
|
public class Containers {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private PageModelsController controller;
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Path(PageModelsApp.CONTAINERS_PATH)
|
||||||
|
@Produces("application/json; charset=utf-8")
|
||||||
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
|
@AuthorizationRequired
|
||||||
|
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||||
|
public JsonArray getContainers(
|
||||||
|
@PathParam(PageModelsApp.APP_NAME) final String appPath,
|
||||||
|
@PathParam(PageModelsApp.PAGE_MODEL_NAME) final String pageModelName) {
|
||||||
|
|
||||||
|
final CcmApplication app = controller.findCcmApplication(
|
||||||
|
String.format("/%s/", appPath));
|
||||||
|
|
||||||
|
final PageModel pageModel = controller.findPageModel(app,
|
||||||
|
pageModelName);
|
||||||
|
|
||||||
|
final JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
|
||||||
|
pageModel
|
||||||
|
.getContainers()
|
||||||
|
.stream()
|
||||||
|
.map(containerModel -> mapContainerModelToJson(containerModel))
|
||||||
|
.forEach(arrayBuilder::add);
|
||||||
|
|
||||||
|
return arrayBuilder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Path(PageModelsApp.CONTAINER_PATH)
|
||||||
|
@Produces("application/json; charset=utf-8")
|
||||||
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
|
@AuthorizationRequired
|
||||||
|
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||||
|
public JsonObject getContainer(
|
||||||
|
@PathParam(PageModelsApp.APP_NAME) final String appPath,
|
||||||
|
@PathParam(PageModelsApp.PAGE_MODEL_NAME) final String pageModelName,
|
||||||
|
@PathParam(PageModelsApp.CONTAINER_KEY) final String containerKey) {
|
||||||
|
|
||||||
|
final CcmApplication app = controller.findCcmApplication(
|
||||||
|
String.format("/%s/", appPath));
|
||||||
|
|
||||||
|
final PageModel pageModel = controller.findPageModel(app,
|
||||||
|
pageModelName);
|
||||||
|
|
||||||
|
final ContainerModel container = controller.findContainer(app,
|
||||||
|
pageModel,
|
||||||
|
containerKey);
|
||||||
|
|
||||||
|
return mapContainerModelToJson(container);
|
||||||
|
}
|
||||||
|
|
||||||
|
private JsonObject mapContainerModelToJson(
|
||||||
|
final ContainerModel containerModel) {
|
||||||
|
|
||||||
|
return Json
|
||||||
|
.createObjectBuilder()
|
||||||
|
.add("containerId", Long.toString(containerModel.getContainerId()))
|
||||||
|
.add("uuid", containerModel.getUuid())
|
||||||
|
.add("containerUuid", containerModel.getContainerUuid())
|
||||||
|
.add("key", containerModel.getKey())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -20,30 +20,25 @@ package org.libreccm.pagemodel.rs;
|
||||||
|
|
||||||
import org.libreccm.core.CoreConstants;
|
import org.libreccm.core.CoreConstants;
|
||||||
import org.libreccm.l10n.GlobalizationHelper;
|
import org.libreccm.l10n.GlobalizationHelper;
|
||||||
import org.libreccm.pagemodel.ComponentModel;
|
|
||||||
import org.libreccm.pagemodel.ComponentModelRepository;
|
import org.libreccm.pagemodel.ComponentModelRepository;
|
||||||
import org.libreccm.pagemodel.ContainerModel;
|
|
||||||
import org.libreccm.pagemodel.ContainerModelRepository;
|
import org.libreccm.pagemodel.ContainerModelRepository;
|
||||||
import org.libreccm.pagemodel.PageModel;
|
import org.libreccm.pagemodel.PageModel;
|
||||||
import org.libreccm.pagemodel.PageModelRepository;
|
import org.libreccm.pagemodel.PageModelRepository;
|
||||||
import org.libreccm.security.AuthorizationRequired;
|
import org.libreccm.security.AuthorizationRequired;
|
||||||
import org.libreccm.security.RequiresPrivilege;
|
import org.libreccm.security.RequiresPrivilege;
|
||||||
import org.libreccm.web.ApplicationRepository;
|
|
||||||
import org.libreccm.web.CcmApplication;
|
import org.libreccm.web.CcmApplication;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import javax.enterprise.context.RequestScoped;
|
import javax.enterprise.context.RequestScoped;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.json.Json;
|
import javax.json.Json;
|
||||||
import javax.json.JsonArray;
|
import javax.json.JsonArray;
|
||||||
import javax.json.JsonArrayBuilder;
|
import javax.json.JsonArrayBuilder;
|
||||||
import javax.json.JsonObject;
|
import javax.json.JsonObject;
|
||||||
import javax.json.JsonObjectBuilder;
|
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
|
import javax.ws.rs.Consumes;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
import javax.ws.rs.NotFoundException;
|
import javax.ws.rs.NotFoundException;
|
||||||
|
import javax.ws.rs.PUT;
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.PathParam;
|
import javax.ws.rs.PathParam;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
|
|
@ -53,11 +48,11 @@ import javax.ws.rs.Produces;
|
||||||
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||||
*/
|
*/
|
||||||
@RequestScoped
|
@RequestScoped
|
||||||
@Path("/{appPath}")
|
@Path("/")
|
||||||
public class PageModels {
|
public class PageModels {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private ApplicationRepository appRepo;
|
private PageModelsController controller;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private ComponentModelRepository componentModelRepo;
|
private ComponentModelRepository componentModelRepo;
|
||||||
|
|
@ -77,7 +72,7 @@ public class PageModels {
|
||||||
// @Transactional(Transactional.TxType.REQUIRED)
|
// @Transactional(Transactional.TxType.REQUIRED)
|
||||||
// @AuthorizationRequired
|
// @AuthorizationRequired
|
||||||
// @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
// @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||||
// public List<Map<String, String>> findAllPageModels(
|
// public List<Map<String, String>> getAllPageModels(
|
||||||
// @PathParam("appPath") String appPath) {
|
// @PathParam("appPath") String appPath) {
|
||||||
//
|
//
|
||||||
// final CcmApplication app = findCcmApplication(
|
// final CcmApplication app = findCcmApplication(
|
||||||
|
|
@ -90,16 +85,16 @@ public class PageModels {
|
||||||
// }
|
// }
|
||||||
@GET
|
@GET
|
||||||
// @Path("/{appPath}")
|
// @Path("/{appPath}")
|
||||||
@Path("/")
|
@Path(PageModelsApp.PAGE_MODELS_PATH)
|
||||||
@Produces("application/json; charset=utf-8")
|
@Produces("application/json; charset=utf-8")
|
||||||
@Transactional(Transactional.TxType.REQUIRED)
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
@AuthorizationRequired
|
@AuthorizationRequired
|
||||||
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||||
public JsonArray findAllPageModels(
|
public JsonArray getAllPageModels(
|
||||||
@PathParam("appPath") String appPath) {
|
@PathParam(PageModelsApp.APP_NAME) String appPath) {
|
||||||
|
|
||||||
final CcmApplication app = findCcmApplication(
|
final CcmApplication app = controller
|
||||||
String.format("/%s/", appPath));
|
.findCcmApplication(String.format("/%s/", appPath));
|
||||||
final JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
|
final JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
|
||||||
pageModelRepo
|
pageModelRepo
|
||||||
.findDraftByApplication(app)
|
.findDraftByApplication(app)
|
||||||
|
|
@ -111,135 +106,49 @@ public class PageModels {
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
// @Path("/{appPath}/{pageModelName}")
|
@Path(PageModelsApp.PAGE_MODEL_PATH)
|
||||||
@Path("/{pageModelName}")
|
|
||||||
@Produces("application/json; charset=utf-8")
|
@Produces("application/json; charset=utf-8")
|
||||||
@Transactional(Transactional.TxType.REQUIRED)
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
@AuthorizationRequired
|
@AuthorizationRequired
|
||||||
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||||
public JsonObject findPageModel(
|
public JsonObject getPageModel(
|
||||||
@PathParam("appPath") final String appPath,
|
@PathParam(PageModelsApp.APP_NAME) final String appPath,
|
||||||
@PathParam("pageModelName") final String pageModelName) {
|
@PathParam(PageModelsApp.PAGE_MODEL_NAME) final String pageModelName) {
|
||||||
|
|
||||||
final CcmApplication app = findCcmApplication(
|
final CcmApplication app = controller
|
||||||
String.format("/%s/", appPath));
|
.findCcmApplication(String.format("/%s/", appPath));
|
||||||
|
final PageModel pageModel = controller.findPageModel(app,
|
||||||
final PageModel pageModel = pageModelRepo
|
pageModelName);
|
||||||
.findDraftByApplicationAndName(app, pageModelName)
|
|
||||||
.orElseThrow(() -> new NotFoundException(String.format(
|
|
||||||
"No PageModel with name \"%s\" for application \"%s\".",
|
|
||||||
pageModelName, appPath)));
|
|
||||||
return mapPageModelToJson(pageModel);
|
return mapPageModelToJson(pageModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@PUT
|
||||||
// @Path("/{appPath}/{pageModelName}/containers")
|
@Path(PageModelsApp.PAGE_MODEL_PATH)
|
||||||
@Path("/{pageModelName}/containers")
|
@Consumes("application/json; charset=utf-8")
|
||||||
@Produces("application/json; charset=utf-8")
|
@Produces("application/json; charset=utf-8")
|
||||||
@Transactional(Transactional.TxType.REQUIRED)
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
@AuthorizationRequired
|
@AuthorizationRequired
|
||||||
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||||
public JsonArray getContainers(
|
public JsonObject putPageModel(
|
||||||
@PathParam("appPath") final String appPath,
|
@PathParam(PageModelsApp.APP_NAME) final String appPath,
|
||||||
@PathParam("pageModelName") final String pageModelName) {
|
@PathParam(PageModelsApp.PAGE_MODEL_NAME) final String pageModelName,
|
||||||
|
final JsonObject pageModelJson) {
|
||||||
|
|
||||||
final CcmApplication app = findCcmApplication(
|
final CcmApplication app = controller
|
||||||
String.format("/%s/", appPath));
|
.findCcmApplication(String.format("/%s/", appPath));
|
||||||
|
|
||||||
final PageModel pageModel = pageModelRepo
|
final PageModel pageModel;
|
||||||
.findDraftByApplicationAndName(app, pageModelName)
|
if (controller.existsPageModel(app, pageModelName)) {
|
||||||
.orElseThrow(() -> new NotFoundException(String.format(
|
pageModel = controller.findPageModel(app, pageModelName);
|
||||||
"No PageModel with name \"%s\" for application \"%s\".",
|
} else {
|
||||||
pageModelName, appPath)));
|
pageModel = new PageModel();
|
||||||
|
pageModel.setApplication(app);
|
||||||
|
}
|
||||||
|
pageModel.setName(pageModelName);
|
||||||
|
|
||||||
final JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
|
controller.savePageModel(pageModel);
|
||||||
pageModel
|
|
||||||
.getContainers()
|
|
||||||
.stream()
|
|
||||||
.map(containerModel -> mapContainerModelToJson(containerModel))
|
|
||||||
.forEach(arrayBuilder::add);
|
|
||||||
|
|
||||||
return arrayBuilder.build();
|
return mapPageModelToJson(controller.findPageModel(app, pageModelName));
|
||||||
}
|
|
||||||
|
|
||||||
@GET
|
|
||||||
@Path("/{pageModelName}/containers/{key}")
|
|
||||||
@Produces("application/json; charset=utf-8")
|
|
||||||
@Transactional(Transactional.TxType.REQUIRED)
|
|
||||||
@AuthorizationRequired
|
|
||||||
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
|
||||||
public JsonObject getContainer(
|
|
||||||
@PathParam("appPath") final String appPath,
|
|
||||||
@PathParam("pageModelName") final String pageModelName,
|
|
||||||
@PathParam("key") final String key) {
|
|
||||||
|
|
||||||
final CcmApplication app = findCcmApplication(
|
|
||||||
String.format("/%s/", appPath));
|
|
||||||
|
|
||||||
final PageModel pageModel = pageModelRepo
|
|
||||||
.findDraftByApplicationAndName(app, pageModelName)
|
|
||||||
.orElseThrow(() -> new NotFoundException(String.format(
|
|
||||||
"No PageModel with name \"%s\" for application \"%s\".",
|
|
||||||
pageModelName, appPath)));
|
|
||||||
|
|
||||||
final ContainerModel container = containerRepo
|
|
||||||
.findContainerByKeyAndPageModel(key, pageModel)
|
|
||||||
.orElseThrow(() -> new NotFoundException(String
|
|
||||||
.format("The PageModel \"%s\" of application \"%s\" does not have "
|
|
||||||
+ "a container identified by the key \"%s\".",
|
|
||||||
pageModelName,
|
|
||||||
appPath,
|
|
||||||
key)));
|
|
||||||
|
|
||||||
return mapContainerModelToJson(container);
|
|
||||||
}
|
|
||||||
|
|
||||||
@GET
|
|
||||||
@Path("/{pageModelName}/containers/{key}/components")
|
|
||||||
@Produces("application/json; charset=utf-8")
|
|
||||||
@Transactional(Transactional.TxType.REQUIRED)
|
|
||||||
@AuthorizationRequired
|
|
||||||
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
|
||||||
public JsonArray getComponents(
|
|
||||||
@PathParam("appPath") final String appPath,
|
|
||||||
@PathParam("pageModelName") final String pageModelName,
|
|
||||||
@PathParam("key") final String key) {
|
|
||||||
|
|
||||||
final CcmApplication app = findCcmApplication(
|
|
||||||
String.format("/%s/", appPath));
|
|
||||||
|
|
||||||
final PageModel pageModel = pageModelRepo
|
|
||||||
.findDraftByApplicationAndName(app, pageModelName)
|
|
||||||
.orElseThrow(() -> new NotFoundException(String.format(
|
|
||||||
"No PageModel with name \"%s\" for application \"%s\".",
|
|
||||||
pageModelName, appPath)));
|
|
||||||
|
|
||||||
final ContainerModel container = containerRepo
|
|
||||||
.findContainerByKeyAndPageModel(key, pageModel)
|
|
||||||
.orElseThrow(() -> new NotFoundException(String
|
|
||||||
.format("The PageModel \"%s\" of application \"%s\" does not have "
|
|
||||||
+ "a container identified by the key \"%s\".",
|
|
||||||
pageModelName,
|
|
||||||
appPath,
|
|
||||||
key)));
|
|
||||||
|
|
||||||
final JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
|
|
||||||
container
|
|
||||||
.getComponents()
|
|
||||||
.stream()
|
|
||||||
.map(component -> mapComponentModelToJson(component))
|
|
||||||
.forEach(arrayBuilder::add);
|
|
||||||
|
|
||||||
return arrayBuilder.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
private CcmApplication findCcmApplication(final String appPath) {
|
|
||||||
|
|
||||||
return appRepo
|
|
||||||
.retrieveApplicationForPath(appPath)
|
|
||||||
.orElseThrow(() -> new NotFoundException(String
|
|
||||||
.format("No application with path \"%s\" found.",
|
|
||||||
appPath)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private JsonObject mapPageModelToJson(final PageModel pageModel) {
|
private JsonObject mapPageModelToJson(final PageModel pageModel) {
|
||||||
|
|
@ -261,65 +170,4 @@ public class PageModels {
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, String> mapPageModelToDataMap(final PageModel pageModel) {
|
|
||||||
|
|
||||||
final Map<String, String> dataMap = new HashMap<>();
|
|
||||||
|
|
||||||
dataMap.put("description",
|
|
||||||
globalizationHelper
|
|
||||||
.getValueFromLocalizedString(pageModel.getDescription()));
|
|
||||||
dataMap.put("modelUuid", pageModel.getModelUuid());
|
|
||||||
dataMap.put("name", pageModel.getName());
|
|
||||||
dataMap.put("pageModelId", Long.toString(pageModel.getPageModelId()));
|
|
||||||
dataMap.put("title",
|
|
||||||
globalizationHelper
|
|
||||||
.getValueFromLocalizedString(pageModel.getTitle()));
|
|
||||||
dataMap.put("type", pageModel.getType());
|
|
||||||
dataMap.put("uuid", pageModel.getUuid());
|
|
||||||
dataMap.put("version", pageModel.getVersion().toString());
|
|
||||||
|
|
||||||
return dataMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
private JsonObject mapContainerModelToJson(
|
|
||||||
final ContainerModel containerModel) {
|
|
||||||
|
|
||||||
return Json
|
|
||||||
.createObjectBuilder()
|
|
||||||
.add("containerId", Long.toString(containerModel.getContainerId()))
|
|
||||||
.add("uuid", containerModel.getUuid())
|
|
||||||
.add("containerUuid", containerModel.getContainerUuid())
|
|
||||||
.add("key", containerModel.getKey())
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
private JsonObject mapComponentModelToJson(
|
|
||||||
final ComponentModel componentModel) {
|
|
||||||
|
|
||||||
final JsonObjectBuilder objectBuilder = Json
|
|
||||||
.createObjectBuilder()
|
|
||||||
.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());
|
|
||||||
}
|
|
||||||
|
|
||||||
return objectBuilder.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,11 +35,36 @@ import javax.ws.rs.core.Application;
|
||||||
@ApplicationPath("/page-models")
|
@ApplicationPath("/page-models")
|
||||||
public class PageModelsApp extends Application {
|
public class PageModelsApp extends Application {
|
||||||
|
|
||||||
|
protected static final String APP_NAME = "appName";
|
||||||
|
protected static final String PAGE_MODEL_NAME = "pageModelName";
|
||||||
|
protected static final String CONTAINER_KEY = "containerKey";
|
||||||
|
protected static final String COMPONENT_KEY = "componentKey";
|
||||||
|
|
||||||
|
protected static final String PAGE_MODELS_PATH = "/{" + APP_NAME + "}";
|
||||||
|
protected static final String PAGE_MODEL_PATH = PAGE_MODELS_PATH
|
||||||
|
+ "/{"
|
||||||
|
+ PAGE_MODEL_NAME
|
||||||
|
+ "}";
|
||||||
|
protected static final String CONTAINERS_PATH = PAGE_MODEL_PATH
|
||||||
|
+ "/containers";
|
||||||
|
protected static final String CONTAINER_PATH = CONTAINERS_PATH
|
||||||
|
+ "/{"
|
||||||
|
+ CONTAINER_KEY
|
||||||
|
+ "}";
|
||||||
|
protected static final String COMPONENTS_PATH = CONTAINER_PATH
|
||||||
|
+ "/components";
|
||||||
|
protected static final String COMPONENT_PATH = COMPONENTS_PATH
|
||||||
|
+ "/{"
|
||||||
|
+ COMPONENT_KEY
|
||||||
|
+ "}";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Class<?>> getClasses() {
|
public Set<Class<?>> getClasses() {
|
||||||
|
|
||||||
final Set<Class<?>> classes = new HashSet<>();
|
final Set<Class<?>> classes = new HashSet<>();
|
||||||
classes.add(PageModels.class);
|
classes.add(PageModels.class);
|
||||||
|
classes.add(Containers.class);
|
||||||
|
classes.add(Components.class);
|
||||||
|
|
||||||
return classes;
|
return classes;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,127 @@
|
||||||
|
/*
|
||||||
|
* 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.rs;
|
||||||
|
|
||||||
|
import org.libreccm.pagemodel.ComponentModel;
|
||||||
|
import org.libreccm.pagemodel.ComponentModelRepository;
|
||||||
|
import org.libreccm.pagemodel.ContainerModel;
|
||||||
|
import org.libreccm.pagemodel.ContainerModelRepository;
|
||||||
|
import org.libreccm.pagemodel.PageModel;
|
||||||
|
import org.libreccm.pagemodel.PageModelRepository;
|
||||||
|
import org.libreccm.web.ApplicationRepository;
|
||||||
|
import org.libreccm.web.CcmApplication;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import javax.enterprise.context.RequestScoped;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.transaction.Transactional;
|
||||||
|
import javax.ws.rs.NotFoundException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||||
|
*/
|
||||||
|
@RequestScoped
|
||||||
|
class PageModelsController {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private ApplicationRepository appRepo;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private ComponentModelRepository componentModelRepo;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private ContainerModelRepository containerRepo;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private PageModelRepository pageModelRepo;
|
||||||
|
|
||||||
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
|
protected CcmApplication findCcmApplication(final String appPath) {
|
||||||
|
|
||||||
|
return appRepo
|
||||||
|
.retrieveApplicationForPath(Objects.requireNonNull(appPath))
|
||||||
|
.orElseThrow(() -> new NotFoundException(String
|
||||||
|
.format("No application with path \"%s\" found.",
|
||||||
|
appPath)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
|
protected ComponentModel findComponentModel(
|
||||||
|
final CcmApplication app,
|
||||||
|
final PageModel pageModel,
|
||||||
|
final ContainerModel containerModel,
|
||||||
|
final String componentKey) {
|
||||||
|
|
||||||
|
return componentModelRepo
|
||||||
|
.findComponentByContainerAndKey(containerModel, componentKey)
|
||||||
|
.orElseThrow(() -> new NotFoundException(String
|
||||||
|
.format("The Container \"%s\" of the PageModel \"%s\" of application"
|
||||||
|
+ "\"%s\" does not contain a component with the key \"%s\".",
|
||||||
|
containerModel.getKey(),
|
||||||
|
pageModel.getName(),
|
||||||
|
app.getPrimaryUrl(),
|
||||||
|
componentKey)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
|
protected ContainerModel findContainer(final CcmApplication app,
|
||||||
|
final PageModel pageModel,
|
||||||
|
final String containerKey) {
|
||||||
|
|
||||||
|
return containerRepo
|
||||||
|
.findContainerByKeyAndPageModel(
|
||||||
|
Objects.requireNonNull(containerKey),
|
||||||
|
Objects.requireNonNull(pageModel))
|
||||||
|
.orElseThrow(() -> new NotFoundException(String
|
||||||
|
.format("The PageModel \"%s\" of application \"%s\" does not have "
|
||||||
|
+ "a container identified by the key \"%s\".",
|
||||||
|
pageModel.getName(),
|
||||||
|
app.getPrimaryUrl(),
|
||||||
|
containerKey)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
|
protected boolean existsPageModel(final CcmApplication app,
|
||||||
|
final String pageModelName) {
|
||||||
|
return pageModelRepo
|
||||||
|
.findDraftByApplicationAndName(app, pageModelName)
|
||||||
|
.isPresent();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
|
protected PageModel findPageModel(final CcmApplication app,
|
||||||
|
final String pageModelName) {
|
||||||
|
|
||||||
|
return pageModelRepo
|
||||||
|
.findDraftByApplicationAndName(
|
||||||
|
Objects.requireNonNull(app),
|
||||||
|
Objects.requireNonNull(pageModelName))
|
||||||
|
.orElseThrow(() -> new NotFoundException(String.format(
|
||||||
|
"No PageModel with name \"%s\" for application \"%s\".",
|
||||||
|
pageModelName, app.getPrimaryUrl())));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
|
protected void savePageModel(final PageModel pageModel) {
|
||||||
|
pageModelRepo.save(pageModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue