Skip to content

Commit

Permalink
Merge remote-tracking branch '4Science-bitbucket/dspace-cris-7' into …
Browse files Browse the repository at this point in the history
…dspace-cris-7
  • Loading branch information
LucaGiamminonni committed Nov 6, 2023
2 parents c45e7d8 + d369d2f commit 5e711e2
Show file tree
Hide file tree
Showing 12 changed files with 443 additions and 59 deletions.
10 changes: 8 additions & 2 deletions dspace-api/src/main/java/org/dspace/content/ItemServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,7 @@ private List<CrisLayoutField> getThumbnailFields(List<CrisLayoutTab> crisLayoutT
* @param context
* @param item
* @param bundle
* @param metadata
* @param value
* @param requireOriginal
* @throws SQLException
* @return Bitstream
*/
Expand Down Expand Up @@ -2138,4 +2136,12 @@ public boolean isLatestVersion(Context context, Item item) throws SQLException {

}

@Override
public void addResourcePolicy(Context context, Item item, int actionID, EPerson eperson)
throws SQLException, AuthorizeException {
ResourcePolicy resourcePolicy =
this.authorizeService.createResourcePolicy(context, item, null, eperson, actionID, null);
item.getResourcePolicies().add(resourcePolicy);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -922,4 +922,17 @@ public Iterator<Item> findRelatedItemsByAuthorityControlledFields(Context contex
*/
public boolean isLatestVersion(Context context, Item item) throws SQLException;

/**
* Adds a resource policy to the specified item for the given action and EPerson.
*
* @param context the DSpace context
* @param item the item to add the policy to
* @param actionID the ID of the action to add the policy for
* @param eperson the EPerson to add the policy for
* @throws SQLException if a database error occurs
* @throws AuthorizeException if the current user is not authorized to perform this action
*/
void addResourcePolicy(Context context, Item item, int actionID, EPerson eperson)
throws SQLException, AuthorizeException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public Iterator<Item> findByRelation(Context context, Item item, String relation
DiscoverQuery discoverQuery = new DiscoverQuery();
discoverQuery.setDSpaceObjectFilter(IndexableItem.TYPE);
discoverQuery.setDiscoveryConfigurationName(discoveryConfiguration.getId());
discoverQuery.setScopeObject(new IndexableItem(item));
List<String> defaultFilterQueries = discoveryConfiguration.getDefaultFilterQueries();
for (String defaultFilterQuery : defaultFilterQueries) {
discoverQuery.addFilterQueries(MessageFormat.format(defaultFilterQuery, item.getID()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,9 @@ public void setMetadataFieldMap(@SuppressWarnings("rawtypes") Map metadataFieldM
super.setMetadataFieldMap(metadataFieldMap);
}

@Override
public boolean canImportMultipleRecords() {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -311,36 +311,91 @@ public boolean canImportFromFile(String originalName) {
/*
* Get a collection of record from File,
* The first match will be return.
*
*
* @param file The file from which will read records
* @param originalName The original file name or full path
* @return a single record contains the metadatum
* @throws FileMultipleOccurencesException if more than one entry is found
*/
public ImportRecord getRecord(File file, String originalName)
throws FileMultipleOccurencesException, FileSourceException {
ImportRecord importRecords = null;
for (MetadataSource metadataSource : importSources.values()) {
try (InputStream fileInputStream = new FileInputStream(file)) {
if (metadataSource instanceof FileSource) {
FileSource fileSource = (FileSource)metadataSource;
if (fileSource.isValidSourceForFile(originalName)) {
importRecords = fileSource.getRecord(fileInputStream);
break;
try (InputStream fileInputStream = new FileInputStream(file)) {
FileSource fileSource = this.getFileSource(fileInputStream, originalName);
try {
if (fileSource.isValidSourceForFile(originalName)) {
return fileSource.getRecord(fileInputStream);
}
} catch (FileSourceException e) {
log.debug(fileSource.getImportSource() + " isn't a valid parser for file", e);
}
//catch statements is required because we could have supported format (i.e. XML)
//which fail on schema validation
} catch (FileMultipleOccurencesException e) {
log.debug("File contains multiple metadata, return with error");
throw e;
} catch (IOException e1) {
throw new FileSourceException("File cannot be read, may be null");
}
return null;
}

/**
* Get a collection of record from File,
*
* @param file The file from which will read records
* @param originalName The original file name or full path
* @return records containing metdatum
* @throws FileMultipleOccurencesException if the import configured for the {@code file}
* doesn't allow multiple records import.
* @throws FileSourceException if the file cannot be read.
*/
public List<ImportRecord> getRecords(File file, String originalName)
throws FileMultipleOccurencesException, FileSourceException {
try (InputStream fileInputStream = new FileInputStream(file)) {
FileSource fileSource = this.getFileSource(fileInputStream, originalName);
try {
if (fileSource.isValidSourceForFile(originalName)) {
List<ImportRecord> records = fileSource.getRecords(fileInputStream);
if (!fileSource.canImportMultipleRecords() && records.size() > 1) {
throw new FileMultipleOccurencesException(
"Found " + records.size() + " entries in file ( " +
originalName +
" ) but import source ( " +
fileSource.getImportSource() +
" ) not allowed to import multiple records"
);
}
return records;
}
} catch (FileSourceException e) {
log.debug(fileSource.getImportSource() + " isn't a valid parser for file", e);
}
//catch statements is required because we could have supported format (i.e. XML)
//which fail on schema validation
} catch (FileSourceException e) {
log.debug(metadataSource.getImportSource() + " isn't a valid parser for file");
} catch (FileMultipleOccurencesException e) {
log.debug("File contains multiple metadata, return with error");
throw e;
} catch (IOException e1) {
throw new FileSourceException("File cannot be read, may be null");
} catch (IOException e1) {
throw new FileSourceException("File cannot be read, may be null");
}
return null;
}

protected FileSource getFileSource(File file, String originalName) throws FileSourceException {
try (InputStream fileInputStream = new FileInputStream(file)) {
return getFileSource(file, originalName);
} catch (IOException e1) {
throw new FileSourceException("File cannot be read, may be null");
}
}

protected FileSource getFileSource(InputStream fileInputStream, String originalName) {
for (MetadataSource metadataSource : importSources.values()) {
if (metadataSource instanceof FileSource) {
FileSource fileSource = (FileSource)metadataSource;
if (fileSource.isValidSourceForFile(originalName)) {
return fileSource;
}
}
}
return importRecords;
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public abstract class AbstractPlainMetadataSource

/**
* Set the file extensions supported by this metadata service
*
*
* @param supportedExtensions the file extensions (xml,txt,...) supported by this service
*/
public void setSupportedExtensions(List<String> supportedExtensions) {
Expand All @@ -64,6 +64,9 @@ public List<String> getSupportedExtensions() {
@Override
public List<ImportRecord> getRecords(InputStream is) throws FileSourceException {
List<PlainMetadataSourceDto> datas = readData(is);
if (datas == null) {
return List.of();
}
List<ImportRecord> records = new ArrayList<>();
for (PlainMetadataSourceDto item : datas) {
records.add(toRecord(item));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public ImportRecord getRecord(InputStream inputStream)

/**
* This method is used to decide if the FileSource manage the file format
*
*
* @param originalName the file file original name
* @return true if the FileSource can parse the file, false otherwise
*/
Expand All @@ -67,4 +67,13 @@ public default boolean isValidSourceForFile(String originalName) {
return false;
}

/**
* This method is used to determine if we can import multiple records at once placed in the same source file.
*
* @return true if allowed to import multiple records in the same file, false otherwise
*/
public default boolean canImportMultipleRecords() {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -314,17 +314,20 @@ private Item createProfileItem(Context context, EPerson ePerson, Collection coll

item = installItemService.installItem(context, workspaceItem);

context.uncacheEntity(workspaceItem);

if (isNewProfileNotVisibleByDefault()) {
Group anonymous = groupService.findByName(context, ANONYMOUS);
authorizeService.removeGroupPolicies(context, item, anonymous);
}

authorizeService.addPolicy(context, item, READ, ePerson);
itemService.addResourcePolicy(context, item, READ, ePerson);

if (isAdditionOfWritePolicyOnProfileEnabled()) {
authorizeService.addPolicy(context, item, WRITE, ePerson);
itemService.addResourcePolicy(context, item, WRITE, ePerson);
}


return reloadItem(context, item);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public RegistrationRest convert(RegistrationData registrationData, Projection pr
EPerson ePerson = null;
try {
ePerson = accountService.getEPerson(context, registrationData.getToken());
if (ePerson == null && registrationData.getRegistrationType().equals(RegistrationTypeEnum.ORCID)) {
ePerson = context.getCurrentUser();
}
} catch (SQLException | AuthorizeException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,9 @@ public Iterable<WorkspaceItemRest> upload(Context context, HttpServletRequest re
for (MultipartFile mpFile : uploadfiles) {
File file = Utils.getFile(mpFile, "upload-loader", "filedataloader");
try {
ImportRecord record = importService.getRecord(file, mpFile.getOriginalFilename());
if (record != null) {
records.add(record);
List<ImportRecord> recordsFound = importService.getRecords(file, mpFile.getOriginalFilename());
if (recordsFound != null && !recordsFound.isEmpty()) {
records.addAll(recordsFound);
break;
}
} catch (Exception e) {
Expand All @@ -334,11 +334,15 @@ public Iterable<WorkspaceItemRest> upload(Context context, HttpServletRequest re
} catch (Exception e) {
log.error("Error importing metadata", e);
}
WorkspaceItem source = submissionService.
createWorkspaceItem(context, getRequestService().getCurrentRequest());
merge(context, records, source);
result = new ArrayList<>();
result.add(source);
result = new ArrayList<>(records.size());
for (ImportRecord importRecord : records) {
WorkspaceItem source = submissionService.
createWorkspaceItem(context, getRequestService().getCurrentRequest());

merge(context, importRecord, source);

result.add(source);
}

//perform upload of bitstream if there is exact one result and convert workspaceitem to entity rest
if (!result.isEmpty()) {
Expand All @@ -348,18 +352,17 @@ public Iterable<WorkspaceItemRest> upload(Context context, HttpServletRequest re
//load bitstream into bundle ORIGINAL only if there is one result (approximately this is the
// right behaviour for pdf file but not for other bibliographic format e.g. bibtex)
if (result.size() == 1) {
ClassLoader loader = this.getClass().getClassLoader();
for (int i = 0; i < submissionConfig.getNumberOfSteps(); i++) {
SubmissionStepConfig stepConfig = submissionConfig.getStep(i);
ClassLoader loader = this.getClass().getClassLoader();
Class stepClass;
try {
stepClass = loader.loadClass(stepConfig.getProcessingClassName());
Object stepInstance = stepClass.newInstance();
Class<?> stepClass = loader.loadClass(stepConfig.getProcessingClassName());
Object stepInstance = stepClass.getConstructor().newInstance();
if (UploadableStep.class.isAssignableFrom(stepClass)) {
UploadableStep uploadableStep = (UploadableStep) stepInstance;
for (MultipartFile mpFile : uploadfiles) {
ErrorRest err = uploadableStep.upload(context,
submissionService, stepConfig, wi, mpFile);
ErrorRest err =
uploadableStep.upload(context, submissionService, stepConfig, wi, mpFile);
if (err != null) {
errors.add(err);
}
Expand Down Expand Up @@ -449,7 +452,7 @@ private BaseObjectRest<?> findItemRestById(Context context, String itemId) throw
return authorizationRestUtil.getObject(context, objectId);
}

private void merge(Context context, List<ImportRecord> records, WorkspaceItem item) throws SQLException {
private void merge(Context context, ImportRecord record, WorkspaceItem item) throws SQLException {
for (MetadataValue metadataValue : itemService.getMetadata(
item.getItem(), Item.ANY, Item.ANY, Item.ANY, Item.ANY)) {
itemService.clearMetadata(context, item.getItem(),
Expand All @@ -458,13 +461,11 @@ private void merge(Context context, List<ImportRecord> records, WorkspaceItem it
metadataValue.getMetadataField().getQualifier(),
metadataValue.getLanguage());
}
for (ImportRecord record : records) {
if (record != null && record.getValueList() != null) {
for (MetadatumDTO metadataValue : record.getValueList()) {
itemService.addMetadata(context, item.getItem(), metadataValue.getSchema(),
metadataValue.getElement(), metadataValue.getQualifier(), null,
metadataValue.getValue());
}
if (record != null && record.getValueList() != null) {
for (MetadatumDTO metadataValue : record.getValueList()) {
itemService.addMetadata(context, item.getItem(), metadataValue.getSchema(),
metadataValue.getElement(), metadataValue.getQualifier(), null,
metadataValue.getValue());
}
}
}
Expand Down
Loading

0 comments on commit 5e711e2

Please sign in to comment.