/** * Serialize the schema registry. If the parameter 'schema' is null or empty, save all schemas * * @param context DSpace Context * @param xmlSerializer XML serializer * @param schema schema (may be null to save all) * @throws SQLException if database error * @throws SAXException if XML error * @throws RegistryExportException if export error */ public static void saveSchema(Context context, XMLSerializer xmlSerializer, String schema) throws SQLException, SAXException, RegistryExportException { if (schema != null && !"".equals(schema)) { // Find a single named schema MetadataSchema mdSchema = metadataSchemaService.find(context, schema); saveSchema(xmlSerializer, mdSchema); } else { // Find all schemas List<MetadataSchema> mdSchemas = metadataSchemaService.findAll(context); for (MetadataSchema mdSchema : mdSchemas) { saveSchema(xmlSerializer, mdSchema); } } }
/** * Save a registry to a filepath * * @param file filepath * @param schema schema definition to save * @throws SQLException if database error * @throws IOException if IO error * @throws SAXException if XML error * @throws RegistryExportException if export error */ public static void saveRegistry(String file, String schema) throws SQLException, IOException, SAXException, RegistryExportException { // create a context Context context = new Context(); context.setIgnoreAuthorization(true); OutputFormat xmlFormat = new OutputFormat(Method.XML, "UTF-8", true); xmlFormat.setLineWidth(120); xmlFormat.setIndent(4); XMLSerializer xmlSerializer = new XMLSerializer(new BufferedWriter(new FileWriter(file)), xmlFormat); // XMLSerializer xmlSerializer = new XMLSerializer(System.out, xmlFormat); xmlSerializer.startDocument(); xmlSerializer.startElement("dspace-dc-types", null); // Save the schema definition(s) saveSchema(context, xmlSerializer, schema); List<MetadataField> mdFields = null; // If a single schema has been specified if (schema != null && !"".equals(schema)) { // Get the id of that schema MetadataSchema mdSchema = metadataSchemaService.find(context, schema); if (mdSchema == null) { throw new RegistryExportException("no schema to export"); } // Get the metadata fields only for the specified schema mdFields = metadataFieldService.findAllInSchema(context, mdSchema); } else { // Get the metadata fields for all the schemas mdFields = metadataFieldService.findAll(context); } // Output the metadata fields for (MetadataField mdField : mdFields) { saveType(context, xmlSerializer, mdField); } xmlSerializer.endElement("dspace-dc-types"); xmlSerializer.endDocument(); // abort the context, as we shouldn't have changed it!! context.abort(); }
/** * 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; }