Commit 390e1ddf authored by Lukasz Waskiewicz's avatar Lukasz Waskiewicz
Browse files

refs #1100 poprawki w rest api

zmiany ścieżek
wycięcie zbędnych pól w response ("id" na kilku obiektach)
korekta linków do akcji
akcje kasowania, wznawiania, anulowania, przerywania
parent e556368d
......@@ -10,6 +10,8 @@ import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;
import pl.edu.icm.cocos.services.api.model.CocosBean;
@Entity
......@@ -34,6 +36,7 @@ public class CocosColumn extends CocosBean {
@JoinColumn(name = "TABLE_ID")
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JsonIgnore
private CocosTableBase table;
public String getName() {
......
......@@ -32,8 +32,6 @@ public class CocosUserTable extends CocosTableBase {
@ManyToOne(fetch=FetchType.EAGER, optional=false)
private CocosSimulation simulation;
public CocosUserCreateTableQuery getCreateTableQuery() {
return createTableQuery;
}
......
......@@ -3,6 +3,7 @@ package pl.edu.icm.cocos.web.controllers.api;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
......@@ -14,6 +15,7 @@ import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
......@@ -28,13 +30,15 @@ import pl.edu.icm.cocos.services.api.CocosQueryService;
import pl.edu.icm.cocos.services.api.CocosSimulationService;
import pl.edu.icm.cocos.services.api.CocosUserFileQueryService;
import pl.edu.icm.cocos.services.api.exceptions.AccessViolationException;
import pl.edu.icm.cocos.services.api.exceptions.CocosQueryErrorException;
import pl.edu.icm.cocos.services.api.model.CocosFileType;
import pl.edu.icm.cocos.services.api.model.CocosSimulation;
import pl.edu.icm.cocos.services.api.model.metadata.CocosUserTable;
import pl.edu.icm.cocos.services.api.model.query.CocosQuery;
import pl.edu.icm.cocos.services.api.model.query.CocosQueryAbortInitiator;
import pl.edu.icm.cocos.services.api.model.query.CocosQueryExecution;
import pl.edu.icm.cocos.services.api.model.query.CocosUserCreateTableQuery;
import pl.edu.icm.cocos.services.api.model.query.CocosUserFileQuery;
import pl.edu.icm.cocos.services.api.model.query.CocosUserQueryBase;
import pl.edu.icm.cocos.services.api.utils.filter.QueryFilter;
import pl.edu.icm.cocos.web.utils.BasePaginationRequestBuilder;
import pl.edu.icm.cocos.web.utils.PaginationRequestHolder;
......@@ -67,38 +71,40 @@ public class RestApiController {
// SUBMIT
@RequestMapping(path = "/submit", method = RequestMethod.POST)
@RequestMapping(path = "/simulations/{simulationBusinessId}/queries", method = RequestMethod.POST, params = { "query" })
@PreAuthorize("@simulationAuthorization.hasPermissionFor(#simulationBusinessId)")
public CocosQuery executeQuery(@RequestParam String simulationBusinessId, @RequestParam String query,
@RequestParam(required= false, defaultValue = "false") Boolean userNotification) {
public void executeQuery(@PathVariable String simulationBusinessId, @RequestParam String query,
@RequestParam(required = false, defaultValue = "false") Boolean userNotification, HttpServletResponse response) throws IOException {
final CocosSimulation simulation = simulationService.getByBusinessId(simulationBusinessId);
return cocosQueryService.createQuery(simulation.getId(), StringUtils.trim(query), null, userNotification);
final CocosQuery newQuery = cocosQueryService.createQuery(simulation.getId(), StringUtils.trim(query), null, userNotification);
response.sendRedirect("queries/" + newQuery.getId());
}
@RequestMapping(path = "/submitTable", method = RequestMethod.POST)
@RequestMapping(path = "/simulations/{simulationBusinessId}/tables", method = RequestMethod.POST, params = { "query" })
@PreAuthorize("@simulationAuthorization.hasPermissionFor(#simulationBusinessId)")
public CocosUserCreateTableQuery executeTableQuery(@RequestParam String simulationBusinessId, @RequestParam String query, @RequestParam String tableName,
@RequestParam(required= false, defaultValue = StringUtils.EMPTY) String description, @RequestParam(required= false, defaultValue = "false") Boolean userNotification) {
public void executeTableQuery(@PathVariable String simulationBusinessId, @RequestParam String query, @RequestParam String tableName,
@RequestParam(required = false, defaultValue = StringUtils.EMPTY) String description,
@RequestParam(required = false, defaultValue = "false") Boolean userNotification, HttpServletResponse response) throws IOException {
final CocosSimulation simulation = simulationService.getByBusinessId(simulationBusinessId);
return cocosQueryService.createQuery(simulation.getId(), query, tableName, description, userNotification);
final CocosQuery newQuery = cocosQueryService.createQuery(simulation.getId(), query, tableName, description, userNotification);
response.sendRedirect("tables/" + newQuery.getId());
}
@RequestMapping(path = "/submitFile", method = RequestMethod.POST)
@RequestMapping(path = "/simulations/{simulationBusinessId}/files", method = RequestMethod.POST, params = { "query" })
@PreAuthorize("@simulationAuthorization.hasPermissionFor(#simulationBusinessId)")
public CocosUserFileQuery executeFileQuery(@RequestParam String simulationBusinessId, @RequestParam String query, @RequestParam String fileName,
@RequestParam CocosFileType filetype, @RequestParam(required= false, defaultValue = StringUtils.EMPTY) String description,
@RequestParam(required= false, defaultValue = "false") Boolean userNotification) {
public void executeFileQuery(@PathVariable String simulationBusinessId, @RequestParam String query, @RequestParam String fileName,
@RequestParam CocosFileType filetype, @RequestParam(required = false, defaultValue = StringUtils.EMPTY) String description,
@RequestParam(required = false, defaultValue = "false") Boolean userNotification, HttpServletResponse response) throws IOException {
final CocosSimulation simulation = simulationService.getByBusinessId(simulationBusinessId);
return cocosFileService.createQuery(simulation.getId(), query, fileName, filetype, description, userNotification);
final CocosQuery newQuery = cocosFileService.createQuery(simulation.getId(), query, fileName, filetype, description, userNotification);
response.sendRedirect("files/" + newQuery.getId());
}
// LIST
@RequestMapping(path = "/listQueries")
@RequestMapping(path = "/simulations/{simulationBusinessId}/queries")
@PreAuthorize("@simulationAuthorization.hasPermissionFor(#simulationBusinessId)")
public Object listQueries(
@RequestParam String simulationBusinessId,
@PathVariable String simulationBusinessId,
@RequestParam(name="page", required= false) Integer page,
@RequestParam(name="pageSize", required= false) Integer pageSize,
@RequestParam(name="sortField", required= false) String sortField,
......@@ -108,10 +114,10 @@ public class RestApiController {
return cocosQueryService.getUserSimulationQueries(simulationService.getByBusinessId(simulationBusinessId).getId(), holder.getFilter(), holder.getPageable());
}
@RequestMapping(path = "/listFiles")
@RequestMapping(path = "/simulations/{simulationBusinessId}/files")
@PreAuthorize("@simulationAuthorization.hasPermissionFor(#simulationBusinessId)")
public Object listFiles(
@RequestParam String simulationBusinessId,
@PathVariable String simulationBusinessId,
@RequestParam(name="page", required= false) Integer page,
@RequestParam(name="pageSize", required= false) Integer pageSize,
@RequestParam(name="sortField", required= false) String sortField,
......@@ -121,49 +127,106 @@ public class RestApiController {
return cocosFileService.getFiles(simulationService.getByBusinessId(simulationBusinessId), holder.getFilter(), holder.getPageable());
}
@RequestMapping(path = "/listTables")
@RequestMapping(path = "/simulations/{simulationBusinessId}/tables")
@PreAuthorize("@simulationAuthorization.hasPermissionFor(#simulationBusinessId)")
public Object listTables(@RequestParam String simulationBusinessId ) {
public Object listTables(@PathVariable String simulationBusinessId) {
return metadataService.getUserTables(simulationService.getByBusinessId(simulationBusinessId));
}
@RequestMapping(path = "/simulations")
public Object listSimulations() {
return simulationService.getSimulations();
}
private void checkQueryInSimulation(String simulationBusinessId, Long queryId) {
CocosUserQueryBase query = cocosQueryService.fetchUserQuery(queryId);
if (!StringUtils.equals(query.getSimulation().getBusinessId(), simulationBusinessId)) {
throw new CocosQueryErrorException("No query with id: " + queryId + " in simulation '" + simulationBusinessId + "'");
}
}
// FETCH RESULTS
@RequestMapping(path = "/simulations/{simulationBusinessId}/queries/{queryId}")
public Object fetchQuery(@PathVariable String simulationBusinessId, @PathVariable Long queryId) {
checkQueryInSimulation(simulationBusinessId, queryId);
return cocosQueryService.fetchUserQuery(queryId);
}
@RequestMapping(path = "/queryResults")
public Object fetchResults(@RequestParam Long queryId,
@RequestMapping(path = "/simulations/{simulationBusinessId}/files/{queryId}")
public Object fetchFileQuery(@PathVariable String simulationBusinessId, @PathVariable Long queryId) {
checkQueryInSimulation(simulationBusinessId, queryId);
return cocosQueryService.fetchUserQuery(queryId);
}
@RequestMapping(path = "/simulations/{simulationBusinessId}/tables/{queryId}")
public Object fetchTableQuery(@PathVariable String simulationBusinessId, @PathVariable Long queryId) {
checkQueryInSimulation(simulationBusinessId, queryId);
return cocosQueryService.fetchUserQuery(queryId);
}
@RequestMapping(path = "/simulations/{simulationBusinessId}/queries/{queryId}/results")
public Object fetchResults(@PathVariable String simulationBusinessId, @PathVariable Long queryId,
@RequestParam(name="page", required= false) Integer page,
@RequestParam(name="pageSize", required= false) Integer pageSize) {
checkQueryInSimulation(simulationBusinessId, queryId);
PaginationRequestHolder<Void> holder = new BasePaginationRequestBuilder(page, pageSize, "rowNumber", Direction.ASC, null).build(null);
CocosQueryExecution queryResult = queriesUtils.fetchExecution(queryId, null);
return cocosQueryResultService.getResultRows(queryResult, holder.getPageable());
}
@RequestMapping(path = "/fileInfo")
public CocosUserFileQuery fetchFileInfo(@RequestParam Long queryId) {
return (CocosUserFileQuery) cocosQueryService.fetchUserQuery(queryId);
@RequestMapping(path = "/simulations/{simulationBusinessId}/tables/{queryId}/metadata")
public Object fetchTableMetadata(@PathVariable String simulationBusinessId, @PathVariable Long queryId) {
checkQueryInSimulation(simulationBusinessId, queryId);
return metadataService.getUserTableByName(((CocosUserCreateTableQuery) cocosQueryService.fetchUserQuery(queryId)).getTableName()).getColumns();
}
@RequestMapping(path = "/tableMetadata")
public CocosUserTable fetchTableMetadata(@RequestParam String tableName) {
return metadataService.getUserTableByName(tableName);
@RequestMapping(path = "/simulations/{simulationBusinessId}/tables/{queryId}/sampleData")
public Object sampleData(@PathVariable String simulationBusinessId, @PathVariable Long queryId) {
checkQueryInSimulation(simulationBusinessId, queryId);
return metadataService.getSampleData(((CocosUserCreateTableQuery) cocosQueryService.fetchUserQuery(queryId)).getTableName()).getRows();
}
// DOWNLOAD
@RequestMapping(path = "/queryResultsDownload")
public ResponseEntity<byte[]> downloadResults(@RequestParam Long queryId, @RequestParam CocosFileType fileType) throws IOException {
@RequestMapping(path = "/simulations/{simulationBusinessId}/queries/{queryId}/download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<byte[]> downloadResults(@PathVariable String simulationBusinessId, @PathVariable Long queryId, @RequestParam CocosFileType fileType) throws IOException {
checkQueryInSimulation(simulationBusinessId, queryId);
CocosQueryExecution queryResult = queriesUtils.fetchExecution(queryId, null);
Resource resource = resultDownloadService.getQueryResult(queryResult, fileType);
return new ResponseEntity<byte[]>(IOUtils.toByteArray(resource.getInputStream()), HttpStatus.OK);
}
@RequestMapping(path = "/fileDownload")
public RedirectView downloadFile(@RequestParam Long queryId) {
@RequestMapping(path = "/simulations/{simulationBusinessId}/files/{queryId}/download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public RedirectView downloadFile(@PathVariable String simulationBusinessId, @PathVariable Long queryId) {
checkQueryInSimulation(simulationBusinessId, queryId);
return new RedirectView("/simulation/particles/downloadFile?queryId=" + queryId, true);
}
@RequestMapping(path = "/simulations/{simulationBusinessId}/queries/{queryId}/remove")
public Object removeQuery(@PathVariable String simulationBusinessId, @PathVariable Long queryId) {
checkQueryInSimulation(simulationBusinessId, queryId);
return cocosQueryService.removeQuery(queryId);
}
@RequestMapping(path = "/simulations/{simulationBusinessId}/queries/{queryId}/abort")
public Object abortQuery(@PathVariable String simulationBusinessId, @PathVariable Long queryId) {
checkQueryInSimulation(simulationBusinessId, queryId);
return cocosQueryService.abortQuery(queryId, CocosQueryAbortInitiator.USER);
}
@RequestMapping(path = "/simulations/{simulationBusinessId}/queries/{queryId}/cancel")
public Object cancelQuery(@PathVariable String simulationBusinessId, @PathVariable Long queryId) {
checkQueryInSimulation(simulationBusinessId, queryId);
return cocosQueryService.cancelQuery(queryId);
}
@RequestMapping(path = "/simulations/{simulationBusinessId}/queries/{queryId}/resume")
public Object resumeQuery(@PathVariable String simulationBusinessId, @PathVariable Long queryId) {
checkQueryInSimulation(simulationBusinessId, queryId);
CocosQuery query = cocosQueryService.fetchQuery(queryId);
return cocosFileService.resumeQuery((CocosUserFileQuery) query);
}
@ResponseStatus(code = HttpStatus.UNAUTHORIZED)
@ExceptionHandler(AccessViolationException.class)
public ErrorInfo handleAccessViolation(HttpServletRequest req, Exception ex) {
......
package pl.edu.icm.cocos.web.controllers.api.hateoas;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Link;
import pl.edu.icm.cocos.services.api.model.CocosFileType;
import pl.edu.icm.cocos.services.api.model.query.CocosQuery;
import pl.edu.icm.cocos.web.controllers.api.RestApiController;
import pl.edu.icm.cocos.web.model.CocosQueryAction;
import pl.edu.icm.cocos.web.utils.ActionAwareCocosQueryWrapper;
public abstract class BaseCocosQueryResourceEnricher<T> implements ResourceEnricher<T> {
@Autowired
private ActionAwareCocosQueryWrapper wrapper;
protected List<Link> prepareLinks(CocosQuery query) {
List<Link> links = new ArrayList<>();
List<CocosQueryAction> actions = wrapper.fetchAvalibleActions(query);
for (CocosQueryAction action : actions) {
switch (action) {
case ABORT:
links.add(linkTo(methodOn(RestApiController.class).abortQuery(query.getSimulation().getBusinessId(), query.getId())).withRel("abort"));
break;
case CANCEL:
links.add(linkTo(methodOn(RestApiController.class).cancelQuery(query.getSimulation().getBusinessId(), query.getId())).withRel("cancel"));
break;
case DOWNLOAD:
links.add(linkTo(methodOn(RestApiController.class).downloadFile(query.getSimulation().getBusinessId(), query.getId())).withRel("download"));
break;
case FILE_INFORMATION:
links.add(linkTo(methodOn(RestApiController.class).fetchFileQuery(query.getSimulation().getBusinessId(), query.getId())).withRel("fileInfo"));
break;
case REMOVE:
links.add(linkTo(methodOn(RestApiController.class).removeQuery(query.getSimulation().getBusinessId(), query.getId())).withRel("remove"));
break;
case RESUME:
links.add(linkTo(methodOn(RestApiController.class).resumeQuery(query.getSimulation().getBusinessId(), query.getId())).withRel("resume"));
break;
case SAMPLE_DATA:
links.add(linkTo(methodOn(RestApiController.class).sampleData(query.getSimulation().getBusinessId(), query.getId())).withRel("sampelData"));
break;
case SHOW_METADATA:
links.add(linkTo(methodOn(RestApiController.class).fetchTableMetadata(query.getSimulation().getBusinessId(), query.getId())).withRel("metadata"));
break;
case SHOW_RESULTS:
links.add(linkTo(methodOn(RestApiController.class).fetchResults(query.getSimulation().getBusinessId(), query.getId(), 1, 5)).withRel("results"));
break;
case DOWNLOAD_RESULTS:
try{
links.add(linkTo(methodOn(RestApiController.class).downloadResults(query.getSimulation().getBusinessId(), query.getId(), CocosFileType.CSV)).withRel("download"));
} catch (IOException e) {
// mock invocation
}
break;
}
}
return links;
}
}
package pl.edu.icm.cocos.web.controllers.api.hateoas;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;
import java.util.ArrayList;
import java.util.List;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.Resource;
import org.springframework.stereotype.Component;
import pl.edu.icm.cocos.services.api.model.CocosSimulation;
import pl.edu.icm.cocos.web.controllers.api.RestApiController;
@Component
public class CocosSimulationResourceEnricher implements ResourceEnricher<CocosSimulation> {
@Override
public boolean isApplicable(Class<? extends Object> clazz) {
return CocosSimulation.class.equals(clazz);
}
@Override
public Resource<CocosSimulation> enrich(Object source) {
CocosSimulation simulation = (CocosSimulation) source;
List<Link> links = new ArrayList<>();
links.add(linkTo(methodOn(RestApiController.class).listQueries(simulation.getBusinessId(), null, null, null, null, null)).withRel("queries"));
links.add(linkTo(methodOn(RestApiController.class).listTables(simulation.getBusinessId())).withRel("tables"));
links.add(linkTo(methodOn(RestApiController.class).listFiles(simulation.getBusinessId(), null, null, null, null, null)).withRel("files"));
return new Resource<CocosSimulation>(simulation, links);
}
}
package pl.edu.icm.cocos.web.controllers.api.hateoas;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;
import java.util.ArrayList;
import java.util.List;
import org.springframework.hateoas.Link;
......@@ -11,10 +7,9 @@ import org.springframework.hateoas.Resource;
import org.springframework.stereotype.Component;
import pl.edu.icm.cocos.services.api.model.query.CocosUserFileQuery;
import pl.edu.icm.cocos.web.controllers.api.RestApiController;
@Component
public class CocosUserFileQueryResourceEnricher implements ResourceEnricher<CocosUserFileQuery> {
public class CocosUserFileQueryResourceEnricher extends BaseCocosQueryResourceEnricher<CocosUserFileQuery> {
@Override
public boolean isApplicable(Class<? extends Object> clazz) {
......@@ -24,9 +19,7 @@ public class CocosUserFileQueryResourceEnricher implements ResourceEnricher<Coco
@Override
public Resource<CocosUserFileQuery> enrich(Object source) {
CocosUserFileQuery query = (CocosUserFileQuery) source;
List<Link> links = new ArrayList<>();
links.add(linkTo(methodOn(RestApiController.class).fetchFileInfo(query.getId())).withRel("fileInfo"));
links.add(linkTo(methodOn(RestApiController.class).downloadFile(query.getId())).withRel("download"));
List<Link> links = prepareLinks(query);
return new Resource<CocosUserFileQuery>(query, links);
}
......
package pl.edu.icm.cocos.web.controllers.api.hateoas;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.Resource;
import org.springframework.stereotype.Component;
import pl.edu.icm.cocos.services.api.model.CocosFileType;
import pl.edu.icm.cocos.services.api.model.query.CocosUserQuery;
import pl.edu.icm.cocos.web.controllers.api.RestApiController;
@Component
public class CocosUserQueryResourceEnricher implements ResourceEnricher<CocosUserQuery> {
public class CocosUserQueryResourceEnricher extends BaseCocosQueryResourceEnricher<CocosUserQuery> {
@Override
public boolean isApplicable(Class<? extends Object> clazz) {
......@@ -26,13 +19,8 @@ public class CocosUserQueryResourceEnricher implements ResourceEnricher<CocosUse
@Override
public Resource<CocosUserQuery> enrich(Object source) {
CocosUserQuery query = (CocosUserQuery) source;
List<Link> links = new ArrayList<>();
links.add(linkTo(methodOn(RestApiController.class).fetchResults(query.getId(), 1, 5)).withRel("results"));
try {
links.add(linkTo(methodOn(RestApiController.class).downloadResults(query.getId(), CocosFileType.CSV)).withRel("download"));
} catch (IOException e) {
// mock invocation
}
List<Link> links = prepareLinks(query);
return new Resource<CocosUserQuery>(query, links);
}
......
package pl.edu.icm.cocos.web.controllers.api.hateoas;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;
import java.util.ArrayList;
import java.util.List;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.Resource;
import org.springframework.stereotype.Component;
import pl.edu.icm.cocos.services.api.model.metadata.CocosUserTable;
import pl.edu.icm.cocos.web.controllers.api.RestApiController;
import pl.edu.icm.cocos.services.api.model.query.CocosUserCreateTableQuery;
@Component
public class CocosUserTableQueryResourceEnricher implements ResourceEnricher<CocosUserTable> {
public class CocosUserTableQueryResourceEnricher extends BaseCocosQueryResourceEnricher<CocosUserCreateTableQuery> {
@Override
public boolean isApplicable(Class<? extends Object> clazz) {
return CocosUserTable.class.equals(clazz);
return CocosUserCreateTableQuery.class.equals(clazz);
}
@Override
public Resource<CocosUserTable> enrich(Object source) {
CocosUserTable table = (CocosUserTable) source;
List<Link> links = new ArrayList<>();
links.add(linkTo(methodOn(RestApiController.class).fetchTableMetadata(table.getName())).withRel("metadata"));
return new Resource<CocosUserTable>(table, links);
public Resource<CocosUserCreateTableQuery> enrich(Object source) {
CocosUserCreateTableQuery query = (CocosUserCreateTableQuery) source;
List<Link> links = prepareLinks(query);
return new Resource<CocosUserCreateTableQuery>(query, links);
}
}
package pl.edu.icm.cocos.web.controllers.api.hateoas;
import java.util.List;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.Resource;
import org.springframework.stereotype.Component;
import pl.edu.icm.cocos.services.api.model.metadata.CocosUserTable;
@Component
public class CocosUserTableResourceEnricher extends BaseCocosQueryResourceEnricher<CocosUserTable> {
@Override
public boolean isApplicable(Class<? extends Object> clazz) {
return CocosUserTable.class.equals(clazz);
}
@Override
public Resource<CocosUserTable> enrich(Object source) {
CocosUserTable table = (CocosUserTable) source;
List<Link> links = prepareLinks(table.getCreateTableQuery());
return new Resource<CocosUserTable>(table, links);
}
}
......@@ -8,7 +8,7 @@ import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
@Component
public class CollectionElementEnricher {
public class ElementEnricher {
@Autowired
private ResourceConverter converter;
......@@ -21,4 +21,8 @@ public class CollectionElementEnricher {
return proceed.map(converter);
}
public Object enrichElement(Object proceed) {
return converter.convert(proceed);
}
}
......@@ -9,6 +9,7 @@ import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
......@@ -31,12 +32,12 @@ import pl.edu.icm.cocos.web.controllers.api.RestApiController;
public class HateoasWrapperAspect {
@Autowired
private CollectionElementEnricher collectionEnricher;
private ElementEnricher elementEnricher;
@SuppressWarnings({ "rawtypes", "unchecked" })
@Around(value = "@annotation(requestMapping) "
+ "&& (execution(public * pl.edu.icm.cocos.web.controllers.api.RestApiController.list*(..)) "
+ "|| execution(public * pl.edu.icm.cocos.web.controllers.api.RestApiController.fetchResults(..))"
+ "&& (execution(public Object pl.edu.icm.cocos.web.controllers.api.RestApiController.list*(..)) "
+ "|| execution(public Object pl.edu.icm.cocos.web.controllers.api.RestApiController.fetch*(..))"
+ ")")
public Object callFetch(ProceedingJoinPoint pjp, RequestMapping requestMapping) throws Throwable {
Object[] args = pjp.getArgs();
......@@ -50,6 +51,12 @@ public class HateoasWrapperAspect {
proceed = enrichElements(proceed);
if (proceed instanceof Collection) {
return new Resources((Collection) proceed, links);
} else if (proceed instanceof Resource) {
final List<Link> innerLinks = ((Resource) proceed).getLinks();