Skip to content

Commit

Permalink
Merge branch 'dspace-cris-2023_02_x' of https://bitbucket.org/4Scienc…
Browse files Browse the repository at this point in the history
…e/dspace-cris into main-cris
  • Loading branch information
abollini committed May 12, 2024
2 parents 00aa90c + f808825 commit c82ee8a
Show file tree
Hide file tree
Showing 60 changed files with 1,463 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,27 @@ public void internalRun() throws Exception {

}

private void performExport(Collection collection) throws Exception {

String fileName = xlsCollectionCrosswalk.getFileName();

ByteArrayOutputStream out = new ByteArrayOutputStream();
xlsCollectionCrosswalk.disseminate(context, collection, out);
private void performExport(Collection collection) {
try {
xlsCollectionCrosswalk.setHandler(handler);
String fileName = xlsCollectionCrosswalk.getFileName();

ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
context.setMode(Context.Mode.READ_WRITE);
handler.writeFilestream(context, fileName, in, xlsCollectionCrosswalk.getMIMEType());
ByteArrayOutputStream out = new ByteArrayOutputStream();
xlsCollectionCrosswalk.disseminate(context, collection, out);

handler.logInfo("Items exported successfully into file named " + fileName);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
context.setMode(Context.Mode.READ_WRITE);
handler.writeFilestream(context, fileName, in, xlsCollectionCrosswalk.getMIMEType());

handler.logInfo("Items exported successfully into file named " + fileName);
} catch (Exception e) {
e.printStackTrace();
} finally {
xlsCollectionCrosswalk.setHandler(null);
}
}


private void assignCurrentUserInContext() throws SQLException {
UUID uuid = getEpersonIdentifier();
if (uuid != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
package org.dspace.app.bulkimport.service;

import java.util.Iterator;
import java.util.function.BiConsumer;
import java.util.logging.Level;

import org.apache.poi.ss.usermodel.Workbook;
import org.dspace.app.bulkimport.model.EntityRow;
Expand Down Expand Up @@ -56,6 +58,6 @@ public interface BulkImportWorkbookBuilder {
* @param items the items to be placed inside the generated workbook
* @return the workbook
*/
Workbook buildForItems(Context context, Collection collection, Iterator<Item> items);

Workbook buildForItems(Context context, Collection collection, Iterator<Item> items,
BiConsumer<Level, String> logHandler);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package org.dspace.app.bulkimport.service;

import static com.google.common.collect.Iterators.filter;
import static com.google.common.collect.Iterators.transform;
import static org.dspace.app.bulkedit.BulkImport.ACCESS_CONDITION_HEADER;
import static org.dspace.app.bulkedit.BulkImport.ADDITIONAL_ACCESS_CONDITION_HEADER;
Expand All @@ -27,15 +28,16 @@
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.logging.Level;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
Expand All @@ -55,14 +57,18 @@
import org.dspace.content.dto.ItemDTO;
import org.dspace.content.dto.MetadataValueDTO;
import org.dspace.content.dto.ResourcePolicyDTO;
import org.dspace.content.integration.crosswalks.XlsCollectionCrosswalk;
import org.dspace.content.service.CollectionService;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.core.CrisConstants;
import org.dspace.core.exception.SQLRuntimeException;
import org.dspace.scripts.handler.DSpaceRunnableHandler;
import org.dspace.submit.model.AccessConditionOption;
import org.dspace.submit.model.UploadConfiguration;
import org.dspace.submit.model.UploadConfigurationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.datetime.DateFormatter;

Expand All @@ -74,7 +80,7 @@
*/
public class BulkImportWorkbookBuilderImpl implements BulkImportWorkbookBuilder {

private static final Logger LOGGER = LogManager.getLogger(BulkImportWorkbookBuilderImpl.class);
private static final Logger LOGGER = LoggerFactory.getLogger(BulkImportWorkbookBuilderImpl.class);

private static final DateFormatter DATE_FORMATTER = new DateFormatter("yyyy-MM-dd");

Expand All @@ -87,7 +93,11 @@ public class BulkImportWorkbookBuilderImpl implements BulkImportWorkbookBuilder
@Autowired
private CollectionService collectionService;

@Autowired
private XlsCollectionCrosswalk xlsCollectionCrosswalk;

private DCInputsReader reader;
private DSpaceRunnableHandler handler;

@PostConstruct
private void postConstruct() {
Expand All @@ -100,13 +110,22 @@ private void postConstruct() {

@Override
public <T> Workbook build(Context ctx, Collection collection, Iterator<T> sources, ItemDTOConverter<T> converter) {
Iterator<ItemDTO> itemIterator = transform(sources, source -> converter.convert(ctx, source));
Iterator<ItemDTO> itemIterator =
filter(
transform(sources, source -> converter.convert(ctx, source)),
Objects::nonNull
);
return build(ctx, collection, itemIterator);
}

@Override
public Workbook buildForItems(Context context, Collection collection, Iterator<Item> items) {
Iterator<ItemDTO> itemIterator = transform(items, item -> convertItem(context, collection, item));
public Workbook buildForItems(Context context, Collection collection, Iterator<Item> items,
BiConsumer<Level, String> logHandler) {
Iterator<ItemDTO> itemIterator =
filter(
transform(items, item -> convertItem(context, collection, item, logHandler)),
Objects::nonNull
);
return build(context, collection, itemIterator);
}

Expand Down Expand Up @@ -186,11 +205,13 @@ private void writeWorkbookContent(Iterator<ItemDTO> items, BulkImportWorkbook wo
}

private void writeMainSheet(ItemDTO item, BulkImportSheet mainSheet) {
if (item == null) {
return;
}

mainSheet.appendRow();

for (String header : mainSheet.getHeaders()) {

if (header.equals(ID_HEADER)) {
mainSheet.setValueOnLastRow(header, item.getId().toString());
continue;
Expand All @@ -201,9 +222,12 @@ private void writeMainSheet(ItemDTO item, BulkImportSheet mainSheet) {
continue;
}

item.getMetadataValues(header).forEach(value -> writeMetadataValue(mainSheet, header, value));
}
List<MetadataValueDTO> metadataValues = item.getMetadataValues(header);

if (metadataValues != null) {
metadataValues.forEach(value -> writeMetadataValue(mainSheet, header, value));
}
}
}

private void writeNestedMetadataSheet(ItemDTO item, BulkImportSheet nestedMetadataSheet) {
Expand Down Expand Up @@ -243,8 +267,10 @@ private void writeNestedMetadataRow(ItemDTO item, BulkImportSheet nestedMetadata
}

private void writeBitstreamSheet(ItemDTO item, BulkImportSheet sheet) {
for (BitstreamDTO bitstream : item.getBitstreams()) {
writeBitstreamRow(sheet, item, bitstream);
if (item != null) {
for (BitstreamDTO bitstream : item.getBitstreams()) {
writeBitstreamRow(sheet, item, bitstream);
}
}
}

Expand Down Expand Up @@ -432,12 +458,12 @@ private int getMetadataGroupSize(ItemDTO item, String metadataGroupFieldName) {
private void autoSizeColumns(List<BulkImportSheet> sheets) {
sheets.forEach(sheet -> autoSizeColumns(sheet.getSheet()));
}

private ItemDTO convertItem(Context context, Collection collection, Item item) {

private ItemDTO convertItem(
Context context, Collection collection, Item item, BiConsumer<Level, String> logHandler) {
if (isNotInCollection(context, item, collection)) {
throw new IllegalArgumentException("It is not possible to export items from two different collections: "
+ "item " + item.getID() + " is not in collection " + collection.getID());
logHandler.accept(Level.WARNING, "Skipping item " + item.getID() +
" because it is not mapped from collection " + collection.getID());
return null;
}

ItemDTO itemDTO = itemToItemDTOConverter.convert(context, item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,6 @@ private void doNodes(Node n) throws SAXException, SearchServiceException, Submis
* by the collection handle.
*/
private void processMap(Node e) throws SAXException, SearchServiceException {
// create a context
Context context = new Context();

NodeList nl = e.getChildNodes();
int len = nl.getLength();
for (int i = 0; i < len; i++) {
Expand All @@ -484,14 +481,8 @@ private void processMap(Node e) throws SAXException, SearchServiceException {
}
if (id != null) {
collectionToSubmissionConfig.put(id, value);

} else {
// get all collections for this entity-type
List<Collection> collections = collectionService.findAllCollectionsByEntityType( context,
entityType);
for (Collection collection : collections) {
collectionToSubmissionConfig.putIfAbsent(collection.getHandle(), value);
}
collectionToSubmissionConfig.putIfAbsent(entityType, value);
}
} // ignore any child node that isn't a "name-map"
}
Expand Down
30 changes: 25 additions & 5 deletions dspace-api/src/main/java/org/dspace/content/ItemServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1924,17 +1924,37 @@ public int countWithdrawnItems(Context context) throws SQLException {

@Override
public boolean canCreateNewVersion(Context context, Item item) throws SQLException {
boolean userAuthorized = false;
if (authorizeService.isAdmin(context, item)) {
return true;
userAuthorized = true;
}

if (context.getCurrentUser() != null
&& context.getCurrentUser().equals(item.getSubmitter())) {
return configurationService.getPropertyAsType(
"versioning.submitterCanCreateNewVersion", false);
&& context.getCurrentUser().equals(item.getSubmitter())) {
userAuthorized = configurationService.getPropertyAsType(
"versioning.submitterCanCreateNewVersion", false);
}

if (!userAuthorized) {
List<String> allowedGroups = List
.of(configurationService.getArrayProperty("versioning.allowed.groups"));
userAuthorized = allowedGroups.stream().anyMatch(groupName -> {
try {
return groupService.isMember(context, groupName);
} catch (SQLException e) {
return false;
}
});
}

return false;
if (userAuthorized) {
List<String> allowedEntities = List
.of(configurationService.getArrayProperty("versioning.enabled.entities"));
String entityType = getEntityType(item);
return entityType != null && allowedEntities.contains(entityType);
}

return userAuthorized;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public Choices getMatches(String query, int start, int limit, String locale) {
}
}
Choice[] vArray = new Choice[v.size()];
return new Choices(v.toArray(vArray), start, found, Choices.CF_AMBIGUOUS, false, dflt);
return new Choices(v.toArray(vArray), start, found, Choices.CF_UNSET, false, dflt);
}

@Override
Expand All @@ -187,8 +187,8 @@ public Choices getBestMatch(String text, String locale) {
for (int i = 0; i < valuesLocale.length; ++i) {
if (text.equalsIgnoreCase(valuesLocale[i])) {
Choice v[] = new Choice[1];
v[0] = new Choice(String.valueOf(i), valuesLocale[i], labelsLocale[i]);
return new Choices(v, 0, v.length, Choices.CF_UNCERTAIN, false, 0);
v[0] = new Choice(null, valuesLocale[i], labelsLocale[i]);
return new Choices(v, 0, v.length, Choices.CF_UNSET, false, 0);
}
}
return new Choices(Choices.CF_NOTFOUND);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,21 +163,33 @@ private Choices getMatches(String text, int start, int limit, String locale, boo
private List<Choice> getChoiceListFromQueryResults(SolrDocumentList results, String searchTitle,
boolean onlyExactMatches) {
return results
.stream()
.map(doc -> {
String title;
if (onlyExactMatches && isForceInternalTitle() || !onlyExactMatches) {
Object fieldValue = doc.getFieldValue("dc.title");
title = fieldValue instanceof String ? (String) fieldValue
: ((ArrayList<String>) fieldValue).get(0);
} else {
title = searchTitle;
}
Map<String, String> extras = ItemAuthorityUtils.buildExtra(getPluginInstanceName(), doc);
return new Choice((String) doc.getFieldValue("search.resourceid"),
title,
title, extras, DEFAULT);
}).collect(Collectors.toList());
.stream()
.map(doc -> {
String title = searchTitle;
List<String> objectNames = List.of();
if (onlyExactMatches && isForceInternalTitle() || !onlyExactMatches) {
Object fieldValue = doc.getFieldValue("objectname");
if (fieldValue != null) {
if (fieldValue instanceof String) {
title = (String) fieldValue;
} else {
objectNames = (ArrayList<String>) fieldValue;
title = objectNames.get(0);
}
} else {
title = ((ArrayList<String>) doc.getFieldValue("dc.title"))
.stream()
.findFirst()
.orElse(searchTitle);
}
}
String uuid = (String) doc.getFieldValue("search.resourceid");
Map<String, String> extras = ItemAuthorityUtils.buildExtra(getPluginInstanceName(),
doc, objectNames, uuid);
return new Choice(uuid,
title,
title, extras);
}).collect(Collectors.toList());
}

@Override
Expand Down
Loading

0 comments on commit c82ee8a

Please sign in to comment.