/*
 * Copyright 2018 EPAM Systems
 *
 * 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 com.epam.ta.reportportal.ws.converter.converters;

import com.epam.ta.reportportal.entity.item.issue.IssueEntity;
import com.epam.ta.reportportal.ws.model.issue.Issue;
import com.google.common.base.Preconditions;

import java.util.function.Function;

/**
 * @author Pavel Bortnik
 */
public final class IssueConverter {

	private IssueConverter() {
		//static only
	}

	public static final Function<Issue, IssueEntity> TO_ISSUE = from -> {
		IssueEntity issue = new IssueEntity();
		issue.setAutoAnalyzed(from.getAutoAnalyzed());
		issue.setIgnoreAnalyzer(from.getIgnoreAnalyzer());
		issue.setIssueDescription(from.getComment());
		return issue;
	};

	/**
	 * Converts issue from db to model
	 */
	public static final Function<IssueEntity, Issue> TO_MODEL = issueEntity -> {
		Preconditions.checkNotNull(issueEntity);
		Issue issue = new Issue();
		issue.setIssueType(issueEntity.getIssueType().getLocator());
		issue.setAutoAnalyzed(issueEntity.getAutoAnalyzed());
		issue.setIgnoreAnalyzer(issueEntity.getIgnoreAnalyzer());
		issue.setComment(issueEntity.getIssueDescription());

		//TODO EXTERNAL SYSTEMS CONVERTER
		//		Set<TestItemIssue.ExternalSystemIssue> externalSystemIssues = issueEntity.getExternalSystemIssues();
		//		if (null != externalSystemIssues) {
		//			issue.setExternalSystemIssues(externalSystemIssues.stream().map(IssueConverter.TO_MODEL_EXTERNAL).collect(Collectors.toSet()));
		//		}
		return issue;
	};
	//
	//	/**
	//	 * Converts external system from db to model
	//	 */
	//	public static final Function<TestItemIssue.ExternalSystemIssue, Issue.ExternalSystemIssue> TO_MODEL_EXTERNAL = externalSystemIssue -> {
	//		Issue.ExternalSystemIssue issueResource = new Issue.ExternalSystemIssue();
	//		issueResource.setSubmitDate(externalSystemIssue.getSubmitDate());
	//		issueResource.setTicketId(externalSystemIssue.getTicketId());
	//		issueResource.setSubmitter(externalSystemIssue.getSubmitter());
	//		issueResource.setExternalSystemId(externalSystemIssue.getExternalSystemId());
	//		issueResource.setUrl(externalSystemIssue.getUrl());
	//		return issueResource;
	//	};
}

@@ -645,24 +645,75 @@
 onverters;%0A%0A
+import com.epam.ta.reportportal.entity.bts.Ticket;%0A
 import com.e
@@ -859,24 +859,51 @@
 onditions;%0A%0A
+import java.util.Optional;%0A
 import java.
@@ -925,16 +925,52 @@
 unction;
+%0Aimport java.util.stream.Collectors;
 %0A%0A/**%0A *
@@ -1801,195 +1801,77 @@
 %0A%0A%09%09
-//TODO EXTERNAL SYSTEMS CONVERTER%0A%09%09//%09%09Set%3CTestItemIssue.ExternalSystemIssue%3E externalSystemIssues = issueEntity.getExternalSystemIssues();%0A%09%09//%09%09if (null != externalSystemIssues) %7B%0A%09%09//
+Optional.ofNullable(issueEntity.getTickets()).ifPresent(tickets -%3E %7B%0A
 %09%09%09i
@@ -1899,35 +1899,22 @@
 mIssues(
-externalSystemIssue
+ticket
 s.stream
@@ -1990,13 +1990,11 @@
 ;%0A%09%09
-//%09%09%7D
+%7D);
 %0A%09%09r
@@ -2014,23 +2014,14 @@
 %09%7D;%0A
-%09//%0A%09//%09/**%0A%09//
+%0A%09/**%0A
 %09 * 
@@ -2066,19 +2066,13 @@
 del%0A
-%09//
 %09 */%0A
-%09//
 %09pub
@@ -2102,40 +2102,13 @@
 on%3CT
-estItemIssue.ExternalSystemIssue
+icket
 , Is
@@ -2177,19 +2177,16 @@
 ue -%3E %7B%0A
-%09//
 %09%09Issue.
@@ -2205,29 +2205,22 @@
 emIssue 
-issueResource
+ticket
  = new I
@@ -2252,322 +2252,186 @@
 );%0A%09
-//%09%09issueResource.setSubmitDate(externalSystemIssue.getSubmitDate());%0A%09//%09%09issueResource.setTicketId(externalSystemIssue.getTicketId());%0A%09//%09%09issueResource.setSubmitter(externalSystemIssue.getSubmitter());%0A%09//%09%09issueResource.setExternalSystemId(externalSystemIssue.getExternalSystemId());%0A%09//%09%09issueResource.setUrl
+%09ticket.setTicketId(externalSystemIssue.getTicketId());%0A%09%09ticket.setBtsUrl(externalSystemIssue.getBtsUrl());%0A%09%09ticket.setUrl(externalSystemIssue.getUrl());%0A%09%09ticket.setBtsProject
 (ext
@@ -2454,19 +2454,23 @@
 .get
-Url
+BtsProject
 ());%0A
-%09//
 %09%09re
@@ -2478,26 +2478,16 @@
 urn 
-issueResource;%0A%09//
+ticket;%0A
 %09%7D;%0A

fix ticket converter