CCM NG: sites/pages/themes

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@5042 8810af33-2d31-482b-a856-94f89814c4df
ccm-docs
jensp 2017-10-12 14:55:11 +00:00
parent 903b2d5eb6
commit cafb3364ce
5 changed files with 136 additions and 78 deletions

View File

@ -0,0 +1,76 @@
/*
* Copyright (C) 2017 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.librecms.pages;
import org.libreccm.categorization.Category;
import org.libreccm.categorization.CategoryRepository;
import java.util.Optional;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
public class PageManager {
@Inject
private CategoryRepository categoryRepo;
@Inject
private PageRepository pageRepo;
@Transactional(Transactional.TxType.REQUIRED)
public Page findPageForCategory(final Category category) {
final Optional<Page> page = pageRepo.findPageForCategory(category);
if (page.isPresent()) {
return page.get();
} else {
final Category forCategory = categoryRepo
.findById(category.getObjectId())
.orElseThrow(() -> new IllegalArgumentException(String
.format("No Category with ID %d in the database.",
category.getObjectId())));
if (forCategory.getParentCategory() == null) {
return new Page();
} else {
return findPageForCategory(forCategory.getParentCategory());
}
}
}
@Transactional(Transactional.TxType.REQUIRED)
public Page createPageForCategory(final Category category) {
final Page page = new Page();
page.setCategory(category);
pageRepo.save(page);
return page;
}
}

View File

@ -63,26 +63,6 @@ import static org.librecms.CmsConstants.*;
query = "SELECT (CASE WHEN COUNT(p) > 0 THEN true ELSE false END) " query = "SELECT (CASE WHEN COUNT(p) > 0 THEN true ELSE false END) "
+ "FROM Pages p JOIN p.site s " + "FROM Pages p JOIN p.site s "
+ "WHERE s.defaultSite = true") + "WHERE s.defaultSite = true")
,
@NamedQuery(
name = "Pages.findPageModelForIndexPage",
query = "SELECT DISTINCT m "
+ "FROM PageModel m "
+ "JOIN m.categories c "
+ "WHERE c.category = :category "
+ "AND c.type = '"
+ PagesConstants.CATEGORIZATION_TYPE_PAGE_MODEL_INDEX + "' "
+ "AND m.version = :version")
,
@NamedQuery(
name = "Pages.findPageModelForItemPage",
query = "SELECT DISTINCT m "
+ "FROM PageModel m "
+ "JOIN m.categories c "
+ "WHERE c.category = :category "
+ "AND c.type = '"
+ PagesConstants.CATEGORIZATION_TYPE_PAGE_MODEL_ITEM + "' "
+ "AND m.version = :version")
}) })
public class Pages extends CcmApplication implements Serializable { public class Pages extends CcmApplication implements Serializable {

View File

@ -18,19 +18,29 @@
*/ */
package org.librecms.pages; package org.librecms.pages;
import org.libreccm.categorization.Domain;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
/** /**
* *
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a> * @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/ */
public final class PagesConstants { @RequestScoped
public class PagesManager {
private PagesConstants() {
//Nothing @Inject
private PagesRepository pagesRepo;
public Pages createPages(final Domain domain) {
final Pages pages = new Pages();
pages.setCategoryDomain(domain);
pagesRepo.save(pages);
return pages;
} }
public static final String CATEGORIZATION_TYPE_PAGE_MODEL_INDEX
= "page_model_index";
public static final String CATEGORIZATION_TYPE_PAGE_MODEL_ITEM
= "page_model_item";
} }

View File

