@Override public List<MetadataValue> getLiftMetadata(Context context, Item item) { return itemService.getMetadata(item, lift_schema, lift_element, lift_qualifier, Item.ANY); }
static boolean hasMetadataMatch(Item item, String fieldList, Pattern regex) { if (fieldList.equals("*")) { for (MetadataValue md : itemService .getMetadata(item, org.dspace.content.Item.ANY, org.dspace.content.Item.ANY, org.dspace.content.Item.ANY, org.dspace.content.Item.ANY)) { if (regex.matcher(md.getValue()).matches()) { return true; } } } else { for (String field : fieldList.split(",")) { for (MetadataValue md : itemService.getMetadataByMetadataString(item, field.trim())) { if (regex.matcher(md.getValue()).matches()) { return true; } } } } return false; }
protected static List<MockMetadataValue> item2Metadata(Item item) { List<MetadataValue> dcvs = itemService.getMetadata(item, Item.ANY, Item.ANY, Item.ANY, Item.ANY); List<MockMetadataValue> result = new ArrayList<>(); for (MetadataValue metadataValue : dcvs) { result.add(new MockMetadataValue(metadataValue)); } return result; }
static boolean hasOnlyMetadataMatch(Item item, String fieldList, Pattern regex) { boolean matches = false; if (fieldList.equals("*")) { for (MetadataValue md : itemService .getMetadata(item, org.dspace.content.Item.ANY, org.dspace.content.Item.ANY, org.dspace.content.Item.ANY, org.dspace.content.Item.ANY)) { if (regex.matcher(md.getValue()).matches()) { matches = true; } else { return false; } } } else { for (String field : fieldList.split(",")) { for (MetadataValue md : itemService.getMetadataByMetadataString(item, field.trim())) { if (regex.matcher(md.getValue()).matches()) { matches = true; } else { return false; } } } } return matches; }
/** * Get the URLs to check * * @param item The item to extract URLs from * @return An array of URL Strings */ protected List<String> getURLs(Item item) { // Get URIs from anyschema.anyelement.uri.* List<MetadataValue> urls = itemService.getMetadata(item, Item.ANY, Item.ANY, "uri", Item.ANY); ArrayList<String> theURLs = new ArrayList<String>(); for (MetadataValue url : urls) { theURLs.add(url.getValue()); } return theURLs; }
/** * Generate a list of metadata elements for the given DSpace item. * * @param item The item to derive metadata from * @return list of metadata */ protected List<MockMetadataValue> item2Metadata(Item item) { List<MetadataValue> dcvs = itemService.getMetadata(item, Item.ANY, Item.ANY, Item.ANY, Item.ANY); List<MockMetadataValue> result = new ArrayList<>(); for (MetadataValue metadataValue : dcvs) { result.add(new MockMetadataValue(metadataValue)); } return result; }
/** * Test that item has adequate metadata. * Check item for the minimal DC metadata required to ingest a * new item, and throw a PackageValidationException if test fails. * Used by all SIP processors as a common sanity test. * * @param item - item to test. * @throws PackageValidationException if validation error */ public static void checkItemMetadata(Item item) throws PackageValidationException { List<MetadataValue> t = itemService.getMetadata(item, MetadataSchema.DC_SCHEMA, "title", null, Item.ANY); if (t == null || t.size() == 0) { throw new PackageValidationException("Item cannot be created without the required \"title\" DC metadata."); } }
protected DCDate recoverEmbargoDate(Item item) { DCDate liftDate = null; List<MetadataValue> lift = itemService.getMetadata(item, lift_schema, lift_element, lift_qualifier, Item.ANY); if (lift.size() > 0) { liftDate = new DCDate(lift.get(0).getValue()); // sanity check: do not allow an embargo lift date in the past. if (liftDate.toDate().before(new Date())) { liftDate = null; } } return liftDate; }
@Override protected List<String> getURLs(Item item) { // Get all metadata elements that start with http:// or https:// List<MetadataValue> urls = itemService.getMetadata(item, Item.ANY, Item.ANY, Item.ANY, Item.ANY); ArrayList<String> theURLs = new ArrayList<String>(); for (MetadataValue url : urls) { if ((url.getValue().startsWith("http://")) || (url.getValue().startsWith("https://"))) { theURLs.add(url.getValue()); } } return theURLs; } }
@Override public void doPreProcessing(Context context, InProgressSubmission wsi) { for (MetadataListener listener : listeners) { for (String metadata : listener.getMetadata().keySet()) { String[] tokenized = Utils.tokenize(metadata); List<MetadataValue> mm = itemService.getMetadata(wsi.getItem(), tokenized[0], tokenized[1], tokenized[2], Item.ANY); if (mm != null && !mm.isEmpty()) { metadataMap.put(metadata, mm); } else { metadataMap.put(metadata, new ArrayList<MetadataValue>()); } mappingIdentifier.put(metadata, listener.getMetadata().get(metadata)); } } }
/** * Returns first value that matches Creative Commons 'shibboleth', * or null if no matching values. * NB: this method will succeed only for metadata fields holding CC URIs * * @param item - the item to read * @return value - the first CC-matched value, or null if no such value */ public String ccItemValue(Item item) { List<MetadataValue> dcvalues = itemService.getMetadata(item, params[0], params[1], params[2], params[3]); for (MetadataValue dcvalue : dcvalues) { if ((dcvalue.getValue()).indexOf(ccShib) != -1) { // return first value that matches the shib return dcvalue.getValue(); } } return null; }
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) { // FIXME: probably we want to be slightly more careful about qualifiers and languages // // 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); }
protected void updateItems(AuthorityValue authority) { try { Iterator<Item> itemIterator = itemService .findByMetadataFieldAuthority(context, authority.getField(), authority.getId()); while (itemIterator.hasNext()) { Item next = itemIterator.next(); List<MetadataValue> metadata = itemService.getMetadata(next, authority.getField(), authority.getId()); authority.updateItem(context, next, metadata.get(0)); //should be only one List<MetadataValue> metadataAfter = itemService .getMetadata(next, authority.getField(), authority.getId()); if (!metadata.get(0).getValue().equals(metadataAfter.get(0).getValue())) { print.println("Updated item with handle " + next.getHandle()); } } } catch (Exception e) { log.error("Error updating item", e); print.println("Error updating item. " + Arrays.toString(e.getStackTrace())); } }
protected void populateHandleMetadata(Context context, Item item, String handle) throws SQLException, IOException, AuthorizeException { String handleref = handleService.getCanonicalForm(handle); // Add handle as identifier.uri DC value. // First check that identifier doesn't already exist. boolean identifierExists = false; List<MetadataValue> identifiers = itemService .getMetadata(item, MetadataSchema.DC_SCHEMA, "identifier", "uri", Item.ANY); for (MetadataValue identifier : identifiers) { if (handleref.equals(identifier.getValue())) { identifierExists = true; } } if (!identifierExists) { itemService.addMetadata(context, item, MetadataSchema.DC_SCHEMA, "identifier", "uri", null, handleref); } } }
/** * Removes the passed value from the set of values for the field in passed item. * * @param context The relevant DSpace Context. * @param item - the item to update * @param value - the value to remove * @throws IOException A general class of exceptions produced by failed or interrupted I/O operations. * @throws SQLException An exception that provides information on a database access error or other errors. * @throws AuthorizeException Exception indicating the current user of the context does not have permission * to perform a particular action. */ public void removeItemValue(Context context, Item item, String value) throws AuthorizeException, IOException, SQLException { if (value != null) { List<MetadataValue> dcvalues = itemService.getMetadata(item, params[0], params[1], params[2], params[3]); ArrayList<String> arrayList = new ArrayList<String>(); for (MetadataValue dcvalue : dcvalues) { if (!dcvalue.getValue().equals(value)) { arrayList.add(dcvalue.getValue()); } } itemService.clearMetadata(context, item, params[0], params[1], params[2], params[3]); itemService.addMetadata(context, item, params[0], params[1], params[2], params[3], arrayList); } }
/** * Discover the different schemas in use and output a separate metadata XML * file for each schema. * * @param c DSpace context * @param i DSpace Item * @param destDir destination directory * @param migrate Whether to use the migrate option or not * @throws Exception if error */ protected void writeMetadata(Context c, Item i, File destDir, boolean migrate) throws Exception { Set<String> schemas = new HashSet<String>(); List<MetadataValue> dcValues = itemService.getMetadata(i, Item.ANY, Item.ANY, Item.ANY, Item.ANY); for (MetadataValue metadataValue : dcValues) { schemas.add(metadataValue.getMetadataField().getMetadataSchema().getName()); } // Save each of the schemas into it's own metadata file for (String schema : schemas) { writeMetadata(c, schema, i, destDir, migrate); } }
/** * Loads a DOI out of the metadata of an DSpaceObject. * * @param dso DSpace object to get DOI metadata from * @return The DOI or null if no DOI was found. * @throws DOIIdentifierException if identifier error */ public String getDOIOutOfObject(DSpaceObject dso) throws DOIIdentifierException { // FIXME if (!(dso instanceof Item)) { throw new IllegalArgumentException("We currently support DOIs for " + "Items only, not for " + contentServiceFactory .getDSpaceObjectService(dso).getTypeText(dso) + "."); } Item item = (Item) dso; List<MetadataValue> metadata = itemService.getMetadata(item, MD_SCHEMA, DOI_ELEMENT, DOI_QUALIFIER, null); for (MetadataValue id : metadata) { if (id.getValue().startsWith( DOI.RESOLVER + String.valueOf(SLASH) + PREFIX + String.valueOf(SLASH) + NAMESPACE_SEPARATOR)) { return doiService.DOIFromExternalFormat(id.getValue()); } } return null; }
@Override public void additionalIndex(Context context, DSpaceObject dso, SolrInputDocument document) { if (dso instanceof Item) { Item item = (Item) dso; List<MetadataValue> dcValues = itemService.getMetadata(item, Item.ANY, Item.ANY, Item.ANY, Item.ANY); List<String> toIgnoreMetadataFields = SearchUtils.getIgnoredMetadataFields(item.getType()); for (MetadataValue dcValue : dcValues) { if (!toIgnoreMetadataFields.contains(dcValue.getMetadataField().toString('.'))) { document.addField("a_spell", dcValue.getValue()); } } } } }
/** * Returns the value that matches the value mapped to the passed key if any. * NB: this only delivers a license name (if present in field) given a license URI * * @param item - the item to read * @param key - the key for desired value * @return value - the value associated with key or null if no such value * @throws IOException A general class of exceptions produced by failed or interrupted I/O operations. * @throws SQLException An exception that provides information on a database access error or other errors. * @throws AuthorizeException Exception indicating the current user of the context does not have permission * to perform a particular action. */ public String keyedItemValue(Item item, String key) throws AuthorizeException, IOException, SQLException { CCLookup ccLookup = new CCLookup(); ccLookup.issue(key); String matchValue = ccLookup.getLicenseName(); List<MetadataValue> dcvalues = itemService.getMetadata(item, params[0], params[1], params[2], params[3]); for (MetadataValue dcvalue : dcvalues) { if (dcvalue.getValue().equals(matchValue)) { return dcvalue.getValue(); } } return null; }
protected void copyMetadata(Context context, Item itemNew, Item nativeItem) throws SQLException { List<MetadataValue> md = itemService.getMetadata(nativeItem, Item.ANY, Item.ANY, Item.ANY, Item.ANY); for (MetadataValue aMd : md) { MetadataField metadataField = aMd.getMetadataField(); MetadataSchema metadataSchema = metadataField.getMetadataSchema(); String unqualifiedMetadataField = metadataSchema.getName() + "." + metadataField.getElement(); if (getIgnoredMetadataFields().contains(metadataField.toString('.')) || getIgnoredMetadataFields().contains(unqualifiedMetadataField + "." + Item.ANY)) { //Skip this metadata field continue; } itemService .addMetadata(context, itemNew, metadataField, aMd.getLanguage(), aMd.getValue(), aMd.getAuthority(), aMd.getConfidence()); } }