CCM NG:
- Deliver theme files using JAX-RS - ACSObjectCategoryForm now works again (#2800) git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@5218 8810af33-2d31-482b-a856-94f89814c4df
parent
0ad4e7ed8a
commit
bb21d54174
|
|
@ -27,6 +27,7 @@ import org.libreccm.core.CcmObjectRepository;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import javax.enterprise.context.RequestScoped;
|
import javax.enterprise.context.RequestScoped;
|
||||||
|
|
@ -42,10 +43,10 @@ class ACSObjectCategoryController {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private CategoryRepository categoryRepo;
|
private CategoryRepository categoryRepo;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private CategoryManager categoryManager;
|
private CategoryManager categoryManager;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private CcmObjectRepository ccmObjectRepo;
|
private CcmObjectRepository ccmObjectRepo;
|
||||||
|
|
||||||
|
|
@ -81,19 +82,19 @@ class ACSObjectCategoryController {
|
||||||
object.getObjectId())));
|
object.getObjectId())));
|
||||||
|
|
||||||
final Category cat = categoryRepo
|
final Category cat = categoryRepo
|
||||||
.findById(category.getObjectId())
|
.findById(category.getObjectId())
|
||||||
.orElseThrow(() -> new IllegalArgumentException(String
|
.orElseThrow(() -> new IllegalArgumentException(String
|
||||||
.format("No Category with ID %d in the database.",
|
.format("No Category with ID %d in the database.",
|
||||||
category.getObjectId())));
|
category.getObjectId())));
|
||||||
|
|
||||||
categoryManager.addObjectToCategory(ccmObject, cat);
|
categoryManager.addObjectToCategory(ccmObject, cat);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(Transactional.TxType.REQUIRED)
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
protected void removeObjectFromCategory(final CcmObject object,
|
protected void removeObjectFromCategory(final CcmObject object,
|
||||||
final Category category)
|
final Category category)
|
||||||
throws ObjectNotAssignedToCategoryException {
|
throws ObjectNotAssignedToCategoryException {
|
||||||
|
|
||||||
Objects.requireNonNull(object);
|
Objects.requireNonNull(object);
|
||||||
Objects.requireNonNull(category);
|
Objects.requireNonNull(category);
|
||||||
|
|
||||||
|
|
@ -104,12 +105,24 @@ class ACSObjectCategoryController {
|
||||||
object.getObjectId())));
|
object.getObjectId())));
|
||||||
|
|
||||||
final Category cat = categoryRepo
|
final Category cat = categoryRepo
|
||||||
.findById(category.getObjectId())
|
.findById(category.getObjectId())
|
||||||
.orElseThrow(() -> new IllegalArgumentException(String
|
.orElseThrow(() -> new IllegalArgumentException(String
|
||||||
.format("No Category with ID %d in the database.",
|
.format("No Category with ID %d in the database.",
|
||||||
category.getObjectId())));
|
category.getObjectId())));
|
||||||
|
|
||||||
categoryManager.removeObjectFromCategory(ccmObject, cat);
|
categoryManager.removeObjectFromCategory(ccmObject, cat);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
|
protected long getParentCategoryId(final Category forCategory) {
|
||||||
|
|
||||||
|
final Category category = categoryRepo
|
||||||
|
.findById(forCategory.getObjectId())
|
||||||
|
.orElseThrow(() -> new IllegalArgumentException(String
|
||||||
|
.format("No Category with ID %d in the database.",
|
||||||
|
forCategory.getObjectId())));
|
||||||
|
|
||||||
|
return category.getParentCategory().getObjectId();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -194,7 +194,11 @@ public abstract class ACSObjectCategoryForm extends Form {
|
||||||
final Category category) {
|
final Category category) {
|
||||||
|
|
||||||
if (category.getParentCategory() != null) {
|
if (category.getParentCategory() != null) {
|
||||||
ancestorCats.add(category.getParentCategory().getObjectId());
|
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
|
||||||
|
final ACSObjectCategoryController controller = cdiUtil
|
||||||
|
.findBean(ACSObjectCategoryController.class);
|
||||||
|
ancestorCats
|
||||||
|
.add(controller.getParentCategoryId(category));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,95 @@
|
||||||
|
/*
|
||||||
|
* 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.theming;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
|
import javax.enterprise.context.RequestScoped;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.ws.rs.GET;
|
||||||
|
import javax.ws.rs.NotFoundException;
|
||||||
|
import javax.ws.rs.Path;
|
||||||
|
import javax.ws.rs.PathParam;
|
||||||
|
import javax.ws.rs.WebApplicationException;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||||
|
*/
|
||||||
|
@RequestScoped
|
||||||
|
@Path("/{theme}")
|
||||||
|
public class ThemeFiles {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private Themes themes;
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Path("/{path}")
|
||||||
|
public Response getFile(@PathParam("theme") final String theme,
|
||||||
|
@PathParam("path") final String path) {
|
||||||
|
|
||||||
|
final ThemeInfo info = themes
|
||||||
|
.getTheme(theme, ThemeVersion.LIVE)
|
||||||
|
.orElseThrow(() -> new NotFoundException(String
|
||||||
|
.format("Theme \"%s\" does not exist.", theme)));
|
||||||
|
|
||||||
|
final InputStream inputStream = themes
|
||||||
|
.getFileFromTheme(info, path)
|
||||||
|
.orElseThrow(() -> new NotFoundException(String
|
||||||
|
.format("The file \"%s\" does exist in the theme \"%s\".",
|
||||||
|
path,
|
||||||
|
theme)));
|
||||||
|
|
||||||
|
final MediaType mediaType = getMediaTypeFromPath(path);
|
||||||
|
|
||||||
|
final BufferedReader reader = new BufferedReader(
|
||||||
|
new InputStreamReader(inputStream));
|
||||||
|
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
try {
|
||||||
|
int value = reader.read();
|
||||||
|
while (value != -1) {
|
||||||
|
outputStream.write(value);
|
||||||
|
value = reader.read();
|
||||||
|
}
|
||||||
|
} catch (IOException ex) {
|
||||||
|
throw new WebApplicationException(ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
final byte[] data = outputStream.toByteArray();
|
||||||
|
return Response.ok(data, mediaType).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private MediaType getMediaTypeFromPath(final String path) {
|
||||||
|
|
||||||
|
if (path.endsWith(".css")) {
|
||||||
|
return new MediaType("text", "css");
|
||||||
|
} else {
|
||||||
|
return MediaType.WILDCARD_TYPE;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -23,6 +23,7 @@ import org.apache.logging.log4j.Logger;
|
||||||
import org.libreccm.core.UnexpectedErrorException;
|
import org.libreccm.core.UnexpectedErrorException;
|
||||||
import org.libreccm.pagemodel.PageModel;
|
import org.libreccm.pagemodel.PageModel;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -144,4 +145,36 @@ public class Themes implements Serializable {
|
||||||
return processor.process(page, theme, provider);
|
return processor.process(page, theme, provider);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve a file from a theme using the {@link ThemeProvider} for the
|
||||||
|
* theme.
|
||||||
|
*
|
||||||
|
* @param theme The theme from which the file is retrieved.
|
||||||
|
* @param path The path of the file relative to the root directory of the
|
||||||
|
* theme.
|
||||||
|
*
|
||||||
|
* @return An {@link Optional} containing an {@link InputStream} for reading
|
||||||
|
* the contents of the file or an empty {@link Optional} if the file
|
||||||
|
* was not found.
|
||||||
|
*/
|
||||||
|
public Optional<InputStream> getFileFromTheme(final ThemeInfo theme,
|
||||||
|
final String path) {
|
||||||
|
|
||||||
|
final Instance<? extends ThemeProvider> forTheme = providers.select(
|
||||||
|
theme.getProvider());
|
||||||
|
|
||||||
|
if (forTheme.isUnsatisfied()) {
|
||||||
|
LOGGER.error("ThemeProvider \"{}\" not found.",
|
||||||
|
theme.getProvider().getName());
|
||||||
|
throw new UnexpectedErrorException(String.format(
|
||||||
|
"ThemeProvider \"%s\" not found.",
|
||||||
|
theme.getProvider().getName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
final ThemeProvider provider = forTheme.get();
|
||||||
|
return provider.getThemeFileAsStream(theme.getName(),
|
||||||
|
theme.getVersion(),
|
||||||
|
path);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
* 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.theming;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import javax.ws.rs.ApplicationPath;
|
||||||
|
import javax.ws.rs.core.Application;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||||
|
*/
|
||||||
|
@ApplicationPath("/theming")
|
||||||
|
public class ThemesService extends Application {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Class<?>> getClasses() {
|
||||||
|
|
||||||
|
final Set<Class<?>> classes = new HashSet<>();
|
||||||
|
classes.add(ThemeFiles.class);
|
||||||
|
|
||||||
|
return classes;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -4,31 +4,36 @@
|
||||||
xmlns:ccm="http://xmlns.libreccm.org"
|
xmlns:ccm="http://xmlns.libreccm.org"
|
||||||
exclude-result-prefixes="ccm xsl">
|
exclude-result-prefixes="ccm xsl">
|
||||||
|
|
||||||
<xsl:output method="html"
|
<xsl:output method="html"
|
||||||
doctype-system="about:legacy-compat"
|
doctype-system="about:legacy-compat"
|
||||||
indent="yes"
|
indent="yes"
|
||||||
encoding="utf-8"/>
|
encoding="utf-8"/>
|
||||||
|
|
||||||
<xsl:template match="page">
|
<xsl:template match="page">
|
||||||
|
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Category page</title>
|
<title>Category page</title>
|
||||||
<link rel="stylesheet" href="/themes/ccm/style.css" />
|
<link rel="stylesheet" href="{ccm:getContextPath()}/theming/ccm/style.css" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<main>
|
<main>
|
||||||
<ul class="news">
|
<ul class="news">
|
||||||
<xsl:for-each select="/page/newsList/items">
|
<xsl:for-each select="/page/newsList/items">
|
||||||
<span>
|
<li>
|
||||||
<pre>
|
<span>
|
||||||
<xsl:value-of select="count(./attachments[name='.images']/attachments[1])" />
|
<!--<pre>
|
||||||
</pre>
|
<xsl:value-of select="count(./attachments[name='.images']/attachments[1])" />
|
||||||
<img src="/content-sections/info/images/{./attachments[name='.images']/attachments[0]/asset/uuid}" width="354" height="286" alt="" />
|
</pre>-->
|
||||||
</span>
|
<img src="{ccm:getContextPath()}/content-sections/info/images/uuid-{./attachments[name='.images']/attachments[1]/asset/uuid}"
|
||||||
<span>
|
width="354"
|
||||||
<xsl:value-of select="./title" />
|
height="286"
|
||||||
</span>
|
alt="" />
|
||||||
|
</span>
|
||||||
|
<span>
|
||||||
|
<xsl:value-of select="./title" />
|
||||||
|
</span>
|
||||||
|
</li>
|
||||||
</xsl:for-each>
|
</xsl:for-each>
|
||||||
</ul>
|
</ul>
|
||||||
<div class="boxes">
|
<div class="boxes">
|
||||||
|
|
@ -38,7 +43,7 @@
|
||||||
<xsl:value-of select="./title" />
|
<xsl:value-of select="./title" />
|
||||||
</h1>
|
</h1>
|
||||||
<p>
|
<p>
|
||||||
<img src="/content-sections/info/images/{./attachments[name='.images']/attachments[0]/asset/uuid}" alt="" />
|
<img src="{ccm:getContextPath()}/content-sections/info/images/uuid-{./attachments[name='.images']/attachments[1]/asset/uuid}" alt="" />
|
||||||
<xsl:value-of select="./description" />
|
<xsl:value-of select="./description" />
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue