/** * Utility method for pattern-matching metadata elements. This method will return <code>true * </code> if the given schema, element, qualifier and language match the schema, element, * qualifier and language of the <code>DCValue</code> object passed in. Any or all of the element, * qualifier and language passed in can be the <code>Item.ANY</code> wildcard. * * @param schema the schema for the metadata field. <em>Must</em> match the <code>name</code> of * an existing metadata schema. * @param element the element to match, or <code>Item.ANY</code> * @param qualifier the qualifier to match, or <code>Item.ANY</code> * @param language the language to match, or <code>Item.ANY</code> * @param metadataValue the Dublin Core value * @return <code>true</code> if there is a match */ protected boolean match( String schema, String element, String qualifier, String language, MetadataValue metadataValue) { MetadataField metadataField = metadataValue.getMetadataField(); MetadataSchema metadataSchema = metadataField.getMetadataSchema(); // We will attempt to disprove a match - if we can't we have a match if (!element.equals(Item.ANY) && !element.equals(metadataField.getElement())) { // Elements do not match, no wildcard return false; } if (qualifier == null) { // Value must be unqualified if (metadataField.getQualifier() != null) { // Value is qualified, so no match return false; } } else if (!qualifier.equals(Item.ANY)) { // Not a wildcard, so qualifier must match exactly if (!qualifier.equals(metadataField.getQualifier())) { return false; } } if (language == null) { // Value must be null language to match if (metadataValue.getLanguage() != null) { // Value is qualified, so no match return false; } } else if (!language.equals(Item.ANY)) { // Not a wildcard, so language must match exactly if (!language.equals(metadataValue.getLanguage())) { return false; } } if (!schema.equals(Item.ANY)) { if (metadataSchema != null && !metadataSchema.getName().equals(schema)) { // The namespace doesn't match return false; } } // If we get this far, we have a match return true; }
protected MetadataField getMetadataField(DCValue dcv) throws SQLException, AuthorizeException { if (allMetadataFields == null) { allMetadataFields = MetadataField.findAll(ourContext); } if (allMetadataFields != null) { int schemaID = getMetadataSchemaID(dcv); for (MetadataField field : allMetadataFields) { if (field.getSchemaID() == schemaID && StringUtils.equals(field.getElement(), dcv.element) && StringUtils.equals(field.getQualifier(), dcv.qualifier)) { return field; } } } return null; }
List<DCValue> get(Context c, int resourceId, int resourceTypeId, Logger log) throws SQLException { if (metadata == null) { metadata = new ArrayList<DCValue>(); // Get Dublin Core metadata TableRowIterator tri = retrieveMetadata(resourceId, resourceTypeId); if (tri != null) { try { while (tri.hasNext()) { TableRow resultRow = tri.next(); // Get the associated metadata field and schema information int fieldID = resultRow.getIntColumn("metadata_field_id"); MetadataField field = MetadataField.find(c, fieldID); if (field == null) { log.error("Loading item - cannot find metadata field " + fieldID); } else { MetadataSchema schema = MetadataSchema.find(c, field.getSchemaID()); if (schema == null) { log.error( "Loading item - cannot find metadata schema " + field.getSchemaID() + ", field " + fieldID); } else { // Make a DCValue object DCValue dcv = new DCValue(); dcv.element = field.getElement(); dcv.qualifier = field.getQualifier(); dcv.value = resultRow.getStringColumn("text_value"); dcv.language = resultRow.getStringColumn("text_lang"); // dcv.namespace = schema.getNamespace(); dcv.schema = schema.getName(); dcv.authority = resultRow.getStringColumn("authority"); dcv.confidence = resultRow.getIntColumn("confidence"); // Add it to the list metadata.add(dcv); } } } } finally { // close the TableRowIterator to free up resources if (tri != null) { tri.close(); } } } } return metadata; }
@Override public void addMetadata( Context context, T dso, MetadataField metadataField, String lang, List<String> values, List<String> authorities, List<Integer> confidences) throws SQLException { boolean authorityControlled = metadataAuthorityService.isAuthorityControlled(metadataField); boolean authorityRequired = metadataAuthorityService.isAuthorityRequired(metadataField); // We will not verify that they are valid entries in the registry // until update() is called. for (int i = 0; i < values.size(); i++) { MetadataValue metadataValue = metadataValueService.create(context, dso, metadataField); metadataValue.setLanguage(lang == null ? null : lang.trim()); // Logic to set Authority and Confidence: // - normalize an empty string for authority to NULL. // - if authority key is present, use given confidence or NOVALUE if not given // - otherwise, preserve confidence if meaningful value was given since it may document a // failed authority lookup // - CF_UNSET signifies no authority nor meaningful confidence. // - it's possible to have empty authority & CF_ACCEPTED if e.g. user deletes authority key if (authorityControlled) { if (authorities != null && authorities.get(i) != null && authorities.get(i).length() > 0) { metadataValue.setAuthority(authorities.get(i)); metadataValue.setConfidence( confidences == null ? Choices.CF_NOVALUE : confidences.get(i)); } else { metadataValue.setAuthority(null); metadataValue.setConfidence(confidences == null ? Choices.CF_UNSET : confidences.get(i)); } // authority sanity check: if authority is required, was it supplied? // XXX FIXME? can't throw a "real" exception here without changing all the callers to expect // it, so use a runtime exception if (authorityRequired && (metadataValue.getAuthority() == null || metadataValue.getAuthority().length() == 0)) { throw new IllegalArgumentException( "The metadata field \"" + metadataField.toString() + "\" requires an authority key but none was provided. Value=\"" + values.get(i) + "\""); } } if (values.get(i) != null) { // remove control unicode char String temp = values.get(i).trim(); char[] dcvalue = temp.toCharArray(); for (int charPos = 0; charPos < dcvalue.length; charPos++) { if (Character.isISOControl(dcvalue[charPos]) && !String.valueOf(dcvalue[charPos]).equals("\u0009") && !String.valueOf(dcvalue[charPos]).equals("\n") && !String.valueOf(dcvalue[charPos]).equals("\r")) { dcvalue[charPos] = ' '; } } metadataValue.setValue(String.valueOf(dcvalue)); ; } else { metadataValue.setValue(null); } // An update here isn't needed, this is persited upon the merge of the owning object // metadataValueService.update(context, metadataValue); dso.addDetails(metadataField.toString()); } }