Android Open Source - evernote-sdk-android Async Linked Note Store Client






From Project

Back to project page evernote-sdk-android.

License

The source code is released under:

Apache License

If you think the Android project evernote-sdk-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2012 Evernote Corporation./*from  w  ww  .j  a  va  2 s  . c  o  m*/
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.evernote.client.android;

import com.evernote.edam.error.EDAMNotFoundException;
import com.evernote.edam.error.EDAMSystemException;
import com.evernote.edam.error.EDAMUserException;
import com.evernote.edam.notestore.NoteFilter;
import com.evernote.edam.notestore.NotesMetadataList;
import com.evernote.edam.notestore.NotesMetadataResultSpec;
import com.evernote.edam.type.LinkedNotebook;
import com.evernote.edam.type.Note;
import com.evernote.edam.type.Notebook;
import com.evernote.edam.type.SharedNotebook;
import com.evernote.thrift.TException;
import com.evernote.thrift.protocol.TProtocol;
import com.evernote.thrift.transport.TTransportException;

import java.util.Arrays;
import java.util.List;

/**
 *
 * This is a wrapper/helper class that manages the connection to a linked notestore. It maintains two
 * {@link AsyncLinkedNoteStoreClient} objects, one points to the users personal store and the other to
 * linked notebooks shard.
 *
 * These helper methods make network calls across both shards to return the appropriate data.
 *
 *
 *
 * @author @tylersmithnet
 */
public class AsyncLinkedNoteStoreClient {
  /**
   * References users main note store
   */
  private AsyncNoteStoreClient mMainNoteStoreClient;
  private AsyncNoteStoreClient mLinkedStoreClient;
  private String mAuthToken;
  private ClientFactory mClientFactory;

  AsyncLinkedNoteStoreClient(TProtocol iprot, TProtocol oprot, String authenticationToken, ClientFactory clientFactory) throws TTransportException {
    mLinkedStoreClient = new AsyncNoteStoreClient(iprot, oprot, authenticationToken);
    mMainNoteStoreClient = EvernoteSession.getOpenSession().getClientFactory().createNoteStoreClient();
    mAuthToken = authenticationToken;
    mClientFactory = clientFactory;
  }

  /**
   * Returns the {@link AsyncNoteStoreClient} object that has been instantiated to the appropriate shard
   * @return
   */
  public AsyncNoteStoreClient getAsyncClient() {
    return mLinkedStoreClient;
  }

  AsyncNoteStoreClient getAsyncPersonalClient() {
    return mMainNoteStoreClient;
  }

  String getAuthenticationToken() {
    return mAuthToken;
  }

  void setAuthToken(String authenticationToken) {
    mAuthToken = authenticationToken;
  }

  ClientFactory getClientFactory() {
    return mClientFactory;
  }

  /**
   * Helper method to create a note asynchronously in a linked/business notebook
   *
   * @param note
   * @param linkedNotebook
   * @param callback
   */
  public void createNoteAsync(final Note note, final LinkedNotebook linkedNotebook, final OnClientCallback<Note> callback) {
    AsyncReflector.execute(this, callback, "createNote", note, linkedNotebook);

  }

  /**
   * Helper method to create a note synchronously in a linked notebook
   *
   * @param note
   * @param linkedNotebook
   * @return
   * @throws com.evernote.edam.error.EDAMUserException
   *
   * @throws com.evernote.edam.error.EDAMSystemException
   *
   * @throws com.evernote.thrift.TException
   * @throws com.evernote.edam.error.EDAMNotFoundException
   *
   */
  public Note createNote(Note note, LinkedNotebook linkedNotebook) throws EDAMUserException, EDAMSystemException, TException, EDAMNotFoundException {
    SharedNotebook sharedNotebook = getAsyncClient().getClient().getSharedNotebookByAuth(getAuthenticationToken());
    note.setNotebookGuid(sharedNotebook.getNotebookGuid());
    return getAsyncClient().getClient().createNote(getAuthenticationToken(), note);

  }