@ -69,40 +69,6 @@ public class PagesRepository extends AbstractEntityRepository<Long, Pages> {
} }
} }
@Transactional(Transactional.TxType.REQUIRED)
public Optional<PageModel> findPageModelForIndexPage(
final Category category,
final PageModelVersion version) {
final TypedQuery<PageModel> query = getEntityManager()
.createNamedQuery("Pages.findPageModelForIndexPage", PageModel.class);
query.setParameter("category", category);
query.setParameter("version", version.toString());
try {
return Optional.of(query.getSingleResult());
} catch(NoResultException ex) {
return Optional.empty();
}
}
@Transactional(Transactional.TxType.REQUIRED)
public Optional<PageModel> findPageModelForItemPage(
final Category category,
final PageModelVersion version) {
final TypedQuery<PageModel> query = getEntityManager()
.createNamedQuery("Pages.findPageModelForItemPage", PageModel.class);
query.setParameter("category", category);
query.setParameter("version", version.toString());
try {
return Optional.of(query.getSingleResult());
} catch(NoResultException ex) {
return Optional.empty();
}
}
@Override @Override
public Class<Pages> getEntityClass() { public Class<Pages> getEntityClass() {
return Pages.class; return Pages.class;

View File

@ -23,7 +23,6 @@ import org.libreccm.categorization.CategoryManager;
import org.libreccm.categorization.CategoryRepository; import org.libreccm.categorization.CategoryRepository;
import org.libreccm.pagemodel.PageModel; import org.libreccm.pagemodel.PageModel;
import org.libreccm.pagemodel.PageModelManager; import org.libreccm.pagemodel.PageModelManager;
import org.libreccm.pagemodel.PageModelVersion;
import org.libreccm.sites.Site; import org.libreccm.sites.Site;
import org.libreccm.sites.SiteRepository; import org.libreccm.sites.SiteRepository;
import org.libreccm.theming.ThemeInfo; import org.libreccm.theming.ThemeInfo;
@ -32,7 +31,6 @@ import org.libreccm.theming.Themes;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
@ -75,6 +73,12 @@ public class PagesRouter {
@Inject @Inject
private PageModelManager pageModelManager; private PageModelManager pageModelManager;
@Inject
private PageRepository pageRepo;
@Inject
private PageManager pageManager;
@Inject @Inject
private SiteRepository siteRepo; private SiteRepository siteRepo;
@ -84,7 +88,7 @@ public class PagesRouter {
@Path("/index.{lang}.html") @Path("/index.{lang}.html")
@Produces("text/html") @Produces("text/html")
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public String getCategoryIndexPage( public String getCategoryIndexPageAsHtml(
@Context @Context
final UriInfo uriInfo, final UriInfo uriInfo,
@PathParam("page") @PathParam("page")
@ -126,19 +130,18 @@ public class PagesRouter {
page, page,
domain))); domain)));
final Optional<PageModel> pageModel = pagesRepo final Page pageConf = pageManager.findPageForCategory(category);
.findPageModelForIndexPage(category,
PageModelVersion final PageModel pageModel = pageConf.getIndexPageModel();
.valueOf(pageModelVersion));
final Map<String, Object> parameters = new HashMap<>(); final Map<String, Object> parameters = new HashMap<>();
parameters.put("currentCategory", category); parameters.put("currentCategory", category);
final Map<String, Object> buildResult; final Map<String, Object> buildResult;
if (pageModel.isPresent()) { if (pageModel == null) {
buildResult = pageBuilder.buildPage(pageModel.get(), parameters);
} else {
buildResult = pageBuilder.buildPage(parameters); buildResult = pageBuilder.buildPage(parameters);
} else {
buildResult = pageBuilder.buildPage(pageModel, parameters);
} }
final ThemeInfo themeInfo; final ThemeInfo themeInfo;
@ -163,11 +166,11 @@ public class PagesRouter {
return themes.process(buildResult, themeInfo); return themes.process(buildResult, themeInfo);
} }
@Path("/index.{lang}.tree") @Path("/index.{lang}.json")
@Produces("text/html") @Produces("text/html")
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public String getCategoryIndexPageTree( public String getCategoryIndexPageAsJson(
@Context @Context
final UriInfo uriInfo, final UriInfo uriInfo,
@PathParam("page") @PathParam("page")
@ -183,7 +186,30 @@ public class PagesRouter {
@QueryParam("pagemodel-version") @QueryParam("pagemodel-version")
@DefaultValue("LIVE") @DefaultValue("LIVE")
final String pageModelVersion) { final String pageModelVersion) {
throw new UnsupportedOperationException();
}
@Path("/index.{lang}.xml")
@Produces("text/html")
@Transactional(Transactional.TxType.REQUIRED)
public String getCategoryIndexPageAsXml(
@Context
final UriInfo uriInfo,
@PathParam("page")
final String page,
@PathParam("lang")
final String language,
@QueryParam("theme")
@DefaultValue("--DEFAULT--")
final String theme,
@QueryParam("theme-version")
@DefaultValue("LIVE")
final String themeVersion,
@QueryParam("pagemodel-version")
@DefaultValue("LIVE")
final String pageModelVersion) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }