Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standard guestbook using Cloud Datastore API rather than Objectify #388

Merged
merged 3 commits into from
Oct 27, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Style fixes and corrections.
  • Loading branch information
wsh committed Oct 27, 2016
commit 02a8173d423084a6e334f9d30dd2931cab8bd0a1
4 changes: 2 additions & 2 deletions appengine/guestbook-cloud-datastore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ Then start the [Cloud Datastore Emulator](https://cloud.google.com/datastore/doc
Finally, in a new shell, [set the Datastore Emulator environmental variables](https://cloud.google.com/datastore/docs/tools/datastore-emulator#setting_environment_variables)
and run

mvn clean appengine:devserver
mvn clean appengine:run

## Deploying

Modify `appengine-web.xml` to reflect your app ID and version, then:

mvn clean appengine:update
mvn clean appengine:deploy
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* 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
* <p>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
* <p>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
* <p>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.
*/

Expand All @@ -25,14 +23,15 @@
import com.google.cloud.datastore.FullEntity.Builder;
import com.google.cloud.datastore.IncompleteKey;
import com.google.cloud.datastore.Key;
import com.google.common.base.MoreObjects;

import java.util.Date;
import java.util.Objects;

public class Greeting {
private Guestbook book;

public Key key;

public String authorEmail;
public String authorId;
public String content;
Expand All @@ -54,11 +53,6 @@ public Greeting(String book, String content, String id, String email) {
authorId = id;
}

/**
* Load greeting from Datastore entity
*
* @param entity
*/
public Greeting(Entity entity) {
key = entity.hasKey() ? entity.key() : null;
authorEmail = entity.contains("authorEmail") ? entity.getString("authorEmail") : null;
Expand Down Expand Up @@ -102,16 +96,28 @@ public boolean equals(Object o) {
return false;
}
Greeting greeting = (Greeting) o;
return Objects.equals(key, greeting.key) &&
Objects.equals(authorEmail, greeting.authorEmail) &&
Objects.equals(authorId, greeting.authorId) &&
Objects.equals(content, greeting.content) &&
Objects.equals(date, greeting.date);
return Objects.equals(key, greeting.key)
&& Objects.equals(authorEmail, greeting.authorEmail)
&& Objects.equals(authorId, greeting.authorId)
&& Objects.equals(content, greeting.content)
&& Objects.equals(date, greeting.date);
}

@Override
public int hashCode() {
return Objects.hash(key, authorEmail, authorId, content, date);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("key", key)
.add("authorEmail", authorEmail)
.add("authorId", authorId)
.add("content", content)
.add("date", date)
.add("book", book)
.toString();
}
}
//[END all]
//[END all]
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* 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
* <p>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
* <p>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
* <p>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.example.guestbook;

import static com.example.guestbook.Persistence.getDatastore;
Expand All @@ -27,20 +24,25 @@
import com.google.cloud.datastore.KeyFactory;
import com.google.cloud.datastore.Query;
import com.google.cloud.datastore.QueryResults;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;

import java.util.List;
import java.util.Objects;

//[START all]
public class Guestbook {
private static final KeyFactory kf = getKeyFactory(Guestbook.class);

private static final KeyFactory keyFactory = getKeyFactory(Guestbook.class);
private final Key key;

public final String book;

public Guestbook(String book) {
this.book = book == null ? "default" : book;
key = kf.newKey(this.book); // There is a 1:1 mapping between Guestbook names and Guestbook objects
key =
keyFactory.newKey(
this.book); // There is a 1:1 mapping between Guestbook names and Guestbook objects
}

public Key getKey() {
Expand All @@ -49,12 +51,13 @@ public Key getKey() {

public List<Greeting> getGreetings() {
// This query requires the index defined in index.yaml to work because of the orderBy on date.
EntityQuery query = Query.entityQueryBuilder()
.kind("Greeting")
.filter(hasAncestor(key))
.orderBy(desc("date"))
.limit(5)
.build();
EntityQuery query =
Query.entityQueryBuilder()
.kind("Greeting")
.filter(hasAncestor(key))
.orderBy(desc("date"))
.limit(5)
.build();

QueryResults<Entity> results = getDatastore().run(query);

Expand All @@ -65,5 +68,31 @@ public List<Greeting> getGreetings() {

return resultListBuilder.build();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Guestbook guestbook = (Guestbook) o;
return Objects.equals(book, guestbook.book) && Objects.equals(key, guestbook.key);
}

@Override
public int hashCode() {
return Objects.hash(book, key);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("keyFactory", keyFactory)
.add("book", book)
.add("key", key)
.toString();
}
}
//[END all]
//[END all]
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* 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
* <p>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
* <p>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
* <p>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.example.guestbook;

import com.google.cloud.datastore.Datastore;
Expand All @@ -34,12 +31,12 @@ public static Datastore getDatastore() {
return datastore.get();
}

public static KeyFactory getKeyFactory(Class<?> c) {
return getDatastore().newKeyFactory().kind(c.getSimpleName());
}

public static void setDatastore(Datastore datastore) {
Persistence.datastore.set(datastore);
}

public static KeyFactory getKeyFactory(Class<?> c) {
return getDatastore().newKeyFactory().kind(c.getSimpleName());
}
}
//[END all]
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* 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
* <p>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
* <p>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
* <p>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.
*/

Expand All @@ -20,6 +18,7 @@
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -29,12 +28,11 @@
public class SignGuestbookServlet extends HttpServlet {
// Process the HTTP POST of the form
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
Greeting greeting;

UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser(); // Find out who the user is.
User user = userService.getCurrentUser(); // Find out who the user is.

String guestbookName = req.getParameter("guestbookName");
String content = req.getParameter("content");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>your-app-id-here</application>
<version>your-app-version-here</version>
<threadsafe>true</threadsafe>

<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
</system-properties>
<threadsafe>true</threadsafe>

<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
</system-properties>
</appengine-web-app>
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ indexes:
ancestor: yes
properties:
- name: date
direction: desc
direction: desc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
# <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
# </system-properties>
#

# Set the default logging level for all loggers to WARNING
.level = WARNING
.level=WARNING
34 changes: 17 additions & 17 deletions appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- [START standard_mappings] -->
<servlet>
<servlet-name>sign</servlet-name>
<servlet-class>com.example.guestbook.SignGuestbookServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- [START standard_mappings] -->
<servlet>
<servlet-name>sign</servlet-name>
<servlet-class>com.example.guestbook.SignGuestbookServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>sign</servlet-name>
<url-pattern>/sign</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>sign</servlet-name>
<url-pattern>/sign</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>guestbook.jsp</welcome-file>
</welcome-file-list>
<!-- [END standard_mappings] -->
<welcome-file-list>
<welcome-file>guestbook.jsp</welcome-file>
</welcome-file-list>
<!-- [END standard_mappings] -->
</web-app>
Loading