  /**
   * Helper method to list linked/business notebooks asynchronously
   *
   * @see {@link com.evernote.edam.notestore.NoteStore.Client#listLinkedNotebooks(String)}
   *
   * @param callback
   */
  public void listNotebooksAsync(final OnClientCallback<List<LinkedNotebook>> callback) {
    AsyncReflector.execute(getAsyncPersonalClient(), callback, "listNotebooks", getAuthenticationToken());
  }

  /**
   * Helper method to list linked notebooks synchronously
   *
   * @see {@link com.evernote.edam.notestore.NoteStore.Client#listLinkedNotebooks(String)}
   *
   */
  public List<LinkedNotebook> listNotebooks() throws EDAMUserException, EDAMSystemException, TException, EDAMNotFoundException {
    return getAsyncPersonalClient().getClient().listLinkedNotebooks(getAsyncPersonalClient().getAuthenticationToken());
  }

  /**
   * Create Linked Notebook from a Notebook
   *
   * Asynchronous call
   *
   * @param callback
   */
  public void createNotebookAsync(Notebook notebook, OnClientCallback<LinkedNotebook> callback) {
    AsyncReflector.execute(this, callback, "createNotebook", notebook);
  }

  /**
   * Create Linked Notebook from a Notebook
   *
   * Synchronous call
   *
   * @return {@link LinkedNotebook} with guid from server
   */
  public LinkedNotebook createNotebook(Notebook notebook) throws TException, EDAMUserException, EDAMSystemException, EDAMNotFoundException {

    Notebook originalNotebook = getAsyncClient().getClient().createNotebook(getAuthenticationToken(), notebook);

    SharedNotebook sharedNotebook = originalNotebook.getSharedNotebooks().get(0);
    LinkedNotebook linkedNotebook = new LinkedNotebook();
    linkedNotebook.setShareKey(sharedNotebook.getShareKey());
    linkedNotebook.setShareName(originalNotebook.getName());
    linkedNotebook.setUsername(EvernoteSession.getOpenSession().getAuthenticationResult().getBusinessUser().getUsername());
    linkedNotebook.setShardId(EvernoteSession.getOpenSession().getAuthenticationResult().getBusinessUser().getShardId());

    return getAsyncPersonalClient().getClient().createLinkedNotebook(getAsyncPersonalClient().getAuthenticationToken(), linkedNotebook);
  }

  /**
   * Providing a LinkedNotebook referencing a linked/business account, perform a delete
   *
   * Asynchronous call
   * @param callback
   */
  public void deleteNotebookAsync(LinkedNotebook linkedNotebook, OnClientCallback<Integer> callback) {
    AsyncReflector.execute(this, callback, "deleteNotebook", linkedNotebook);
  }

  /**
   * Providing a LinkedNotebook referencing a linked account, perform a delete
   *
   * Synchronous call
   *
   * @return guid of notebook deleted
   */
  public int deleteNotebook(LinkedNotebook linkedNotebook) throws TException, EDAMUserException, EDAMSystemException, EDAMNotFoundException {

    SharedNotebook sharedNotebook = getAsyncClient().getClient().getSharedNotebookByAuth(getAuthenticationToken());

    Long[] ids = {sharedNotebook.getId()};
    getAsyncClient().getClient().expungeSharedNotebooks(getAuthenticationToken(), Arrays.asList(ids));
    return getAsyncPersonalClient().getClient().expungeLinkedNotebook(getAsyncPersonalClient().getAuthenticationToken(), linkedNotebook.getGuid());
  }

  /**
   * Will return the {@link Notebook} associated with the {@link LinkedNotebook} from the linked/business account
   *
   * Asynchronous call
   *
   * @param linkedNotebook
   * @param callback
   */
  public void getCorrespondingNotebookAsync(LinkedNotebook linkedNotebook, OnClientCallback<Notebook> callback) {
    AsyncReflector.execute(this, callback, "getCorrespondingNotebook", linkedNotebook);
  }

