package io.hops.kmon.alert;

import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import io.hops.kmon.alert.Alert.Provider;
import io.hops.kmon.alert.Alert.Severity;

/**
 *
 * @author Hamidreza Afzali <afzali@kth.se>
 * 
 * See: https://abhirockzz.wordpress.com/2015/02/19/valid-cdi-scoped-for-session-ejb-beans/
 */
@Stateless
public class AlertEJB {

    @PersistenceContext(unitName = "kthfsPU")
    private EntityManager em;

    public AlertEJB() {
    }

    public List<Alert> find(Date fromDate, Date toDate) {

        TypedQuery<Alert> query = em.createNamedQuery("Alerts.findAll", Alert.class)
                .setParameter("fromdate", fromDate).setParameter("todate", toDate) ;
        return query.getResultList();
    }
    
    public List<Alert> find(Date fromDate, Date toDate, Severity severity) {
        TypedQuery<Alert> query = em.createNamedQuery("Alerts.findBy-Severity", Alert.class)
                .setParameter("fromdate", fromDate).setParameter("todate", toDate)
                .setParameter("severity", severity.toString());
        return query.getResultList();
    }
    
    public List<Alert> find(Date fromDate, Date toDate, Provider provider) {
        TypedQuery<Alert> query = em.createNamedQuery("Alerts.findBy-Provider", Alert.class)
                .setParameter("fromdate", fromDate).setParameter("todate", toDate)
                .setParameter("provider", provider.toString());
        return query.getResultList();
    }
    
    public List<Alert> find(Date fromDate, Date toDate, Provider provider, Severity severity) {
        TypedQuery<Alert> query = em.createNamedQuery("Alerts.findBy-Provider-Severity", Alert.class)
                .setParameter("fromdate", fromDate).setParameter("todate", toDate)
                .setParameter("provider", provider).setParameter("severity", severity);
        return query.getResultList();
    }    
    
    public void persistAlert(Alert alert) {
        em.persist(alert);
    }
    
    public void removeAlert(Alert alert) {
       em.remove(em.merge(alert));
    }
    
    public void removeAllAlerts() {
       em.createNamedQuery("Alerts.removeAll").executeUpdate();
    }    
}

@@ -360,17 +360,16 @@
 %3E%0A *
- 
 %0A * See:
  htt
@@ -364,16 +364,19 @@
 %0A * See:
+%0A *
  https:/
@@ -492,18 +492,16 @@
 tEJB %7B%0A%0A
-  
   @Persi
@@ -536,18 +536,16 @@
 hfsPU%22)%0A
-  
   privat
@@ -565,18 +565,16 @@
 er em;%0A%0A
-  
   public
@@ -591,17 +591,13 @@
 ) %7B%0A
-  
   %7D%0A%0A
-  
   pu
@@ -649,28 +649,24 @@
 ate) %7B%0A%0A    
- 
-   
 TypedQuery%3CA
@@ -722,36 +722,32 @@
 %22, Alert.class)%0A
-    
             .set
@@ -812,15 +812,10 @@
 ate)
- 
 ;%0A
-    
     
@@ -840,37 +840,29 @@
 sultList();%0A
-  
   %7D%0A
-    %0A  
+%0A
   public Lis
@@ -920,36 +920,32 @@
 severity) %7B%0A    
- 
-   
 TypedQuery%3CAlert
@@ -991,32 +991,44 @@
 indBy-Severity%22,
+%0A           
  Alert.class)%0A  
@@ -1017,36 +1017,32 @@
    Alert.class)%0A
-    
             .set
@@ -1108,36 +1108,32 @@
 te)%0A            
-  
-  
 .setParameter(%22s
@@ -1156,36 +1156,32 @@
 ty.toString());%0A
-    
     return query
@@ -1196,35 +1196,27 @@
 ltList();%0A  
-  %7D%0A    %0A  
+%7D%0A%0A
   public Lis
@@ -1274,28 +1274,24 @@
 provider) %7B%0A
-    
     TypedQue
@@ -1353,16 +1353,28 @@
 ovider%22,
+%0A           
  Alert.c
@@ -1371,36 +1371,32 @@
    Alert.class)%0A
-    
             .set
@@ -1462,36 +1462,32 @@
 te)%0A            
-    
 .setParameter(%22p
@@ -1518,20 +1518,16 @@
 ing());%0A
-    
     retu
@@ -1550,35 +1550,27 @@
 ltList();%0A  
-  %7D%0A    %0A  
+%7D%0A%0A
   public Lis
@@ -1629,16 +1629,26 @@
 rovider,
+%0A         
  Severit
@@ -1657,28 +1657,24 @@
 severity) %7B%0A
-    
     TypedQue
@@ -1703,32 +1703,45 @@
 reateNamedQuery(
+%0A            
 %22Alerts.findBy-P
@@ -1764,36 +1764,32 @@
 %22, Alert.class)%0A
-    
             .set
@@ -1855,36 +1855,32 @@
 te)%0A            
-   
- 
 .setParameter(%22p
@@ -1897,16 +1897,27 @@
 provider
+.toString()
 ).setPar
@@ -1915,32 +1915,45 @@
 )).setParameter(
+%0A            
 %22severity%22, seve
@@ -1956,23 +1956,30 @@
 severity
-);%0A    
+.toString());%0A
     retu
@@ -2008,25 +2008,13 @@
 ();%0A
-  
   %7D
-    %0A    %0A  
+%0A%0A
   pu
@@ -2051,20 +2051,16 @@
 lert) %7B%0A
-    
     em.p
@@ -2072,37 +2072,29 @@
 t(alert);%0A  
-  %7D%0A    %0A 
+%7D%0A%0A
   
- 
 public void 
@@ -2124,19 +2124,16 @@
 ) %7B%0A    
-   
 em.remov
@@ -2158,19 +2158,11 @@
 ;%0A  
-  %7D%0A    %0A  
+%7D%0A%0A
   pu
@@ -2191,19 +2191,16 @@
 rts() %7B%0A
-   
     em.c
@@ -2258,14 +2258,8 @@
 ;%0A  
-  %7D    
+%7D
 %0A%7D%0A

fix AlertEJB