示例#1
0
  /**
   * 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();
  }
  private List<List<MetadataField>> getMetadataFieldsList(
      org.dspace.core.Context context, List<String> query_field) throws SQLException {
    List<List<MetadataField>> listFieldList = new ArrayList<List<MetadataField>>();
    for (String s : query_field) {
      ArrayList<MetadataField> fields = new ArrayList<MetadataField>();
      listFieldList.add(fields);
      if (s.equals("*")) {
        continue;
      }
      String schema = "";
      String element = "";
      String qualifier = null;
      String[] parts = s.split("\\.");
      if (parts.length > 0) {
        schema = parts[0];
      }
      if (parts.length > 1) {
        element = parts[1];
      }
      if (parts.length > 2) {
        qualifier = parts[2];
      }

      if (Item.ANY.equals(qualifier)) {
        for (MetadataField mf :
            metadataFieldService.findFieldsByElementNameUnqualified(context, schema, element)) {
          fields.add(mf);
        }
      } else {
        MetadataField mf = metadataFieldService.findByElement(context, schema, element, qualifier);
        if (mf != null) {
          fields.add(mf);
        }
      }
    }
    return listFieldList;
  }
  @Override
  public void addMetadata(
      Context context,
      T dso,
      String schema,
      String element,
      String qualifier,
      String lang,
      List<String> values,
      List<String> authorities,
      List<Integer> confidences)
      throws SQLException {

    // We will not verify that they are valid entries in the registry
    // until update() is called.
    MetadataField metadataField =
        metadataFieldService.findByElement(context, schema, element, qualifier);
    if (metadataField == null) {
      throw new SQLException("bad_dublin_core schema=" + schema + "." + element + "." + qualifier);
    }
    addMetadata(context, dso, metadataField, lang, values, authorities, confidences);
  }