package codeine;

import java.util.List;

import javax.inject.Inject;

import org.apache.log4j.Logger;
import org.joda.time.DateTime;

import codeine.configuration.IConfigurationManager;
import codeine.db.mysql.connectors.AlertsMysqlConnector;
import codeine.db.mysql.connectors.AlertsMysqlConnectorDatabaseConnectorListProvider;
import codeine.executer.Task;
import codeine.jsons.mails.AlertsCollectionType;
import codeine.jsons.mails.CollectorNotificationJson;
import codeine.mail.Mail;
import codeine.mail.MailStrategy;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;

public class MonitorDBTask implements Task {

	private static final Logger log = Logger.getLogger(MonitorDBTask.class);

	private IConfigurationManager configurationManager;
	private AggregateNotification mailCreator;
	private AggregateMailPrepare mailPrepare;
	private MailStrategy mailsStrategy;
	private CollectionTypeGetter collectionTypeGetter;
	private List<AlertsMysqlConnector> alertsConnectors;

	
	@Inject 
	public MonitorDBTask(IConfigurationManager configurationManager, AggregateNotification mailCreator,
			AggregateMailPrepare mailPrepare, MailStrategy mailsStrategy, CollectionTypeGetter collectionTypeGetter,
			AlertsMysqlConnectorDatabaseConnectorListProvider alertsMysqlConnectorDatabaseConnectorListProvider) {
		super();
		this.configurationManager = configurationManager;
		this.mailCreator = mailCreator;
		this.mailPrepare = mailPrepare;
		this.mailsStrategy = mailsStrategy;
		this.collectionTypeGetter = collectionTypeGetter;
		this.alertsConnectors = alertsMysqlConnectorDatabaseConnectorListProvider.get();
	}

	@Override
	public void run() {
		List<AlertsCollectionType> collType = collectionTypeGetter.getCollectionType(new DateTime());
		log.debug("starting collection " + collType);
		for (AlertsCollectionType alertsCollectionType : collType) {
			workOnCollectionType(alertsCollectionType);
		}
		if (collType.contains(AlertsCollectionType.Daily)){
			for (AlertsMysqlConnector c : alertsConnectors) {
				c.removeOldAlerts();
			}
		}
	}

	private void workOnCollectionType(AlertsCollectionType alertsCollectionType) {
		Multimap<String, CollectorNotificationJson> allItems = HashMultimap.create();
		for (AlertsMysqlConnector c : alertsConnectors) {
			allItems.putAll(c.getAlertsAndUpdate(alertsCollectionType));
		}
		List<NotificationContent> notificationContent = mailCreator.prepareMailsToUsers(alertsCollectionType, allItems, configurationManager.getConfiguredProjects());
		List<Mail> mails = mailPrepare.prepare(notificationContent, alertsCollectionType);
		for (Mail mail : mails) {
			log.info("sending mail to " + mail.recipients() + " with subject " + mail.subject());
			mailsStrategy.sendMail(mail);
		}
	}
}

@@ -2295,16 +2295,26 @@
 tors) %7B%0A
+%09%09%09try %7B%0A%09
 %09%09%09allIt
@@ -2369,16 +2369,100 @@
 Type));%0A
+%09%09%09%7D catch (Exception e) %7B%0A%09%09%09%09log.info(%22error fetching alerts from db %22 + c);%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%09Li

handle exception when connecting to db