amichair commented on code in PR #2452: URL: https://github.com/apache/james-project/pull/2452#discussion_r1802415022
########## 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(); Review Comment: The TransactionWorker api isn't as shown above, but was easy enough to adjust. However the Throwing part I gave a few tries with several variations and types but couldn't get to work yet. If you have a working snippet that could save me some time trying to mess with the java/scala type system incompatibilities (Function1 is not a Function etc)... btw I think it would have been nice if at least the events source api part remained in java (even if other module implementations were in scala) rather than making the interface itself use scala objects and have the scala types leak into the java code... scala was designed to try an play nice with java, but not necessarily the other way around. but perhaps that's just my own preference or lack of experience stitching them together :-) -- 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