amichair commented on code in PR #2452:
URL: https://github.com/apache/james-project/pull/2452#discussion_r1804600989


##########
event-sourcing/event-store-jpa/src/main/java/org/apache/james/eventsourcing/eventstore/jpa/JPAEventStore.java:
##########
@@ -0,0 +1,176 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.eventsourcing.eventstore.jpa;
+
+
+import static 
org.apache.james.eventsourcing.eventstore.jpa.model.JPAEvent.DELETE_AGGREGATE_QUERY;
+import static 
org.apache.james.eventsourcing.eventstore.jpa.model.JPAEvent.SELECT_AGGREGATE_QUERY;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import jakarta.inject.Inject;
+import jakarta.persistence.EntityManager;
+import jakarta.persistence.EntityManagerFactory;
+import jakarta.persistence.EntityTransaction;
+import jakarta.persistence.PersistenceException;
+import jakarta.persistence.PersistenceUnit;
+
+import org.apache.james.backends.jpa.EntityManagerUtils;
+import org.apache.james.eventsourcing.AggregateId;
+import org.apache.james.eventsourcing.Event;
+import org.apache.james.eventsourcing.eventstore.EventStore;
+import org.apache.james.eventsourcing.eventstore.EventStoreFailedException;
+import org.apache.james.eventsourcing.eventstore.History;
+import org.apache.james.eventsourcing.eventstore.JsonEventSerializer;
+import org.apache.james.eventsourcing.eventstore.jpa.model.JPAEvent;
+import org.reactivestreams.Publisher;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.google.common.base.Preconditions;
+
+import reactor.core.publisher.Mono;
+import scala.collection.Iterable;
+
+
+public class JPAEventStore implements EventStore {
+
+    /**
+     * The entity manager to access the database.
+     */
+    private EntityManagerFactory entityManagerFactory;
+
+    /**
+     * The JSON serializer to serialize the event data.
+     */
+    private JsonEventSerializer jsonEventSerializer;
+
+    /**
+     * Set the serializer to use.
+     */
+    @Inject
+    public void setJsonEventSerializer(JsonEventSerializer 
jsonEventSerializer) {
+        this.jsonEventSerializer = jsonEventSerializer;
+    }
+
+    /**
+     * Set the entity manager to use.
+     */
+    @Inject
+    @PersistenceUnit(unitName = "James")
+    public void setEntityManagerFactory(EntityManagerFactory 
entityManagerFactory) {
+        this.entityManagerFactory = entityManagerFactory;
+    }
+
+    @Override
+    public Publisher<Void> appendAll(Iterable<Event> events) {
+        if (events.isEmpty()) {
+            return Mono.empty();
+        }
+        Preconditions.checkArgument(Event.belongsToSameAggregate(events));
+        AggregateId aggregateId = events.head().getAggregateId();
+        EntityManager entityManager = 
entityManagerFactory.createEntityManager();
+        final EntityTransaction transaction = entityManager.getTransaction();
+        try {
+            transaction.begin();
+            events.foreach(e -> {
+                try {
+                    JPAEvent jpaEvent = new JPAEvent(aggregateId, e.eventId(), 
jsonEventSerializer.serialize(e));
+                    entityManager.persist(jpaEvent);
+                } catch (JsonProcessingException ex) {
+                    throw new RuntimeException(ex);
+                }
+                return e;
+            });
+            transaction.commit();
+        } catch (PersistenceException e) {
+            if (transaction.isActive()) {
+                transaction.rollback();
+            }
+            EventStoreFailedException esfe = new 
EventStoreFailedException("Unable to add events");
+            esfe.initCause(e);
+            throw esfe;
+        } finally {
+            EntityManagerUtils.safelyClose(entityManager);
+        }
+        return Mono.empty();
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public Publisher<History> getEventsOfAggregate(AggregateId aggregateId) {
+        Preconditions.checkNotNull(aggregateId);
+        EntityManager entityManager = 
entityManagerFactory.createEntityManager();
+        try {
+            List<Event> events = (List<Event>) 
entityManager.createNamedQuery(SELECT_AGGREGATE_QUERY)
+                .setParameter("aggregateId", aggregateId.asAggregateKey())
+                .getResultStream()
+                .map(e -> {
+                    try {
+                        return jsonEventSerializer.deserialize(((JPAEvent) 
e).getEvent());
+                    } catch (IOException ex) {
+                        throw new RuntimeException(ex);
+                    }
+                })
+                .collect(Collectors.toList());
+            return Mono.fromSupplier(() -> History.of(events.toArray(new 
Event[0])));
+        } catch (PersistenceException e) {
+            throw new RuntimeException("Unable to get events", e);
+        } finally {
+            EntityManagerUtils.safelyClose(entityManager);
+        }
+    }
+
+    @Override
+    public Publisher<Void> remove(AggregateId aggregateId) {
+        EntityManager entityManager = 
entityManagerFactory.createEntityManager();
+        final EntityTransaction transaction = entityManager.getTransaction();

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org
For additional commands, e-mail: notifications-h...@james.apache.org

Reply via email to