/*
 * Copyright (C) 2015 NS Solutions Corporation, All Rights Reserved.
 */
package com.htmlhifive.pitalium.explorer.api;

import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
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 com.htmlhifive.pitalium.explorer.entity.Screenshot;
import com.htmlhifive.pitalium.explorer.entity.TestExecutionAndEnvironment;
import com.htmlhifive.pitalium.explorer.response.TestExecutionResult;
import com.htmlhifive.pitalium.explorer.service.ExplorerService;

@Controller
@RequestMapping("/api")
public class ApiController {

	@Autowired
	private ExplorerService service;

	@PostConstruct
	public void init() {
		service.init();
	}

	/**
	 * Gets list of the test execution. If pageSize equals to zero, the default page size is used. If pageSize equals to
	 * -1, the entire list is returned.
	 * 
	 * @param page Which page to show.
	 * @param pageSize Page size.
	 * @return Page of test execution
	 */
	@RequestMapping(value = "/listTestExecution", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
	@ResponseBody
	public ResponseEntity<Page<TestExecutionResult>> listTestExecution(@RequestParam(defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "0") int pageSize,
			@RequestParam(defaultValue = "") String searchTestMethod,
			@RequestParam(defaultValue = "") String searchTestScreen) {
		Page<TestExecutionResult> list = service.findTestExecution(searchTestMethod, searchTestScreen, page, pageSize);
		return new ResponseEntity<Page<TestExecutionResult>>(list, HttpStatus.OK);
	}

	/**
	 * Gets list of the screenshots which is narrowed down by a test execution.
	 *
	 * @param executionId test execution id
	 * @return list of screenshots
	 */
	@RequestMapping(value = "/listScreenshot", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
	@ResponseBody
	public ResponseEntity<List<Screenshot>> listScreenshot(@RequestParam Integer testExecutionId,
			@RequestParam(defaultValue = "") String searchTestMethod,
			@RequestParam(defaultValue = "") String searchTestScreen) {
		List<Screenshot> list = service.findScreenshot(testExecutionId, searchTestMethod, searchTestScreen);
		return new ResponseEntity<List<Screenshot>>(list, HttpStatus.OK);
	}

	/**
	 * Gets the screenshot from screenshot id.
	 *
	 * @param screenshotId screenshot id
	 * @return screenshot
	 */
	@RequestMapping(value = "/getScreenshot", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
	@ResponseBody
	public ResponseEntity<Screenshot> getDetail(@RequestParam Integer screenshotId) {
		Screenshot item = service.getScreenshot(screenshotId);
		return new ResponseEntity<Screenshot>(item, HttpStatus.OK);
	}

	/**
	 * Gets list of the screenshots which is narrowed down by a test execution id and test environment id.
	 * If pageSize equals to zero, the default page size is used. If pageSize equals to
	 * -1, the entire list is returned.
	 * 
	 * @param testExecutionId test execution id
	 * @param testEnviromentId test environment id
	 * @param page Which page to show.
	 * @param pageSize Page size.
	 * @return Page of test execution
	 */
	@RequestMapping(value = "/listScreenshot", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
	@ResponseBody
	public ResponseEntity<Page<Screenshot>> listScreenshot(@RequestParam Integer testExecutionId,
			@RequestParam Integer testEnvironmentId,
			@RequestParam(defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "-1") int pageSize) {
		Page<Screenshot> screenshotPage = service.findScreenshot(testExecutionId, testEnvironmentId, page, pageSize);
		return new ResponseEntity<Page<Screenshot>>(screenshotPage, HttpStatus.OK);
	}

	/**
	 * Gets list of the TestExecutionAndEnvironment.
	 * If pageSize equals to zero, the default page size is used. If pageSize equals to
	 * -1, the entire list is returned.
	 * 
	 * @param page Which page to show.
	 * @param pageSize Page size.
	 * @return Page of test execution and environment
	 */
	@RequestMapping(value = "/listCompositeTestExecution", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
	@ResponseBody
	public ResponseEntity<Page<TestExecutionAndEnvironment>> listCompositeTestExecution(
			@RequestParam(defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "-1") int pageSize) {
		Page<TestExecutionAndEnvironment> testPage = service.findTestExecutionAndEnviroment(page, pageSize);
		return new ResponseEntity<Page<TestExecutionAndEnvironment>>(testPage, HttpStatus.OK);
	}

}

@@ -741,84 +741,8 @@
 ot;%0A
-import com.htmlhifive.pitalium.explorer.entity.TestExecutionAndEnvironment;%0A
 impo
@@ -3102,1874 +3102,6 @@
 %09%7D%0A%0A
-%09/**%0A%09 * Gets list of the screenshots which is narrowed down by a test execution id and test environment id.%0A%09 * If pageSize equals to zero, the default page size is used. If pageSize equals to%0A%09 * -1, the entire list is returned.%0A%09 * %0A%09 * @param testExecutionId test execution id%0A%09 * @param testEnviromentId test environment id%0A%09 * @param page Which page to show.%0A%09 * @param pageSize Page size.%0A%09 * @return Page of test execution%0A%09 */%0A%09@RequestMapping(value = %22/listScreenshot%22, method = RequestMethod.GET, produces = %22application/json;charset=utf-8%22)%0A%09@ResponseBody%0A%09public ResponseEntity%3CPage%3CScreenshot%3E%3E listScreenshot(@RequestParam Integer testExecutionId,%0A%09%09%09@RequestParam Integer testEnvironmentId,%0A%09%09%09@RequestParam(defaultValue = %221%22) int page,%0A%09%09%09@RequestParam(value = %22limit%22, defaultValue = %22-1%22) int pageSize) %7B%0A%09%09Page%3CScreenshot%3E screenshotPage = service.findScreenshot(testExecutionId, testEnvironmentId, page, pageSize);%0A%09%09return new ResponseEntity%3CPage%3CScreenshot%3E%3E(screenshotPage, HttpStatus.OK);%0A%09%7D%0A%0A%09/**%0A%09 * Gets list of the TestExecutionAndEnvironment.%0A%09 * If pageSize equals to zero, the default page size is used. If pageSize equals to%0A%09 * -1, the entire list is returned.%0A%09 * %0A%09 * @param page Which page to show.%0A%09 * @param pageSize Page size.%0A%09 * @return Page of test execution and environment%0A%09 */%0A%09@RequestMapping(value = %22/listCompositeTestExecution%22, method = RequestMethod.GET, produces = %22application/json;charset=utf-8%22)%0A%09@ResponseBody%0A%09public ResponseEntity%3CPage%3CTestExecutionAndEnvironment%3E%3E listCompositeTestExecution(%0A%09%09%09@RequestParam(defaultValue = %221%22) int page,%0A%09%09%09@RequestParam(value = %22limit%22, defaultValue = %22-1%22) int pageSize) %7B%0A%09%09Page%3CTestExecutionAndEnvironment%3E testPage = service.findTestExecutionAndEnviroment(page, pageSize);%0A%09%09return new ResponseEntity%3CPage%3CTestExecutionAndEnvironment%3E%3E(testPage, HttpStatus.OK);%0A%09%7D%0A%0A
 %7D%0A

Revert "#19 APIの追加"