Skip to content

Commit

Permalink
[Task 71441] altered the processFiles endpoint and added the process …
Browse files Browse the repository at this point in the history
…filetypes endpoint with tests
  • Loading branch information
Raf-atmire committed Jun 18, 2020
1 parent 31c87c2 commit de357b5
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import org.apache.commons.collections4.ListUtils;
Expand All @@ -23,7 +25,9 @@
import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.service.AuthorizeService;
import org.dspace.content.Bitstream;
import org.dspace.content.Item;
import org.dspace.content.MetadataField;
import org.dspace.content.MetadataValue;
import org.dspace.content.ProcessStatus;
import org.dspace.content.dao.ProcessDAO;
import org.dspace.content.service.BitstreamFormatService;
Expand Down Expand Up @@ -227,4 +231,18 @@ public int countTotal(Context context) throws SQLException {
return processDAO.countRows(context);
}

@Override
public List<String> getFileTypesForProcessBitstreams(Context context, Process process) {
List<Bitstream> list = getBitstreams(context, process);
Set<String> fileTypesSet = new HashSet<>();
for (Bitstream bitstream : list) {
List<MetadataValue> metadata = bitstreamService.getMetadata(bitstream,
Process.BITSTREAM_TYPE_METADATAFIELD, Item.ANY);
if (metadata != null && !metadata.isEmpty()) {
fileTypesSet.add(metadata.get(0).getValue());
}
}
return new ArrayList<>(fileTypesSet);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,6 @@ public void appendFile(Context context, Process process, InputStream is, String
*/
int countTotal(Context context) throws SQLException;

public List<String> getFileTypesForProcessBitstreams(Context context, Process process);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest.model;

import java.util.LinkedList;
import java.util.List;

import org.dspace.app.rest.RestResourceController;

public class ProcessFileTypesRest extends BaseObjectRest<String> {

public static final String NAME = "filetypes";
public static final String PLURAL_NAME = "filetypes";
public static final String CATEGORY = RestAddressableModel.SYSTEM;

private List<String> values;

/**
* Generic getter for the values
* @return the values value of this ProcessFileTypesRest
*/
public List<String> getValues() {
return values;
}

/**
* Generic setter for the values
* @param values The values to be set on this ProcessFileTypesRest
*/
public void setValues(List<String> values) {
this.values = values;
}

public void addValue(String value) {
if (values == null) {
values = new LinkedList<>();
}
values.add(value);
}

@Override
public String getCategory() {
return CATEGORY;
}

@Override
public Class getController() {
return RestResourceController.class;
}

@Override
public String getType() {
return NAME;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@
name = ProcessRest.FILES,
method = "getFilesFromProcess"
),
@LinkRest(
name = ProcessRest.FILE_TYPES,
method = "getFileTypesFromProcess"
)
})
public class ProcessRest extends BaseObjectRest<Integer> {
public static final String NAME = "process";
public static final String PLURAL_NAME = "processes";
public static final String CATEGORY = RestAddressableModel.SYSTEM;

public static final String FILES = "files";
public static final String FILE_TYPES = "filetypes";
public String getCategory() {
return CATEGORY;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest.model.hateoas;

import org.dspace.app.rest.model.ProcessFileTypesRest;
import org.dspace.app.rest.model.hateoas.annotations.RelNameDSpaceResource;

@RelNameDSpaceResource(ProcessFileTypesRest.NAME)
public class ProcessFileTypesResource extends HALResource<ProcessFileTypesRest> {

public ProcessFileTypesResource(ProcessFileTypesRest content) {
super(content);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest.repository;

import java.sql.SQLException;
import java.util.List;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;

import org.dspace.app.rest.model.ProcessFileTypesRest;
import org.dspace.app.rest.model.ProcessRest;
import org.dspace.app.rest.projection.Projection;
import org.dspace.authorize.AuthorizeException;
import org.dspace.core.Context;
import org.dspace.scripts.Process;
import org.dspace.scripts.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;

@Component(ProcessRest.CATEGORY + "." + ProcessRest.NAME + "." + ProcessRest.FILE_TYPES)
public class ProcessFileTypesLinkRepository extends AbstractDSpaceRestRepository implements LinkRestRepository {

@Autowired
private ProcessService processService;

@Autowired
private ProcessRestRepository processRestRepository;

@PreAuthorize("hasAuthority('ADMIN')")
public ProcessFileTypesRest getFileTypesFromProcess(@Nullable HttpServletRequest request,
Integer processId,
@Nullable Pageable optionalPageable,
Projection projection) throws SQLException, AuthorizeException {

Context context = obtainContext();
Process process = processService.find(context, processId);
if (process == null) {
throw new ResourceNotFoundException("Process with id " + processId + " was not found");
}
List<String> fileTypes = processService.getFileTypesForProcessBitstreams(context, process);
ProcessFileTypesRest processFileTypesRest = new ProcessFileTypesRest();
processFileTypesRest.setId("filetypes-" + processId);
processFileTypesRest.setValues(fileTypes);
return processFileTypesRest;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@
package org.dspace.app.rest.repository;

import java.sql.SQLException;
import java.util.List;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dspace.app.rest.model.BitstreamRest;
import org.dspace.app.rest.model.ProcessFileWrapperRest;
import org.dspace.app.rest.model.ProcessRest;
import org.dspace.app.rest.projection.Projection;
import org.dspace.authorize.AuthorizeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;

/**
* This is the {@link LinkRestRepository} implementation that takes care of retrieving the
* {@link ProcessFileWrapperRest} for the Process endpoints
* This is the {@link LinkRestRepository} implementation that takes care of retrieving the list of
* {@link org.dspace.content.Bitstream} objects for the Process endpoints
*
*/
@Component(ProcessRest.CATEGORY + "." + ProcessRest.NAME + "." + ProcessRest.FILES)
Expand All @@ -37,28 +38,24 @@ public class ProcessFilesLinkRepository extends AbstractDSpaceRestRepository imp
private ProcessRestRepository processRestRepository;

/**
* This method will retrieve all the files from the process and wrap them into a {@link ProcessFileWrapperRest}
* object to return
* This method will retrieve all the files from the process
* @param request The current request
* @param processId The processId for the Process to use
* @param optionalPageable Pageable if applicable
* @param projection Projection if applicable
* @return A {@link ProcessFileWrapperRest} object filled with the bitstreams from the process
* @return A list of {@link BitstreamRest} objects filled
* @throws SQLException If something goes wrong
* @throws AuthorizeException If something goes wrong
*/
@PreAuthorize("hasAuthority('ADMIN')")
public ProcessFileWrapperRest getFilesFromProcess(@Nullable HttpServletRequest request,
Integer processId,
@Nullable Pageable optionalPageable,
Projection projection) throws SQLException, AuthorizeException {
public Page<BitstreamRest> getFilesFromProcess(@Nullable HttpServletRequest request,
Integer processId,
@Nullable Pageable optionalPageable,
Projection projection) throws SQLException, AuthorizeException {


ProcessFileWrapperRest processFileWrapperRest = new ProcessFileWrapperRest();
processFileWrapperRest.setBitstreams(processRestRepository.getProcessBitstreams(processId));
processFileWrapperRest.setProcessId(processId);

return processFileWrapperRest;
List<BitstreamRest> list = processRestRepository.getProcessBitstreams(processId);
Pageable pageable = utils.getPageable(optionalPageable);
return utils.getPage(list, pageable);
}

/**
Expand All @@ -68,13 +65,13 @@ public ProcessFileWrapperRest getFilesFromProcess(@Nullable HttpServletRequest r
* @param fileType The filetype that the bitstream has to be
* @param pageable Pageable if applicable
* @param projection The current projection
* @return The BitstreamRest object that corresponds with the Process and type
* @return The BitstreamRest object that corresponds with the Process and type
* @throws SQLException If something goes wrong
* @throws AuthorizeException If something goes wrong
*/
@PreAuthorize("hasPermission(#processId, 'PROCESS', 'READ')")
public BitstreamRest getResource(HttpServletRequest request, String processId, String fileType,
Pageable pageable, Projection projection)
Pageable pageable, Projection projection)
throws SQLException, AuthorizeException {
if (log.isTraceEnabled()) {
log.trace("Retrieving Files with type " + fileType + " from Process with ID: " + processId);
Expand Down
Loading

0 comments on commit de357b5

Please sign in to comment.