/**
 * Copyright Notice
 *
 * This is a work of the U.S. Government and is not subject to copyright
 * protection in the United States. Foreign copyrights may apply.
 * 
 * Licensed 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 gov.va.isaac.workflow.engine;

import gov.va.isaac.AppContext;
import gov.va.isaac.ExtendedAppContext;
import java.rmi.RemoteException;
import javax.inject.Singleton;
import org.jvnet.hk2.annotations.Service;
import org.kie.api.runtime.manager.RuntimeEngine;
import org.kie.api.task.TaskService;
import org.kie.services.client.api.RemoteRestRuntimeFactory;

/**
 * {@link RemoteWfEngine}
 *
 * @author alo
 * @author <a href="mailto:daniel.armbrust.list@gmail.com">Dan Armbrust</a>
 */
@Service
@Singleton
public class RemoteWfEngine
{
	private RuntimeEngine remoteEngine;

	private RemoteWfEngine()
	{
		//for HK2
	}

	public RuntimeEngine getRemoteEngine() throws RemoteException
	{
		if (remoteEngine != null)
		{
			return remoteEngine;
		}
		else
		{
			//TODO prompt the user, block, wait for their response - let them update credentials
			try
			{
				RemoteRestRuntimeFactory restSessionFactory = new RemoteRestRuntimeFactory(AppContext.getAppConfiguration().getWorkflowServerDeploymentID(), AppContext
						.getAppConfiguration().getWorkflowServerURLasURL(), ExtendedAppContext.getCurrentlyLoggedInUserProfile().getWorkflowUsername(),
						ExtendedAppContext.getCurrentlyLoggedInUserProfile().getWorkflowPassword());
				remoteEngine = restSessionFactory.newRuntimeEngine();
				return remoteEngine;
			}
			catch (RuntimeException e)
			{
				throw new RemoteException("Error connecting to server", e);
			}
		}
	}

	public TaskService getRemoteTaskService() throws RemoteException
	{
		try
		{
			return getRemoteEngine().getTaskService();
		}
		catch (RuntimeException e)
		{
			throw new RemoteException("Server error", e);
		}
	}
}

@@ -1417,12 +1417,27 @@
 %7B%0A%09%09
-if (
+RuntimeEngine re = 
 remo
@@ -1444,16 +1444,26 @@
 teEngine
+;%0A%09%09if (re
  != null
@@ -1476,34 +1476,24 @@
 %09%09%09return re
-moteEngine
 ;%0A%09%09%7D%0A%09%09else
@@ -1742,16 +1742,23 @@
 ntID(), 
+%0A%09%09%09%09%09%09
 AppConte
@@ -1759,23 +1759,16 @@
 pContext
-%0A%09%09%09%09%09%09
 .getAppC
@@ -1811,16 +1811,23 @@
 sURL(), 
+%0A%09%09%09%09%09%09
 Extended
@@ -1983,26 +1983,16 @@
 ;%0A%09%09%09%09re
-moteEngine
  = restS
@@ -2029,23 +2029,16 @@
 ();%0A%09%09%09%09
-return 
 remoteEn
@@ -2037,24 +2037,44 @@
 remoteEngine
+ = re;%0A%09%09%09%09return re
 ;%0A%09%09%09%7D%0A%09%09%09ca
@@ -2398,10 +2398,165 @@
 %0A%09%09%7D%0A%09%7D%0A
+%09%0A%09/**%0A%09 * Calling this will force the next call that requires remote access to reconnect%0A%09 */%0A%09public void closeConnection()%0A%09%7B%0A%09%09remoteEngine = null;%0A%09%7D%0A
 %7D%0A

allow close of current connection