@@ -0,0 +1,3611 @@
+// Copyright 2019 Google LLC%0A//%0A// Licensed under the Apache License, Version 2.0 (the %22License%22);%0A// you may not use this file except in compliance with the License.%0A// You may obtain a copy of the License at%0A//%0A//     https://www.apache.org/licenses/LICENSE-2.0%0A//%0A// Unless required by applicable law or agreed to in writing, software%0A// distributed under the License is distributed on an %22AS IS%22 BASIS,%0A// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A// See the License for the specific language governing permissions and%0A// limitations under the License.%0A%0Apackage com.google.sps.servlets;%0A%0Aimport com.google.appengine.api.blobstore.BlobstoreService;%0Aimport com.google.appengine.api.blobstore.BlobstoreServiceFactory;%0Aimport com.google.appengine.api.datastore.DatastoreService;%0Aimport com.google.appengine.api.datastore.DatastoreServiceFactory;%0Aimport com.google.appengine.api.datastore.Entity;%0Aimport com.google.appengine.api.datastore.Key;%0Aimport com.google.appengine.api.datastore.KeyFactory;%0Aimport com.google.appengine.api.users.UserService;%0Aimport com.google.appengine.api.users.UserServiceFactory;%0Aimport com.google.sps.data.HttpServletUtils;%0Aimport com.google.sps.data.Vendor;%0Aimport java.io.IOException;%0Aimport javax.servlet.annotation.WebServlet;%0Aimport javax.servlet.http.HttpServlet;%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0A%0A/**%0A * Delete a vendor's business with all the information related to it%0A */%0A@WebServlet(%22/delete-salecard%22)%0Apublic class DeleteSaleCardServlet extends HttpServlet %7B%0A%0A  @Override%0A  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException %7B%0A    UserService userService = UserServiceFactory.getUserService();%0A    %0A    if (!userService.isUserLoggedIn()) %7B%0A      System.out.println(%22User is not logged in.%22);%0A      response.sendError(HttpServletResponse.SC_UNAUTHORIZED, %22User is not logged in.%22);%0A      return;%0A    %7D%0A%0A    String vendorId = userService.getCurrentUser().getUserId();%0A    Key vendorKey = KeyFactory.createKey(%22Vendor%22, vendorId);%0A    Entity vendorEntity = HttpServletUtils.getVendorEntity(vendorId);%0A    if (vendorEntity == null) %7B%0A      System.out.println(%22User is not registered.%22);%0A      response.sendError(HttpServletResponse.SC_UNAUTHORIZED, %22User is not registered.%22);%0A      return;%0A    %7D%0A    Vendor vendorObject = new Vendor(vendorEntity);%0A%0A    if (vendorObject.getSaleCard() == null) %7B%0A      System.out.println(%22Vendor does not have a salecard.%22);%0A      response.sendError(HttpServletResponse.SC_BAD_REQUEST, %22Vendor does not have a salecard.%22);%0A      return;%0A    %7D%0A%0A    // Delete salecard's picture from blobstore%0A    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();%0A    blobstoreService.delete(vendorObject.getSaleCard().getPicture().getBlobKey());%0A%0A    // Generate Keys to delete from datastore%0A    Key saleCardKey = KeyFactory.createKey(vendorKey, %22SaleCard%22, vendorObject.getSaleCard().getId());%0A    Key pictureKey = KeyFactory.createKey(saleCardKey, %22Picture%22, vendorObject.getSaleCard().getPicture().getId());%0A    Key locationKey = KeyFactory.createKey(saleCardKey, %22LocationData%22, vendorObject.getSaleCard().getLocation().getId());%0A%0A    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();%0A    datastore.delete(locationKey);    %0A    datastore.delete(pictureKey);%0A    datastore.delete(saleCardKey);%0A%0A    // Update vendor's embedded salecard%0A    vendorEntity.setProperty(%22saleCard%22, null);%0A    datastore.put(vendorEntity);%0A      %0A    response.sendRedirect(%22/%22);%0A  %7D%0A%7D%0A

Add delete salecard servlet