|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.appengine; |
| 18 | + |
| 19 | +import com.google.appengine.api.datastore.DatastoreService; |
| 20 | +import com.google.appengine.api.datastore.DatastoreServiceFactory; |
| 21 | +import com.google.appengine.api.datastore.Entity; |
| 22 | +import com.google.appengine.api.datastore.EntityNotFoundException; |
| 23 | +import com.google.appengine.api.datastore.Key; |
| 24 | +import com.google.appengine.api.datastore.KeyFactory; |
| 25 | +import com.google.common.collect.ImmutableList; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | + |
| 29 | +import javax.servlet.ServletException; |
| 30 | +import javax.servlet.http.HttpServlet; |
| 31 | +import javax.servlet.http.HttpServletRequest; |
| 32 | +import javax.servlet.http.HttpServletResponse; |
| 33 | + |
| 34 | +/** |
| 35 | + * A startup handler to populate the datastore with example entities. |
| 36 | + */ |
| 37 | +public class StartupServlet extends HttpServlet { |
| 38 | + static final String IS_POPULATED_ENTITY = "IsPopulated"; |
| 39 | + static final String IS_POPULATED_KEY_NAME = "is-populated"; |
| 40 | + |
| 41 | + private static final String PERSON_ENTITY = "Person"; |
| 42 | + private static final String NAME_PROPERTY = "name"; |
| 43 | + private static final ImmutableList<String> US_PRESIDENTS = |
| 44 | + ImmutableList.<String>builder() |
| 45 | + .add("George Washington") |
| 46 | + .add("John Adams") |
| 47 | + .add("Thomas Jefferson") |
| 48 | + .add("James Madison") |
| 49 | + .add("James Monroe") |
| 50 | + .add("John Quincy Adams") |
| 51 | + .add("Andrew Jackson") |
| 52 | + .add("Martin Van Buren") |
| 53 | + .add("William Henry Harrison") |
| 54 | + .add("John Tyler") |
| 55 | + .add("James K. Polk") |
| 56 | + .add("Zachary Taylor") |
| 57 | + .add("Millard Fillmore") |
| 58 | + .add("Franklin Pierce") |
| 59 | + .add("James Buchanan") |
| 60 | + .add("Abraham Lincoln") |
| 61 | + .add("Andrew Johnson") |
| 62 | + .add("Ulysses S. Grant") |
| 63 | + .add("Rutherford B. Hayes") |
| 64 | + .add("James A. Garfield") |
| 65 | + .add("Chester A. Arthur") |
| 66 | + .add("Grover Cleveland") |
| 67 | + .add("Benjamin Harrison") |
| 68 | + .add("Grover Cleveland") |
| 69 | + .add("William McKinley") |
| 70 | + .add("Theodore Roosevelt") |
| 71 | + .add("William Howard Taft") |
| 72 | + .add("Woodrow Wilson") |
| 73 | + .add("Warren G. Harding") |
| 74 | + .add("Calvin Coolidge") |
| 75 | + .add("Herbert Hoover") |
| 76 | + .add("Franklin D. Roosevelt") |
| 77 | + .add("Harry S. Truman") |
| 78 | + .add("Dwight D. Eisenhower") |
| 79 | + .add("John F. Kennedy") |
| 80 | + .add("Lyndon B. Johnson") |
| 81 | + .add("Richard Nixon") |
| 82 | + .add("Gerald Ford") |
| 83 | + .add("Jimmy Carter") |
| 84 | + .add("Ronald Reagan") |
| 85 | + .add("George H. W. Bush") |
| 86 | + .add("Bill Clinton") |
| 87 | + .add("George W. Bush") |
| 88 | + .add("Barack Obama") |
| 89 | + .build(); |
| 90 | + |
| 91 | + @Override |
| 92 | + protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
| 93 | + throws ServletException, IOException { |
| 94 | + resp.setContentType("text/plain"); |
| 95 | + DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); |
| 96 | + |
| 97 | + Key isPopulatedKey = KeyFactory.createKey(IS_POPULATED_ENTITY, IS_POPULATED_KEY_NAME); |
| 98 | + boolean isAlreadyPopulated; |
| 99 | + try { |
| 100 | + datastore.get(isPopulatedKey); |
| 101 | + isAlreadyPopulated = true; |
| 102 | + } catch (EntityNotFoundException expected) { |
| 103 | + isAlreadyPopulated = false; |
| 104 | + } |
| 105 | + if (isAlreadyPopulated) { |
| 106 | + resp.getWriter().println("ok"); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + ImmutableList.Builder<Entity> people = ImmutableList.builder(); |
| 111 | + for (String name : US_PRESIDENTS) { |
| 112 | + Entity person = new Entity(PERSON_ENTITY); |
| 113 | + person.setProperty(NAME_PROPERTY, name); |
| 114 | + people.add(person); |
| 115 | + } |
| 116 | + datastore.put(people.build()); |
| 117 | + datastore.put(new Entity(isPopulatedKey)); |
| 118 | + resp.getWriter().println("ok"); |
| 119 | + } |
| 120 | +} |
0 commit comments