Skip to content

Commit

Permalink
Merge pull request #2791 from 4Science/CST-3091
Browse files Browse the repository at this point in the history
DS-4514 Start new submission by uploading a Bibliographic File (using Live Import)
  • Loading branch information
tdonohue committed Aug 6, 2020
2 parents 2027b03 + d2e969f commit fe9a7e6
Show file tree
Hide file tree
Showing 22 changed files with 1,354 additions and 290 deletions.
@@ -0,0 +1,107 @@
/**
* 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.importer.external.bibtex.service;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Resource;

import org.dspace.importer.external.exception.FileSourceException;
import org.dspace.importer.external.service.components.AbstractPlainMetadataSource;
import org.dspace.importer.external.service.components.dto.PlainMetadataKeyValueItem;
import org.dspace.importer.external.service.components.dto.PlainMetadataSourceDto;
import org.jbibtex.BibTeXDatabase;
import org.jbibtex.BibTeXEntry;
import org.jbibtex.BibTeXParser;
import org.jbibtex.Key;
import org.jbibtex.ParseException;
import org.jbibtex.Value;

/**
* Implements a metadata importer for BibTeX files
*
* @author Pasquale Cavallo (pasquale.cavallo at 4science dot it)
*/
public class BibtexImportMetadataSourceServiceImpl extends AbstractPlainMetadataSource {


/**
* The string that identifies this import implementation as
* MetadataSource implementation
*
* @return the identifying uri
*/
@Override
public String getImportSource() {
return "BibTeXMetadataSource";
}

@Override
protected List<PlainMetadataSourceDto> readData (InputStream
inputStream) throws FileSourceException {
List<PlainMetadataSourceDto> list = new ArrayList<>();
BibTeXDatabase database;
try {
database = parseBibTex(inputStream);
} catch (IOException | ParseException e) {
throw new FileSourceException("Unable to parse file with BibTeX parser");
}
if (database == null || database.getEntries() == null) {
throw new FileSourceException("File results in an empty list of metadata");
}
if (database.getEntries() != null) {
for (Entry<Key, BibTeXEntry> entry : database.getEntries().entrySet()) {
PlainMetadataSourceDto item = new PlainMetadataSourceDto();
List<PlainMetadataKeyValueItem> keyValues = new ArrayList<>();
item.setMetadata(keyValues);
PlainMetadataKeyValueItem keyValueItem = new PlainMetadataKeyValueItem();
keyValueItem.setKey(entry.getValue().getType().getValue());
keyValueItem.setValue(entry.getKey().getValue());
keyValues.add(keyValueItem);
if (entry.getValue().getFields() != null) {
for (Entry<Key,Value> subentry : entry.getValue().getFields().entrySet()) {
PlainMetadataKeyValueItem innerItem = new PlainMetadataKeyValueItem();
innerItem.setKey(subentry.getKey().getValue());
innerItem.setValue(subentry.getValue().toUserString());
keyValues.add(innerItem);
}
}
list.add(item);
}
}
return list;
}

private BibTeXDatabase parseBibTex(InputStream inputStream) throws IOException, ParseException {
Reader reader = new InputStreamReader(inputStream);
BibTeXParser bibtexParser = new BibTeXParser();
return bibtexParser.parse(reader);
}


/**
* Retrieve the MetadataFieldMapping containing the mapping between RecordType
* (in this case PlainMetadataSourceDto.class) and Metadata
*
* @return The configured MetadataFieldMapping
*/
@Override
@SuppressWarnings("unchecked")
@Resource(name = "bibtexMetadataFieldMap")
public void setMetadataFieldMap(@SuppressWarnings("rawtypes") Map metadataFieldMap) {
super.setMetadataFieldMap(metadataFieldMap);
}

}
@@ -0,0 +1,29 @@
/**
* 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.importer.external.exception;

/**
* This exception could be throws when more than one element is found
* in a method that works on one only.
*
* @author Pasquale Cavallo (pasquale.cavallo at 4science dot it)
*/

