/**
   * Protected method that deletes all metadata values from the database, should only be called when
   * deleting the item.
   *
   * @param context the dspaceObject
   * @param dso the dspaceObject who's metadata we are to delete
   */
  protected void deleteMetadata(Context context, T dso) throws SQLException {

    Iterator<MetadataValue> metadataValueIterator = dso.getMetadata().iterator();
    while (metadataValueIterator.hasNext()) {
      MetadataValue metadataValue = metadataValueIterator.next();
      metadataValueIterator.remove();
      metadataValueService.delete(context, metadataValue);
    }
  }
 private <T extends MetadataItem> PartialList<Metadata> getMetadatas(
     int offset, int size, String sortBy, Class<T> clazz) {
   PartialList<T> items = persistenceService.getAllItems(clazz, offset, size, sortBy);
   List<Metadata> details = new LinkedList<>();
   for (T definition : items.getList()) {
     details.add(definition.getMetadata());
   }
   return new PartialList<>(details, items.getOffset(), items.getPageSize(), items.getTotalSize());
 }
 @Override
 public void removeMetadataValues(Context context, T dso, List<MetadataValue> values)
     throws SQLException {
   Iterator<MetadataValue> metadata = dso.getMetadata().iterator();
   while (metadata.hasNext()) {
     MetadataValue metadataValue = metadata.next();
     if (values.contains(metadataValue)) {
       metadata.remove();
       //                metadataValueService.delete(context, metadataValue);
     }
   }
   dso.setMetadataModified();
 }
  @Override
  public List<MetadataValue> getMetadata(
      T dso, String schema, String element, String qualifier, String lang) {
    // Build up list of matching values
    List<MetadataValue> values = new ArrayList<MetadataValue>();
    for (MetadataValue dcv : dso.getMetadata()) {
      if (match(schema, element, qualifier, lang, dcv)) {
        values.add(dcv);
      }
    }

    // Create an array of matching values
    return values;
  }
 private <T extends MetadataItem> PartialList<Metadata> getMetadatas(Query query, Class<T> clazz) {
   if (query.isForceRefresh()) {
     persistenceService.refresh();
   }
   definitionsService.resolveConditionType(query.getCondition());
   PartialList<T> items =
       persistenceService.query(
           query.getCondition(), query.getSortby(), clazz, query.getOffset(), query.getLimit());
   List<Metadata> details = new LinkedList<>();
   for (T definition : items.getList()) {
     details.add(definition.getMetadata());
   }
   return new PartialList<>(details, items.getOffset(), items.getPageSize(), items.getTotalSize());
 }
 @Override
 public void update(Context context, T dso) throws SQLException, AuthorizeException {
   if (dso.isMetadataModified()) {
     /*
     Update the order of the metadata values
      */
     // A map created to store the latest place for each metadata field
     Map<MetadataField, Integer> fieldToLastPlace = new HashMap<>();
     List<MetadataValue> metadataValues = dso.getMetadata();
     for (MetadataValue metadataValue : metadataValues) {
       // Retrieve & store the place for each metadata value
       int mvPlace = getMetadataValuePlace(fieldToLastPlace, metadataValue);
       metadataValue.setPlace(mvPlace);
     }
   }
 }
 @Override
 public void clearMetadata(
     Context context, T dso, String schema, String element, String qualifier, String lang)
     throws SQLException {
   // We will build a list of values NOT matching the values to clear
   Iterator<MetadataValue> metadata = dso.getMetadata().iterator();
   while (metadata.hasNext()) {
     MetadataValue metadataValue = metadata.next();
     if (match(schema, element, qualifier, lang, metadataValue)) {
       metadataValue.setDSpaceObject(null);
       metadata.remove();
       //                metadataValueService.delete(context, metadataValue);
     }
   }
   dso.setMetadataModified();
 }