// Copyright 2020 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 com.google.sps.wikiimport;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Entity;
import com.google.gson.Gson;
import com.google.sps.data.Meal;
import com.google.sps.data.DataConverter;
import java.io.IOException;  
import org.apache.commons.io.IOUtils;
import java.net.URLDecoder;  
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/import")
public class WikiImportServlet extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String json = IOUtils.toString(request.getReader());
        try {
            json = URLDecoder.decode(json);
        } catch (Exception e) {  
            System.out.println("Issue while decoding" +e.getMessage());
            return;  
        } 
        Meal meal = new Gson().fromJson(json, Meal.class);
        System.out.println("meal " + meal.getTitle());

        // Put new Meal Entity to Datastore.
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        datastore.put(DataConverter.createMealEntity(meal));

        response.setContentType("application/json;");
        response.getWriter().print(json);
    }
}
@@ -737,32 +737,82 @@
 ServiceFactory;%0A
+import com.google.appengine.api.datastore.Entity;%0A
 import com.googl
@@ -942,41 +942,204 @@
 ore.
-Entity;%0Aimport com.google.gson.Gs
+PreparedQuery;%0Aimport com.google.appengine.api.datastore.Query;%0Aimport com.google.appengine.api.datastore.Query.SortDirection;%0Aimport com.google.gson.Gson;%0Aimport com.google.gson.JsonSyntaxExcepti
 on;%0A
@@ -1410,20 +1410,80 @@
 va.util.
-List
+Iterator;%0Aimport java.util.List;%0Aimport java.util.logging.Logger
 ;%0Aimport
@@ -1766,16 +1766,111 @@
 vlet %7B%0A%0A
+    private static final Logger logger = Logger.getLogger(WikiImportServlet.class.getName());%0A%0A
     @Ove
@@ -2068,179 +2068,38 @@
     
-json = URLDecoder.decode(json);%0A        %7D catch (Exception e) %7B  %0A            System.out.println(%22Issue while decoding%22 +e.getMessage());%0A            return;  %0A        %7D %0A
+System.out.println(json);%0A    
     
@@ -2165,32 +2165,44 @@
     
-System.out.println(%22meal
+    logger.info(%22Adding meal titled:
  %22 +
@@ -2217,24 +2217,28 @@
 tTitle());%0A%0A
+    
         // P
@@ -2270,16 +2270,20 @@
 astore.%0A
+    
         
@@ -2370,157 +2370,869 @@
     
-datastore.put(DataConverter.createMealEntity(meal));%0A%0A        response.setContentType(%22application/json;%22);%0A        response.getWriter().print(json);
+    Query query = new Query(%22Meal%22).addSort(%22id%22, SortDirection.DESCENDING);%0A            PreparedQuery entities = datastore.prepare(query);%0A            Long id = 0L;%0A            Iterator%3CEntity%3E iterator = entities.asIterator();%0A            if (iterator.hasNext()) %7B%0A                Entity entity = iterator.next();%0A                id = (Long)entity.getProperty(%22id%22) + 1;%0A            %7D%0A            datastore.put(DataConverter.createMealEntity(%0A                new Meal(%0A                    id,%0A                    meal.getTitle(),%0A                    meal.getDescription(),%0A                    meal.getIngredients(),%0A                    meal.getType()%0A                )%0A            ));%0A        %7D catch (JsonSyntaxException e) %7B%0A            System.out.println(e);%0A            response.sendError(HttpServletResponse.SC_BAD_REQUEST);%0A            return;%0A        %7D
 %0A   

Update WikiImportServlet