/**
   * Fetches a single change package fragment.
   *
   * @param sessionId the {@link SessionId} representing the requesting user
   * @param proxyId the ID that identifies the list of stored fragments
   * @param fragmentIndex allows to request different change package fragments
   * @return a {@link ChangePackageEnvelope} containing the change package fragment
   * @throws ESException in case the mandatory session adapter is missing
   */
  @ESMethod(MethodId.DOWNLOADCHANGEPACKAGEFRAGMENT)
  public ChangePackageEnvelope downloadChangePackageFragment(
      SessionId sessionId, String proxyId, int fragmentIndex) throws ESException {

    final ESSessionId resolvedSession =
        getAccessControl().getSessions().resolveSessionById(sessionId.getId());
    final SessionId session = APIUtil.toInternal(SessionId.class, resolvedSession);
    final Optional<ChangePackageFragmentProviderAdapter> maybeAdapter =
        ESCollections.find(session.eAdapters(), ChangePackageFragmentProviderAdapter.class);

    if (!maybeAdapter.isPresent()) {
      throw new ESException(
          Messages.VersionSubInterfaceImpl_ChangePackageFragmentProviderAdapterMissing + sessionId);
    }

    final ChangePackageFragmentProviderAdapter adapter = maybeAdapter.get();
    final ChangePackageEnvelope envelope =
        VersioningFactory.eINSTANCE.createChangePackageEnvelope();
    final List<String> fragment = adapter.getFragment(proxyId, fragmentIndex);

    envelope.getFragment().addAll(fragment);
    envelope.setFragmentCount(adapter.getFragmentSize(proxyId));
    envelope.setFragmentIndex(fragmentIndex);

    if (envelope.isLast()) {
      adapter.markAsConsumed(proxyId);
    }

    return envelope;
  }
  /**
   * Given a {@link ChangePackageEnvelope} which contains a change package fragment, stores one or
   * more fragments by attaching them to a session specific adapter.
   *
   * @param sessionId the {@link SessionId} belonging to the calling user
   * @param projectId the {@link ProjectId}
   * @param envelope the {@link ChangePackageEnvelope} containing the fragment
   * @return an ID identifying the stored fragment(s)
   * @throws ESException in case the fragment couldn't be stored
   */
  @ESMethod(MethodId.UPLOADCHANGEPACKAGEFRAGMENT)
  public String uploadChangePackageFragment(
      SessionId sessionId, ProjectId projectId, ChangePackageEnvelope envelope) throws ESException {

    final String proxyId = generateProxyId(projectId.getId());

    final ESSessionId resolvedSession =
        getAccessControl().getSessions().resolveSessionById(sessionId.getId());

    if (resolvedSession == null) {
      throw new ESException(
          MessageFormat.format(Messages.VersionSubInterfaceImpl_0, sessionId.getId()));
    }

    final SessionId session = APIUtil.toInternal(SessionId.class, resolvedSession);
    final Optional<ChangePackageFragmentUploadAdapter> maybeAdapter =
        ESCollections.find(session.eAdapters(), ChangePackageFragmentUploadAdapter.class);
    ChangePackageFragmentUploadAdapter adapter;

    if (!maybeAdapter.isPresent()) {
      adapter = new ChangePackageFragmentUploadAdapter();
      session.eAdapters().add(adapter);
    } else {
      adapter = maybeAdapter.get();
    }

    adapter.addFragment(proxyId, envelope.getFragment());
    if (envelope.isLast()) {
      adapter.markAsComplete(proxyId);
    }

    return proxyId;
  }