/**
   * Get escidoc XML representation of ContentRelations md-records.
   *
   * @param id objid of ContentRelation resource
   * @param name name of a md-record
   * @return escidoc XML representation of ContentRelation
   * @throws ContentRelationNotFoundException Thrown if under provided id no ContentRelation could
   *     be found
   * @throws MdRecordNotFoundException e
   * @throws SystemException Thrown if internal error occurs.
   */
  @Override
  public String retrieveMdRecord(final String id, final String name)
      throws ContentRelationNotFoundException, MdRecordNotFoundException, SystemException {

    final ContentRelationCreate cr = setContentRelation(id);
    enrichWithMetadataContent(cr);
    final List<MdRecordCreate> mdRecords = cr.getMetadataRecords();
    if (mdRecords != null) {
      for (final MdRecordCreate mr : mdRecords) {
        if (mr.getName().equals(name)) {
          return this.contentRelationXmlProvider.getContentRelationMdRecord(cr, mr);
        }
      }
    }
    throw new MdRecordNotFoundException("The md-record with name=" + name + " does not exist.");
  }
 /**
  * Enrich all meta data records with the (blob) content from repository.
  *
  * @param cr ContentRelation
  * @throws WebserverSystemException Thrown if access to repository (Feodra) failed.
  */
 private static void enrichWithMetadataContent(final ContentRelationCreate cr) {
   final List<MdRecordCreate> mdRecords = cr.getMetadataRecords();
   if (mdRecords != null) {
     for (final MdRecordCreate mdRecord : mdRecords) {
       if (mdRecord.getContent() == null) {
         final Datastream ds =
             new Datastream(
                 mdRecord.getName(),
                 cr.getObjid(),
                 cr.getProperties().getVersionDate(),
                 mdRecord.getMimeType(),
                 mdRecord.getDatastreamLocation(),
                 mdRecord.getControlGroup());
         mdRecord.setContent(new String(ds.getStream(), Charset.forName("UTF-8")));
       }
     }
   }
 }
  /**
   * @param cr ContentRelation
   * @throws FedoraSystemException Thrown if access to Fedora failed.
   * @throws IntegritySystemException Thrown if data integrity is violated.
   */
  private void setMetadata(final ContentRelationCreate cr) throws IntegritySystemException {

    final DatastreamProfilesTO dsProfiles =
        getFedoraServiceClient().getDatastreamProfiles(cr.getObjid(), null);

    for (final DatastreamProfileTO datastreamProfileTO : dsProfiles.getDatastreamProfile()) {
      if (datastreamProfileTO.getDsAltID().contains(Datastream.METADATA_ALTERNATE_ID)
          && !DatastreamState.D.value().equals(datastreamProfileTO.getDsState())) {
        final MdRecordCreate mdRecord = new MdRecordCreate();

        try {
          mdRecord.setName(datastreamProfileTO.getDsID());
          cr.addMdRecord(mdRecord);
        } catch (final InvalidContentException e) {
          throw new IntegritySystemException(e);
        }

        mdRecord.setLabel(datastreamProfileTO.getDsLabel());
        mdRecord.setChecksum(datastreamProfileTO.getDsChecksum());
        mdRecord.setMimeType(datastreamProfileTO.getDsMIME());
        mdRecord.setControlGroup(datastreamProfileTO.getDsControlGroup());
        mdRecord.setDatastreamLocation(datastreamProfileTO.getDsLocation());
        mdRecord.getRepositoryIndicator().setResourceIsNew(false);

        if (ChecksumType.DISABLED.toString().equals(datastreamProfileTO.getDsChecksumType())
            || "none".equals(datastreamProfileTO.getDsChecksumType())) {
          mdRecord.setChecksumEnabled(false);
        } else {
          mdRecord.setChecksumEnabled(true);
        }

        // alternate ids
        if (datastreamProfileTO.getDsAltID().size() > 1) {
          mdRecord.setType(datastreamProfileTO.getDsAltID().get(1));

          if (datastreamProfileTO.getDsAltID().size() > 2) {
            mdRecord.setSchema(datastreamProfileTO.getDsAltID().get(2));
          }
        } else if (datastreamProfileTO.getDsAltID().size() <= 3) {
          LOGGER.warn("Expected 3 entries in datastream profile alternative IDs.");
        }
      }
    }
  }