package org.camunda.bpm.extension.cloud.event.service;

import java.util.Collection;
import java.util.HashMap;

import org.camunda.bpm.extension.cloud.event.service.rest.TaskEvent;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class EventCache {

  private HashMap<String, TaskEvent> eventCash;

  public EventCache() {
    this.eventCash = new HashMap<String, TaskEvent>();
  }

  public EventCache(HashMap<String, TaskEvent> eventCash) {
    this.eventCash = eventCash;
  }

  public void putEvent(TaskEvent task) {
    log.info("Event cached: {}", task);
    eventCash.put(generateKey(task), task);
  }

  public TaskEvent getEvent(String key) {
    return eventCash.get(key);
  }

  public Collection<TaskEvent> getEvents() {
    return this.eventCash.values();
  }

  public void removeEvent(TaskEvent task) {
    log.info("Event removed from cache: {}", task);
    this.eventCash.remove(generateKey(task));
  }

  public String generateKey(TaskEvent taskEvent) {
    return String.format("%s:%s:%s", taskEvent.getEngineId(), taskEvent.getTaskDefinitionKey(), taskEvent.getTaskId());
  }
}

@@ -60,56 +60,34 @@
 ort 
-java.util.Collection;%0Aimport java.util.HashMap;%0A
+lombok.extern.slf4j.Slf4j;
 %0Aimp
@@ -213,33 +213,54 @@
 ort 
-lombok.extern.slf4j.Slf4j
+java.util.Collection;%0Aimport java.util.HashMap
 ;%0A%0A@
@@ -582,79 +582,78 @@
     
-log.info(%22Event cached: %7B%7D%22, task);%0A    eventCash.put(generateKey(task)
+eventCash.put(task.getTaskId(), task);%0A    log.info(%22Event cached: %7B%7D%22
 , ta
@@ -866,24 +866,69 @@
 ent task) %7B%0A
+    this.eventCash.remove(task.getTaskId());%0A
     log.info
@@ -973,230 +973,9 @@
 ;%0A  
-  this.eventCash.remove(generateKey(task));%0A  %7D%0A%0A  public String generateKey(TaskEvent taskEvent) %7B%0A    return String.format(%22%25s:%25s:%25s%22, taskEvent.getEngineId(), taskEvent.getTaskDefinitionKey(), taskEvent.getTaskId());%0A  
 %7D
+%0A
 %0A%7D%0A

remove generateKey from EventCache