/**
*
* Copyright
* 2009-2015 Jayway Products AB
* 2016-2017 Föreningen Sambruk
*
* Licensed under AGPL, Version 3.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.gnu.org/licenses/agpl.txt
*
* 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 se.streamsource.streamflow.infrastructure.event.application.factory;
import org.qi4j.api.concern.ConcernOf;
import org.qi4j.api.injection.scope.Service;
import org.qi4j.api.injection.scope.Structure;
import org.qi4j.api.structure.Module;
import org.qi4j.api.unitofwork.UnitOfWork;
import org.qi4j.api.unitofwork.UnitOfWorkCallback;
import org.qi4j.api.unitofwork.UnitOfWorkCompletionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import se.streamsource.streamflow.infrastructure.event.application.ApplicationEvent;
import se.streamsource.streamflow.infrastructure.event.application.source.ApplicationEventStore;
import se.streamsource.streamflow.infrastructure.event.domain.factory.DomainEventFactory;
import java.io.IOException;
/**
* Notify transaction listeners when a complete transaction of domain events is available.
*/
public class TransactionNotificationConcern
extends ConcernOf<ApplicationEventFactory>
implements ApplicationEventFactory
{
@Service
ApplicationEventStore eventStore;
@Structure
Module module;
Logger logger = LoggerFactory.getLogger( DomainEventFactory.class );
public ApplicationEvent createEvent( String name, Object[] args )
{
final UnitOfWork unitOfWork = module.unitOfWorkFactory().currentUnitOfWork();
ApplicationEvent event = next.createEvent( name, args );
// Add event to list in UoW
UnitOfWorkApplicationEvents events = unitOfWork.metaInfo().get( UnitOfWorkApplicationEvents.class );
if (events == null)
{
events = new UnitOfWorkApplicationEvents();
unitOfWork.metaInfo().set( events );
unitOfWork.addUnitOfWorkCallback( new UnitOfWorkCallback()
{
public void beforeCompletion() throws UnitOfWorkCompletionException
{
}
public void afterCompletion( UnitOfWorkStatus status )
{
if (status.equals( UnitOfWorkStatus.COMPLETED ))
{
UnitOfWorkApplicationEvents events = unitOfWork.metaInfo().get( UnitOfWorkApplicationEvents.class );
try
{
eventStore.storeEvents( events.getEvents() );
} catch (IOException e)
{
logger.error( "Could not store events", e );
// How do we handle this? This is a major error!
}
}
}
} );
}
events.add( event );
return event;
}
}