/*
Apache2 License Notice
Copyright 2017 Alex Barry

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

http://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 adrestia;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.gridfs.GridFSDBFile;

import java.io.IOException;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
* Rest Controller defining the Asset API.
* Responsible for handling and responding to all Asset API Requests.
*/
@Controller
public class AssetHistoryController {

  // Spring Data Mongo Repository allowing access to standard Mongo operations
  @Autowired
  AssetHistoryRepository assetHistories;

  // Asset Controller Logger
  private static final Logger logger =
      LogManager.getLogger("adrestia.AssetController");

  /**
  * Retrieve an Asset History.
  */
  @GetMapping("/v1/asset-history/{assetId}")
  @ResponseBody
  public ResponseEntity<AssetHistory> getHistory(@PathVariable String assetId) {
    HttpStatus returnCode = HttpStatus.OK;
    logger.info("Responding to Asset History Get Request");
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.set("Content-Type", "application/json");

    List<AssetHistory> existingHistoryList = assetHistories.findByAsset(assetId);
    AssetHistory returnAsset = new AssetHistory();
    // If we have an existing history, return it.
    if (existingHistoryList.size() > 0) {
      returnAsset = existingHistoryList.get(0);
    } else {
      returnCode = HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE;
      logger.debug("No Asset Histories found");
    }

    // Create and return the new HTTP Response
    return new ResponseEntity<AssetHistory>(returnAsset, responseHeaders, returnCode);
  }

}

@@ -2700,32 +2700,37 @@
  ResponseEntity%3C
+List%3C
 AssetHistory%3E ge
@@ -2726,16 +2726,17 @@
 History%3E
+%3E
  getHist
@@ -2775,51 +2775,8 @@
 ) %7B%0A
-    HttpStatus returnCode = HttpStatus.OK;%0A
     
@@ -2827,24 +2827,67 @@
  Request%22);%0A
+    HttpStatus returnCode = HttpStatus.OK;%0A
     HttpHead
@@ -2988,17 +2988,16 @@
 json%22);%0A
-%0A
     List
@@ -3078,205 +3078,44 @@
     
-AssetHistory returnAsset = new AssetHistory();%0A    // If we have an existing history, return it.%0A    if (existingHistoryList.size() %3E 0) %7B%0A      returnAsset = existingHistoryList.get(0);%0A    %7D else
+if (existingHistoryList.size() == 0)
  %7B%0A 
@@ -3308,16 +3308,21 @@
 eEntity%3C
+List%3C
 AssetHis
@@ -3330,19 +3330,28 @@
 ory%3E
-(returnAsse
+%3E(existingHistoryLis
 t, r

Return all histories associated with an asset, rather than only the first