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