use of org.dspace.content.MetadataValue in project DSpace by DSpace.
the class DSpaceObjectMetadataRemoveOperation method remove.
/**
* Removes a metadata from the dso at a given index (or all of that type if no index was given)
*
* @param context context patch is being performed in
* @param dso dso being patched
* @param dsoService service doing the patch in db
* @param metadataField md field being patched
* @param index index at where we want to delete metadata
*/
private void remove(Context context, DSpaceObject dso, DSpaceObjectService dsoService, MetadataField metadataField, String index) {
metadataPatchUtils.checkMetadataFieldNotNull(metadataField);
try {
if (index == null) {
// remove all metadata of this type
dsoService.clearMetadata(context, dso, metadataField.getMetadataSchema().getName(), metadataField.getElement(), metadataField.getQualifier(), Item.ANY);
} else {
// remove metadata at index
List<MetadataValue> metadataValues = dsoService.getMetadata(dso, metadataField.getMetadataSchema().getName(), metadataField.getElement(), metadataField.getQualifier(), Item.ANY);
int indexInt = Integer.parseInt(index);
if (indexInt >= 0 && metadataValues.size() > indexInt && metadataValues.get(indexInt) != null) {
// remove that metadata
dsoService.removeMetadataValues(context, dso, Arrays.asList(metadataValues.get(indexInt)));
} else {
throw new UnprocessableEntityException("UnprocessableEntityException - There is no metadata of " + "this type at that index");
}
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("This index (" + index + ") is not valid number.", e);
} catch (ArrayIndexOutOfBoundsException e) {
throw new UnprocessableEntityException("There is no metadata of this type at that index");
} catch (SQLException e) {
throw new DSpaceBadRequestException("SQLException in DspaceObjectMetadataRemoveOperation.remove " + "trying to remove metadata from dso.", e);
}
}
use of org.dspace.content.MetadataValue in project DSpace by DSpace.
the class DSpaceObjectMetadataReplaceOperation method replaceSinglePropertyOfMdValue.
/**
* Replaces single property of a specific mdValue object
* @param dso dso being patched
* @param dsoService service doing the patch in db
* @param metadataField md field being patched
* @param index index of md being replaced
* @param propertyOfMd property of md being replaced
* @param valueMdProperty new value of property of md being replaced
*/
private void replaceSinglePropertyOfMdValue(DSpaceObject dso, DSpaceObjectService dsoService, MetadataField metadataField, String index, String propertyOfMd, String valueMdProperty) {
try {
List<MetadataValue> metadataValues = dsoService.getMetadata(dso, metadataField.getMetadataSchema().getName(), metadataField.getElement(), metadataField.getQualifier(), Item.ANY);
int indexInt = Integer.parseInt(index);
if (indexInt >= 0 && metadataValues.size() > indexInt && metadataValues.get(indexInt) != null) {
// Alter only asked propertyOfMd
MetadataValue existingMdv = metadataValues.get(indexInt);
if (propertyOfMd.equals("authority")) {
existingMdv.setAuthority(valueMdProperty);
}
if (propertyOfMd.equals("confidence")) {
existingMdv.setConfidence(Integer.valueOf(valueMdProperty));
}
if (propertyOfMd.equals("language")) {
existingMdv.setLanguage(valueMdProperty);
}
if (propertyOfMd.equals("value")) {
existingMdv.setValue(valueMdProperty);
}
dsoService.setMetadataModified(dso);
} else {
throw new UnprocessableEntityException("There is no metadata of this type at that index");
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Not all numbers are valid numbers. " + "(Index and confidence should be nr)", e);
}
}
use of org.dspace.content.MetadataValue in project DSpace by DSpace.
the class ReceiptGenerator method addPublishDate.
/**
* Add the date of publication from the bibliographic metadata
*
* @param result represents the results of a deposit request
* @param receipt deposit receipt
*/
protected void addPublishDate(DepositResult result, DepositReceipt receipt) {
List<MetadataValue> dcv = itemService.getMetadataByMetadataString(result.getItem(), "dc.date.issued");
if (dcv != null && !dcv.isEmpty()) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date published = sdf.parse(dcv.get(0).getValue());
receipt.getWrappedEntry().setPublished(published);
} catch (ParseException e) {
// we tried, but never mind
log.warn("Couldn't add published date", e);
}
}
}
use of org.dspace.content.MetadataValue in project DSpace by DSpace.
the class SimpleDCEntryIngester method addUniqueMetadata.
private void addUniqueMetadata(Context context, MetadataValueInfo info, Item item) throws SQLException {
String qual = info.qualifier;
if (info.qualifier == null) {
qual = Item.ANY;
}
String lang = info.language;
if (info.language == null) {
lang = Item.ANY;
}
List<MetadataValue> existing = itemService.getMetadata(item, info.schema, info.element, qual, lang);
for (MetadataValue dcValue : existing) {
// if the submitted value is already attached to the item, just skip it
if (dcValue.getValue().equals(info.value)) {
return;
}
}
// if we get to here, go on and add the metadata
itemService.addMetadata(context, item, info.schema, info.element, info.qualifier, info.language, info.value);
}
use of org.dspace.content.MetadataValue in project DSpace by DSpace.
the class MetadataImport method resolveEntityRef.
/**
* Gets the UUID of the item indicated by the given target reference,
* which may be a direct UUID string, a row reference
* of the form rowName:VALUE, or a metadata value reference of the form schema.element[.qualifier]:VALUE.
*
* The reference may refer to a previously-processed item in the CSV or an item in the database.
*
* @param context the context to use.
* @param reference the target reference which may be a UUID, metadata reference, or rowName reference.
* @return the uuid.
* @throws MetadataImportException if the target reference is malformed or ambiguous (refers to multiple items).
*/
private UUID resolveEntityRef(Context context, String reference) throws MetadataImportException {
// value reference
UUID uuid = null;
if (!reference.contains(":")) {
// assume it's a UUID
try {
return UUID.fromString(reference);
} catch (IllegalArgumentException e) {
throw new MetadataImportException("Error in CSV row " + rowCount + ":\n" + "Not a UUID or indirect entity reference: '" + reference + "'");
}
}
if (reference.contains("::virtual::")) {
return UUID.fromString(StringUtils.substringBefore(reference, "::virtual::"));
} else if (!reference.startsWith("rowName:")) {
// Not a rowName ref; so it's a metadata value reference
MetadataValueService metadataValueService = ContentServiceFactory.getInstance().getMetadataValueService();
MetadataFieldService metadataFieldService = ContentServiceFactory.getInstance().getMetadataFieldService();
int i = reference.indexOf(":");
String mfValue = reference.substring(i + 1);
String[] mf = reference.substring(0, i).split("\\.");
if (mf.length < 2) {
throw new MetadataImportException("Error in CSV row " + rowCount + ":\n" + "Bad metadata field in reference: '" + reference + "' (expected syntax is schema.element[.qualifier])");
}
String schema = mf[0];
String element = mf[1];
String qualifier = mf.length == 2 ? null : mf[2];
try {
MetadataField mfo = metadataFieldService.findByElement(context, schema, element, qualifier);
Iterator<MetadataValue> mdv = metadataValueService.findByFieldAndValue(context, mfo, mfValue);
if (mdv.hasNext()) {
MetadataValue mdvVal = mdv.next();
uuid = mdvVal.getDSpaceObject().getID();
if (mdv.hasNext()) {
throw new MetadataImportException("Error in CSV row " + rowCount + ":\n" + "Ambiguous reference; multiple matches in db: " + reference);
}
}
} catch (SQLException e) {
throw new MetadataImportException("Error in CSV row " + rowCount + ":\n" + "Error looking up item by metadata reference: " + reference, e);
}
}
// Lookup UUIDs that may have already been processed into the csvRefMap
// See populateRefAndRowMap() for how the csvRefMap is populated
// See getMatchingCSVUUIDs() for how the reference param is sourced from the csvRefMap
Set<UUID> csvUUIDs = getMatchingCSVUUIDs(reference);
if (csvUUIDs.size() > 1) {
throw new MetadataImportException("Error in CSV row " + rowCount + ":\n" + "Ambiguous reference; multiple matches in csv: " + reference);
} else if (csvUUIDs.size() == 1) {
UUID csvUUID = csvUUIDs.iterator().next();
if (csvUUID.equals(uuid)) {
// one match from csv and db (same item)
return uuid;
} else if (uuid != null) {
throw new MetadataImportException("Error in CSV row " + rowCount + ":\n" + "Ambiguous reference; multiple matches in db and csv: " + reference);
} else {
// one match from csv
return csvUUID;
}
} else {
// size == 0; the reference does not exist throw an error
if (uuid == null) {
throw new MetadataImportException("Error in CSV row " + rowCount + ":\n" + "No matches found for reference: " + reference + "\nKeep in mind you can only reference entries that are " + "listed before " + "this one within the CSV.");
} else {
// one match from db
return uuid;
}
}
}
Aggregations