/** * Get a list of all communities in the system. These are alphabetically sorted by community name. * * @param context DSpace context object * @return the communities in the system */ @Override public List<Community> findAll(Context context, MetadataField sortField) throws SQLException { StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append("SELECT community FROM Community as community "); addMetadataLeftJoin( queryBuilder, Community.class.getSimpleName().toLowerCase(), Arrays.asList(sortField)); addMetadataSortQuery(queryBuilder, Arrays.asList(sortField), ListUtils.EMPTY_LIST); Query query = createQuery(context, queryBuilder.toString()); query.setParameter(sortField.toString(), sortField.getFieldID()); return list(query); }
/** * Serialize a single metadata field registry entry to xml * * @param context DSpace context * @param xmlSerializer xml serializer * @param mdField DSpace metadata field * @throws SAXException if XML error * @throws RegistryExportException if export error * @throws SQLException if database error * @throws IOException if IO error */ private static void saveType(Context context, XMLSerializer xmlSerializer, MetadataField mdField) throws SAXException, RegistryExportException, SQLException, IOException { // If we haven't been given a field, it's an error if (mdField == null) { throw new RegistryExportException("no field to export"); } // Get the data from the metadata field String schemaName = getSchemaName(context, mdField); String element = mdField.getElement(); String qualifier = mdField.getQualifier(); String scopeNote = mdField.getScopeNote(); // We must have a schema and element if (schemaName == null || element == null) { throw new RegistryExportException("incomplete field information"); } // Output the parent tag xmlSerializer.startElement("dc-type", null); // Output the schema name xmlSerializer.startElement("schema", null); xmlSerializer.characters(schemaName.toCharArray(), 0, schemaName.length()); xmlSerializer.endElement("schema"); // Output the element xmlSerializer.startElement("element", null); xmlSerializer.characters(element.toCharArray(), 0, element.length()); xmlSerializer.endElement("element"); // Output the qualifier, if present if (qualifier != null) { xmlSerializer.startElement("qualifier", null); xmlSerializer.characters(qualifier.toCharArray(), 0, qualifier.length()); xmlSerializer.endElement("qualifier"); } else { xmlSerializer.comment("unqualified"); } // Output the scope note, if present if (scopeNote != null) { xmlSerializer.startElement("scope_note", null); xmlSerializer.characters(scopeNote.toCharArray(), 0, scopeNote.length()); xmlSerializer.endElement("scope_note"); } else { xmlSerializer.comment("no scope note"); } xmlSerializer.endElement("dc-type"); }
/** * 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; }
public void addBody(Body body) throws WingException, SQLException, AuthorizeException { // Get all our parameters MetadataSchema[] schemas = MetadataSchema.findAll(context); String idsString = parameters.getParameter("fieldIDs", null); ArrayList<MetadataField> fields = new ArrayList<MetadataField>(); for (String id : idsString.split(",")) { MetadataField field = MetadataField.find(context, Integer.valueOf(id)); fields.add(field); } // DIVISION: metadata-field-move Division moved = body.addInteractiveDivision( "metadata-field-move", contextPath + "/admin/metadata-registry", Division.METHOD_POST, "primary administrative metadata-registry"); moved.setHead(T_head1); moved.addPara(T_para1); Table table = moved.addTable("metadata-field-move", fields.size() + 1, 3); Row header = table.addRow(Row.ROLE_HEADER); header.addCell().addContent(T_column1); header.addCell().addContent(T_column2); header.addCell().addContent(T_column3); for (MetadataField field : fields) { String fieldID = String.valueOf(field.getID()); String fieldEelement = field.getElement(); String fieldQualifier = field.getQualifier(); MetadataSchema schema = MetadataSchema.find(context, field.getSchemaID()); String schemaName = schema.getName(); String fieldName = schemaName + "." + fieldEelement; if (fieldQualifier != null && fieldQualifier.length() > 0) fieldName += "." + fieldQualifier; String fieldScopeNote = field.getScopeNote(); Row row = table.addRow(); row.addCell().addContent(fieldID); row.addCell().addContent(fieldName); row.addCell().addContent(fieldScopeNote); } Row row = table.addRow(); Cell cell = row.addCell(1, 3); cell.addContent(T_para2); Select toSchema = cell.addSelect("to_schema"); for (MetadataSchema schema : schemas) { toSchema.addOption(schema.getID(), schema.getNamespace()); } Para buttons = moved.addPara(); buttons.addButton("submit_move").setValue(T_submit_move); buttons.addButton("submit_cancel").setValue(T_submit_cancel); moved.addHidden("administrative-continue").setValue(knot.getId()); }
@Override public void loadMetadata(Item item) { MetadataFieldDAO mfDAO = MetadataFieldDAOFactory.getInstance(context); MetadataSchemaDAO msDAO = MetadataSchemaDAOFactory.getInstance(context); try { TableRowIterator tri = DatabaseManager.queryTable( context, "metadatavalue", "SELECT * FROM MetadataValue " + "WHERE item_id = ? " + "ORDER BY metadata_field_id, place", item.getID()); List<DCValue> metadata = new ArrayList<DCValue>(); for (TableRow row : tri.toList()) { // Get the associated metadata field and schema information int fieldID = row.getIntColumn("metadata_field_id"); MetadataField field = mfDAO.retrieve(fieldID); if (field == null) { log.error("Loading item - cannot find metadata field " + fieldID); } else { MetadataSchema schema = msDAO.retrieve(field.getSchemaID()); // Make a DCValue object DCValue dcv = new DCValue(); dcv.schema = schema.getName(); dcv.element = field.getElement(); dcv.qualifier = field.getQualifier(); dcv.language = row.getStringColumn("text_lang"); dcv.value = row.getStringColumn("text_value"); // Add it to the item metadata.add(dcv); } } item.setMetadata(metadata); } catch (SQLException sqle) { throw new RuntimeException(sqle); } }
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; }
@Override public Iterator<Item> findBySubmitter( Context context, EPerson eperson, MetadataField metadataField, int limit) throws SQLException { StringBuilder query = new StringBuilder(); query.append("SELECT item FROM Item as item "); addMetadataLeftJoin( query, Item.class.getSimpleName().toLowerCase(), Collections.singletonList(metadataField)); query.append(" WHERE item.inArchive = :in_archive"); query.append(" AND item.submitter =:submitter"); addMetadataSortQuery(query, Collections.singletonList(metadataField), null); Query hibernateQuery = createQuery(context, query.toString()); hibernateQuery.setParameter(metadataField.toString(), metadataField.getFieldID()); hibernateQuery.setParameter("in_archive", true); hibernateQuery.setParameter("submitter", eperson); hibernateQuery.setMaxResults(limit); return iterate(hibernateQuery); }
/** * Helper method to retrieve a schema name for the field. Caches the name after looking up the id. * * @param context DSpace Context * @param mdField DSpace metadata field * @return name of schema * @throws SQLException if database error * @throws RegistryExportException if export error */ private static String getSchemaName(Context context, MetadataField mdField) throws SQLException, RegistryExportException { // Get name from cache String name = schemaMap.get(mdField.getMetadataSchema().getSchemaID()); if (name == null) { // Name not retrieved before, so get the schema now MetadataSchema mdSchema = metadataSchemaService.find(context, mdField.getMetadataSchema().getSchemaID()); if (mdSchema != null) { name = mdSchema.getName(); schemaMap.put(mdSchema.getSchemaID(), name); } else { // Can't find the schema throw new RegistryExportException("Can't get schema name for field"); } } return name; }
@Override public void update(Context context, MetadataField metadataField) throws SQLException, AuthorizeException, NonUniqueMetadataException, IOException { // Check authorisation: Only admins may update the metadata registry if (!authorizeService.isAdmin(context)) { throw new AuthorizeException("Only administrators may modiffy the Dublin Core registry"); } // Ensure the element and qualifier are unique within a given schema. if (hasElement( context, metadataField.getFieldID(), metadataField.getMetadataSchema(), metadataField.getElement(), metadataField.getQualifier())) { throw new NonUniqueMetadataException( "Please make " + metadataField.getMetadataSchema().getName() + "." + metadataField.getElement() + "." + metadataField.getQualifier()); } metadataFieldDAO.save(context, metadataField); log.info( LogManager.getHeader( context, "update_metadatafieldregistry", "metadata_field_id=" + metadataField.getFieldID() + "element=" + metadataField.getElement() + "qualifier=" + metadataField.getQualifier())); }
@Override public void delete(Context context, MetadataField metadataField) throws SQLException, AuthorizeException { // Check authorisation: Only admins may create DC types if (!authorizeService.isAdmin(context)) { throw new AuthorizeException("Only administrators may modify the metadata registry"); } log.info( LogManager.getHeader( context, "delete_metadata_field", "metadata_field_id=" + metadataField.getFieldID())); metadataValueService.deleteByMetadataField(context, metadataField); metadataFieldDAO.delete(context, metadataField); }
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; }
/** * Update the metadata field in the database. * * @param context dspace context * @throws SQLException * @throws AuthorizeException * @throws NonUniqueMetadataException * @throws IOException */ public void update(Context context) throws SQLException, AuthorizeException, NonUniqueMetadataException, IOException { // Check authorisation: Only admins may update the metadata registry if (!AuthorizeManager.isAdmin(context)) { throw new AuthorizeException("Only administrators may modiffy the Dublin Core registry"); } // Check to see if the schema ID was altered. If is was then we will // query to ensure that there is not already a duplicate name field. if (row.getIntColumn("metadata_schema_id") != schemaID) { if (MetadataField.hasElement(context, schemaID, element, qualifier)) { throw new NonUniqueMetadataException("Duplcate field name found in target schema"); } } // Ensure the element and qualifier are unique within a given schema. if (!unique(context, schemaID, element, qualifier)) { throw new NonUniqueMetadataException("Please make " + element + "." + qualifier); } row.setColumn("metadata_schema_id", schemaID); row.setColumn("element", element); row.setColumn("qualifier", qualifier); row.setColumn("scope_note", scopeNote); DatabaseManager.update(context, row); decache(); log.info( LogManager.getHeader( context, "update_metadatafieldregistry", "metadata_field_id=" + getFieldID() + "element=" + getElement() + "qualifier=" + getQualifier())); }
@Override public MetadataField create( Context context, MetadataSchema metadataSchema, String element, String qualifier, String scopeNote) throws AuthorizeException, SQLException, NonUniqueMetadataException { // Check authorisation: Only admins may create DC types if (!authorizeService.isAdmin(context)) { throw new AuthorizeException("Only administrators may modify the metadata registry"); } // Ensure the element and qualifier are unique within a given schema. if (hasElement(context, -1, metadataSchema, element, qualifier)) { throw new NonUniqueMetadataException( "Please make " + element + "." + qualifier + " unique within schema #" + metadataSchema.getSchemaID()); } // Create a table row and update it with the values MetadataField metadataField = new MetadataField(); metadataField.setElement(element); metadataField.setQualifier(qualifier); metadataField.setScopeNote(scopeNote); metadataField.setMetadataSchema(metadataSchema); metadataField = metadataFieldDAO.create(context, metadataField); metadataFieldDAO.save(context, metadataField); log.info( LogManager.getHeader( context, "create_metadata_field", "metadata_field_id=" + metadataField.getFieldID())); return metadataField; }
/** * Simple command-line rig for testing the DIM output of a stylesheet. Usage: java * XSLTIngestionCrosswalk <crosswalk-name> <input-file> */ public static void main(String[] argv) throws Exception { if (argv.length < 2) { System.err.println("Usage: java XSLTIngestionCrosswalk [-l] <crosswalk-name> <input-file>"); System.exit(1); } int i = 0; boolean list = false; // skip first arg if it's the list option if (argv.length > 2 && argv[0].equals("-l")) { ++i; list = true; } IngestionCrosswalk xwalk = (IngestionCrosswalk) PluginManager.getNamedPlugin(IngestionCrosswalk.class, argv[i]); if (xwalk == null) { System.err.println( "Error, cannot find an IngestionCrosswalk plugin for: \"" + argv[i] + "\""); System.exit(1); } XSLTransformer xform = ((XSLTIngestionCrosswalk) xwalk).getTransformer(DIRECTION); if (xform == null) throw new CrosswalkInternalException( "Failed to initialize transformer, probably error loading stylesheet."); SAXBuilder builder = new SAXBuilder(); Document inDoc = builder.build(new FileInputStream(argv[i + 1])); XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); Document dimDoc = null; List dimList = null; if (list) { dimList = xform.transform(inDoc.getRootElement().getChildren()); outputter.output(dimList, System.out); } else { dimDoc = xform.transform(inDoc); outputter.output(dimDoc, System.out); dimList = dimDoc.getRootElement().getChildren(); } // Sanity-check the generated DIM, make sure it would load. Context context = new Context(); Iterator di = dimList.iterator(); while (di.hasNext()) { // skip over comment, text and other trash some XSLs generate.. Object o = di.next(); if (!(o instanceof Element)) continue; Element elt = (Element) o; if (elt.getName().equals("field") && elt.getNamespace().equals(DIM_NS)) { String schema = elt.getAttributeValue("mdschema"); String element = elt.getAttributeValue("element"); String qualifier = elt.getAttributeValue("qualifier"); MetadataSchema ms = MetadataSchema.find(context, schema); if (ms == null) { System.err.println( "DIM Error, Cannot find metadata schema for: schema=\"" + schema + "\" (... element=\"" + element + "\", qualifier=\"" + qualifier + "\")"); } else { if (qualifier != null && qualifier.equals("")) { System.err.println( "DIM Warning, qualifier is empty string: " + " schema=\"" + schema + "\", element=\"" + element + "\", qualifier=\"" + qualifier + "\""); qualifier = null; } MetadataField mf = MetadataField.findByElement(context, ms.getSchemaID(), element, qualifier); if (mf == null) System.err.println( "DIM Error, Cannot find metadata field for: schema=\"" + schema + "\", element=\"" + element + "\", qualifier=\"" + qualifier + "\""); } } else { // ("Got unexpected element in DIM list: "+elt.toString()); throw new MetadataValidationException( "Got unexpected element in DIM list: " + elt.toString()); } } }
private void addDCValue(Context c, Item i, String schema, Node n) throws TransformerException, SQLException, AuthorizeException { String value = getStringValue(n); // n.getNodeValue(); // compensate for empty value getting read as "null", which won't display if (value == null) { value = ""; } // //getElementData(n, "element"); String element = getAttributeValue(n, "element"); String qualifier = getAttributeValue(n, "qualifier"); // NodeValue(); // //getElementData(n, // "qualifier"); String language = getAttributeValue(n, "language"); if (language != null) { language = language.trim(); } if (!isQuiet) { System.out.println( "\tSchema: " + schema + " Element: " + element + " Qualifier: " + qualifier + " Value: " + value); } if ("none".equals(qualifier) || "".equals(qualifier)) { qualifier = null; } // if language isn't set, use the system's default value if (StringUtils.isEmpty(language)) { language = ConfigurationManager.getProperty("default.language"); } // a goofy default, but there it is if (language == null) { language = "en"; } if (!isTest) { i.addMetadata(schema, element, qualifier, language, value); } else { // If we're just test the import, let's check that the actual metadata field exists. MetadataSchema foundSchema = MetadataSchema.find(c, schema); if (foundSchema == null) { System.out.println("ERROR: schema '" + schema + "' was not found in the registry."); return; } int schemaID = foundSchema.getSchemaID(); MetadataField foundField = MetadataField.findByElement(c, schemaID, element, qualifier); if (foundField == null) { System.out.println( "ERROR: Metadata field: '" + schema + "." + element + "." + qualifier + "' was not found in the registry."); return; } } }
@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()); } }
/** * Return true if and only if the schema has a field with the given element and qualifier pair. * * @param context dspace context * @param schemaID schema by ID * @param element element name * @param qualifier qualifier name * @return true if the field exists * @throws SQLException * @throws AuthorizeException */ private static boolean hasElement(Context context, int schemaID, String element, String qualifier) throws SQLException, AuthorizeException { return MetadataField.findByElement(context, schemaID, element, qualifier) != null; }