Started to refactor PageModelsApi to same structure used for other endpoints
parent
1c34927e92
commit
7819bd4146
|
|
@ -18,12 +18,12 @@
|
|||
*/
|
||||
package org.libreccm.api.pagemodels;
|
||||
|
||||
import com.arsdigita.kernel.KernelConfig;
|
||||
|
||||
import org.libreccm.api.dto.ListView;
|
||||
import org.libreccm.api.pagemodels.dto.PageModelDto;
|
||||
import org.libreccm.configuration.ConfigurationManager;
|
||||
import org.libreccm.core.CoreConstants;
|
||||
import org.libreccm.l10n.GlobalizationHelper;
|
||||
import org.libreccm.pagemodel.ContainerModel;
|
||||
import org.libreccm.pagemodel.PageModel;
|
||||
import org.libreccm.pagemodel.PageModelManager;
|
||||
import org.libreccm.pagemodel.PageModelRepository;
|
||||
|
|
@ -35,10 +35,6 @@ import java.net.URI;
|
|||
|
||||
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.Consumes;
|
||||
import javax.ws.rs.DELETE;
|
||||
|
|
@ -53,7 +49,10 @@ import javax.ws.rs.Produces;
|
|||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.ws.rs.DefaultValue;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
|
|
@ -87,6 +86,8 @@ public class PageModels {
|
|||
* CcmApplication}.
|
||||
*
|
||||
* @param appName The path of the {@code app}.
|
||||
* @param limit
|
||||
* @param offset
|
||||
*
|
||||
* @return A JSON array with the data of all {@link PageModel}s of the
|
||||
* {@link CcmApplication} {@code app}.
|
||||
|
|
@ -101,22 +102,26 @@ public class PageModels {
|
|||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
@AuthorizationRequired
|
||||
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||
public JsonArray getAllPageModels(
|
||||
@PathParam("appName") String appName
|
||||
public ListView<PageModelDto> getAllPageModels(
|
||||
@PathParam("appName") String appName,
|
||||
@QueryParam("limit") @DefaultValue("20") final int limit,
|
||||
@QueryParam("offset") @DefaultValue("0") final int offset
|
||||
) {
|
||||
Objects.requireNonNull(appName);
|
||||
|
||||
final CcmApplication app = controller.findCcmApplication(
|
||||
String.format("/%s/", appName)
|
||||
);
|
||||
final JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
|
||||
return new ListView<>(
|
||||
pageModelRepo
|
||||
.findDraftByApplication(app)
|
||||
.findDraftByApplication(app, limit, offset)
|
||||
.stream()
|
||||
.map(this::mapPageModelToJson)
|
||||
.forEach(arrayBuilder::add);
|
||||
|
||||
return arrayBuilder.build();
|
||||
.map(this::buildPageModelDto)
|
||||
.collect(Collectors.toList()),
|
||||
pageModelRepo.countByApplication(app),
|
||||
limit,
|
||||
offset
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -145,7 +150,7 @@ public class PageModels {
|
|||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
@AuthorizationRequired
|
||||
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||
public JsonObject getPageModel(
|
||||
public PageModelDto getPageModel(
|
||||
@PathParam("appName") final String appName,
|
||||
@PathParam("pageModelName") final String pageModelName
|
||||
) {
|
||||
|
|
@ -158,7 +163,7 @@ public class PageModels {
|
|||
final PageModel pageModel = controller.findPageModel(
|
||||
app, pageModelName
|
||||
);
|
||||
return mapPageModelToJson(pageModel);
|
||||
return buildPageModelDto(pageModel);
|
||||
}
|
||||
|
||||
@POST
|
||||
|
|
@ -221,7 +226,7 @@ public class PageModels {
|
|||
public Response putPageModel(
|
||||
@PathParam("appName") final String appName,
|
||||
@PathParam("pageModelName") final String pageModelName,
|
||||
final JsonObject pageModelData
|
||||
final PageModelDto pageModelData
|
||||
) {
|
||||
Objects.requireNonNull(appName);
|
||||
Objects.requireNonNull(pageModelName);
|
||||
|
|
@ -230,9 +235,6 @@ public class PageModels {
|
|||
String.format("/%s/", appName)
|
||||
);
|
||||
|
||||
final KernelConfig kernelConfig = confManager
|
||||
.findConfiguration(KernelConfig.class);
|
||||
|
||||
final PageModel pageModel;
|
||||
final boolean created;
|
||||
if (controller.existsPageModel(app, pageModelName)) {
|
||||
|
|
@ -242,18 +244,28 @@ public class PageModels {
|
|||
pageModel = pageModelManager.createPageModel(pageModelName, app);
|
||||
created = true;
|
||||
}
|
||||
if (pageModelData.containsKey("title")) {
|
||||
pageModel.getTitle().addValue(
|
||||
kernelConfig.getDefaultLocale(),
|
||||
pageModelData.getString("title")
|
||||
if (pageModelData.getTitle() != null) {
|
||||
pageModelData
|
||||
.getTitle()
|
||||
.getValues()
|
||||
.entrySet()
|
||||
.stream()
|
||||
.forEach(
|
||||
entry -> pageModel.getTitle().addValue(
|
||||
entry.getKey(), entry.getValue()
|
||||
)
|
||||
);
|
||||
}
|
||||
if (pageModelData.containsKey("description")) {
|
||||
pageModel
|
||||
.getDescription()
|
||||
.addValue(
|
||||
kernelConfig.getDefaultLocale(),
|
||||
pageModelData.getString("description")
|
||||
if (pageModelData.getDescription() != null) {
|
||||
pageModelData.
|
||||
getDescription()
|
||||
.getValues()
|
||||
.entrySet()
|
||||
.stream()
|
||||
.forEach(
|
||||
entry -> pageModel.getDescription().addValue(
|
||||
entry.getKey(), entry.getValue()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -319,22 +331,14 @@ public class PageModels {
|
|||
).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method for mapping a {@link PageModel} object to JSON:
|
||||
*
|
||||
* @param pageModel The {@link PageModel} to map.
|
||||
*
|
||||
* @return A {@link JsonObject} object with the data of the provided {@link
|
||||
* PageModel}.
|
||||
*/
|
||||
private JsonObject mapPageModelToJson(final PageModel pageModel) {
|
||||
|
||||
private PageModelDto buildPageModelDto(final PageModel pageModel) {
|
||||
Objects.requireNonNull(pageModel);
|
||||
|
||||
final long lastPublished;
|
||||
final Optional<PageModel> liveModel = pageModelManager.getLiveVersion(
|
||||
pageModel
|
||||
);
|
||||
|
||||
if (liveModel.isPresent()
|
||||
&& liveModel.get().getLastModified() != null) {
|
||||
lastPublished = liveModel.get().getLastModified().getTime();
|
||||
|
|
@ -348,54 +352,12 @@ public class PageModels {
|
|||
lastModified = pageModel.getLastModified().getTime();
|
||||
}
|
||||
|
||||
return Json
|
||||
.createObjectBuilder()
|
||||
.add("containers", mapContainersToJson(pageModel))
|
||||
.add(
|
||||
"description",
|
||||
globalizationHelper.getValueFromLocalizedString(
|
||||
pageModel.getDescription()
|
||||
)
|
||||
)
|
||||
.add("modelUuid", pageModel.getModelUuid())
|
||||
.add("name", pageModel.getName())
|
||||
.add("pageModelId", Long.toString(pageModel.getPageModelId()))
|
||||
.add(
|
||||
"title",
|
||||
globalizationHelper.getValueFromLocalizedString(
|
||||
pageModel.getTitle()
|
||||
)
|
||||
)
|
||||
.add("type", pageModel.getType())
|
||||
.add("uuid", pageModel.getUuid())
|
||||
.add("version", pageModel.getVersion().toString())
|
||||
.add("publicationStatus",
|
||||
getPublicationStatus(pageModel).toString()
|
||||
)
|
||||
.add("lastModified", lastModified)
|
||||
.add("lastPublished", lastPublished)
|
||||
.build();
|
||||
}
|
||||
|
||||
private JsonArray mapContainersToJson(final PageModel pageModel) {
|
||||
final JsonArrayBuilder containers = Json.createArrayBuilder();
|
||||
|
||||
pageModel
|
||||
.getContainers()
|
||||
.stream()
|
||||
.map(this::mapContainerToJson)
|
||||
.forEach(container -> containers.add(container));
|
||||
|
||||
return containers.build();
|
||||
}
|
||||
|
||||
private JsonObject mapContainerToJson(final ContainerModel container) {
|
||||
return Json
|
||||
.createObjectBuilder()
|
||||
.add("containerUuid", container.getContainerUuid())
|
||||
.add("key", container.getKey())
|
||||
.add("uuid", container.getUuid())
|
||||
.build();
|
||||
return new PageModelDto(
|
||||
pageModel,
|
||||
lastPublished,
|
||||
lastModified,
|
||||
getPublicationStatus(pageModel)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -64,10 +64,10 @@ class PageModelsController {
|
|||
*/
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
protected CcmApplication findCcmApplication(final String appPath) {
|
||||
|
||||
return appRepo
|
||||
.retrieveApplicationForPath(Objects.requireNonNull(appPath))
|
||||
.orElseThrow(() -> new NotFoundException(
|
||||
.orElseThrow(
|
||||
() -> new NotFoundException(
|
||||
String.format(
|
||||
"No application with path \"%s\" found.",
|
||||
appPath)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* 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.ContainerModel;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||
*/
|
||||
public class ContainerModelDto {
|
||||
|
||||
private long containerId;
|
||||
|
||||
private String uuid;
|
||||
|
||||
private String containerUuid;
|
||||
|
||||
private String key;
|
||||
|
||||
public ContainerModelDto() {
|
||||
// Nothing
|
||||
}
|
||||
|
||||
public ContainerModelDto(final ContainerModel fromContainerModel) {
|
||||
this.containerId = fromContainerModel.getContainerId();
|
||||
this.uuid = fromContainerModel.getUuid();
|
||||
this.containerUuid = fromContainerModel.getContainerUuid();
|
||||
this.key = fromContainerModel.getKey();
|
||||
}
|
||||
|
||||
public String getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public void setUuid(final String uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
public String getContainerUuid() {
|
||||
return containerUuid;
|
||||
}
|
||||
|
||||
public void setContainerUuid(final String containerUuid) {
|
||||
this.containerUuid = containerUuid;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(final String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,184 @@
|
|||
/*
|
||||
* 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.api.pagemodels.PublicationStatus;
|
||||
import org.libreccm.l10n.LocalizedString;
|
||||
import org.libreccm.pagemodel.PageModel;
|
||||
import org.libreccm.pagemodel.PageModelVersion;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||
*/
|
||||
public class PageModelDto {
|
||||
|
||||
private long pageModelId;
|
||||
|
||||
private String uuid;
|
||||
|
||||
private String modelUuid;
|
||||
|
||||
private String name;
|
||||
|
||||
private PageModelVersion version;
|
||||
|
||||
private LocalizedString title;
|
||||
|
||||
private LocalizedString description;
|
||||
|
||||
private String type;
|
||||
|
||||
private long lastPublished;
|
||||
|
||||
private long lastModified;
|
||||
|
||||
private PublicationStatus publicationStatus;
|
||||
|
||||
private List<ContainerModelDto> containers;
|
||||
|
||||
public PageModelDto() {
|
||||
containers = new ArrayList<>();
|
||||
}
|
||||
|
||||
public PageModelDto(
|
||||
final PageModel fromPageModel,
|
||||
final long lastPublished,
|
||||
final long lastModified,
|
||||
final PublicationStatus publicationStatus
|
||||
) {
|
||||
this.pageModelId = fromPageModel.getPageModelId();
|
||||
this.uuid = fromPageModel.getUuid();
|
||||
this.modelUuid = fromPageModel.getModelUuid();
|
||||
this.name = fromPageModel.getName();
|
||||
this.version = fromPageModel.getVersion();
|
||||
this.title = fromPageModel.getTitle();
|
||||
this.description = fromPageModel.getDescription();
|
||||
this.type = fromPageModel.getType();
|
||||
this.lastPublished = lastPublished;
|
||||
this.lastModified = lastModified;
|
||||
this.publicationStatus = publicationStatus;
|
||||
this.containers = fromPageModel
|
||||
.getContainers()
|
||||
.stream()
|
||||
.map(ContainerModelDto::new)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public long getPageModelId() {
|
||||
return pageModelId;
|
||||
}
|
||||
|
||||
public void setPageModelId(final long pageModelId) {
|
||||
this.pageModelId = pageModelId;
|
||||
}
|
||||
|
||||
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 getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public PageModelVersion getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(final PageModelVersion version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public LocalizedString getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(final LocalizedString title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public LocalizedString getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(final LocalizedString description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(final String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public long getLastPublished() {
|
||||
return lastPublished;
|
||||
}
|
||||
|
||||
public void setLastPublished(final long lastPublished) {
|
||||
this.lastPublished = lastPublished;
|
||||
}
|
||||
|
||||
public long getLastModified() {
|
||||
return lastModified;
|
||||
}
|
||||
|
||||
public void setLastModified(final long lastModified) {
|
||||
this.lastModified = lastModified;
|
||||
}
|
||||
|
||||
public PublicationStatus getPublicationStatus() {
|
||||
return publicationStatus;
|
||||
}
|
||||
|
||||
public void setPublicationStatus(final PublicationStatus publicationStatus) {
|
||||
this.publicationStatus = publicationStatus;
|
||||
}
|
||||
|
||||
public List<ContainerModelDto> getContainers() {
|
||||
return new ArrayList<>(containers);
|
||||
}
|
||||
|
||||
public void setContainers(List<ContainerModelDto> containers) {
|
||||
this.containers = new ArrayList<>(containers);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue