From 802bb2091250c9eea3caff432056632a53f5bd18 Mon Sep 17 00:00:00 2001 From: jensp Date: Thu, 12 Oct 2017 10:25:05 +0000 Subject: [PATCH] Next part of the sites/pages/themes complex git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@5041 8810af33-2d31-482b-a856-94f89814c4df --- .../main/java/org/librecms/pages/Page.java | 129 ++++++++++++++++++ .../org/librecms/pages/PageRepository.java | 77 +++++++++++ .../org/libreccm/pagemodel/PageModel.java | 90 +++++++++--- .../pagemodel/PageModelRepository.java | 2 +- 4 files changed, 280 insertions(+), 18 deletions(-) create mode 100644 ccm-cms/src/main/java/org/librecms/pages/Page.java create mode 100644 ccm-cms/src/main/java/org/librecms/pages/PageRepository.java diff --git a/ccm-cms/src/main/java/org/librecms/pages/Page.java b/ccm-cms/src/main/java/org/librecms/pages/Page.java new file mode 100644 index 000000000..d0b2d714d --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/pages/Page.java @@ -0,0 +1,129 @@ +/* + * 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.pagemodel.PageModel; + +import java.io.Serializable; +import java.util.Collections; +import java.util.HashMap; + +import java.util.Map; + +import javax.persistence.Column; +import javax.persistence.Embedded; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToOne; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +import static org.librecms.CmsConstants.*; + +/** + * + * @author Jens Pelzetter + */ +@Entity +@Table(name = "PAGES", schema = DB_SCHEMA) +@NamedQueries( + @NamedQuery( + name = "Page.findForCategory", + query = "SELECT p FROM Page p WHERE p.category = :category" + ) +) +public class Page implements Serializable { + + private static final long serialVersionUID = 5108486858438122008L; + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "PAGE_ID") + private long pageId; + + @OneToOne + @JoinColumn(name = "CATEGORY_ID") + private Category category; + + @ManyToOne + @JoinColumn(name = "INDEX_PAGE_MODEL_ID") + private PageModel indexPageModel; + + @ManyToOne + @JoinColumn(name = "ITEM_PAGE_MODEL_ID") + private PageModel itemPageModel; + + @Embedded + @JoinTable(name = "PAGE_THEME_CONFIGURATIONS", + schema = DB_SCHEMA, + joinColumns = { + @JoinColumn(name = "PAGE_ID") + }) + private Map themeConfiguration; + + public long getPageId() { + return pageId; + } + + protected void setPageId(final long pageId) { + this.pageId = pageId; + } + + public Category getCategory() { + return category; + } + + public void setCategory(final Category category) { + this.category = category; + } + + public PageModel getIndexPageModel() { + return indexPageModel; + } + + public void setIndexPageModel(final PageModel indexPageModel) { + this.indexPageModel = indexPageModel; + } + + public PageModel getItemPageModel() { + return itemPageModel; + } + + public void setItemPageModel(final PageModel itemPageModel) { + this.itemPageModel = itemPageModel; + } + + public Map getThemeConfiguration() { + return Collections.unmodifiableMap(themeConfiguration); + } + + public void setThemeConfiguration( + final Map themeConfiguration) { + + this.themeConfiguration = new HashMap<>(themeConfiguration); + } + +} diff --git a/ccm-cms/src/main/java/org/librecms/pages/PageRepository.java b/ccm-cms/src/main/java/org/librecms/pages/PageRepository.java new file mode 100644 index 000000000..5d02d5db7 --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/pages/PageRepository.java @@ -0,0 +1,77 @@ +/* + * 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.core.AbstractEntityRepository; +import org.libreccm.core.CoreConstants; +import org.libreccm.security.RequiresPrivilege; + +import java.util.Optional; + +import javax.enterprise.context.RequestScoped; +import javax.persistence.NoResultException; +import javax.persistence.TypedQuery; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +public class PageRepository extends AbstractEntityRepository{ + + public Optional findPageForCategory(final Category category) { + + final TypedQuery query = getEntityManager() + .createNamedQuery("Page.findForCategory", Page.class); + query.setParameter("category", category); + + try { + return Optional.of(query.getSingleResult()); + } catch(NoResultException ex) { + return Optional.empty(); + } + } + + @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) + @Override + public void save(final Page page) { + super.save(page); + } + + @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) + @Override + public void delete(final Page page) { + super.delete(page); + } + + + @Override + public Class getEntityClass() { + return Page.class; + } + + @Override + public boolean isNew(final Page page) { + return page.getPageId() == 0; + } + + + +} diff --git a/ccm-core/src/main/java/org/libreccm/pagemodel/PageModel.java b/ccm-core/src/main/java/org/libreccm/pagemodel/PageModel.java index 5dcf30652..ac2187f06 100644 --- a/ccm-core/src/main/java/org/libreccm/pagemodel/PageModel.java +++ b/ccm-core/src/main/java/org/libreccm/pagemodel/PageModel.java @@ -18,7 +18,6 @@ */ package org.libreccm.pagemodel; -import org.libreccm.core.CcmObject; import org.libreccm.core.CoreConstants; import org.libreccm.l10n.LocalizedString; import org.libreccm.web.CcmApplication; @@ -37,6 +36,9 @@ import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.JoinColumn; @@ -109,10 +111,31 @@ import javax.persistence.Table; + "AND p.version = org.libreccm.pagemodel.PageModelVersion.LIVE" ) }) -public class PageModel extends CcmObject implements Serializable { +public class PageModel implements Serializable { private static final long serialVersionUID = 7252512839926020978L; + /** + * + * The ID of the entity in the database. + * + */ + @Id + @Column(name = "PAGE_MODEL_ID") + @GeneratedValue(strategy = GenerationType.AUTO) + private long pageModelId; + + /** + * + * The UUID of this {@code PageModel}. Please note that this UUID identifies + * the dataset not the model. Therefore the draft and the live version have + * different values for this field. + * + */ + @Column(name = "UUID", length = 255, nullable = false) + @NotNull + private String uuid; + /** * The UUID of the model. Same for draft and live version. */ @@ -185,6 +208,22 @@ public class PageModel extends CcmObject implements Serializable { description = new LocalizedString(); } + public long getPageModelId() { + return pageModelId; + } + + protected void setPageModelId(final long pageModelId) { + this.pageModelId = pageModelId; + } + + public String getUuid() { + return uuid; + } + + protected void setUuid(final String uuid) { + this.uuid = uuid; + } + public String getModelUuid() { return modelUuid; } @@ -265,7 +304,9 @@ public class PageModel extends CcmObject implements Serializable { @Override public int hashCode() { - int hash = super.hashCode(); + int hash = 7; + hash = 71 * hash + (int) (pageModelId ^ (pageModelId >>> 32)); + hash = 71 * hash + Objects.hashCode(uuid); hash = 71 * hash + Objects.hashCode(name); hash = 71 * hash + Objects.hashCode(title); hash = 71 * hash + Objects.hashCode(description); @@ -281,11 +322,6 @@ public class PageModel extends CcmObject implements Serializable { if (obj == null) { return false; } - - if (!super.equals(obj)) { - return false; - } - if (!(obj instanceof PageModel)) { return false; } @@ -293,6 +329,12 @@ public class PageModel extends CcmObject implements Serializable { if (!other.canEqual(this)) { return false; } + if (pageModelId != other.getPageModelId()) { + return false; + } + if (!Objects.equals(uuid, other.getUuid())) { + return false; + } if (!Objects.equals(name, other.getName())) { return false; } @@ -305,21 +347,35 @@ public class PageModel extends CcmObject implements Serializable { return Objects.equals(description, other.getDescription()); } - @Override public boolean canEqual(final Object obj) { return obj instanceof PageModel; } @Override + + public final String toString() { + + return toString(""); + + } + public String toString(final String data) { - return super.toString(String.format(", name = \"%s\", " - + "title = %s, " - + "description = %s%s", - super.toString(), - name, - Objects.toString(title), - Objects.toString(description), - data)); + + return String.format("%s{ " + + "pageModelId = %d, " + + "uuid = %s, " + + "name = \"%s\", " + + "title = %s, " + + "description = %s, " + + "type = \"%s\"" + + " }", + super.toString(), + pageModelId, + uuid, + name, + Objects.toString(title), + Objects.toString(description), + type); } } diff --git a/ccm-core/src/main/java/org/libreccm/pagemodel/PageModelRepository.java b/ccm-core/src/main/java/org/libreccm/pagemodel/PageModelRepository.java index 90f616ae9..7b8bf2075 100644 --- a/ccm-core/src/main/java/org/libreccm/pagemodel/PageModelRepository.java +++ b/ccm-core/src/main/java/org/libreccm/pagemodel/PageModelRepository.java @@ -50,7 +50,7 @@ public class PageModelRepository extends AbstractEntityRepository