  /**
   * Will return the {@link com.evernote.edam.type.Notebook} associated with the {@link com.evernote.edam.type.LinkedNotebook} from the linked account
   *
   * Synchronous call
   *
   * @param linkedNotebook
   */
  public Notebook getCorrespondingNotebook(LinkedNotebook linkedNotebook) throws TException, EDAMUserException, EDAMSystemException, EDAMNotFoundException {
    SharedNotebook sharedNotebook = getAsyncClient().getClient().getSharedNotebookByAuth(getAuthenticationToken());
    return getAsyncClient().getClient().getNotebook(getAuthenticationToken(), sharedNotebook.getNotebookGuid());
  }

  /**
   * Checks writable permissions of {@link LinkedNotebook} on Linked/business account
   *
   * Asynchronous call
   *
   * @param linkedNotebook
   * @param callback
   */
  public void isNotebookWritableAsync(LinkedNotebook linkedNotebook, OnClientCallback<Boolean> callback) {
    AsyncReflector.execute(this, callback, "isLinkedNotebookWritable", linkedNotebook);
  }
  /**
   * Checks writable permissions of {@link LinkedNotebook} on Linked account
   *
   * Synchronous call
   *
   * @param linkedNotebook
   */
  public boolean isNotebookWritable(LinkedNotebook linkedNotebook) throws EDAMUserException, TException, EDAMSystemException, EDAMNotFoundException {
    Notebook notebook = getCorrespondingNotebook(linkedNotebook);
    return !notebook.getRestrictions().isNoCreateNotes();
  }

  /**
   * Helper method to find notes in a linked notebook asynchronously
   *
   * @see {@link com.evernote.edam.notestore.NoteStore.Client#findNotesMetadata(String, com.evernote.edam.notestore.NoteFilter, int, int, com.evernote.edam.notestore.NotesMetadataResultSpec)}
   *
   */

  public void findNotesMetadataAsync(NoteFilter filter,
                                      int offset,
                                      int maxNotes,
                                      NotesMetadataResultSpec resultSpec,
                                      OnClientCallback<NotesMetadataList> callback) {
    AsyncReflector.execute(this, callback, "findNotesMetadata", filter, offset, maxNotes, resultSpec);
  }

  /**
   * Helper method to find notes in a linked notebook
   *
   * @see {@link com.evernote.edam.notestore.NoteStore.Client#findNotesMetadata(String, com.evernote.edam.notestore.NoteFilter, int, int, com.evernote.edam.notestore.NotesMetadataResultSpec)}
   *
   */
  public NotesMetadataList findNotesMetadata(NoteFilter filter,
                                             int offset,
                                             int maxNotes,
                                             NotesMetadataResultSpec resultSpec) throws EDAMUserException, EDAMSystemException, EDAMNotFoundException, TException {
    return getAsyncClient().getClient().findNotesMetadata(getAuthenticationToken(), filter, offset, maxNotes, resultSpec);
  }

}




Java Source Code List

com.evernote.android.sample.HelloEDAM.java
com.evernote.android.sample.ImagePicker.java
com.evernote.android.sample.ParentActivity.java
com.evernote.android.sample.SearchNotes.java
com.evernote.android.sample.SimpleNote.java
com.evernote.client.android.AsyncBusinessNoteStoreClient.java
com.evernote.client.android.AsyncLinkedNoteStoreClient.java
com.evernote.client.android.AsyncNoteStoreClient.java
com.evernote.client.android.AsyncReflector.java
com.evernote.client.android.AsyncUserStoreClient.java
com.evernote.client.android.AuthenticationResult.java
com.evernote.client.android.BootstrapManager.java
com.evernote.client.android.ClientFactory.java
com.evernote.client.android.EvernoteOAuthActivity.java
com.evernote.client.android.EvernoteSession.java
com.evernote.client.android.EvernoteUtil.java
com.evernote.client.android.InvalidAuthenticationException.java
com.evernote.client.android.OnClientCallback.java
com.evernote.client.android.SessionPreferences.java
com.evernote.client.conn.mobile.DiskBackedByteStore.java
com.evernote.client.conn.mobile.FileDataException.java
com.evernote.client.conn.mobile.FileData.java
com.evernote.client.conn.mobile.TEvernoteHttpClient.java
com.evernote.client.oauth.EvernoteAuthToken.java
com.evernote.client.oauth.YinxiangApi.java