Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.keycloak.audit.jpa;
  
 
 import java.util.Map;
 import java.util.Set;
 import java.util.UUID;

Author(s):
Stian Thorgersen
 
 public class JpaAuditProvider implements AuditProvider {
 
     private static final ObjectMapper mapper = new ObjectMapper();
     private static final TypeReference<Map<StringString>> mapType = new TypeReference<Map<StringString>>() {
     };
     private static final Logger logger = Logger.getLogger(JpaAuditProvider.class);
 
     private EntityManager em;
     private EntityTransaction tx;
     private Set<EventTypeincludedEvents;
 
     public JpaAuditProvider(EntityManager emSet<EventTypeincludedEvents) {
         this. = em;
         this. = includedEvents;
     }
 
     @Override
     public EventQuery createQuery() {
         return new JpaEventQuery();
     }
 
     @Override
     public void clear() {
         beginTx();
         .createQuery("delete from EventEntity").executeUpdate();
     }
 
     @Override
     public void clear(String realmId) {
         beginTx();
         .createQuery("delete from EventEntity where realmId = :realmId").setParameter("realmId"realmId).executeUpdate();
     }
 
     @Override
     public void clear(String realmIdlong olderThan) {
         beginTx();
         .createQuery("delete from EventEntity where realmId = :realmId and time < :time").setParameter("realmId"realmId).setParameter("time"olderThan).executeUpdate();
     }
 
     @Override
     public void onEvent(Event event) {
         if (.contains(event.getEvent())) {
             beginTx();
             .persist(convert(event));
         }
     }
 
     @Override
     public void close() {
         if ( != null) {
             .commit();
         }
 
         .close();
     }
 
     private void beginTx() {
         if ( == null) {
              = .getTransaction();
             .begin();
         }
     }
 
     static EventEntity convert(Event o) {
         EventEntity e = new EventEntity();
         e.setId(UUID.randomUUID().toString());
         e.setTime(o.getTime());
         e.setEvent(o.getEvent().toString());
         e.setRealmId(o.getRealmId());
         e.setClientId(o.getClientId());
         e.setUserId(o.getUserId());
         e.setSessionId(o.getSessionId());
         e.setIpAddress(o.getIpAddress());
         e.setError(o.getError());
         try {
             e.setDetailsJson(.writeValueAsString(o.getDetails()));
         } catch (IOException ex) {
             .error("Failed to write log details"ex);
         }
        return e;
    }
    static Event convert(EventEntity o) {
        Event e = new Event();
        e.setTime(o.getTime());
        e.setEvent(EventType.valueOf(o.getEvent()));
        e.setRealmId(o.getRealmId());
        e.setClientId(o.getClientId());
        e.setUserId(o.getUserId());
        e.setSessionId(o.getSessionId());
        e.setIpAddress(o.getIpAddress());
        e.setError(o.getError());
        try {
            Map<StringStringdetails = .readValue(o.getDetailsJson(), );
            e.setDetails(details);
        } catch (IOException ex) {
            .error("Failed to read log details"ex);
        }
        return e;
    }
New to GrepCode? Check out our FAQ X