From 84c125128b3ba9868321a420cc2b52f40fba579c Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Wed, 1 Jul 2020 21:21:10 +0200 Subject: [PATCH] Get methods for CcmApplicationsApi --- .../api/admin/web/CcmApplicationsApi.java | 85 +++++++++- .../java/org/libreccm/api/dto/ListView.java | 3 +- .../libreccm/sites/SiteAwareApplication.java | 20 +++ .../sites/SiteAwareApplicationRepository.java | 158 ++++++++++++++++++ .../libreccm/web/ApplicationRepository.java | 2 +- .../java/org/libreccm/web/CcmApplication.java | 29 +++- 6 files changed, 284 insertions(+), 13 deletions(-) create mode 100644 ccm-core/src/main/java/org/libreccm/sites/SiteAwareApplicationRepository.java diff --git a/ccm-core/src/main/java/org/libreccm/api/admin/web/CcmApplicationsApi.java b/ccm-core/src/main/java/org/libreccm/api/admin/web/CcmApplicationsApi.java index 1aeed0def..b8100accc 100644 --- a/ccm-core/src/main/java/org/libreccm/api/admin/web/CcmApplicationsApi.java +++ b/ccm-core/src/main/java/org/libreccm/api/admin/web/CcmApplicationsApi.java @@ -18,19 +18,28 @@ */ package org.libreccm.api.admin.web; +import org.libreccm.api.Identifier; +import org.libreccm.api.IdentifierParser; import org.libreccm.api.admin.categorization.dto.DomainOwnershipData; import org.libreccm.api.admin.web.dto.CcmApplicationDto; import org.libreccm.api.dto.ListView; import org.libreccm.core.CoreConstants; import org.libreccm.security.AuthorizationRequired; import org.libreccm.security.RequiresPrivilege; +import org.libreccm.sites.SiteAwareApplicationRepository; +import org.libreccm.web.ApplicationRepository; +import org.libreccm.web.CcmApplication; + +import java.util.stream.Collectors; import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; import javax.transaction.Transactional; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; +import javax.ws.rs.NotFoundException; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; @@ -48,6 +57,15 @@ import javax.ws.rs.core.Response; @Path("/applications/") public class CcmApplicationsApi { + @Inject + private ApplicationRepository applicationRepository; + + @Inject + private IdentifierParser identifierParser; + + @Inject + private SiteAwareApplicationRepository siteAwareApplicationRepository; + @GET @Path("/") @Produces(MediaType.APPLICATION_JSON) @@ -60,7 +78,29 @@ public class CcmApplicationsApi { @QueryParam("limit") @DefaultValue("20") final int limit, @QueryParam("offset") @DefaultValue("0") final int offset ) { - throw new UnsupportedOperationException(); + if (siteAwareOnly) { + return new ListView<>( + siteAwareApplicationRepository + .findAll(limit, offset) + .stream() + .map(CcmApplicationDto::new) + .collect(Collectors.toList()), + applicationRepository.countAll(), + limit, + offset + ); + } else { + return new ListView<>( + applicationRepository + .findAll(limit, offset) + .stream() + .map(CcmApplicationDto::new) + .collect(Collectors.toList()), + applicationRepository.countAll(), + limit, + offset + ); + } } @GET @@ -72,7 +112,7 @@ public class CcmApplicationsApi { public CcmApplicationDto getApplication( @PathParam("appIdentifier") final String appIdentifier ) { - throw new UnsupportedOperationException(); + return new CcmApplicationDto(findApplication(appIdentifier)); } @POST @@ -148,4 +188,45 @@ public class CcmApplicationsApi { throw new UnsupportedOperationException(); } + private CcmApplication findApplication(final String applicationIdentifier) { + final Identifier identifier = identifierParser.parseIdentifier( + applicationIdentifier + ); + switch (identifier.getType()) { + case ID: + return applicationRepository + .findById(Long.parseLong(identifier.getIdentifier())) + .orElseThrow( + () -> new NotFoundException( + String.format( + "No CcmApplicationWith UUID %s found.", + identifier.getIdentifier() + ) + ) + ); + case UUID: + return applicationRepository + .findByUuid(identifier.getIdentifier()) + .orElseThrow( + () -> new NotFoundException( + String.format( + "No CcmApplication with UUID %s found.", + identifier.getIdentifier() + ) + ) + ); + default: + return applicationRepository + .retrieveApplicationForPath(identifier.getIdentifier()) + .orElseThrow( + () -> new NotFoundException( + String.format( + "No CcmApplication with UUID %s found.", + identifier.getIdentifier() + ) + ) + ); + } + } + } diff --git a/ccm-core/src/main/java/org/libreccm/api/dto/ListView.java b/ccm-core/src/main/java/org/libreccm/api/dto/ListView.java index c80b7c121..e996dc750 100644 --- a/ccm-core/src/main/java/org/libreccm/api/dto/ListView.java +++ b/ccm-core/src/main/java/org/libreccm/api/dto/ListView.java @@ -18,6 +18,7 @@ */ package org.libreccm.api.dto; +import java.util.ArrayList; import java.util.List; /** @@ -61,7 +62,7 @@ public class ListView { } public List getList() { - return list; + return new ArrayList<>(list); } public long getCount() { diff --git a/ccm-core/src/main/java/org/libreccm/sites/SiteAwareApplication.java b/ccm-core/src/main/java/org/libreccm/sites/SiteAwareApplication.java index 318fd9886..9c51c82d5 100644 --- a/ccm-core/src/main/java/org/libreccm/sites/SiteAwareApplication.java +++ b/ccm-core/src/main/java/org/libreccm/sites/SiteAwareApplication.java @@ -49,6 +49,26 @@ import javax.persistence.Table; @Entity @Table(name = "SITE_AWARE_APPLICATIONS", schema = DB_SCHEMA) @NamedQueries({ + @NamedQuery( + name = "CcmApplication.retrieveApplicationForPath", + query = "SELECT a FROM SiteAwareApplication a " + + "WHERE a.primaryUrl = :path" + ), + @NamedQuery( + name = "CcmApplication.findByType", + query = "SELECT A FROM SiteAwareApplication a " + + "WHERE a.applicationType = :type" + ), + @NamedQuery( + name = "CcmApplication.findByUuid", + query = "SELECT a FROM SiteAwareApplication a " + + "WHERE a.uuid = :uuid" + ), + @NamedQuery( + name = "CcmApplication.findByPrimaryUrl", + query = "SELECT a FROM SiteAwareApplication a " + + "WHERE a.primaryUrl = :primaryUrl" + ), @NamedQuery( name = "SiteAwareApplication.findForSite", query = "SELECT a FROM SiteAwareApplication a WHERE a.site = :site" diff --git a/ccm-core/src/main/java/org/libreccm/sites/SiteAwareApplicationRepository.java b/ccm-core/src/main/java/org/libreccm/sites/SiteAwareApplicationRepository.java new file mode 100644 index 000000000..77a5c4c34 --- /dev/null +++ b/ccm-core/src/main/java/org/libreccm/sites/SiteAwareApplicationRepository.java @@ -0,0 +1,158 @@ +/* + * Copyright (C) 2020 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.sites; + +import org.libreccm.core.AbstractEntityRepository; +import org.libreccm.core.CoreConstants; +import org.libreccm.security.AuthorizationRequired; +import org.libreccm.security.RequiresPrivilege; +import org.libreccm.web.CcmApplication; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +import javax.enterprise.context.RequestScoped; +import javax.persistence.NoResultException; +import javax.persistence.TypedQuery; +import javax.transaction.Transactional; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +public class SiteAwareApplicationRepository + extends AbstractEntityRepository { + + private static final long serialVersionUID = 165550885824851765L; + + @Override + public Class getEntityClass() { + return SiteAwareApplication.class; + } + + @Override + public String getIdAttributeName() { + return "objectId"; + } + + @Override + public Long getIdOfEntity(final SiteAwareApplication entity) { + return entity.getObjectId(); + } + + @Override + public boolean isNew(final SiteAwareApplication application) { + return application.getObjectId() == 0; + } + + @Override + public void initNewEntity(final SiteAwareApplication application) { + + super.initNewEntity(application); + application.setUuid(UUID.randomUUID().toString()); + application.setApplicationType(application.getClass().getName()); + } + + /** + * Retrieve the application mounted at the provided {@code path}. + * + * @param path The path on which the application is mounted. + * + * @return The application mounted at {@code path} or {@code null} if there + * is no application mounted at that {@code path}. + */ + @Transactional(Transactional.TxType.REQUIRED) + public Optional retrieveApplicationForPath( + final String path) { + try { + return Optional.of( + getEntityManager() + .createNamedQuery( + "CcmApplication.retrieveApplicationForPath", + SiteAwareApplication.class + ) + .setParameter("path", path) + .getSingleResult() + ); + } catch (NoResultException ex) { + return Optional.empty(); + } + } + + /** + * Find all applications of the specific {@code type}. + * + * @param type The type of the application. + * + * @return A list of the installed applications of the provided + * {@code type}. + */ + @Transactional(Transactional.TxType.REQUIRED) + public List findByType(final String type) { + final TypedQuery query = getEntityManager() + .createNamedQuery( + "SiteAwareApplication.findByType", + SiteAwareApplication.class + ); + query.setParameter("type", type); + + return query.getResultList(); + } + + /** + * Finds a {@link CcmApplication} by its uuid. + * + * @param uuid The uuid of the item to find + * + * @return An optional either with the found item or empty + */ + public Optional findByUuid(final String uuid) { + final TypedQuery query = getEntityManager() + .createNamedQuery( + "Site.findByUuid.findByUuid", + SiteAwareApplication.class + ); + query.setParameter("uuid", uuid); + + try { + return Optional.of(query.getSingleResult()); + } catch (NoResultException ex) { + return Optional.empty(); + } + } + + @AuthorizationRequired + @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) + @Transactional(Transactional.TxType.REQUIRED) + @Override + public void save(final SiteAwareApplication application) { + super.save(application); + } + + @AuthorizationRequired + @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) + @Transactional(Transactional.TxType.REQUIRED) + @Override + public void delete(final SiteAwareApplication application) { + super.delete(application); + } + +} diff --git a/ccm-core/src/main/java/org/libreccm/web/ApplicationRepository.java b/ccm-core/src/main/java/org/libreccm/web/ApplicationRepository.java index 0174b4436..0cbdd62c7 100644 --- a/ccm-core/src/main/java/org/libreccm/web/ApplicationRepository.java +++ b/ccm-core/src/main/java/org/libreccm/web/ApplicationRepository.java @@ -51,7 +51,7 @@ public class ApplicationRepository public String getIdAttributeName() { return "objectId"; } - + @Override public Long getIdOfEntity(final CcmApplication entity) { return entity.getObjectId(); diff --git a/ccm-core/src/main/java/org/libreccm/web/CcmApplication.java b/ccm-core/src/main/java/org/libreccm/web/CcmApplication.java index 68491b571..ff275c499 100644 --- a/ccm-core/src/main/java/org/libreccm/web/CcmApplication.java +++ b/ccm-core/src/main/java/org/libreccm/web/CcmApplication.java @@ -59,15 +59,26 @@ import javax.persistence.Table; @Entity @Table(name = "APPLICATIONS", schema = DB_SCHEMA) @NamedQueries({ - @NamedQuery(name = "CcmApplication.retrieveApplicationForPath", - query = "SELECT a FROM CcmApplication a " - + "WHERE a.primaryUrl = :path"), - @NamedQuery(name = "CcmApplication.findByType", - query = "SELECT A FROM CcmApplication a " - + "WHERE a.applicationType = :type"), - @NamedQuery(name = "CcmApplication.findByUuid", - query = "SELECT a FROM CcmApplication a " - + "WHERE a.uuid = :uuid") + @NamedQuery( + name = "CcmApplication.retrieveApplicationForPath", + query = "SELECT a FROM CcmApplication a " + + "WHERE a.primaryUrl = :path" + ), + @NamedQuery( + name = "CcmApplication.findByType", + query = "SELECT A FROM CcmApplication a " + + "WHERE a.applicationType = :type" + ), + @NamedQuery( + name = "CcmApplication.findByUuid", + query = "SELECT a FROM CcmApplication a " + + "WHERE a.uuid = :uuid" + ), + @NamedQuery( + name = "CcmApplication.findByPrimaryUrl", + query = "SELECT a FROM CcmApplication a " + + "WHERE a.primaryUrl = :primaryUrl" + ) }) @NamedEntityGraphs({ @NamedEntityGraph(