タグとノートはGUIDで紐づいているので、タグの名前を更新するとノートについているタグ名も変わる(当たり前だけど)。
タグを作成、更新するテストプログラム。expungeTag()はAPIキー発行時には許可されない(使えるようにするためには別途申請が必要)。
public class TagTest {
private static String consumerKey = "consumer key";
private static String consumerSecret = "consumer secret";
private static String evernoteHost = "sandbox.evernote.com";
private static String userName = "user name";
private static String password = "password";
private static NoteStore.Client setupNoteStore(String evernoteHost, User user) throws TException {
// NoteStore クライアントのセットアップ
String noteStoreUrl = "https://" + evernoteHost + "/edam/note/" + user.getShardId();
THttpClient trans = new THttpClient(noteStoreUrl);
TBinaryProtocol prot = new TBinaryProtocol(trans);
NoteStore.Client noteStore = new NoteStore.Client(prot, prot);
return noteStore;
}
public static void main(String[] args) {
UserStore.Client userStore = null;
NoteStore.Client noteStore = null;
AuthenticationResult authResult = null;
try {
userStore = AuthUtil.setupUserStore(evernoteHost);
authResult = AuthUtil.authenticate(userStore, consumerKey, consumerSecret, evernoteHost, userName, password);
User user = authResult.getUser();
String authToken = authResult.getAuthenticationToken();
noteStore = setupNoteStore(evernoteHost, user);
Notebook notebook = noteStore.getDefaultNotebook(authToken);
// 新規タグの作成
System.out.println("** CREATE TAG **");
Tag tag1 = new Tag();
tag1.setName("tag1");
Tag created1 = noteStore.createTag(authToken, tag1);
System.out.println("タグ\"" + created1.getName() + "\"が作成されました。(guid=" + created1.getGuid() + ")");
Tag tag2 = new Tag();
tag2.setName("tag2");
Tag created2 = noteStore.createTag(authToken, tag2);
System.out.println("タグ\"" + created2.getName() + "\"が作成されました。(guid=" + created2.getGuid() + ")");
// タグつきのノートを作成
Note note = new Note();
note.setNotebookGuid(notebook.getGuid());
note.setTitle("Testnote with Tags - " + new Date().getTime());
note.addToTagGuids(created1.getGuid()); // 既存タグのGUIDを指定
note.addToTagGuids(created2.getGuid());
note.addToTagNames("tag3"); // ここで存在しないタグ名を指定すると自動的にタグが作成される
Note note_created = noteStore.createNote(authToken, note);
System.out.println("ノート\"" + note_created.getTitle() + "\"が作成されました。");
// ノートについているタグを取得
List<String> tagNames = noteStore.getNoteTagNames(authToken, note_created.getGuid());
System.out.println("Tags: ");
for (String tagName : tagNames) {
System.out.println(" - " + tagName);
}
// タグの取得
System.out.println("** GET TAG **");
Tag tag1_1 = noteStore.getTag(authToken, created1.getGuid());
System.out.println("タグ\"" + tag1_1.getName() + "\"を取得しました。");
// タグの更新
System.out.println("** UPDATE TAG **");
tag1_1.setName("tag1_update");
noteStore.updateTag(authToken, tag1_1);
System.out.println("タグ\"" + tag1_1.getName() + "\"を更新しました。");
// 全タグのリストを取得
System.out.println("** LIST TAGS **");
List<Tag> tags = noteStore.listTags(authToken);
for (Tag tag : tags) {
System.out.println(" - " + tag.getName());
}
// タグの削除
// XXX デフォルトでは許可されない
// System.out.println("** EXPUNGE TAG **");
// noteStore.expungeTag(authToken, created1.getGuid());
} catch (Exception e) {
e.printStackTrace();
}
}
}
ちょっとはまったのが、ノートにつけられているタグ名を取得する方法。NoteStore#getNote()でとってきたノートに対してNote#getTagGuids()でGUIDのリストがとれるけれど、Note#getTagNames()だと、NullPointerException が発生してタグ名のリストがとれない。フォーラムで調べてみるとどうも、GUIDは入れてくれるけれどタグ名は入れてくれないらしい(整合性のためらしい)。getTagNames()はNote#addToTagNames()を使って(ローカルで)追加したタグ名だけをとるためのもののようだ(でもNullPointerExceptionはちょっとひどい・・・)。
GUIDのリストだけあっても仕方ないのでどうするのかと思ったら、一発でノートについているタグ名を取得するNoteStore#getNoteTagNames()というメソッドがあった。
参考: Evernote User Forum - tagNames is Empty
実行例。
** CREATE TAG ** タグ"tag1"が作成されました。(guid=dc4717bd-8faf-44d5-963e-c3a779e5faaa) タグ"tag2"が作成されました。(guid=701dd298-ccda-4e70-b8d5-6c38b3d1291d) ノート"Testnote with Tags - 1290311169789"が作成されました。 Tags: - tag1 - tag2 - tag3 ** GET TAG ** タグ"tag1"を取得しました。 ** UPDATE TAG ** タグ"tag1_update"を更新しました。 ** LIST TAGS ** - tag1_update - tag2 - tag3