Skip to content

Commit 2ca64b2

Browse files
committed
Merge branch 'develop'
2 parents a09d6d2 + 218b3ec commit 2ca64b2

File tree

16 files changed

+351
-56
lines changed

16 files changed

+351
-56
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.pscuderi.appengineangulardemo.model;
2+
3+
import com.googlecode.objectify.annotation.Cache;
4+
import com.googlecode.objectify.annotation.Entity;
5+
import com.googlecode.objectify.annotation.Id;
6+
7+
@Cache
8+
@Entity
9+
public class AppUser {
10+
@Id
11+
private String id;
12+
13+
private String email;
14+
15+
public AppUser() {}
16+
17+
public AppUser(String id, String email) {
18+
this.id = id;
19+
this.email = email;
20+
}
21+
22+
public String getId() {
23+
return id;
24+
}
25+
26+
public void setId(String id) {
27+
this.id = id;
28+
}
29+
30+
public String getEmail() {
31+
return email;
32+
}
33+
34+
public void setEmail(String email) {
35+
this.email = email;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.pscuderi.appengineangulardemo.model;
2+
3+
import com.google.gson.annotations.Expose;
4+
import com.googlecode.objectify.annotation.Cache;
5+
import com.googlecode.objectify.annotation.Entity;
6+
import com.googlecode.objectify.annotation.Id;
7+
import com.googlecode.objectify.annotation.Index;
8+
9+
@Cache
10+
@Entity
11+
public class ToDoItem {
12+
@Id
13+
@Expose
14+
private Long id;
15+
16+
@Index
17+
private String ownerId;
18+
19+
@Expose
20+
private String action;
21+
22+
@Expose
23+
private boolean done;
24+
25+
public ToDoItem() { }
26+
27+
public ToDoItem(String ownerId, String action, boolean done) {
28+
this.ownerId = ownerId;
29+
this.action = action;
30+
this.done = done;
31+
}
32+
33+
public Long getId() {
34+
return id;
35+
}
36+
37+
public String getOwnerId() {
38+
return ownerId;
39+
}
40+
41+
public void setOwnerId(String ownerId) {
42+
this.ownerId = ownerId;
43+
}
44+
45+
public String getAction() {
46+
return action;
47+
}
48+
49+
public boolean getDone() {
50+
return done;
51+
}
52+
53+
public void setDone(boolean done) {
54+
this.done = done;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.pscuderi.appengineangulardemo.service;
2+
3+
import com.google.appengine.api.users.User;
4+
import com.googlecode.objectify.Objectify;
5+
import com.pscuderi.appengineangulardemo.model.AppUser;
6+
7+
public final class AppUserService {
8+
private AppUserService() {}
9+
10+
public static AppUser getAppUser(User user) {
11+
Objectify ofy = OfyService.ofy();
12+
13+
AppUser appUser = ofy.load().type(AppUser.class).id(user.getUserId()).now();
14+
15+
if (appUser == null) { // appUser wasn't in the datastore
16+
appUser = new AppUser(user.getUserId(), user.getEmail());
17+
ofy.save().entity(appUser).now();
18+
}
19+
else { // appUser is already in the datastore
20+
21+
// update properties if they've changed
22+
if (!appUser.getEmail().equalsIgnoreCase(user.getEmail())) {
23+
appUser.setEmail(user.getEmail());
24+
ofy.save().entity(appUser).now();
25+
}
26+
}
27+
28+
return appUser;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.pscuderi.appengineangulardemo.service;
2+
3+
import com.googlecode.objectify.Objectify;
4+
import com.googlecode.objectify.ObjectifyFactory;
5+
import com.googlecode.objectify.ObjectifyService;
6+
import com.pscuderi.appengineangulardemo.model.AppUser;
7+
import com.pscuderi.appengineangulardemo.model.ToDoItem;
8+
9+
public final class OfyService {
10+
static {
11+
factory().register(AppUser.class);
12+
factory().register(ToDoItem.class);
13+
}
14+
15+
private OfyService() {}
16+
17+
public static Objectify ofy() {
18+
return ObjectifyService.ofy();
19+
}
20+
21+
public static ObjectifyFactory factory() {
22+
return ObjectifyService.factory();
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.pscuderi.appengineangulardemo.service;
2+
3+
import java.util.List;
4+
import java.util.logging.Level;
5+
import java.util.logging.Logger;
6+
7+
import com.pscuderi.appengineangulardemo.model.AppUser;
8+
import com.pscuderi.appengineangulardemo.model.ToDoItem;
9+
10+
public final class ToDoService {
11+
private static final Logger log = Logger.getLogger(ToDoService.class.getName());
12+
13+
private ToDoService() {}
14+
15+
public static List<ToDoItem> getToDoList(AppUser user) {
16+
return OfyService.ofy().load().type(ToDoItem.class).filter("ownerId", user.getId()).list();
17+
}
18+
19+
public static ToDoItem getById(Long id) {
20+
return OfyService.ofy().load().type(ToDoItem.class).id(id).now();
21+
}
22+
23+
public static void persistOrUpdate(AppUser user, ToDoItem newItem) {
24+
// data security
25+
if (newItem.getId() != null) {
26+
ToDoItem oldItem = getById(newItem.getId());
27+
if (!oldItem.getOwnerId().equals(user.getId())) {
28+
log.log(
29+
Level.WARNING,
30+
"appUserId " + user.getId() + " doesn't own itemId " + newItem.getId());
31+
return;
32+
}
33+
}
34+
35+
// ownerId isn't serialized, so we probably need to set it
36+
if (newItem.getOwnerId() == null) {
37+
newItem.setOwnerId(user.getId());
38+
}
39+
40+
OfyService.ofy().save().entity(newItem).now();
41+
}
42+
}

demo-war/src/main/java/com/pscuderi/appengineangulardemo/servlet/GetUserServlet.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@SuppressWarnings("serial")
1212
public class GetUserServlet extends HttpServlet {
1313
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
14-
resp.setContentType("text/json");
14+
resp.setContentType("application/json");
1515
resp.getWriter().println(ServletUtils.toJson(UserServiceFactory.getUserService().getCurrentUser()));
1616
}
1717
}

demo-war/src/main/java/com/pscuderi/appengineangulardemo/servlet/GuestbookServlet.java

-28
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.pscuderi.appengineangulardemo.servlet;
2+
3+
import java.io.IOException;
4+
5+
import javax.servlet.http.HttpServlet;
6+
import javax.servlet.http.HttpServletRequest;
7+
import javax.servlet.http.HttpServletResponse;
8+
9+
import com.pscuderi.appengineangulardemo.model.AppUser;
10+
import com.pscuderi.appengineangulardemo.model.ToDoItem;
11+
import com.pscuderi.appengineangulardemo.service.ToDoService;
12+
import com.pscuderi.appengineangulardemo.util.ServletUtils;
13+
14+
@SuppressWarnings("serial")
15+
public class ToDoListServlet extends HttpServlet {
16+
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
17+
AppUser user = ServletUtils.getUserAndRedirectIfNotAuthenticated(req, resp);
18+
19+
if (user != null) {
20+
resp.setContentType("application/json");
21+
resp.getWriter().println(ServletUtils.toCustomJson(ToDoService.getToDoList(user)));
22+
}
23+
}
24+
25+
public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
26+
AppUser user = ServletUtils.getUserAndRedirectIfNotAuthenticated(req, resp);
27+
28+
ToDoItem item = ServletUtils.fromJson(req.getReader(), ToDoItem.class);
29+
30+
if (item != null) {
31+
ToDoService.persistOrUpdate(user, item);
32+
}
33+
}
34+
}

demo-war/src/main/java/com/pscuderi/appengineangulardemo/util/ServletUtils.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,23 @@
1010
import com.google.appengine.api.users.UserService;
1111
import com.google.appengine.api.users.UserServiceFactory;
1212
import com.google.gson.Gson;
13+
import com.google.gson.GsonBuilder;
14+
import com.pscuderi.appengineangulardemo.model.AppUser;
15+
import com.pscuderi.appengineangulardemo.service.AppUserService;
1316

1417
public final class ServletUtils {
1518
private ServletUtils() { }
1619

1720
private static Gson gson = new Gson();
21+
private static Gson customGson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
1822

1923
public static String toJson(Object o) {
2024
return gson.toJson(o);
2125
}
26+
27+
public static String toCustomJson(Object o) {
28+
return customGson.toJson(o);
29+
}
2230

2331
public static <T> T fromJson(String json, Class<T> clazz) {
2432
return gson.fromJson(json, clazz);
@@ -40,12 +48,15 @@ public static <T> T fromJson(BufferedReader reader, Class<T> clazz) throws IOExc
4048
return fromJson(sb.toString(), clazz);
4149
}
4250

43-
public static User getUserAndRedirectIfNotAuthenticated(HttpServletRequest req, HttpServletResponse resp) throws IOException {
51+
public static AppUser getUserAndRedirectIfNotAuthenticated(HttpServletRequest req, HttpServletResponse resp) throws IOException {
4452
UserService userService = UserServiceFactory.getUserService();
4553
User user = userService.getCurrentUser();
54+
4655
if (user == null) {
4756
resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
57+
return null;
4858
}
49-
return user;
59+
60+
return AppUserService.getAppUser(user);
5061
}
5162
}

demo-war/src/main/webapp/WEB-INF/web.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
</servlet-mapping>
3030

3131
<servlet>
32-
<servlet-name>Guestbook</servlet-name>
33-
<servlet-class>com.pscuderi.appengineangulardemo.servlet.GuestbookServlet</servlet-class>
32+
<servlet-name>ToDoList</servlet-name>
33+
<servlet-class>com.pscuderi.appengineangulardemo.servlet.ToDoListServlet</servlet-class>
3434
</servlet>
3535
<servlet-mapping>
36-
<servlet-name>Guestbook</servlet-name>
37-
<url-pattern>/Guestbook</url-pattern>
36+
<servlet-name>ToDoList</servlet-name>
37+
<url-pattern>/ToDoList</url-pattern>
3838
</servlet-mapping>
3939

4040
<welcome-file-list>
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.starter-template {
1+
.home {
22
padding: 40px 15px;
33
text-align: center;
4-
}
4+
}

demo-war/src/main/webapp/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<div class="navbar-collapse" ng-controller="HeaderController">
1313
<ul class="nav navbar-nav navbar-left">
1414
<li ng-class="{active: isActive('/')}"><a href="#">Home</a></li>
15-
<li ng-show="isAuthenticated" ng-class="{active: isActive('/guestbook')}"><a href="#/guestbook">Guestbook</a></li>
15+
<li ng-show="isAuthenticated" ng-class="{active: isActive('/todolist')}"><a href="#/todolist">ToDo List</a></li>
1616
</ul>
1717
<ul class="nav navbar-nav navbar-right">
1818
<li ng-show="isAuthenticated"><a ng-href="{{logoutPath()}}">Logout</a></li>

0 commit comments

Comments
 (0)