diff --git a/src/main/java/shef/data/Ingredient.java b/src/main/java/shef/data/Ingredient.java index 5c1dc37..adfdff6 100644 --- a/src/main/java/shef/data/Ingredient.java +++ b/src/main/java/shef/data/Ingredient.java @@ -21,7 +21,7 @@ public class Ingredient { public Ingredient() { } - + public Ingredient(EmbeddedEntity entity) { } diff --git a/src/main/java/shef/data/Recipe.java b/src/main/java/shef/data/Recipe.java index 2e61060..93bd4dc 100644 --- a/src/main/java/shef/data/Recipe.java +++ b/src/main/java/shef/data/Recipe.java @@ -32,8 +32,12 @@ public class Recipe { private String key; private String name; private String description; + private double time; + private double servings; + private String imageKey; private Set tags; private Set ingredients; + private Set equipment; private List steps; private Set spinOffs; private long timestamp; @@ -66,8 +70,12 @@ public Recipe(String name, String description, Set tags, Set ing public Recipe(Entity recipeEntity) { this.name = (String) recipeEntity.getProperty("name"); this.description = (String) recipeEntity.getProperty("description"); + this.time = (double) recipeEntity.getProperty("time"); + this.servings = (double) recipeEntity.getProperty("servings"); + this.imageKey = (String) recipeEntity.getProperty("imageKey"); this.tags = getTagsFromEntity((Collection) recipeEntity.getProperty("tags")); this.ingredients = getIngredientsFromEntity((Collection) recipeEntity.getProperty("ingredients")); + this.equipment = getEquipmentFromEntity((Collection) recipeEntity.getProperty("equipment")); this.steps = getStepsFromEntity((Collection) recipeEntity.getProperty("steps")); this.timestamp = (long) recipeEntity.getProperty("timestamp"); } @@ -252,6 +260,15 @@ private Set getIngredientsFromEntity(Collection entityIn return ingredientsSet; } + /** Returns the equipment of an EmbeddedEntity as a Set. */ + private Set getEquipmentFromEntity(Collection entityEquipment) { + Set equipmentSet = new HashSet<>(); + for (EmbeddedEntity equipment : entityEquipment) { + equipmentSet.add((String) equipment.getProperty("equipment")); + } + return equipmentSet; + } + /** Returns the steps of an EmbeddedEntity as a List. */ private List getStepsFromEntity(Collection entitySteps) { List stepsList = new LinkedList<>(); diff --git a/src/main/java/shef/servlets/BlobServlet.java b/src/main/java/shef/servlets/BlobServlet.java index 32167f0..c5c4ff2 100644 --- a/src/main/java/shef/servlets/BlobServlet.java +++ b/src/main/java/shef/servlets/BlobServlet.java @@ -17,11 +17,15 @@ import com.google.appengine.api.blobstore.BlobKey; import com.google.appengine.api.blobstore.BlobstoreService; import com.google.appengine.api.blobstore.BlobstoreServiceFactory; +import com.google.appengine.api.blobstore.BlobInfo; +import com.google.appengine.api.blobstore.BlobInfoFactory; import java.io.IOException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.util.List; +import java.util.Map; /** Servlet that serves blobstore files. */ @WebServlet("/blob") @@ -33,4 +37,30 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro BlobKey blobKey = new BlobKey(request.getParameter("blob-key")); blobstoreService.serve(blobKey, response); } + + /* + * Returns the String representation of the BlobKey of the uploaded file, or null if the user didn't upload a file. + * Used in alternative to getting the blob's direct URL, which causes crashes on the live server. + */ + public static String getUploadedFileBlobKey(HttpServletRequest request, String formInputElementName) { + BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); + Map> blobs = blobstoreService.getUploads(request); + List blobKeys = blobs.get(formInputElementName); + + // User submitted form without selecting a file, so we can't get a URL. (dev server) + if (blobKeys == null || blobKeys.isEmpty()) { + return null; + } + + // Our form only contains a single file input, so get the first index. + BlobKey blobKey = blobKeys.get(0); + + // User submitted form without selecting a file, so we can't get a URL. (live server) + BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey); + if (blobInfo.getSize() == 0) { + blobstoreService.delete(blobKey); + return null; + } + return blobKey.getKeyString(); + } } \ No newline at end of file diff --git a/src/main/java/shef/servlets/NewRecipeServlet.java b/src/main/java/shef/servlets/NewRecipeServlet.java index ee43642..187aeaa 100644 --- a/src/main/java/shef/servlets/NewRecipeServlet.java +++ b/src/main/java/shef/servlets/NewRecipeServlet.java @@ -42,6 +42,7 @@ public class NewRecipeServlet extends HttpServlet { private DatastoreService datastore; private final String TAG = "tag"; private final String INGREDIENT = "ingredient"; + private final String EQUIPMENT = "equipment"; private final String STEP = "step"; @Override @@ -71,9 +72,13 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr Collection searchStrings = new HashSet<>(); String name = request.getParameter("name"); searchStrings.add(name.toUpperCase()); + double time = Double.parseDouble(request.getParameter("time")); + double servings = Double.parseDouble(request.getParameter("servings")); + String imageKey = BlobServlet.getUploadedFileBlobKey(request, "image"); String description = request.getParameter("description"); Collection tags = getParameters(request, TAG, searchStrings); Collection ingredients = getParameters(request, INGREDIENT, searchStrings); + Collection equipment = getParameters(request, EQUIPMENT, null); Collection steps = getParameters(request, STEP, null); int likes; try { @@ -85,9 +90,13 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr Entity recipe = new Entity("Recipe"); recipe.setProperty("name", name); + recipe.setProperty("time", time); + recipe.setProperty("servings", servings); + recipe.setProperty("imageKey", imageKey); recipe.setProperty("description", description); recipe.setProperty("tags", tags); recipe.setProperty("ingredients", ingredients); + recipe.setProperty("equipment", equipment); recipe.setProperty("steps", steps); recipe.setProperty("search-strings", new ArrayList(searchStrings)); recipe.setProperty("timestamp", timestamp); diff --git a/src/main/java/shef/servlets/RecipeImageUploadUrlServlet.java b/src/main/java/shef/servlets/RecipeImageUploadUrlServlet.java new file mode 100644 index 0000000..57daba7 --- /dev/null +++ b/src/main/java/shef/servlets/RecipeImageUploadUrlServlet.java @@ -0,0 +1,45 @@ +// +// 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 +// +// https://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 shef.servlets; + +import com.google.appengine.api.blobstore.BlobInfo; +import com.google.appengine.api.blobstore.BlobInfoFactory; +import com.google.appengine.api.blobstore.BlobKey; +import com.google.appengine.api.blobstore.BlobstoreService; +import com.google.appengine.api.blobstore.BlobstoreServiceFactory; +import com.google.appengine.api.images.ImagesService; +import com.google.appengine.api.images.ImagesServiceFactory; +import com.google.appengine.api.images.ServingUrlOptions; +import java.net.MalformedURLException; +import java.net.URL; +import java.io.IOException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Map; +import java.util.List; + +/** Gets the Blobstore URL to send the recipe creation form to. */ +@WebServlet("/recipe-image-upload-url") +public class RecipeImageUploadUrlServlet extends HttpServlet { + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); + String uploadUrl = blobstoreService.createUploadUrl("/new-recipe"); + response.setContentType("text/html"); + response.getWriter().println(uploadUrl); + } +} diff --git a/src/main/java/shef/servlets/TestDisplayRecipesServlet.java b/src/main/java/shef/servlets/TestDisplayRecipesServlet.java deleted file mode 100644 index fcb76b9..0000000 --- a/src/main/java/shef/servlets/TestDisplayRecipesServlet.java +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright 2019 Google LLC -// -// 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 -// -// https://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 shef.servlets; - -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import shef.data.TestRecipe; -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.SortDirection; -import com.google.gson.Gson; -import java.io.IOException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -/** Servlet responsible for displaying recipes. */ -@WebServlet("/display-recipes") -public class TestDisplayRecipesServlet extends HttpServlet { - - @Override - public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { - Query query = new Query("Recipe").addSort("timestamp", SortDirection.DESCENDING); - - DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); - PreparedQuery results = datastore.prepare(query); - - List testRecipes = new LinkedList<>(); - for (Entity entity : results.asIterable()) { - long id = entity.getKey().getId(); - ArrayList searchStrings = (ArrayList) entity.getProperty("search-strings"); - long timestamp = (long) entity.getProperty("timestamp"); - - TestRecipe testRecipe = new TestRecipe(id, searchStrings, timestamp); - testRecipes.add(testRecipe); - } - - Gson gson = new Gson(); - - response.setContentType("application/json;"); - response.getWriter().println(gson.toJson(testRecipes)); - } -} diff --git a/src/main/java/shef/servlets/UserServlet.java b/src/main/java/shef/servlets/UserServlet.java index b40c44d..86a16d9 100644 --- a/src/main/java/shef/servlets/UserServlet.java +++ b/src/main/java/shef/servlets/UserServlet.java @@ -92,7 +92,7 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr // Get properties that don't come from the request. String id = userService.getCurrentUser().getUserId(); String email = userService.getCurrentUser().getEmail(); - String profilePicKey = getUploadedFileBlobKey(request, "profile-pic"); + String profilePicKey = BlobServlet.getUploadedFileBlobKey(request, "profile-pic"); // Get properties from the request. Map parameterMap = request.getParameterMap(); @@ -130,28 +130,4 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr datastore.put(finalUser); response.sendRedirect(redirectUrl); } - - // Returns the String representation of the BlobKey of the uploaded file, or null if the user didn't upload a file. - private String getUploadedFileBlobKey(HttpServletRequest request, String formInputElementName) { - BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); - Map> blobs = blobstoreService.getUploads(request); - List blobKeys = blobs.get(formInputElementName); - - // User submitted form without selecting a file, so we can't get a URL. (dev server) - if (blobKeys == null || blobKeys.isEmpty()) { - return null; - } - - // Our form only contains a single file input, so get the first index. - BlobKey blobKey = blobKeys.get(0); - - // User submitted form without selecting a file, so we can't get a URL. (live server) - BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey); - if (blobInfo.getSize() == 0) { - blobstoreService.delete(blobKey); - return null; - } - - return blobKey.getKeyString(); - } } diff --git a/src/main/webapp/create-recipe-blank.html b/src/main/webapp/create-recipe-blank.html index 4de8237..efa8091 100644 --- a/src/main/webapp/create-recipe-blank.html +++ b/src/main/webapp/create-recipe-blank.html @@ -36,7 +36,7 @@ shef - +