-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatastore.java
More file actions
170 lines (145 loc) · 5.82 KB
/
Datastore.java
File metadata and controls
170 lines (145 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.codeu.data;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.SortDirection;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/** Provides access to the data stored in Datastore. */
public class Datastore {
private DatastoreService datastore;
public Datastore() {
datastore = DatastoreServiceFactory.getDatastoreService();
}
/** Stores the Message in Datastore. */
public void storeMessage(Message message) {
Entity messageEntity = new Entity("Message", message.getId().toString());
messageEntity.setProperty("user", message.getUser());
messageEntity.setProperty("text", message.getText());
messageEntity.setProperty("timestamp", message.getTimestamp());
messageEntity.setProperty("recipient", message.getRecipient());
datastore.put(messageEntity);
}
public List<Message> messageConverter(PreparedQuery results) {
List<Message> messages = new ArrayList<>();
for (Entity entity : results.asIterable()) {
try {
String idString = entity.getKey().getName();
UUID id = UUID.fromString(idString);
String user = (String) entity.getProperty("user");
String recipient = (String) entity.getProperty("recipient");
String text = (String) entity.getProperty("text");
long timestamp = (long) entity.getProperty("timestamp");
Message message = new Message(id, user, text, timestamp, recipient);
messages.add(message);
} catch (Exception e) {
System.err.println("Error reading message.");
System.err.println(entity.toString());
e.printStackTrace();
}
}
return messages;
}
/**
* Gets messages posted by a specific user.
*
* @return a list of messages posted by the user, or empty list if user has never posted a
* message. List is sorted by time descending.
*/
public List<Message> getMessages(String user) {
Query query =
new Query("Message")
.setFilter(new Query.FilterPredicate("user", FilterOperator.EQUAL, user))
.addSort("timestamp", SortDirection.DESCENDING);
PreparedQuery results = datastore.prepare(query);
List<Message> messages = messageConverter(results);
return messages;
}
public List<Message> getAllMessages() {
Query query = new Query("Message").addSort("timestamp", SortDirection.DESCENDING);
PreparedQuery results = datastore.prepare(query);
List<Message> messages = messageConverter(results);
return messages;
}
/**
* Gets messages posted to a specific user.
*
* @return a list of messages posted to the user, or empty list if user has never received a
* message. List is sorted by time descending.
*/
public List<Message> getMessagesForRecipient(String recipient) {
Query query =
new Query("Message")
.setFilter(new Query.FilterPredicate("recipient", FilterOperator.EQUAL, recipient))
.addSort("timestamp", SortDirection.DESCENDING);
PreparedQuery results = datastore.prepare(query);
List<Message> messages = messageConverter(results);
return messages;
}
/** Stores the User in Datastore. */
public void storeUser(User user) {
Entity userEntity = new Entity("User", user.getEmail());
userEntity.setProperty("email", user.getEmail());
userEntity.setProperty("aboutMe", user.getAboutMe());
datastore.put(userEntity);
}
/** Returns the User owned by the email address, or null if no matching User was found. */
public User getUser(String email) {
Query query =
new Query("User")
.setFilter(new Query.FilterPredicate("email", FilterOperator.EQUAL, email));
PreparedQuery results = datastore.prepare(query);
Entity userEntity = results.asSingleEntity();
if (userEntity == null) {
return null;
}
String aboutMe = (String) userEntity.getProperty("aboutMe");
User user = new User(email, aboutMe);
return user;
}
public List<UserMarker> getMarkers() {
List<UserMarker> markers = new ArrayList<>();
Query query = new Query("UserMarker");
PreparedQuery results = datastore.prepare(query);
for (Entity entity : results.asIterable()) {
try {
double lat = (double) entity.getProperty("lat");
double lng = (double) entity.getProperty("lng");
String content = (String) entity.getProperty("content");
UserMarker marker = new UserMarker(lat, lng, content);
markers.add(marker);
} catch (Exception e) {
System.err.println("Error reading marker.");
System.err.println(entity.toString());
e.printStackTrace();
}
}
return markers;
}
public void storeMarker(UserMarker marker) {
Entity markerEntity = new Entity("UserMarker");
markerEntity.setProperty("lat", marker.getLat());
markerEntity.setProperty("lng", marker.getLng());
markerEntity.setProperty("content", marker.getContent());
datastore.put(markerEntity);
}
}