// 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.speech.tools.voxetta.services;

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.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.common.annotations.VisibleForTesting; 
import com.google.speech.tools.voxetta.data.Utterance; 
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

/** 
 * Functionality for uploading audio files to Blobstore & uploading the related Utterance
 * object to Datastore.  
 */
public class DatastoreUtteranceService implements UtteranceService {
  
  private DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
  private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

  @Override 
  public void saveUtterance(Utterance utterance) {
    // Create and initialize a new Utterance Entity
    Entity utteranceEntity = new Entity("Utterance");
    utteranceEntity.setProperty("audioKey", utterance.getAudio());
    utteranceEntity.setProperty("userId", utterance.getUserId());
    utteranceEntity.setProperty("promptId", utterance.getPromptId());
    utteranceEntity.setProperty("device", utterance.getDevice());
    utteranceEntity.setProperty("age", utterance.getAge());
    utteranceEntity.setProperty("gender", utterance.getGender());
    
    // Store the Utterance Entity in Datastore 
    datastoreService.put(utteranceEntity); 
  }
  
  @Override
  public String getAudioBlob(HttpServletRequest request) {
    Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(request);
    List<BlobKey> blobKeys = blobs.get("audio");

    // Return null if an audio file was not uploaded
    if (blobKeys == null || blobKeys.isEmpty()) {
      return null; 
    }
 
    // Return the BlobKey of the uploaded file
    return blobKeys.get(0).getKeyString();
  }

  @Override
  public String getAudioBlobUploadUrl() {
    return blobstoreService.createUploadUrl("/upload-utterance"); 
  }

  /** 
   * Allow the servlet's Datastore service to be set for mocking purposes.
   */
  @VisibleForTesting
  public void setDatastoreService(DatastoreService inputService) {
    datastoreService = inputService; 
  }

  /** 
   * Allow the servlet's Blobstore service to be set for mocking purposes.
   */
  @VisibleForTesting
  public void setBlobstoreService(BlobstoreService inputService) {
    blobstoreService = inputService; 
  }
}

@@ -2982,32 +2982,108 @@
 cking purposes.%0A
+   *%0A   * @param inputService The service to serve as the DatastoreService.%0A
    */%0A  @Visible
@@ -3281,24 +3281,100 @@
 g purposes.%0A
+   *%0A   * @param inputService The service to serve as the BlobstoreService.%0A
    */%0A  @Vis

add @param documentation to testing functions