@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);
    }
  }
  @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;
  }
  @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);
  }
  @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 MetadataField findByElement(
     Context context, String metadataSchemaName, String element, String qualifier)
     throws SQLException {
   return metadataFieldDAO.findByElement(context, metadataSchemaName, element, qualifier);
 }
 @Override
 public MetadataField find(Context context, int id) throws SQLException {
   return metadataFieldDAO.findByID(context, MetadataField.class, id);
 }
 /**
  * A sanity check that ensures a given element and qualifier are unique within a given schema. The
  * check happens in code as we cannot use a database constraint.
  *
  * @param context dspace context
  * @param metadataSchema metadataSchema
  * @param element
  * @param qualifier
  * @return true if unique
  * @throws SQLException
  */
 protected boolean hasElement(
     Context context, int fieldId, MetadataSchema metadataSchema, String element, String qualifier)
     throws SQLException {
   return metadataFieldDAO.find(context, fieldId, metadataSchema, element, qualifier) != null;
 }
 @Override
 public List<MetadataField> findAllInSchema(Context context, MetadataSchema metadataSchema)
     throws SQLException {
   return metadataFieldDAO.findAllInSchema(context, metadataSchema);
 }
 @Override
 public List<MetadataField> findAll(Context context) throws SQLException {
   return metadataFieldDAO.findAll(context, MetadataField.class);
 }
 @Override
 public List<MetadataField> findFieldsByElementNameUnqualified(
     Context context, String metadataSchemaName, String element) throws SQLException {
   return metadataFieldDAO.findFieldsByElementNameUnqualified(
       context, metadataSchemaName, element);
 }