Metadatum[] values = item.getMetadata(Item.ANY, Item.ANY, Item.ANY, Item.ANY);
for (Metadatum value : values)
{
// Verify that the schema exists
MetadataSchema mdSchema = MetadataSchema.find(ourContext, value.schema);
if (mdSchema == null && !clearList.contains(value.schema)) {
// add a new schema, giving it a namespace of "unknown". Possibly a very bad idea.
if (schemaChoice.equals("add")) {
mdSchema = new MetadataSchema(value.schema,String.valueOf(new Date().getTime()));
try {
mdSchema.create(ourContext);
mdSchema.setName(value.schema);
mdSchema.setNamespace("unknown"+mdSchema.getSchemaID());
mdSchema.update(ourContext);
} catch (NonUniqueMetadataException e) {
// This case should not be possible
e.printStackTrace();
}
clearList.add(value.schema);
}
// ignore the offending schema, quietly dropping all of its metadata elements before they clog our gears
else if (schemaChoice.equals("ignore")) {
item.clearMetadata(value.schema, Item.ANY, Item.ANY, Item.ANY);
continue;
}
// otherwise, go ahead and generate the error
else {
throw new HarvestingException("The '" + value.schema + "' schema has not been defined in this DSpace instance. ");
}
}
if (mdSchema != null) {
// Verify that the element exists; this part is reachable only if the metadata schema is valid
MetadataField mdField = MetadataField.findByElement(ourContext, mdSchema.getSchemaID(), value.element, value.qualifier);
if (mdField == null) {
if (fieldChoice.equals("add")) {
mdField = new MetadataField(mdSchema, value.element, value.qualifier, null);
try {
mdField.create(ourContext);