/*
 * Copyright 2017 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.converter.converters;

import com.epam.ta.reportportal.database.entity.item.TestItem;
import com.epam.ta.reportportal.ws.model.TestItemResource;
import com.google.common.base.Preconditions;

import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Converts internal DB model to DTO
 *
 * @author Pavel Bortnik
 */
public final class TestItemConverter {

	private TestItemConverter() {
		//static only
	}

	public static final Function<TestItem, TestItemResource> TO_RESOURCE = item -> {
		Preconditions.checkNotNull(item);
		TestItemResource resource = new TestItemResource();
		resource.setDescription(item.getItemDescription());
		resource.setUniqueId(item.getUniqueId());
		resource.setTags(item.getTags());
		resource.setEndTime(item.getEndTime());
		resource.setItemId(item.getId());
		if (null != item.getParameters()) {
			resource.setParameters(item.getParameters().stream().map(ParametersConverter.TO_RESOURCE).collect(Collectors.toList()));
		}
		resource.setIssue(IssueConverter.TO_MODEL.apply(item.getIssue()));
		resource.setName(item.getName());
		resource.setStartTime(item.getStartTime());
		resource.setStatus(item.getStatus() != null ? item.getStatus().toString() : null);
		resource.setType(item.getType() != null ? item.getType().name() : null);
		resource.setParent(item.getParent());
		resource.setHasChilds(item.hasChilds());
		resource.setLaunchId(item.getLaunchRef());
		resource.setStatistics(StatisticsConverter.TO_RESOURCE.apply(item.getStatistics()));

		Optional.ofNullable(item.getRetries())
				.map(items -> items.stream().map(TestItemConverter.TO_RESOURCE).collect(Collectors.toList()))
				.ifPresent(resource::setRetries);
		return resource;
	};
}

@@ -1004,24 +1004,53 @@
 onditions;%0A%0A
+import java.util.Comparator;%0A
 import java.
@@ -1137,16 +1137,63 @@
 ctors;%0A%0A
+import static java.util.Comparator.comparing;%0A%0A
 /**%0A * C
@@ -2525,32 +2525,96 @@
 ter.TO_RESOURCE)
+%0A%09%09%09%09%09%09.sorted(comparing(TestItemResource::getStartTime))%0A%09%09%09%09%09%09
 .collect(Collect

add ordering by start time