CCM NG: Reload Methode in AbstractEntityRepository erstellen (#2807) and getIdOfEntity method including implementation in all repositories

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@5256 8810af33-2d31-482b-a856-94f89814c4df

Former-commit-id: 5667244716
pull/2/head
jensp 2018-02-07 15:49:10 +00:00
parent b9518f962b
commit 4d8b552396
36 changed files with 209 additions and 17 deletions

View File

@ -104,6 +104,11 @@ public class AssetRepository
return "objectId"; return "objectId";
} }
@Override
public Long getIdOfEntity(final Asset entity) {
return entity.getObjectId();
}
@Override @Override
public boolean isNew(final Asset asset) { public boolean isNew(final Asset asset) {
return asset.getObjectId() == 0; return asset.getObjectId() == 0;

View File

@ -115,12 +115,17 @@ public class ContentItemRepository
public Class<ContentItem> getEntityClass() { public Class<ContentItem> getEntityClass() {
return ContentItem.class; return ContentItem.class;
} }
@Override @Override
public String getIdAttributeName() { public String getIdAttributeName() {
return "objectId"; return "objectId";
} }
@Override
public Long getIdOfEntity(final ContentItem entity) {
return entity.getObjectId();
}
@Override @Override
public boolean isNew(final ContentItem item) { public boolean isNew(final ContentItem item) {
return ccmObjectRepo.isNew(item); return ccmObjectRepo.isNew(item);

View File

@ -54,7 +54,7 @@ public class ContentSectionRepository
try { try {
return Optional.of(query.getSingleResult()); return Optional.of(query.getSingleResult());
} catch(NoResultException ex) { } catch (NoResultException ex) {
return Optional.empty(); return Optional.empty();
} }
} }
@ -63,12 +63,17 @@ public class ContentSectionRepository
public Class<ContentSection> getEntityClass() { public Class<ContentSection> getEntityClass() {
return ContentSection.class; return ContentSection.class;
} }
@Override @Override
public String getIdAttributeName() { public String getIdAttributeName() {
return "objectId"; return "objectId";
} }
@Override
public Long getIdOfEntity(final ContentSection entity) {
return entity.getObjectId();
}
@Override @Override
public boolean isNew(final ContentSection section) { public boolean isNew(final ContentSection section) {
return section.getObjectId() == 0; return section.getObjectId() == 0;

View File

@ -54,12 +54,17 @@ public class ContentTypeRepository
public Class<ContentType> getEntityClass() { public Class<ContentType> getEntityClass() {
return ContentType.class; return ContentType.class;
} }
@Override @Override
public String getIdAttributeName() { public String getIdAttributeName() {
return "objectId"; return "objectId";
} }
@Override
public Long getIdOfEntity(final ContentType entity) {
return entity.getObjectId();
}
@Override @Override
public boolean isNew(final ContentType type) { public boolean isNew(final ContentType type) {
return type.getObjectId() == 0; return type.getObjectId() == 0;

View File

@ -61,6 +61,11 @@ public class FolderRepository extends AbstractEntityRepository<Long, Folder> {
return "objectId"; return "objectId";
} }
@Override
public Long getIdOfEntity(final Folder entity) {
return entity.getObjectId();
}
@Override @Override
public boolean isNew(final Folder folder) { public boolean isNew(final Folder folder) {
return folder.getObjectId() == 0; return folder.getObjectId() == 0;

View File

@ -46,7 +46,12 @@ public class MultiPartArticleSectionRepository
public String getIdAttributeName() { public String getIdAttributeName() {
return "sectionId"; return "sectionId";
} }
@Override
public Long getIdOfEntity(final MultiPartArticleSection entity) {
return entity.getSectionId();
}
@Override @Override
public Class<MultiPartArticleSection> getEntityClass() { public Class<MultiPartArticleSection> getEntityClass() {
return MultiPartArticleSection.class; return MultiPartArticleSection.class;
@ -78,10 +83,10 @@ public class MultiPartArticleSectionRepository
final MultiPartArticleSection section) { final MultiPartArticleSection section) {
final TypedQuery<MultiPartArticle> query = getEntityManager() final TypedQuery<MultiPartArticle> query = getEntityManager()
.createNamedQuery("MultiPartArticleSection.findArticleOfSection", .createNamedQuery("MultiPartArticleSection.findArticleOfSection",
MultiPartArticle.class); MultiPartArticle.class);
query.setParameter("section", section); query.setParameter("section", section);
return query.getSingleResult(); return query.getSingleResult();
} }

View File

@ -42,6 +42,11 @@ public class LifecycleDefinitionRepository
return "definitionId"; return "definitionId";
} }
@Override
public Long getIdOfEntity(final LifecycleDefinition entity) {
return entity.getDefinitionId();
}
@Override @Override
public boolean isNew(final LifecycleDefinition lifecycleDefinition) { public boolean isNew(final LifecycleDefinition lifecycleDefinition) {
return lifecycleDefinition.getDefinitionId() == 0; return lifecycleDefinition.getDefinitionId() == 0;

View File

@ -27,23 +27,27 @@ import javax.enterprise.context.RequestScoped;
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a> * @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/ */
@RequestScoped @RequestScoped
public class LifecycleRepository extends AbstractEntityRepository<Long, Lifecycle> { public class LifecycleRepository
extends AbstractEntityRepository<Long, Lifecycle> {
@Override @Override
public Class<Lifecycle> getEntityClass() { public Class<Lifecycle> getEntityClass() {
return Lifecycle.class; return Lifecycle.class;
} }
@Override @Override
public String getIdAttributeName() { public String getIdAttributeName() {
return "lifecycleId"; return "lifecycleId";
} }
@Override
public Long getIdOfEntity(final Lifecycle entity) {
return entity.getLifecycleId();
}
@Override @Override
public boolean isNew(final Lifecycle lifecycle) { public boolean isNew(final Lifecycle lifecycle) {
return lifecycle.getLifecycleId() == 0; return lifecycle.getLifecycleId() == 0;
} }
} }

View File

@ -27,7 +27,7 @@ import javax.enterprise.context.RequestScoped;
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a> * @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/ */
@RequestScoped @RequestScoped
public class PhaseDefinititionRepository public class PhaseDefinititionRepository
extends AbstractEntityRepository<Long, PhaseDefinition> { extends AbstractEntityRepository<Long, PhaseDefinition> {
@Override @Override
@ -39,12 +39,15 @@ public class PhaseDefinititionRepository
public String getIdAttributeName() { public String getIdAttributeName() {
return "definitionId"; return "definitionId";
} }
@Override
public Long getIdOfEntity(final PhaseDefinition entity) {
return entity.getDefinitionId();
}
@Override @Override
public boolean isNew(final PhaseDefinition phaseDefinition) { public boolean isNew(final PhaseDefinition phaseDefinition) {
return phaseDefinition.getDefinitionId() == 0; return phaseDefinition.getDefinitionId() == 0;
} }
} }

View File

@ -41,6 +41,11 @@ public class PhaseRepository extends AbstractEntityRepository<Long, Phase> {
return "phaseId"; return "phaseId";
} }
@Override
public Long getIdOfEntity(final Phase entity) {
return entity.getPhaseId();
}
@Override @Override
public boolean isNew(final Phase phase) { public boolean isNew(final Phase phase) {
return phase.getPhaseId() == 0; return phase.getPhaseId() == 0;

View File

@ -101,4 +101,9 @@ public class PageRepository extends AbstractEntityRepository<Long, Page> {
return page.getObjectId() == 0; return page.getObjectId() == 0;
} }
@Override
public Long getIdOfEntity(final Page entity) {
return entity.getObjectId();
}
} }

View File

@ -87,6 +87,11 @@ public class PagesRepository extends AbstractEntityRepository<Long, Pages> {
return "objectId"; return "objectId";
} }
@Override
public Long getIdOfEntity(final Pages entity) {
return entity.getObjectId();
}
@Override @Override
public boolean isNew(final Pages pages) { public boolean isNew(final Pages pages) {
return pages.getObjectId() == 0; return pages.getObjectId() == 0;

View File

@ -71,6 +71,11 @@ public class CategoryRepository extends AbstractEntityRepository<Long, Category>
return "objectId"; return "objectId";
} }
@Override
public Long getIdOfEntity(final Category entity) {
return entity.getObjectId();
}
@Override @Override
public boolean isNew(final Category entity) { public boolean isNew(final Category entity) {
return entity.getObjectId() == 0; return entity.getObjectId() == 0;

View File

@ -55,6 +55,11 @@ public class DomainRepository extends AbstractEntityRepository<Long, Domain> {
public String getIdAttributeName() { public String getIdAttributeName() {
return "objectId"; return "objectId";
} }
@Override
public Long getIdOfEntity(final Domain entity) {
return entity.getObjectId();
}
@Override @Override
public boolean isNew(final Domain entity) { public boolean isNew(final Domain entity) {

View File

@ -133,6 +133,14 @@ public abstract class AbstractEntityRepository<K, E> implements Serializable {
*/ */
public abstract String getIdAttributeName(); public abstract String getIdAttributeName();
/**
* Get the primary key/id of a entity.
*
* @param entity The entity
* @return The ID of the provided {@code entity}.
*/
public abstract K getIdOfEntity(E entity);
/** /**
* Finds an entity by it ID. * Finds an entity by it ID.
* *
@ -215,6 +223,16 @@ public abstract class AbstractEntityRepository<K, E> implements Serializable {
return Optional.empty(); return Optional.empty();
} }
} }
@Transactional(Transactional.TxType.REQUIRED)
public E reload(final E entity, final String... fetchJoins) {
return findById(getIdOfEntity(entity), fetchJoins)
.orElseThrow(() -> new IllegalArgumentException(String
.format("No Entity of type \"%s\" with ID %s in the database.",
getEntityClass().getName(),
Objects.toString(getIdOfEntity(entity)))));
}
/** /**
* Finds all instances of the entity of the type this repository is * Finds all instances of the entity of the type this repository is

View File

@ -55,6 +55,11 @@ public class CcmObjectRepository extends AbstractEntityRepository<Long, CcmObjec
public String getIdAttributeName() { public String getIdAttributeName() {
return "objectId"; return "objectId";
} }
@Override
public Long getIdOfEntity(final CcmObject entity) {
return entity.getObjectId();
}
@Override @Override
public boolean isNew(final CcmObject entity) { public boolean isNew(final CcmObject entity) {

View File

@ -49,6 +49,11 @@ public class ResourceRepository extends AbstractEntityRepository<Long,
public String getIdAttributeName() { public String getIdAttributeName() {
return "objectId"; return "objectId";
} }
@Override
public Long getIdOfEntity(final Resource entity) {
return entity.getObjectId();
}
@Override @Override
public boolean isNew(final Resource entity) { public boolean isNew(final Resource entity) {

View File

@ -48,6 +48,11 @@ public class ResourceTypeRepository
public String getIdAttributeName() { public String getIdAttributeName() {
return "resourceTypeId"; return "resourceTypeId";
} }
@Override
public Long getIdOfEntity(final ResourceType entity) {
return entity.getResourceTypeId();
}
@Override @Override
public boolean isNew(final ResourceType entity) { public boolean isNew(final ResourceType entity) {

View File

@ -47,6 +47,11 @@ public class ComponentModelRepository
public String getIdAttributeName() { public String getIdAttributeName() {
return "componentModelId"; return "componentModelId";
} }
@Override
public Long getIdOfEntity(final ComponentModel entity) {
return entity.getComponentModelId();
}
@Override @Override
public boolean isNew(final ComponentModel componentModel) { public boolean isNew(final ComponentModel componentModel) {

View File

@ -53,6 +53,11 @@ public class PageModelRepository extends AbstractEntityRepository<Long, PageMode
return "pageModelId"; return "pageModelId";
} }
@Override
public Long getIdOfEntity(final PageModel entity) {
return entity.getPageModelId();
}
@Override @Override
public boolean isNew(final PageModel pageModel) { public boolean isNew(final PageModel pageModel) {

View File

@ -41,12 +41,17 @@ public class GroupRepository extends AbstractEntityRepository<Long, Group> {
public Class<Group> getEntityClass() { public Class<Group> getEntityClass() {
return Group.class; return Group.class;
} }
@Override @Override
public String getIdAttributeName() { public String getIdAttributeName() {
return "partyId"; return "partyId";
} }
@Override
public Long getIdOfEntity(final Group entity) {
return entity.getPartyId();
}
@Override @Override
public boolean isNew(final Group entity) { public boolean isNew(final Group entity) {
if (entity == null) { if (entity == null) {

View File

@ -47,6 +47,11 @@ public class PartyRepository extends AbstractEntityRepository<Long, Party> {
return "partyId"; return "partyId";
} }
@Override
public Long getIdOfEntity(final Party entity) {
return entity.getPartyId();
}
@Override @Override
public boolean isNew(final Party entity) { public boolean isNew(final Party entity) {
if (entity == null) { if (entity == null) {

View File

@ -47,6 +47,11 @@ public class PermissionRepository
return "permissionId"; return "permissionId";
} }
@Override
public Long getIdOfEntity(final Permission entity) {
return entity.getPermissionId();
}
@Override @Override
public boolean isNew(Permission entity) { public boolean isNew(Permission entity) {
if (entity == null) { if (entity == null) {

View File

@ -49,6 +49,11 @@ public class RoleRepository extends AbstractEntityRepository<Long, Role> {
return "roleId"; return "roleId";
} }
@Override
public Long getIdOfEntity(final Role entity) {
return entity.getRoleId();
}
@Override @Override
public boolean isNew(final Role entity) { public boolean isNew(final Role entity) {
if (entity == null) { if (entity == null) {

View File

@ -47,6 +47,11 @@ public class UserRepository extends AbstractEntityRepository<Long, User> {
public String getIdAttributeName() { public String getIdAttributeName() {
return "partyId"; return "partyId";
} }
@Override
public Long getIdOfEntity(final User entity) {
return entity.getPartyId();
}
@Override @Override
public boolean isNew(final User user) { public boolean isNew(final User user) {

View File

@ -127,6 +127,11 @@ public class SiteRepository extends AbstractEntityRepository<Long, Site> {
public String getIdAttributeName() { public String getIdAttributeName() {
return "objectId"; return "objectId";
} }
@Override
public Long getIdOfEntity(final Site entity) {
return entity.getObjectId();
}
@Override @Override
public boolean isNew(final Site site) { public boolean isNew(final Site site) {

View File

@ -51,6 +51,11 @@ public class ApplicationRepository
public String getIdAttributeName() { public String getIdAttributeName() {
return "objectId"; return "objectId";
} }
@Override
public Long getIdOfEntity(final CcmApplication entity) {
return entity.getObjectId();
}
@Override @Override
public boolean isNew(final CcmApplication application) { public boolean isNew(final CcmApplication application) {

View File

@ -50,6 +50,11 @@ public class AssignableTaskRepository
public String getIdAttributeName() { public String getIdAttributeName() {
return "taskId"; return "taskId";
} }
@Override
public Long getIdOfEntity(final AssignableTask entity) {
return entity.getTaskId();
}
@Override @Override
public boolean isNew(final AssignableTask task) { public boolean isNew(final AssignableTask task) {

View File

@ -44,6 +44,11 @@ public class TaskCommentRepository
public String getIdAttributeName() { public String getIdAttributeName() {
return "commentId"; return "commentId";
} }
@Override
public Long getIdOfEntity(final TaskComment entity) {
return entity.getCommentId();
}
@Override @Override
public boolean isNew(TaskComment entity) { public boolean isNew(TaskComment entity) {

View File

@ -46,6 +46,11 @@ public class TaskRepository extends AbstractEntityRepository<Long, Task> {
public String getIdAttributeName() { public String getIdAttributeName() {
return "taskId"; return "taskId";
} }
@Override
public Long getIdOfEntity(final Task entity) {
return entity.getTaskId();
}
@Override @Override
public boolean isNew(final Task task) { public boolean isNew(final Task task) {

View File

@ -47,6 +47,11 @@ public class WorkflowRepository extends AbstractEntityRepository<Long, Workflow>
return "workflowId"; return "workflowId";
} }
@Override
public Long getIdOfEntity(final Workflow entity) {
return entity.getWorkflowId();
}
@Override @Override
public boolean isNew(final Workflow workflow) { public boolean isNew(final Workflow workflow) {
return workflow.getWorkflowId() == 0; return workflow.getWorkflowId() == 0;

View File

@ -34,6 +34,8 @@ import javax.persistence.EntityManager;
public class BlobObjectRepository extends public class BlobObjectRepository extends
AbstractAuditedEntityRepository<Long, BlobObject> { AbstractAuditedEntityRepository<Long, BlobObject> {
private static final long serialVersionUID = 35679591875538616L;
@Inject @Inject
private EntityManager entityManager; private EntityManager entityManager;
@ -47,6 +49,11 @@ public class BlobObjectRepository extends
return "blobObjectId"; return "blobObjectId";
} }
@Override
public Long getIdOfEntity(final BlobObject entity) {
return entity.getBlobObjectId();
}
@Override @Override
public Class<BlobObject> getEntityClass() { public Class<BlobObject> getEntityClass() {
return BlobObject.class; return BlobObject.class;

View File

@ -39,6 +39,11 @@ public class FileRepository extends AbstractResourceRepository<File> {
return "objectId"; return "objectId";
} }
@Override
public Long getIdOfEntity(final File entity) {
return entity.getObjectId();
}
@Override @Override
public TypedQuery<File> getFindByNameQuery() { public TypedQuery<File> getFindByNameQuery() {
return entityManager.createNamedQuery( return entityManager.createNamedQuery(

View File

@ -38,6 +38,11 @@ public class FolderRepository extends AbstractResourceRepository<Folder> {
public String getIdAttributeName() { public String getIdAttributeName() {
return "objectId"; return "objectId";
} }
@Override
public Long getIdOfEntity(final Folder entity) {
return entity.getObjectId();
}
@Override @Override
public TypedQuery<Folder> getFindByNameQuery() { public TypedQuery<Folder> getFindByNameQuery() {

View File

@ -47,6 +47,11 @@ public class RepositoryRepository
return entity.getObjectId(); return entity.getObjectId();
} }
@Override
public Long getIdOfEntity(final Repository entity) {
return entity.getObjectId();
}
@Override @Override
public String getIdAttributeName() { public String getIdAttributeName() {
return "objectId"; return "objectId";

View File

@ -53,6 +53,11 @@ public class ShortcutRepository extends AbstractEntityRepository<Long, Shortcut>
return "shortcutId"; return "shortcutId";
} }
@Override
public Long getIdOfEntity(final Shortcut entity) {
return entity.getShortcutId();
}
@Override @Override
public boolean isNew(final Shortcut entity) { public boolean isNew(final Shortcut entity) {
return entity.getShortcutId() == 0; return entity.getShortcutId() == 0;