niedException {
    final User user = tryGetCurrentUser(UserServiceFactory.getUserService());
    final Store.Api api = store.getApi();
    try {
      final Key key = KeyFactory.stringToKey(surfaceKey);

      final Store.Author me = api.getOrCreateNewAuthor(user);

      final Store.Author author = api.tryGetAuthor(email);
      if (author == null) {
        return null;
      }

      if (!me.hasSurface(key)) {
        throw new Service.AccessDeniedException();
      }

      final Store.Surface surface = api.getSurface(key);
      if (!author.hasSurface(key)) {

        cache.deleteSurfaceKeys(author.getEmail());
        cache.deleteSurface(surface.getKey());

        final Transaction txA = api.begin();
        author.addSurface(surface);
        api.saveAuthor(author);
        txA.commit();

        final Transaction txB = api.begin();
        surface.addAuthorName(author.getName());
        api.saveSurface(surface);
        txB.commit();
      }
      return new AddAuthorToSurfaceResult(author.getName(), surface
          .getLastUpdatedAt());

    } finally {
      api.close();
    }
  }

  public Date changeNoteContent(final String noteKey, final String content)
      throws AccessDeniedException {
    final User user = tryGetCurrentUser(UserServiceFactory.getUserService());
    final Store.Api api = store.getApi();
    try {
      final Key key = KeyFactory.stringToKey(noteKey);
      final Store.Author me = api.getOrCreateNewAuthor(user);

      final Transaction tx = api.begin();
      final Store.Note note = api.getNote(key);
      if (!note.isOwnedBy(me)) {
        throw new Service.AccessDeniedException();
      }
      note.setContent(content);
      final Date result = api.saveNote(note).getLastUpdatedAt();
      tx.commit();

      cache.deleteNotes(getSurfaceKey(note));
      return result;
    } finally {
      api.close();
    }
  }

  public Date changeNotePosition(final String noteKey, final int x,
      final int y, final int width, final int height)
      throws AccessDeniedException {
    final User user = tryGetCurrentUser(UserServiceFactory.getUserService());
    final Store.Api api = store.getApi();
    try {
      final Key key = KeyFactory.stringToKey(noteKey);
      final Store.Author me = api.getOrCreateNewAuthor(user);

      final Transaction tx = api.begin();
      final Store.Note note = api.getNote(key);
      if (!note.isOwnedBy(me)) {
        throw new Service.AccessDeniedException();
      }
      note.setX(x);
      note.setY(y);
      note.setWidth(width);
      note.setHeight(height);
      final Date result = api.saveNote(note).getLastUpdatedAt();
      tx.commit();

      cache.deleteNotes(getSurfaceKey(note));
      return result;
    } finally {
      api.close();
    }
  }

  public CreateObjectResult createNote(final String surfaceKey, final int x,
      final int y, final int width, final int height)
      throws AccessDeniedException {
    final User user = tryGetCurrentUser(UserServiceFactory.getUserService());
    final Store.Api api = store.getApi();
    try {
      final Key key = KeyFactory.stringToKey(surfaceKey);
      final Store.Author me = api.getOrCreateNewAuthor(user);

      if (!me.hasSurface(key)) {
        throw new Service.AccessDeniedException();
      }

      final Transaction tx = api.begin();
      final Store.Surface surface = api.getSurface(key);
      final Store.Note note = new Store.Note(me, x, y, width, height);
      surface.getNotes().add(note);
      api.saveSurface(surface);

      final CreateObjectResult result = new CreateObjectResult(KeyFactory
          .keyToString(note.getKey()), note.getLastUpdatedAt());
      tx.commit();

      cache.deleteNotes(surfaceKey);
      return result;
    } finally {
      api.close();
    }
  }

  public CreateObjectResult createSurface(final String title)
      throws AccessDeniedException {
    final User user = tryGetCurrentUser(UserServiceFactory.getUserService());
    final Store.Api api = store.getApi();
    try {
      final Store.Author me = api.getOrCreateNewAuthor(user);

      final Store.Surface surface = new Store.Surface(title);
      surface.addAuthorName(me.getName());
      api.saveSurface(surface);

      final Transaction tx = api.begin();
      me.addSurface(surface);
      api.saveAuthor(me);
      tx.commit();

      cache.deleteSurfaceKeys(me.getEmail());

      return new CreateObjectResult(KeyFactory.keyToString(surface.getKey()),
          surface.getLastUpdatedAt());
    } finally {
      api.close();
    }
  }

  public GetNotesResult getNotes(String surfaceKey, String since)
      throws AccessDeniedException {
    final User user = tryGetCurrentUser(UserServiceFactory.getUserService());
    return new Service.GetNotesResult(createTimestamp(), getNotes(user,
        surfaceKey, since));
  }

  public GetSurfacesResult getSurfaces(String timestamp)
      throws AccessDeniedException {
    final User user = tryGetCurrentUser(UserServiceFactory.getUserService());
    final Store.Api api = store.getApi();
    try {
      final List<Key> keys = getSurfaceKeys(api, user);
      final Surface[] surfaces = new Surface[keys.size()];
      for (int i = 0, n = keys.size(); i < n; ++i) {
        surfaces[i] = getSurface(api, keys.get(i));
      }
      return new GetSurfacesResult(null, surfaces);
    } finally {
      api.close();
    }
  }

  public UserInfoResult getUserInfo() throws AccessDeniedException {
    final UserService userService = UserServiceFactory.getUserService();
    final User user = tryGetCurrentUser(userService);
    final Store.Api api = store.getApi();
    try {
      final Key surfaceKey = getSurfaceKeys(api, user).get(0);
      final UserInfoResult result = new Service.UserInfoResult(new Author(user
          .getEmail(), user.getNickname()), getSurface(api, surfaceKey),
          userService.createLogoutURL(userService.createLoginURL("/")));
      return result;
    } finally {
      api.close();
    }
  }

  private Note[] getNotes(User user, String surfaceKey, String since)
      throws AccessDeniedException {
    final Store.Api api = store.getApi();
    try {
      final Note[] fromCache = cache.getNotes(user, surfaceKey);
      if (fromCache != null) {
        return getNotesSinceTimestamp(fromCache, since);
      }

      final Key key = KeyFactory.stringToKey(surfaceKey);
      final Store.Author me = api.getOrCreateNewAuthor(user);
      if (!me.hasSurface(key)) {
        throw new Service.AccessDeniedException();
      }
      final Store.Surface surface = api.getSurface(key);
      final Note[] notes = cache.putNotes(user, surfaceKey,
          toClientNotes(surface.getNotes()));
      return getNotesSinceTimestamp(notes, since);
    } finally {
      api.close();
    }
  }

  private Surface getSurface(Store.Api api, Key key) {
    final Surface fromCache = cache.getSurface(key);
    if (fromCache != null) {
      return fromCache;
    }

    return cache.putSurface(key, toClientSurface(api.getSurface(key)));
  }

  private List<Key> getSurfaceKeys(Store.Api api, User user) {
    final String email = user.getEmail();

    final List<Key> fromCache = cache.getSurfaceKeys(email);
    if (fromCache != null) {
      return fromCache;
    }

    final Store.Author author = api.getOrCreateNewAuthor(user);
    return cache.putSurfaceKeys(email, author.getSurfaceKeys());
  }

}
