17
17
package com .google .datastore .snippets ;
18
18
19
19
import com .google .gcloud .datastore .Datastore ;
20
- import com .google .gcloud .datastore .DatastoreException ;
21
20
import com .google .gcloud .datastore .DatastoreOptions ;
22
21
import com .google .gcloud .datastore .DateTime ;
23
22
import com .google .gcloud .datastore .Entity ;
@@ -51,7 +50,8 @@ public class TaskList {
51
50
* Adds a task entity to the Datastore.
52
51
*
53
52
* @param description The task description
54
- * @return The {@link Key} of the entity.
53
+ * @return The {@link Key} of the entity
54
+ * @throws DatastoreException if the ID allocation or put fails
55
55
*/
56
56
Key addTask (String description ) {
57
57
Key key = datastore .allocateId (keyFactory .newKey ());
@@ -70,14 +70,18 @@ Key addTask(String description) {
70
70
* Marks a task entity as done.
71
71
*
72
72
* @param id The ID of the task entity as given by {@link Key#id()}
73
- * @throws DatastoreException if the task does not exist
73
+ * @return true if the task was found, false if not
74
+ * @throws DatastoreException if the transaction fails
74
75
*/
75
- void markDone (long id ) {
76
+ boolean markDone (long id ) {
76
77
Transaction transaction = datastore .newTransaction ();
77
78
try {
78
79
Entity task = transaction .get (keyFactory .newKey (id ));
79
- transaction .put (Entity .builder (task ).set ("done" , true ).build ());
80
+ if (task != null ) {
81
+ transaction .put (Entity .builder (task ).set ("done" , true ).build ());
82
+ }
80
83
transaction .commit ();
84
+ return task != null ;
81
85
} finally {
82
86
if (transaction .active ()) {
83
87
transaction .rollback ();
@@ -89,6 +93,8 @@ void markDone(long id) {
89
93
// [START retrieve_entities]
90
94
/**
91
95
* Returns a list of all task entities in ascending order of creation time.
96
+ *
97
+ * @throws DatastoreException if the query fails
92
98
*/
93
99
Iterator <Entity > listTasks () {
94
100
Query <Entity > query =
@@ -102,6 +108,7 @@ Iterator<Entity> listTasks() {
102
108
* Deletes a task entity.
103
109
*
104
110
* @param id The ID of the task entity as given by {@link Key#id()}
111
+ * @throws DatastoreException if the delete fails
105
112
*/
106
113
void deleteTask (long id ) {
107
114
datastore .delete (keyFactory .newKey (id ));
@@ -158,10 +165,9 @@ void handleCommandLine(String commandLine) {
158
165
case "done" :
159
166
assertArgsLength (args , 2 );
160
167
long id = Long .parseLong (args [1 ]);
161
- try {
162
- markDone (id );
168
+ if (markDone (id )) {
163
169
System .out .println ("task marked done" );
164
- } catch ( DatastoreException e ) {
170
+ } else {
165
171
System .out .printf ("did not find a Task entity with ID %d%n" , id );
166
172
}
167
173
break ;
@@ -178,7 +184,7 @@ void handleCommandLine(String commandLine) {
178
184
case "delete" :
179
185
assertArgsLength (args , 2 );
180
186
deleteTask (Long .parseLong (args [1 ]));
181
- System .out .println ("task deleted" );
187
+ System .out .println ("task deleted (if it existed) " );
182
188
break ;
183
189
default :
184
190
throw new IllegalArgumentException ("unrecognized command: " + command );
0 commit comments