Optimizations for RESTful API for managing containers

Former-commit-id: 96dae5876b
restapi
Jens Pelzetter 2020-08-06 16:38:13 +02:00
parent 429ceeea82
commit 30cd9bae25
4 changed files with 198 additions and 10 deletions

View File

@ -0,0 +1,123 @@
/*
* Copyright (C) 2020 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.api.pagemodels.dto;
import org.libreccm.pagemodel.ComponentModel;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class ComponentModelDto {
private long componentModelId;
private String uuid;
private String modelUuid;
private String key;
private String type;
private String idAttribute;
private String classAttribute;
private String styleAttribute;
ComponentModelDto() {
// Nothing
}
ComponentModelDto(final ComponentModel fromComponentModel) {
this.componentModelId =fromComponentModel.getComponentModelId();
this.uuid = fromComponentModel.getUuid();
this.modelUuid = fromComponentModel.getModelUuid();
this.key = fromComponentModel.getKey();
this.idAttribute = fromComponentModel.getIdAttribute();
this.classAttribute = fromComponentModel.getClassAttribute();
this.styleAttribute = fromComponentModel.getStyleAttribute();
}
public long getComponentModelId() {
return componentModelId;
}
public void setComponentModelId(final long componentModelId) {
this.componentModelId = componentModelId;
}
public String getUuid() {
return uuid;
}
public void setUuid(final String uuid) {
this.uuid = uuid;
}
public String getModelUuid() {
return modelUuid;
}
public void setModelUuid(final String modelUuid) {
this.modelUuid = modelUuid;
}
public String getKey() {
return key;
}
public void setKey(final String key) {
this.key = key;
}
public String getType() {
return type;
}
public void setType(final String type) {
this.type = type;
}
public String getIdAttribute() {
return idAttribute;
}
public void setIdAttribute(final String idAttribute) {
this.idAttribute = idAttribute;
}
public String getClassAttribute() {
return classAttribute;
}
public void setClassAttribute(final String classAttribute) {
this.classAttribute = classAttribute;
}
public String getStyleAttribute() {
return styleAttribute;
}
public void setStyleAttribute(final String styleAttribute) {
this.styleAttribute = styleAttribute;
}
}

View File

@ -53,11 +53,24 @@ import javax.persistence.Table;
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "PAGE_MODEL_COMPONENT_MODELS", schema = CoreConstants.DB_SCHEMA)
@NamedQueries({
@NamedQuery(name = "ComponentModel.findComponentByContainerAndKey",
@NamedQuery(
name = "ComponentModel.findComponentsOfContainer",
query = "SELECT c FROM ComponentModel c "
+ "WHERE c.container = :container "
+ "ORDER BY c.key"
),
@NamedQuery(
name = "ComponentModel.countComponentsOfContainer",
query = "SELECT COUNT(c) FROM ComponentModel c "
+ "WHERE c.container = :container"
),
@NamedQuery(
name = "ComponentModel.findComponentByContainerAndKey",
query = "SELECT c "
+ "FROM ComponentModel c "
+ "WHERE c.container = :container "
+ "AND c.key = :key")
+ "AND c.key = :key"
)
})
public class ComponentModel implements Serializable {

View File

@ -24,6 +24,7 @@ import org.libreccm.security.AuthorizationRequired;
import org.libreccm.security.RequiresPrivilege;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
@ -32,6 +33,7 @@ import javax.enterprise.context.RequestScoped;
import javax.transaction.Transactional;
import java.util.UUID;
import java.util.stream.Stream;
import javax.persistence.EntityGraph;
import javax.persistence.NoResultException;
@ -162,6 +164,54 @@ public class ComponentModelRepository
}
}
@Transactional(Transactional.TxType.REQUIRED)
public List<ComponentModel> findComponentsOfContainerAsList(
final ContainerModel containerModel,
final int limit,
final int offset
) {
return getEntityManager()
.createNamedQuery(
"ComponentModel.findComponentsOfContainer",
ComponentModel.class
)
.setParameter("container", containerModel)
.setFirstResult(offset)
.setMaxResults(limit)
.getResultList();
}
@Transactional(Transactional.TxType.REQUIRED)
public Stream<ComponentModel> findComponentsOfContainerAsStream(
final ContainerModel containerModel,
final int limit,
final int offset
) {
return getEntityManager()
.createNamedQuery(
"ComponentModel.findComponentsOfContainer",
ComponentModel.class
)
.setParameter("container", containerModel)
.setFirstResult(offset)
.setMaxResults(limit)
.getResultStream();
}
@Transactional(Transactional.TxType.REQUIRED)
public long countComponentsOfContainer(
final ContainerModel containerModel,
final int limit,
final int offset
) {
return getEntityManager()
.createNamedQuery(
"ComponentModel.countComponentsOfContainer", Long.class
)
.setParameter("container", containerModel)
.getSingleResult();
}
@Transactional(Transactional.TxType.REQUIRED)
public Optional<ComponentModel> findComponentByContainerAndKey(
final ContainerModel containerModel,

View File

@ -61,7 +61,9 @@ import javax.xml.bind.annotation.XmlTransient;
@NamedQueries({
@NamedQuery(
name = "ContainerModel.findContainersOfPageModel",
query = "SELECT c FROM ContainerModel c WHERE c.pageModel = :pageModel"
query = "SELECT c FROM ContainerModel c "
+ "WHERE c.pageModel = :pageModel "
+ "ORDER BY c.key"
),
@NamedQuery(
name = "ContainerModel.countContainersOfPageModel",