From 1b3479a1e8be56bac38d934a4c86c1793b92ea80 Mon Sep 17 00:00:00 2001 From: jensp Date: Mon, 4 Jul 2016 15:27:37 +0000 Subject: [PATCH] CCM NG: Some repository classes for ccm-cms git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4187 8810af33-2d31-482b-a856-94f89814c4df --- .../main/java/org/librecms/assets/Asset.java | 15 ++- .../org/librecms/assets/AssetRepository.java | 96 +++++++++++++++++++ .../contentsection/ContentItemRepository.java | 76 +++++++++++++++ .../ContentSectionRepository.java | 43 +++++++++ .../librecms/contentsection/ContentType.java | 1 + .../java/org/libreccm/core/CcmObject.java | 4 +- .../libreccm/core/CcmObjectRepository.java | 12 +++ 7 files changed, 245 insertions(+), 2 deletions(-) create mode 100644 ccm-cms/src/main/java/org/librecms/assets/AssetRepository.java create mode 100644 ccm-cms/src/main/java/org/librecms/contentsection/ContentItemRepository.java create mode 100644 ccm-cms/src/main/java/org/librecms/contentsection/ContentSectionRepository.java diff --git a/ccm-cms/src/main/java/org/librecms/assets/Asset.java b/ccm-cms/src/main/java/org/librecms/assets/Asset.java index 05d52346b..e60be1054 100644 --- a/ccm-cms/src/main/java/org/librecms/assets/Asset.java +++ b/ccm-cms/src/main/java/org/librecms/assets/Asset.java @@ -36,6 +36,8 @@ import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; import javax.persistence.Table; import static org.librecms.CmsConstants.*; @@ -48,6 +50,17 @@ import static org.librecms.CmsConstants.*; @Table(schema = DB_SCHEMA, name = "ASSETS") @Inheritance(strategy = InheritanceType.JOINED) @Audited +@NamedQueries({ + @NamedQuery(name = "Asset.findByUuid", + query = "SELECT a FROM Asset a WHERE a.uuid = :uuid"), + @NamedQuery(name = "Asset.findByType", + query = "SELECT a FROM Asset a " + + "WHERE TYPE(a) = :type"), + @NamedQuery(name = "Asset.findByUuidAndType", + query = "SELECT a FROM Asset a " + + "WHERE a.uuid = :uuid " + + "AND TYPE(a) = :type") +}) public class Asset implements Identifiable, Serializable { private static final long serialVersionUID = -3499741368562653529L; @@ -75,7 +88,7 @@ public class Asset implements Identifiable, Serializable { public Asset() { title = new LocalizedString(); } - + public long getAssetId() { return assetId; } diff --git a/ccm-cms/src/main/java/org/librecms/assets/AssetRepository.java b/ccm-cms/src/main/java/org/librecms/assets/AssetRepository.java new file mode 100644 index 000000000..7cc18b744 --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/assets/AssetRepository.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2016 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.assets; + +import org.libreccm.auditing.AbstractAuditedEntityRepository; + +import java.util.Optional; + +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.NoResultException; +import javax.persistence.TypedQuery; + +/** + * + * @author Jens Pelzetter + */ +public class AssetRepository + extends AbstractAuditedEntityRepository { + + @Inject + private EntityManager entityManager; + + @Override + public Long getEntityId(final Asset asset) { + return asset.getAssetId(); + } + + @Override + public Class getEntityClass() { + return Asset.class; + } + + @Override + public boolean isNew(final Asset asset) { + return asset.getAssetId() == 0; + } + + public Optional findByUuid(final String uuid) { + final TypedQuery query = entityManager.createNamedQuery( + "Asset.findByUuid", Asset.class); + query.setParameter("uuid", uuid); + + try { + return Optional.of(query.getSingleResult()); + } catch (NoResultException ex) { + return Optional.empty(); + } + } + + public Optional findByUuidAndType( + final String uuid, final Class clazz) { + + final TypedQuery query = entityManager.createNamedQuery( + "Asset.findByUuidAndType", Asset.class); + query.setParameter("uuid", uuid); + query.setParameter("type", clazz.getName()); + + try { + return Optional.of(query.getSingleResult()); + } catch (NoResultException ex) { + return Optional.empty(); + } + } + + public Optional findByType( + final String uuid, final Class clazz) { + + final TypedQuery query = entityManager.createNamedQuery( + "Asset.findByType", Asset.class); + query.setParameter("type", clazz.getName()); + + try { + return Optional.of(query.getSingleResult()); + } catch (NoResultException ex) { + return Optional.empty(); + } + } + +} diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemRepository.java b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemRepository.java new file mode 100644 index 000000000..a82e405e6 --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemRepository.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2016 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.contentsection; + +import org.libreccm.auditing.AbstractAuditedEntityRepository; +import org.libreccm.core.CcmObject; +import org.libreccm.core.CcmObjectRepository; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.persistence.NoResultException; +import javax.persistence.TypedQuery; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +public class ContentItemRepository + extends AbstractAuditedEntityRepository{ + + @Inject + private CcmObjectRepository ccmObjectRepo; + + @Override + public Long getEntityId(final ContentItem item) { + return item.getObjectId(); + } + + @Override + public Class getEntityClass() { + return ContentItem.class; + } + + @Override + public boolean isNew(final ContentItem item) { + return ccmObjectRepo.isNew(item); + } + + public ContentItem findById(final long itemId) { + final CcmObject result = ccmObjectRepo.findObjectById(itemId); + if (result instanceof ContentItem) { + return (ContentItem) result; + } else { + return null; + } + } + + public ContentItem findByUuid(final String uuid) { + final CcmObject result = ccmObjectRepo.findObjectByUuid(uuid); + if (result instanceof ContentItem) { + return (ContentItem) result; + } else { + return null; + } + } + + //ToDo: Methods for finding items by name, path, content type etc. + +} diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ContentSectionRepository.java b/ccm-cms/src/main/java/org/librecms/contentsection/ContentSectionRepository.java new file mode 100644 index 000000000..94615e5b9 --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ContentSectionRepository.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2016 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.contentsection; + +import org.libreccm.core.AbstractEntityRepository; + +import javax.enterprise.context.RequestScoped; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +public class ContentSectionRepository + extends AbstractEntityRepository { + + @Override + public Class getEntityClass() { + return ContentSection.class; + } + + @Override + public boolean isNew(final ContentSection section) { + return section.getObjectId() == 0; + } + +} diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ContentType.java b/ccm-cms/src/main/java/org/librecms/contentsection/ContentType.java index 0c778c8b2..54ddcac45 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/ContentType.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ContentType.java @@ -79,6 +79,7 @@ public class ContentType extends CcmObject implements Serializable { private ContentItemMode mode; //ToDo references for authoring kit etc + public String getContentItemClass() { return contentItemClass; } diff --git a/ccm-core/src/main/java/org/libreccm/core/CcmObject.java b/ccm-core/src/main/java/org/libreccm/core/CcmObject.java index 6f1a46221..7bf737303 100644 --- a/ccm-core/src/main/java/org/libreccm/core/CcmObject.java +++ b/ccm-core/src/main/java/org/libreccm/core/CcmObject.java @@ -72,7 +72,9 @@ import org.hibernate.envers.Audited; @Inheritance(strategy = InheritanceType.JOINED) @NamedQueries({ @NamedQuery(name = "CcmObject.findById", - query = "SELECT o FROM CcmObject o WHERE o.objectId = :id") + query = "SELECT o FROM CcmObject o WHERE o.objectId = :id"), + @NamedQuery(name = "CcmObject.findByUuid", + query = "SELECT o FROM CcmObject o WHERE o.uuid = :uuid") }) @XmlRootElement(name = "ccm-object", namespace = CORE_XML_NS) //False warning (?). Because this class has been migrated from the old PDL style diff --git a/ccm-core/src/main/java/org/libreccm/core/CcmObjectRepository.java b/ccm-core/src/main/java/org/libreccm/core/CcmObjectRepository.java index 95b74983f..92777bcb9 100644 --- a/ccm-core/src/main/java/org/libreccm/core/CcmObjectRepository.java +++ b/ccm-core/src/main/java/org/libreccm/core/CcmObjectRepository.java @@ -69,5 +69,17 @@ public class CcmObjectRepository extends AbstractEntityRepository query = getEntityManager().createNamedQuery( + "CcmObject.findByUuid", CcmObject.class); + query.setParameter("uuid", uuid); + + try { + return query.getSingleResult(); + } catch (NoResultException ex) { + return null; + } + } }