/*
 * 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))).append(
					"retries.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
@@ -1011,16 +1011,16 @@
 stItem;%0A
-
 import c
@@ -1572,16 +1572,43 @@
 il.List;
+%0Aimport java.util.Optional;
 %0A%0Aimport
@@ -2654,16 +2654,143 @@
 bqo)) %7B%0A
+%09%09%09Optional%3CRetryType%3E retryType = RetryType.fromString((String) dbObject.get(%22retryType%22));%0A%09%09%09if (!retryType.isPresent()) %7B%0A%09
 %09%09%09final
@@ -2834,16 +2834,17 @@
 ring();%0A
+%09
 %09%09%09final
@@ -2965,16 +2965,17 @@
 d(%0A%09%09%09%09%09
+%09
 %22retries
@@ -3031,16 +3031,17 @@
 )));%0A%09%09%09
+%09
 final Li
@@ -3069,16 +3069,17 @@
 stream(%0A
+%09
 %09%09%09%09%09mon
@@ -3184,16 +3184,17 @@
 e).map(%0A
+%09
 %09%09%09%09%09it 
@@ -3244,16 +3244,17 @@
 ());%0A%09%09%09
+%09
 mongoTem
@@ -3333,16 +3333,17 @@
 ry);%0A%09%09%09
+%09
 itemIds.
@@ -3351,16 +3351,17 @@
 dd(id);%0A
+%09
 %09%09%09logRe
@@ -3395,16 +3395,21 @@
 emIds);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%7D

fix trigger