public class FileMultipleOccurencesException extends Exception {

private static final long serialVersionUID = 1222409723339501937L;

public FileMultipleOccurencesException(String message, Throwable cause) {
super(message, cause);
}

public FileMultipleOccurencesException(String message) {
super(message);
}
}
@@ -0,0 +1,28 @@
/**
* 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.importer.external.exception;

/**
* Represents a problem with the File content: e.g. null input stream, invalid content, ...
*
* @author Pasquale Cavallo (pasquale.cavallo at 4science dot it)
*/

public class FileSourceException extends Exception {

private static final long serialVersionUID = 6895579588455260182L;

public FileSourceException(String message, Throwable cause) {
super(message, cause);
}

public FileSourceException(String message) {
super(message);
}
}
Expand Up @@ -117,16 +117,13 @@ public void setMetadataFieldMap(Map<MetadataFieldConfig, MetadataContributor<Rec
public Collection<MetadatumDTO> resultToDCValueMapping(RecordType record) {
List<MetadatumDTO> values = new LinkedList<MetadatumDTO>();


for (MetadataContributor<RecordType> query : getMetadataFieldMap().values()) {
try {
values.addAll(query.contributeMetadata(record));
} catch (Exception e) {
log.error("Error", e);
}

}
return values;

}
}
@@ -0,0 +1,94 @@
/**
* 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.importer.external.metadatamapping.contributor;

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

import org.dspace.importer.external.metadatamapping.MetadataFieldConfig;
import org.dspace.importer.external.metadatamapping.MetadataFieldMapping;
import org.dspace.importer.external.metadatamapping.MetadatumDTO;
import org.dspace.importer.external.service.components.dto.PlainMetadataKeyValueItem;
import org.dspace.importer.external.service.components.dto.PlainMetadataSourceDto;

/**
* Metadata contributor that takes an PlainMetadataSourceDto instance and turns it into a
* collection of metadatum
*
* @author Pasquale Cavallo (pasquale.cavallo at 4science dot it)
*/
public class SimpleMetadataContributor implements MetadataContributor<PlainMetadataSourceDto> {

private MetadataFieldConfig field;

private String key;

private MetadataFieldMapping<PlainMetadataSourceDto,
MetadataContributor<PlainMetadataSourceDto>> metadataFieldMapping;

public SimpleMetadataContributor(MetadataFieldConfig field, String key) {
this.field = field;
this.key = key;
}

public SimpleMetadataContributor() { }

/**
* Set the metadataFieldMapping of this SimpleMetadataContributor
*
* @param metadataFieldMapping the new mapping.
*/
@Override
public void setMetadataFieldMapping(
MetadataFieldMapping<PlainMetadataSourceDto,
MetadataContributor<PlainMetadataSourceDto>> metadataFieldMapping) {
this.metadataFieldMapping = metadataFieldMapping;
}

/**
* Retrieve the metadata associated with the given object.
* It match the key found in PlainMetadataSourceDto instance with the key passed to constructor.
* In case of success, new metadatum is constructer (using field elements and PlainMetadataSourceDto value)
* and added to the list.
*
* @param t A class to retrieve metadata and key to match from. t and contained list "metadata" MUST be not null.
* @return a collection of import records. Only the identifier of the found records may be put in the record.
*/
@Override
public Collection<MetadatumDTO> contributeMetadata(PlainMetadataSourceDto t) {
List<MetadatumDTO> values = new LinkedList<>();
for (PlainMetadataKeyValueItem metadatum : t.getMetadata()) {
if (key.equals(metadatum.getKey())) {
MetadatumDTO dcValue = new MetadatumDTO();
dcValue.setValue(metadatum.getValue());
dcValue.setElement(field.getElement());
dcValue.setQualifier(field.getQualifier());
dcValue.setSchema(field.getSchema());
values.add(dcValue);
}
}
return values;
}

/*
* Setter to inject field item
*/
public void setField(MetadataFieldConfig field) {
this.field = field;
}

/*
* Setter to inject key value
*/
public void setKey(String key) {
this.key = key;
}

}

0 comments on commit fe9a7e6

Please sign in to comment.