Get methods for CcmApplicationsApi

Jens Pelzetter 2020-07-01 21:21:10 +02:00
parent 8a9ec963f4
commit 84c125128b
6 changed files with 284 additions and 13 deletions

View File

@ -18,19 +18,28 @@
*/ */
package org.libreccm.api.admin.web; 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.categorization.dto.DomainOwnershipData;
import org.libreccm.api.admin.web.dto.CcmApplicationDto; import org.libreccm.api.admin.web.dto.CcmApplicationDto;
import org.libreccm.api.dto.ListView; import org.libreccm.api.dto.ListView;
import org.libreccm.core.CoreConstants; import org.libreccm.core.CoreConstants;
import org.libreccm.security.AuthorizationRequired; import org.libreccm.security.AuthorizationRequired;
import org.libreccm.security.RequiresPrivilege; 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.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.transaction.Transactional; import javax.transaction.Transactional;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE; import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue; import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.PUT; import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
@ -48,6 +57,15 @@ import javax.ws.rs.core.Response;
@Path("/applications/") @Path("/applications/")
public class CcmApplicationsApi { public class CcmApplicationsApi {
@Inject
private ApplicationRepository applicationRepository;
@Inject
private IdentifierParser identifierParser;
@Inject
private SiteAwareApplicationRepository siteAwareApplicationRepository;
@GET @GET
@Path("/") @Path("/")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@ -60,7 +78,29 @@ public class CcmApplicationsApi {
@QueryParam("limit") @DefaultValue("20") final int limit, @QueryParam("limit") @DefaultValue("20") final int limit,
@QueryParam("offset") @DefaultValue("0") final int offset @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 @GET
@ -72,7 +112,7 @@ public class CcmApplicationsApi {
public CcmApplicationDto getApplication( public CcmApplicationDto getApplication(
@PathParam("appIdentifier") final String appIdentifier @PathParam("appIdentifier") final String appIdentifier
) { ) {
throw new UnsupportedOperationException(); return new CcmApplicationDto(findApplication(appIdentifier));
} }
@POST @POST
@ -148,4 +188,45 @@ public class CcmApplicationsApi {
throw new UnsupportedOperationException(); 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()
)
)
);
}
}
} }

View File

@ -18,6 +18,7 @@
*/ */
package org.libreccm.api.dto; package org.libreccm.api.dto;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
@ -61,7 +62,7 @@ public class ListView<T> {
} }
public List<T> getList() { public List<T> getList() {
return list; return new ArrayList<>(list);
} }
public long getCount() { public long getCount() {

View File

@ -49,6 +49,26 @@ import javax.persistence.Table;
@Entity @Entity
@Table(name = "SITE_AWARE_APPLICATIONS", schema = DB_SCHEMA) @Table(name = "SITE_AWARE_APPLICATIONS", schema = DB_SCHEMA)
@NamedQueries({ @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( @NamedQuery(
name = "SiteAwareApplication.findForSite", name = "SiteAwareApplication.findForSite",
query = "SELECT a FROM SiteAwareApplication a WHERE a.site = :site" query = "SELECT a FROM SiteAwareApplication a WHERE a.site = :site"

View File

@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
public class SiteAwareApplicationRepository
extends AbstractEntityRepository<Long, SiteAwareApplication> {
private static final long serialVersionUID = 165550885824851765L;
@Override
public Class<SiteAwareApplication> 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<SiteAwareApplication> 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<SiteAwareApplication> findByType(final String type) {
final TypedQuery<SiteAwareApplication> 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<SiteAwareApplication> findByUuid(final String uuid) {
final TypedQuery<SiteAwareApplication> 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);
}
}

View File

@ -59,15 +59,26 @@ import javax.persistence.Table;
@Entity @Entity
@Table(name = "APPLICATIONS", schema = DB_SCHEMA) @Table(name = "APPLICATIONS", schema = DB_SCHEMA)
@NamedQueries({ @NamedQueries({
@NamedQuery(name = "CcmApplication.retrieveApplicationForPath", @NamedQuery(
name = "CcmApplication.retrieveApplicationForPath",
query = "SELECT a FROM CcmApplication a " query = "SELECT a FROM CcmApplication a "
+ "WHERE a.primaryUrl = :path"), + "WHERE a.primaryUrl = :path"
@NamedQuery(name = "CcmApplication.findByType", ),
@NamedQuery(
name = "CcmApplication.findByType",
query = "SELECT A FROM CcmApplication a " query = "SELECT A FROM CcmApplication a "
+ "WHERE a.applicationType = :type"), + "WHERE a.applicationType = :type"
@NamedQuery(name = "CcmApplication.findByUuid", ),
@NamedQuery(
name = "CcmApplication.findByUuid",
query = "SELECT a FROM CcmApplication a " query = "SELECT a FROM CcmApplication a "
+ "WHERE a.uuid = :uuid") + "WHERE a.uuid = :uuid"
),
@NamedQuery(
name = "CcmApplication.findByPrimaryUrl",
query = "SELECT a FROM CcmApplication a "
+ "WHERE a.primaryUrl = :primaryUrl"
)
}) })
@NamedEntityGraphs({ @NamedEntityGraphs({
@NamedEntityGraph( @NamedEntityGraph(