package com.blackducksoftware.integration.hub.dataservices;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.blackducksoftware.integration.hub.api.ComponentVersionRestService;
import com.blackducksoftware.integration.hub.api.NotificationRestService;
import com.blackducksoftware.integration.hub.api.PolicyRestService;
import com.blackducksoftware.integration.hub.api.ProjectVersionRestService;
import com.blackducksoftware.integration.hub.api.VersionBomPolicyRestService;
import com.blackducksoftware.integration.hub.api.notification.NotificationItem;
import com.blackducksoftware.integration.hub.api.notification.PolicyOverrideNotificationItem;
import com.blackducksoftware.integration.hub.api.notification.RuleViolationNotificationItem;
import com.blackducksoftware.integration.hub.api.notification.VulnerabilityNotificationItem;
import com.blackducksoftware.integration.hub.dataservices.items.NotificationContentItem;
import com.blackducksoftware.integration.hub.dataservices.transforms.AbstractNotificationTransform;
import com.blackducksoftware.integration.hub.dataservices.transforms.PolicyViolationOverrideTransform;
import com.blackducksoftware.integration.hub.dataservices.transforms.PolicyViolationTransform;
import com.blackducksoftware.integration.hub.dataservices.transforms.VulnerabilityTransform;
import com.blackducksoftware.integration.hub.exception.BDRestException;
import com.blackducksoftware.integration.hub.exception.HubItemTransformException;
import com.blackducksoftware.integration.hub.rest.RestConnection;
import com.google.gson.Gson;
import com.google.gson.JsonParser;

public class NotificationDataService extends AbstractDataService {
	private final NotificationRestService notificationService;
	private final ProjectVersionRestService projectVersionService;
	private final PolicyRestService policyService;
	private final VersionBomPolicyRestService bomVersionPolicyService;
	private final ComponentVersionRestService componentVersionService;
	private Map<Class<?>, AbstractNotificationTransform> transformMap;

	public NotificationDataService(final RestConnection restConnection, final Gson gson, final JsonParser jsonParser) {
		super(restConnection, gson, jsonParser);
		notificationService = new NotificationRestService(restConnection, gson, jsonParser);
		projectVersionService = new ProjectVersionRestService(restConnection, gson, jsonParser);
		policyService = new PolicyRestService(restConnection, gson, jsonParser);
		bomVersionPolicyService = new VersionBomPolicyRestService(restConnection, gson, jsonParser);
		componentVersionService = new ComponentVersionRestService(restConnection, gson, jsonParser);
		transformMap.put(RuleViolationNotificationItem.class, new PolicyViolationTransform(notificationService,
				projectVersionService, policyService, bomVersionPolicyService, componentVersionService));
		transformMap.put(PolicyOverrideNotificationItem.class, new PolicyViolationOverrideTransform(notificationService,
				projectVersionService, policyService, bomVersionPolicyService, componentVersionService));
		transformMap.put(VulnerabilityNotificationItem.class, new VulnerabilityTransform(notificationService,
				projectVersionService, policyService, bomVersionPolicyService, componentVersionService));
	}

	public List<NotificationContentItem> getNotifications(final Date startDate, final Date endDate, final int limit)
			throws IOException, URISyntaxException, BDRestException {
		final List<NotificationContentItem> contentList = new ArrayList<NotificationContentItem>();
		final List<NotificationItem> itemList = notificationService.getNotifications(startDate, endDate, limit);

		for (final NotificationItem item : itemList) {
			try {
				if (transformMap.containsKey(item.getClass())) {
					final AbstractNotificationTransform converter = transformMap.get(item.getClass());
					contentList.addAll(converter.transform(item));
				}
			} catch (final HubItemTransformException e) {
				// transform what we can do not stop processing
			}
		}

		return contentList;
	}
}

@@ -169,16 +169,42 @@
 l.Date;%0A
+import java.util.HashMap;%0A
 import j
@@ -2124,16 +2124,22 @@
 private 
+final 
 Map%3CClas
@@ -2793,16 +2793,89 @@
 arser);%0A
+%09%09transformMap = new HashMap%3CClass%3C?%3E, AbstractNotificationTransform%3E();%0A
 %09%09transf

fix an issue initializing the transform map.