|
| 1 | +/** |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | + * except in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * <p>http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * <p>Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | + * express or implied. See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +//[START all] |
| 16 | +package com.example.guestbook; |
| 17 | + |
| 18 | +import static com.example.guestbook.Persistence.getDatastore; |
| 19 | + |
| 20 | +import com.google.cloud.datastore.DateTime; |
| 21 | +import com.google.cloud.datastore.Entity; |
| 22 | +import com.google.cloud.datastore.FullEntity; |
| 23 | +import com.google.cloud.datastore.FullEntity.Builder; |
| 24 | +import com.google.cloud.datastore.IncompleteKey; |
| 25 | +import com.google.cloud.datastore.Key; |
| 26 | +import com.google.common.base.MoreObjects; |
| 27 | + |
| 28 | +import java.util.Date; |
| 29 | +import java.util.Objects; |
| 30 | + |
| 31 | +public class Greeting { |
| 32 | + private Guestbook book; |
| 33 | + |
| 34 | + public Key key; |
| 35 | + public String authorEmail; |
| 36 | + public String authorId; |
| 37 | + public String content; |
| 38 | + public Date date; |
| 39 | + |
| 40 | + public Greeting() { |
| 41 | + date = new Date(); |
| 42 | + } |
| 43 | + |
| 44 | + public Greeting(String book, String content) { |
| 45 | + this(); |
| 46 | + this.book = new Guestbook(book); |
| 47 | + this.content = content; |
| 48 | + } |
| 49 | + |
| 50 | + public Greeting(String book, String content, String id, String email) { |
| 51 | + this(book, content); |
| 52 | + authorEmail = email; |
| 53 | + authorId = id; |
| 54 | + } |
| 55 | + |
| 56 | + public Greeting(Entity entity) { |
| 57 | + key = entity.hasKey() ? entity.key() : null; |
| 58 | + authorEmail = entity.contains("authorEmail") ? entity.getString("authorEmail") : null; |
| 59 | + authorId = entity.contains("authorId") ? entity.getString("authorId") : null; |
| 60 | + date = entity.contains("date") ? entity.getDateTime("date").toDate() : null; |
| 61 | + content = entity.contains("content") ? entity.getString("content") : null; |
| 62 | + } |
| 63 | + |
| 64 | + public void save() { |
| 65 | + if (key == null) { |
| 66 | + key = getDatastore().allocateId(makeIncompleteKey()); // Give this greeting a unique ID |
| 67 | + } |
| 68 | + |
| 69 | + Builder<Key> builder = FullEntity.builder(key); |
| 70 | + |
| 71 | + if (authorEmail != null) { |
| 72 | + builder.set("authorEmail", authorEmail); |
| 73 | + } |
| 74 | + |
| 75 | + if (authorId != null) { |
| 76 | + builder.set("authorId", authorId); |
| 77 | + } |
| 78 | + |
| 79 | + builder.set("content", content); |
| 80 | + builder.set("date", DateTime.copyFrom(date)); |
| 81 | + |
| 82 | + getDatastore().put(builder.build()); |
| 83 | + } |
| 84 | + |
| 85 | + private IncompleteKey makeIncompleteKey() { |
| 86 | + // The book is our ancestor key. |
| 87 | + return Key.builder(book.getKey(), "Greeting").build(); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public boolean equals(Object o) { |
| 92 | + if (this == o) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + if (o == null || getClass() != o.getClass()) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + Greeting greeting = (Greeting) o; |
| 99 | + return Objects.equals(key, greeting.key) |
| 100 | + && Objects.equals(authorEmail, greeting.authorEmail) |
| 101 | + && Objects.equals(authorId, greeting.authorId) |
| 102 | + && Objects.equals(content, greeting.content) |
| 103 | + && Objects.equals(date, greeting.date); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public int hashCode() { |
| 108 | + return Objects.hash(key, authorEmail, authorId, content, date); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public String toString() { |
| 113 | + return MoreObjects.toStringHelper(this) |
| 114 | + .add("key", key) |
| 115 | + .add("authorEmail", authorEmail) |
| 116 | + .add("authorId", authorId) |
| 117 | + .add("content", content) |
| 118 | + .add("date", date) |
| 119 | + .add("book", book) |
| 120 | + .toString(); |
| 121 | + } |
| 122 | +} |
| 123 | +//[END all] |
0 commit comments