/*
 * Copyright 2016 EPAM Systems
 *
 *
 * This file is part of EPAM Report Portal.
 * https://github.com/reportportal/commons-dao
 *
 * 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.triggers;

import com.epam.ta.reportportal.database.dao.LogRepository;
import com.epam.ta.reportportal.database.entity.item.TestItem;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.QueryMapper;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
import org.springframework.data.mongodb.core.mapping.event.BeforeDeleteEvent;
import org.springframework.stereotype.Service;

import java.util.List;

import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;
import static java.util.stream.StreamSupport.stream;

/**
 * @author Dzmitry Kavalets
 */
@Service
public class DeleteItemsListener extends AbstractMongoEventListener<TestItem> {

	private MongoTemplate mongoTemplate;
	private LogRepository logRepository;
	private QueryMapper queryMapper;
	private MongoMappingContext mappingContext;

	@Autowired
	public DeleteItemsListener(MongoTemplate mongoTemplate, LogRepository logRepository, MongoMappingContext mappingContext) {
		this.mongoTemplate = mongoTemplate;
		this.logRepository = logRepository;
		this.queryMapper = new QueryMapper(mongoTemplate.getConverter());
		this.mappingContext = mappingContext;
	}

	@Override
	public void onBeforeDelete(BeforeDeleteEvent<TestItem> event) {
		DBObject dbqo = queryMapper.getMappedObject(event.getDBObject(), mappingContext.getPersistentEntity(TestItem.class));
		for (DBObject dbObject : mongoTemplate.getCollection(event.getCollectionName()).find(dbqo)) {
			final String id = dbObject.get("_id").toString();
			final BasicDBObject itemDescendantsQuery = new BasicDBObject("path", new BasicDBObject("$in", singletonList(id)));
			final List<String> itemIds = stream(
					mongoTemplate.getCollection(event.getCollectionName()).find(itemDescendantsQuery).spliterator(), false)
					.map(it -> it.get("_id").toString()).collect(toList());
			mongoTemplate.getCollection(event.getCollectionName()).remove(itemDescendantsQuery);
			itemIds.add(id);
			logRepository.deleteByItemRef(itemIds);
		}
	}
}

@@ -884,16 +884,80 @@
 sitory;%0A
+import com.epam.ta.reportportal.database.entity.item.RetryType;%0A
 import c
@@ -1572,16 +1572,43 @@
 il.List;
+%0Aimport java.util.Optional;
 %0A%0Aimport
@@ -2653,16 +2653,112 @@
 dbqo)) %7B
+%0A%0A%09%09%09Optional%3CRetryType%3E retryType = RetryType.fromString(dbObject.get(%22retryType%22).toString());
 %0A%09%09%09fina
@@ -2917,25 +2917,477 @@
 List(id)));%0A
+%0A
 %09
+%09%09if (!retryType.isPresent()) %7B%0A%09%09%09%09clearData(event.getCollectionName(), itemDescendantsQuery, id);%0A%09%09%09%7D%0A%0A%09%09%09if (retryType.isPresent() && retryType.get().equals(RetryType.LAST)) %7B%0A%09%09%09%09itemDescendantsQuery.append(%22retries.path%22, new BasicDBObject(%22$in%22, singletonList(id)));%0A%09%09%09%09clearData(event.getCollectionName(), itemDescendantsQuery, id);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A%09private void clearData(String collectionName, BasicDBObject itemDescendantsQuery, String id) %7B%0A
 %09%09final List
@@ -3413,17 +3413,16 @@
 stream(%0A
-%09
 %09%09%09%09mong
@@ -3441,34 +3441,25 @@
 tCollection(
-event.getC
+c
 ollectionNam
@@ -3455,26 +3455,24 @@
 llectionName
-()
 ).find(itemD
@@ -3513,19 +3513,18 @@
 lse)
-%0A%09%09%09%09%09
 .map(
+%0A%09%09%09%09
 it -
@@ -3572,17 +3572,16 @@
 t());%0A%09%09
-%09
 mongoTem
@@ -3596,34 +3596,25 @@
 tCollection(
-event.getC
+c
 ollectionNam
@@ -3614,18 +3614,16 @@
 tionName
-()
 ).remove
@@ -3648,17 +3648,16 @@
 ery);%0A%09%09
-%09
 itemIds.
@@ -3665,17 +3665,16 @@
 dd(id);%0A
-%09
 %09%09logRep
@@ -3707,17 +3707,14 @@
 emIds);%0A
-%09
 %09%7D%0A
-%09%7D
 %0A%7D%0A

update trigger