diff --git a/appengine/guestbook-cloud-datastore/README.md b/appengine/guestbook-cloud-datastore/README.md index 94b3edf3de9..1c6d6957c8a 100644 --- a/appengine/guestbook-cloud-datastore/README.md +++ b/appengine/guestbook-cloud-datastore/README.md @@ -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 diff --git a/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Greeting.java b/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Greeting.java index 988118174c0..12eb3d6c6de 100644 --- a/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Greeting.java +++ b/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Greeting.java @@ -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 + *

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 + *

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 + *

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. */ @@ -25,6 +23,8 @@ 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; @@ -32,7 +32,6 @@ public class Greeting { private Guestbook book; public Key key; - public String authorEmail; public String authorId; public String content; @@ -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; @@ -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] \ No newline at end of file +//[END all] diff --git a/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Guestbook.java b/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Guestbook.java index 694010ab5c5..ae407175ecb 100644 --- a/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Guestbook.java +++ b/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Guestbook.java @@ -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 + *

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 + *

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 + *

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; @@ -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() { @@ -49,12 +51,13 @@ public Key getKey() { public List 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 results = getDatastore().run(query); @@ -65,5 +68,31 @@ public List 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] \ No newline at end of file +//[END all] diff --git a/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Persistence.java b/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Persistence.java index 3aee65c89cc..3dbc2285743 100644 --- a/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Persistence.java +++ b/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Persistence.java @@ -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 + *

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 + *

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 + *

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; @@ -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] diff --git a/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/SignGuestbookServlet.java b/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/SignGuestbookServlet.java index eec1a34c2a8..8d6061a7146 100644 --- a/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/SignGuestbookServlet.java +++ b/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/SignGuestbookServlet.java @@ -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 + *

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 + *

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 + *

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. */ @@ -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; @@ -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"); diff --git a/appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/appengine-web.xml b/appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/appengine-web.xml index 14ea1140aa7..bdcb11ebd86 100644 --- a/appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/appengine-web.xml +++ b/appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/appengine-web.xml @@ -1,10 +1,8 @@ - your-app-id-here - your-app-version-here - true - - - - + true + + + + diff --git a/appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/index.yaml b/appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/index.yaml index 03f9df1f728..e9beac04f79 100644 --- a/appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/index.yaml +++ b/appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/index.yaml @@ -4,4 +4,4 @@ indexes: ancestor: yes properties: - name: date - direction: desc \ No newline at end of file + direction: desc diff --git a/appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/logging.properties b/appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/logging.properties index a17206681f0..c2a1d42755d 100644 --- a/appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/logging.properties +++ b/appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/logging.properties @@ -8,6 +8,5 @@ # # # - # Set the default logging level for all loggers to WARNING -.level = WARNING +.level=WARNING diff --git a/appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/web.xml b/appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/web.xml index 1ffb6bb5853..110a5c42fbc 100644 --- a/appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/web.xml +++ b/appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/web.xml @@ -1,22 +1,22 @@ - + - - - sign - com.example.guestbook.SignGuestbookServlet - 1 - + + + sign + com.example.guestbook.SignGuestbookServlet + 1 + - - sign - /sign - + + sign + /sign + - - guestbook.jsp - - + + guestbook.jsp + + diff --git a/appengine/guestbook-cloud-datastore/src/test/java/com/example/guestbook/GreetingTest.java b/appengine/guestbook-cloud-datastore/src/test/java/com/example/guestbook/GreetingTest.java index 8b3eed2451d..6ddaa732d37 100644 --- a/appengine/guestbook-cloud-datastore/src/test/java/com/example/guestbook/GreetingTest.java +++ b/appengine/guestbook-cloud-datastore/src/test/java/com/example/guestbook/GreetingTest.java @@ -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 + *

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 + *

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 + *

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; @@ -18,16 +16,17 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import java.util.List; - import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; +import java.util.List; + @RunWith(JUnit4.class) public class GreetingTest { + @Before public void setUp() { TestUtils.startDatastore(); diff --git a/appengine/guestbook-cloud-datastore/src/test/java/com/example/guestbook/SignGuestbookServletTest.java b/appengine/guestbook-cloud-datastore/src/test/java/com/example/guestbook/SignGuestbookServletTest.java index 03aaa73bff6..243fca2a858 100644 --- a/appengine/guestbook-cloud-datastore/src/test/java/com/example/guestbook/SignGuestbookServletTest.java +++ b/appengine/guestbook-cloud-datastore/src/test/java/com/example/guestbook/SignGuestbookServletTest.java @@ -1,29 +1,22 @@ /** * 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 + *

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 + *

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 + *

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 org.junit.Assert.assertTrue; import static org.mockito.Mockito.when; import com.google.appengine.tools.development.testing.LocalServiceTestHelper; -import java.util.List; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -32,6 +25,10 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import java.util.List; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + @RunWith(JUnit4.class) public class SignGuestbookServletTest { private final LocalServiceTestHelper helper = new LocalServiceTestHelper();