/*
 * Copyright (c) 2013 Red Rainbow IT Solutions GmbH, Germany
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package org.jeecqrs.bundle.jcommondomain.sagas;

import org.jeecqrs.common.commands.CommandBus;
import org.jeecqrs.common.event.Event;
import org.jeecqrs.common.sagas.SagaTimeoutProvider;
import org.jeecqrs.sagas.Saga;
import org.jeecqrs.sagas.SagaFactory;

/**
 *
 */
public class DefaultSagaFactory implements SagaFactory<Event> {

    private final Class<? extends AbstractSaga> sagaClass;
    private final CommandBus commandBus;
    private final SagaTimeoutProvider timeoutProvider;

    public DefaultSagaFactory(
            Class<? extends AbstractSaga> sagaClass,
            CommandBus commandBus,
            SagaTimeoutProvider timeoutProvider) {

        this.sagaClass = sagaClass;
        this.commandBus = commandBus;
        this.timeoutProvider = timeoutProvider;
    }

    @Override
    public Saga<Event> createSaga(String sagaId) {
        AbstractSaga saga = SagaUtil.createInstance(sagaClass, sagaId);
        saga.setCommandBus(commandBus);
        saga.setTimeoutProvider(timeoutProvider);
        return saga;
    }

}

@@ -1262,103 +1262,33 @@
 mon.
-event.Event;%0Aimport org.jeecqrs.common.sagas.SagaTimeoutProvider;%0Aimport org.jeecqrs.sagas.Saga
+sagas.SagaTimeoutProvider
 ;%0Aim
@@ -1330,16 +1330,31 @@
 %0A%0A/**%0A *
+ %0A * @param %3CS%3E
 %0A */%0Apub
@@ -1381,16 +1381,43 @@
 aFactory
+%3CS extends AbstractSaga%3CS%3E%3E
  impleme
@@ -1432,21 +1432,17 @@
 Factory%3C
-Event
+S
 %3E %7B%0A%0A   
@@ -1462,38 +1462,17 @@
 l Class%3C
-? extends AbstractSaga
+S
 %3E sagaCl
@@ -1626,30 +1626,9 @@
 ass%3C
-? extends AbstractSaga
+S
 %3E sa
@@ -1722,17 +1722,16 @@
 ider) %7B%0A
-%0A
         
@@ -1881,18 +1881,8 @@
 ic S
-aga%3CEvent%3E
  cre
@@ -1918,43 +1918,26 @@
     
-AbstractSaga saga = SagaUtil.
+S saga = 
 create
+New
 Inst
@@ -1945,19 +1945,31 @@
 nce(
-sagaClass, 
+);%0A        saga.sagaId(
 saga
@@ -2091,10 +2091,353 @@
 %0A    %7D%0A%0A
+    protected S createNewInstance() %7B%0A        try %7B%0A            return sagaClass.newInstance();%0A%09%7D catch (InstantiationException %7C IllegalAccessException %7C%0A                IllegalArgumentException e) %7B%0A%09    throw new RuntimeException(%22Cannot instantiate object of type %22 %0A                    + sagaClass + %22: %22 + e.getMessage(), e);%0A%09%7D%0A    %7D%0A%0A
 %7D%0A

Move saga creation code into DefaultSagaFactory