/*
 * Copyright 2016 EPAM Systems
 *
 *
 * This file is part of EPAM Report Portal.
 * https://github.com/reportportal/service-api
 *
 * Report Portal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Report Portal is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Report Portal.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.epam.ta.reportportal.ws.controller.internal;

import com.epam.ta.reportportal.commons.Predicates;
import com.epam.ta.reportportal.commons.validation.BusinessRule;
import com.epam.ta.reportportal.database.dao.ExternalSystemRepository;
import com.epam.ta.reportportal.database.entity.ExternalSystem;
import com.epam.ta.reportportal.ws.converter.ExternalSystemResourceAssembler;
import com.epam.ta.reportportal.ws.model.ErrorType;
import com.epam.ta.reportportal.ws.model.externalsystem.ExternalSystemResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.inject.Provider;

/**
 * Allowed for internal (other services) clients ONLY
 * Provides direct access to DAO layer
 *
 * @author Andrei Varabyeu
 */
@Controller
@RequestMapping("/api-internal")
@PreAuthorize("hasRole('COMPONENT')")
public class InternalApiController {

	@Autowired
	private ExternalSystemRepository externalSystemRepository;

	@Autowired
	private Provider<ExternalSystemResourceAssembler> externalSystemResourceAssembler;

	@RequestMapping(value = "/external-system/{systemId}", method = RequestMethod.GET)
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	@ApiIgnore
	public ExternalSystemResource getExternalSystem(@PathVariable String systemId) {
		ExternalSystem externalSystem = externalSystemRepository.findOne(systemId);
		BusinessRule.expect(externalSystem, Predicates.notNull()).verify(ErrorType.EXTERNAL_SYSTEM_NOT_FOUND, systemId);
		return externalSystemResourceAssembler.get().toResource(externalSystem);
	}
}

@@ -1136,16 +1136,27 @@
 nverter.
+converters.
 External
@@ -1157,39 +1157,31 @@
 ternalSystem
-ResourceAssembl
+Convert
 er;%0Aimport c
@@ -1635,39 +1635,8 @@
 e;%0A%0A
-import javax.inject.Provider;%0A%0A
 /**%0A
@@ -1883,28 +1883,16 @@
 ller %7B%0A%0A
-%09@Autowired%0A
 %09private
@@ -1892,16 +1892,22 @@
 private 
+final 
 External
@@ -1968,24 +1968,36 @@
 d%0A%09p
-rivate Provider%3C
+ublic InternalApiController(
 Exte
@@ -2012,57 +2012,107 @@
 emRe
-sourceAssembler%3E externalSystemResourceAssembler;
+pository externalSystemRepository) %7B%0A%09%09this.externalSystemRepository = externalSystemRepository;%0A%09%7D
 %0A%0A%09@
@@ -2536,17 +2536,17 @@
 %09return 
-e
+E
 xternalS
@@ -2554,42 +2554,35 @@
 stem
-ResourceAssembler.get().toResource
+Converter.TO_RESOURCE.apply
 (ext

refactor internal api controller